<commit_before>/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.silverpeas.mailinglist.service.job;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags.Flag;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.silverpeas.mailinglist.service.event.MessageEvent;
import com.silverpeas.mailinglist.service.event.MessageListener;
import com.silverpeas.mailinglist.service.model.MailingListService;
import com.silverpeas.mailinglist.service.model.beans.MailingList;
import com.stratelia.silverpeas.scheduler.SchedulerEvent;
import com.stratelia.silverpeas.scheduler.SchedulerEventHandler;
import com.stratelia.silverpeas.silvertrace.SilverTrace;

public class MessageChecker implements SchedulerEventHandler {

  public static final String IMAP_PROTOCOL = "imap";
  public static final String IMAP_SSL_PROTOCOL = "imaps";
  public static final String POP3_PROTOCOL = "pop3";
  private Map<String, MessageListener> listeners;
  private MailProcessor processor;
  private String mailServer;
  private String login;
  private String password;
  private String protocol;
  private int port;
  private boolean leaveOnServer;
  private Session mailSession;
  private MailingListService mailingListService;

  public MailingListService getMailingListService() {
    return mailingListService;
  }

  public void setMailingListService(MailingListService mailingListService) {
    this.mailingListService = mailingListService;
  }

  public Session getMailSession() {
    return mailSession;
  }

  public void setMailSession(Session mailSession) {
    this.mailSession = mailSession;
  }

  public MessageChecker() {
    this.listeners = new HashMap<String, MessageListener>(10);
  }

  public String getLogin() {
    return login;
  }

