<commit_before>/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2011 <NAME>
 *
 * 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.
 *
 * 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 org.jpos.util;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.ISOUtil;

/**
 * Send e-mail with selected LogEvents to operator account.
 * <b>Configuration properties</b>
 * <pre>
 *    jpos.operator.from=jpos
 *    jpos.operator.to=<EMAIL>
 *    jpos.operator.subject.prefix=[jPOS]
 *    jpos.operator.tags="Operator ISORequest SystemMonitor"
 *    jpos.operator.delay=10000
 *    jpos.mail.smtp.host=localhost
 *    jpos.mail.smtp.auth=true //If true, then smtp server need authentication.
 *    jpos.mail.smtp.user=<EMAIL>
 *    jpos.mail.smtp.password=<PASSWORD>
 *    jpos.mail.smtp.debug=false //Debugs smtp connection
 *    jpos.mail.smtp.attachments=false //If true, sends each event as an attachment, otherwise put them into body 
 * </pre>
 *
 * @author <EMAIL>
 * @version $Id: OperatorLogListener.java 2130 2005-04-11 15:58:24Z apr $
 * 
 */
public class OperatorLogListener 
    implements LogListener, Configurable, Runnable
{
    Configuration cfg;
    BlockingQueue queue;
    boolean logExceptions;

    public OperatorLogListener () {
        super();
        queue = new BlockingQueue();
    }
    public OperatorLogListener (Configuration cfg) {
        super();
        this.cfg = cfg;
        queue = new BlockingQueue();
        new Thread(this).start();
    }
    public void setConfiguration (Configuration cfg) 
        throws ConfigurationException
    {
        this.cfg = cfg;
        assertProperty ("jpos.operator.to");
        assertProperty ("jpos.operator.subject.prefix");
        assertProperty ("jpos.operator.tags");
        assertProperty ("jpos.operator.delay");
        assertProperty ("jpos.mail.smtp.host");

        logExceptions = cfg.get ("jpos.operator.tags").
            indexOf ("exception") >= 0;

        new Thread(this).start();
    }
    
    public void run() {
        Thread.currentThread().setName ("OperatorLogListener");
        int delay = cfg.getInt ("jpos.operator.delay");
        try {
            ISOUtil.sleep (2500);   // initial delay
            for (;;) {
                try {
                    LogEvent ev[] = new LogEvent[1];
                    if (queue.pending() > 0) {
                        ev = new LogEvent [queue.pending()];
                        for (int i=0; i < ev.length; i++)
                            ev[i] = (LogEvent) queue.dequeue();
                    } else 
                        ev[0] = (LogEvent) queue.dequeue();
                    sendMail (ev);
                    if (delay > 0)
                        Thread.sleep (delay);
                } catch (InterruptedException e) { }
            }
        } catch (BlockingQueue.Closed e) { }
    }
    private void sendMail (LogEvent[] ev) {
        String from         = cfg.get    ("jpos.operator.from", "jpos-logger");
        String[] to         = cfg.getAll ("jpos.operator.to");
        String[] cc         = cfg.getAll ("jpos.operator.cc");
        String[] bcc        = cfg.getAll ("jpos.operator.bcc");
        String subject      = cfg.get    ("jpos.operator.subject.prefix");
        String user         = cfg.get    ("jpos.mail.smtp.user", "");
        String passw        = cfg.get    ("jpos.mail.smtp.password","");
        boolean debug       = cfg.getBoolean("jpos.mail.smtp.debug",false);
        boolean attachments = cfg.getBoolean("jpos.mail.smtp.attachments",true);
        boolean auth        = cfg.getBoolean("jpos.mail.smtp.auth", true);

        if (ev.length > 1) 
            subject = subject + ev.length + " events";
        else
            subject = subject + ev[0].getRealm() + " - " +ev[0].getTag();

        // create some properties and get the default Session
        Properties props = System.getProperties();
        props.put("mail.smtp.host", cfg.get ("jpos.mail.smtp.host", "localhost"));
        props.setProperty("mail.smtp.port",cfg.get ("jpos.mail.smtp.port","25"));
        props.setProperty("mail.smtp.user", from);
        props.setProperty("mail.smtp.auth", Boolean.toString(auth));
        
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
        <commit_msg>TLS support contributed by @jpaoletti<commit_after>/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2011 <NAME>
 *
 * 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.
 *
 * 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 org.jpos.util;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.ISOUtil;

/**
 * Send e-mail with selected LogEvents to operator account.
 * <b>Configuration properties</b>
 * <pre>
 *    jpos.operator.from=jpos
 *    jpos.operator.to=<EMAIL>
 *    jpos.operator.subject.prefix=[jPOS]
 *    jpos.operator.tags="Operator ISORequest SystemMonitor"
 *    jpos.operator.delay=10000
 *    jpos.mail.smtp.host=localhost
 *    jpos.mail.smtp.auth=true //If true, then smtp server need authentication.
 *    jpos.mail.smtp.user=<EMAIL>
 *    jpos.mail.smtp.password=<PASSWORD>
 *    jpos.mail.smtp.debug=false //Debugs smtp connection
 *    jpos.mail.smtp.attachments=false //If true, sends each event as an attachment, otherwise put them into body 
 *    jpos.mail.smtp.tls=false //If true, activate TLS 
 * </pre>
 *
 * @author <EMAIL>
 * @version $Id: OperatorLogListener.java 2130 2005-04-11 15:58:24Z apr $
 * 
 */
public class OperatorLogListener 
    implements LogListener, Configurable, Runnable
{
    Configuration cfg;
    BlockingQueue queue;
    boolean logExceptions;

    public OperatorLogListener () {
        super();
        queue = new BlockingQueue();
    }
    public OperatorLogListener (Configuration cfg) {
        super();
        this.cfg = cfg;
        queue = new BlockingQueue();
        new Thread(this).start();
    }
    public void setConfiguration (Configuration cfg) 
        throws ConfigurationException
    {
        this.cfg = cfg;
        assertProperty ("jpos.operator.to");
        assertProperty ("jpos.operator.subject.prefix");
        assertProperty ("jpos.operator.tags");
        assertProperty ("jpos.operator.delay");
        assertProperty ("jpos.mail.smtp.host");

        logExceptions = cfg.get ("jpos.operator.tags").
            indexOf ("exception") >= 0;

        new Thread(this).start();
    }
    
    public void run() {
        Thread.currentThread().setName ("OperatorLogListener");
        int delay = cfg.getInt ("jpos.operator.delay");
        try {
            ISOUtil.sleep (2500);   // initial delay
            for (;;) {
                try {
                    LogEvent ev[] = new LogEvent[1];
                    if (queue.pending() > 0) {
                        ev = new LogEvent [queue.pending()];
                        for (int i=0; i < ev.length; i++)
                            ev[i] = (LogEvent) queue.dequeue();
                    } else 
                        ev[0] = (LogEvent) queue.dequeue();
                    sendMail (ev);
                    if (delay > 0)
                        Thread.sleep (delay);
                } catch (InterruptedException e) { }
            }
        } catch (BlockingQueue.Closed e) { }
    }
    private void sendMail (LogEvent[] ev) {
        String from         = cfg.get    ("jpos.operator.from", "jpos-logger");
        String[] to         = cfg.getAll ("jpos.operator.to");
        String[] cc         = cfg.getAll ("jpos.operator.cc");
        String[] bcc        = cfg.getAll ("jpos.operator.bcc");
        String subject      = cfg.get    ("jpos.operator.subject.prefix");
        String user         = cfg.get    ("jpos.mail.smtp.user", "");
        String passw        = cfg.get    ("jpos.mail.smtp.password","");
        boolean debug       = cfg.getBoolean("jpos.mail.smtp.debug",false);
        boolean attachments = cfg.getBoolean("jpos.mail.smtp.attachments",true);
        boolean auth        = cfg.getBoolean("jpos.mail.smtp.auth", true);

        if (ev.length > 1) 
            subject = subject + ev.length + " events";
        else
            subject = subject + ev[0].getRealm() + " - " +ev[0].getTag();

        // create some properties and get the default Session
        Properties props = System.getProperties();
        props.put("mail.smtp.host", cfg.get ("jpos.mail.smtp.host", "localhost"));
        props.setProperty("mail.smtp.starttls.enable", cfg.get("jpos.mail.smtp.tls", "false"));
        props.setProperty("mail.smtp.port",cfg.get ("jpos.mail.smtp.port","25"));
        props.setProperty("mail.smtp.user", from);
        props.setProperty("mail.smtp.auth", Boolean.toString(auth));
        
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
        
{'fraction_non_alphanumeric': 0.12253318980983136, 'fraction_numerical': 0.00843200574094008, 'mean_word_length': 2.4410493827160495, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, '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': '3308900', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3181, 'n_words': 812}