  public void setLogin(String login) {
    this.login = login;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getMailServer() {
    return mailServer;
  }

  public void setMailServer(String mailServer) {
    this.mailServer = mailServer;
  }

  public boolean isLeaveOnServer() {
    if (POP3_PROTOCOL.equalsIgnoreCase(this.protocol)) {
      this.leaveOnServer = false;
    }
    return leaveOnServer;
  }

  public void setLeaveOnServer(boolean leaveOnServer) {
    this.leaveOnServer = leaveOnServer;
  }

  public int getPort() {
    if (port <= 0) {
      if (POP3_PROTOCOL.equals(getProtocol())) {
        setPort(110);
      } else if (IMAP_PROTOCOL.equals(getProtocol())) {
        setPort(143);
      } else if (IMAP_SSL_PROTOCOL.equals(getProtocol())) {
        setPort(993);
      }
      setPort(110);
    }
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public String getProtocol() {
    return this.protocol;
  }

  public void setProtocol(String protocol) {
    if (POP3_PROTOCOL.equalsIgnoreCase(protocol)
        || IMAP_PROTOCOL.equalsIgnoreCase(protocol)
        || IMAP_SSL_PROTOCOL.equalsIgnoreCase(protocol)) {
      this.protocol = protocol.toLowerCase();
    } else {
      this.protocol = POP3_PROTOCOL;
    }
    if (isImap()) {
      System.setProperty("mail.imap.partialfetch", "false");
    }
  }

  /**
   * Adds a new listener to the list of listeners.
   * @param listener the listener to be added.
   */
  public synchronized void addMessageListener(MessageListener listener) {
      this.listeners.put(listener.getComponentId(), listener);
  }

  /**
   * Gets the new messages on the Mail Server and processes them.
   * @param date the date of the checking.
   */
  public void checkNewMessages(Date date) {
    Store mailAccount = null;
    Folder inbox = null;
    Map<String, MessageListener> listenersByEmail = prepareListeners();
    try {
      mailSession.setDebug(true);
      mailAccount = mailSession.getStore(getProtocol());
      mailAccount.connect(getMailServer(), getPort(), getLogin(), getPassword());
      inbox = mailAccount.getFolder("INBOX");
      if (inbox == null) {
        throw new MessagingException("No POP3 INBOX");
      }
      // -- Open the folder for read write --
      inbox.open(Folder.READ_WRITE);

      // -- Get the message wrappers and process them --
      Message[] msgs = inbox.getMessages();
      if (isImap()) {
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.FLAGS);
        inbox.fetch(msgs, profile);
      }
      Map<MessageListener, MessageEvent> eventsMap = new HashMap<MessageListener, MessageEvent>();
      for (int msgNum = 0; msgNum < msgs.length; msgNum++) {
        try {
          MimeMessage message = (MimeMessage) msgs[msgNum];
          if (isImap()) {
            if (!message.getFlags().contains(Flag.SEEN)
                && !message.getFlags().contains(Flag.DELETED)) {
              message = new MimeMessage(message);
              processEmail(message, eventsMap, listenersByEmail);<commit_msg>Remove the hard-coded debug setting for the mail session in the MessageChecker#checkNewMessages() method<commit_after>/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.silverpeas.mailinglist.service.job;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags.Flag;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.silverpeas.mailinglist.service.event.MessageEvent;
import com.silverpeas.mailinglist.service.event.MessageListener;
import com.silverpeas.mailinglist.service.model.MailingListService;
import com.silverpeas.mailinglist.service.model.beans.MailingList;
import com.stratelia.silverpeas.scheduler.SchedulerEvent;
import com.stratelia.silverpeas.scheduler.SchedulerEventHandler;
import com.stratelia.silverpeas.silvertrace.SilverTrace;

public class MessageChecker implements SchedulerEventHandler {

  public static final String IMAP_PROTOCOL = "imap";
  public static final String IMAP_SSL_PROTOCOL = "imaps";
  public static final String POP3_PROTOCOL = "pop3";
  private Map<String, MessageListener> listeners;
  private MailProcessor processor;
  private String mailServer;
  private String login;
  private String password;
  private String protocol;
  private int port;
  private boolean leaveOnServer;
  private Session mailSession;
  private MailingListService mailingListService;

  public MailingListService getMailingListService() {
    return mailingListService;
  }

  public void setMailingListService(MailingListService mailingListService) {
    this.mailingListService = mailingListService;
  }

  public Session getMailSession() {
    return mailSession;
  }

  public void setMailSession(Session mailSession) {
    this.mailSession = mailSession;
  }

  public MessageChecker() {
    this.listeners = new HashMap<String, MessageListener>(10);
  }

  public String getLogin() {
    return login;
  }

  public void setLogin(String login) {
    this.login = login;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getMailServer() {
    return mailServer;
  }

  public void setMailServer(String mailServer) {
    this.mailServer = mailServer;
  }

  public boolean isLeaveOnServer() {
    if (POP3_PROTOCOL.equalsIgnoreCase(this.protocol)) {
      this.leaveOnServer = false;
    }
    return leaveOnServer;
  }

  public void setLeaveOnServer(boolean leaveOnServer) {
    this.leaveOnServer = leaveOnServer;
  }

  public int getPort() {
    if (port <= 0) {
      if (POP3_PROTOCOL.equals(getProtocol())) {
        setPort(110);
      } else if (IMAP_PROTOCOL.equals(getProtocol())) {
        setPort(143);
      } else if (IMAP_SSL_PROTOCOL.equals(getProtocol())) {
        setPort(993);
      }
      setPort(110);
    }
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public String getProtocol() {
    return this.protocol;
  }

  public void setProtocol(String protocol) {
    if (POP3_PROTOCOL.equalsIgnoreCase(protocol)
        || IMAP_PROTOCOL.equalsIgnoreCase(protocol)
        || IMAP_SSL_PROTOCOL.equalsIgnoreCase(protocol)) {
      this.protocol = protocol.toLowerCase();
    } else {
      this.protocol = POP3_PROTOCOL;
    }
    if (isImap()) {
      System.setProperty("mail.imap.partialfetch", "false");
    }
  }

  /**
   * Adds a new listener to the list of listeners.
   * @param listener the listener to be added.
   */
  public synchronized void addMessageListener(MessageListener listener) {
      this.listeners.put(listener.getComponentId(), listener);
  }

  /**
   * Gets the new messages on the Mail Server and processes them.
   * @param date the date of the checking.
   */
  public void checkNewMessages(Date date) {
    Store mailAccount = null;
    Folder inbox = null;
    Map<String, MessageListener> listenersByEmail = prepareListeners();
    try {
      mailAccount = mailSession.getStore(getProtocol());
      mailAccount.connect(getMailServer(), getPort(), getLogin(), getPassword());
      inbox = mailAccount.getFolder("INBOX");
      if (inbox == null) {
        throw new MessagingException("No POP3 INBOX");
      }
      // -- Open the folder for read write --
      inbox.open(Folder.READ_WRITE);

      // -- Get the message wrappers and process them --
      Message[] msgs = inbox.getMessages();
      if (isImap()) {
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.FLAGS);
        inbox.fetch(msgs, profile);
      }
      Map<MessageListener, MessageEvent> eventsMap = new HashMap<MessageListener, MessageEvent>();
      for (int msgNum = 0; msgNum < msgs.length; msgNum++) {
        try {
          MimeMessage message = (MimeMessage) msgs[msgNum];
          if (isImap()) {
            if (!message.getFlags().contains(Flag.SEEN)
                && !message.getFlags().contains(Flag.DELETED)) {
              message = new MimeMessage(message);
              processEmail(message, eventsMap, listenersByEmail);
{'fraction_non_alphanumeric': 0.09414306659464465, 'fraction_numerical': 0.005247318465931013, 'mean_word_length': 3.602272727272727, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4269567', 'n_tokens_mistral': 3780, 'n_tokens_neox': 3691, 'n_words': 1052}