www.pudn.com > mailfetcher2.rar > PopBox.java


package mfetcher;

//
// PopBox
//
// 27.01.1999 John Mettraux
//

//
// Copyright (C) 1999 John Mettraux
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

import java.util.*;
import java.io.*;


public class PopBox
    extends Thread
{
//
// DATAS

    public static PrintWriter messageLog = null;

    private Vector exceptionListeners = new Vector();
    private Vector reportListeners = new Vector();

    String popServer;
    String user;
    String pass;
    boolean delete;
    String smtpHost;
    String[] targets;
    
    int messagesFetched;
    int messagesResent;
    int targetNb;
    
    String version = null;

//
// CONSTRUCTORS

    public PopBox
        (String popServer, String user, String pass, boolean delete, String smtpHost, String[] targets)
    {
        super();
        
        this.popServer = popServer;
        this.user = user;
        this.pass = pass;
        this.delete = delete;
        this.smtpHost = smtpHost;
        this.targets = targets;
        
        messagesFetched = 0;
        messagesResent = 0;
        targetNb = 0;
    }

//
// PUBLIC METHODS

    public void addExceptionListener (ExceptionListener el)
    {
        exceptionListeners.addElement(el);
    }
    
    public void removeExceptionListener (ExceptionListener el)
    {
        exceptionListeners.removeElement(el);
    }

    public void addReportListener (ReportListener rl)
    {
        reportListeners.addElement(rl);
    }
    
    public void removeReportListener (ReportListener rl)
    {
        reportListeners.removeElement(rl);
    }

    public void run ()
    {
        
        PopFetcher pf = new PopFetcher(popServer, user, pass, 0, delete);
            // sleep time doesn't matter here
        if (messageLog != null) {
            pf.addMessageListener(new MessageListener () {
                
                //
                // each message fetched is resent...
                
                public void messageReceived(MessageEvent me)
                {
                    String from = me.getFrom();
                    String to = me.getTo();
                    String subject = me.getSubject();

                    messageLog.println(user+" from:"+from+" subject:"+subject+" to: "+to); 
                    messageLog.flush();
                }
            });
        }
        pf.addMessageListener(new MessageListener () {
            
            //
            // each message fetched is resent...
            
            public void messageReceived(MessageEvent me)
            {
                String from = me.getFrom();
                try {
                    messagesFetched++;
                    SmtpMailer sm = new SmtpMailer(smtpHost, from);
                    //System.out.println("Version = "+version);
                    if (version != null)
                        sm.setVersion(version);
                    sm.sendMail(targets, me.getMessageBody());
                    targetNb = sm.getTargets();
                    messagesResent++;
                } catch (Exception e) {
                    //System.out.println("Exception while fetching... "+e);
                    PopBox.this.processException(e);
                }
            }
        });
        pf.setExceptionListener(new ExceptionListener () {
            public void processException (String account, Exception e)
            {
                //
                // just relaying the exception to upper level
                //
                // so account doesn't matter
                PopBox.this.processException(e);
            }
        });
        pf.run();
            // don't start... just run... the thread here is PopBox itself !
        doReport();    
            
    }
    
    public static PopBox parse (String configurationLine)
        throws Exception
    {
        StringTokenizer st = new StringTokenizer(configurationLine);
        
        String _popServer;
        String _user;
        String _pass;
        boolean _delete;
        String _smtpHost;
        
        try {
            st.nextToken(); // skip 'popserver'
            _popServer = st.nextToken();
            st.nextToken(); // skip 'user'
            _user = st.nextToken();
            st.nextToken(); // skip 'pass'
            _pass = st.nextToken();
            st.nextToken(); // skip 'delete'
            _delete = st.nextToken().equals("true");
            st.nextToken(); // skip 'smtpHost'
            _smtpHost = st.nextToken();
        } catch (Exception e) {
            throw new Exception("Configuration parsing exception in line :\n"+configurationLine+"\n"+e);
        }
        
        int i = configurationLine.indexOf(" target ");
        String sTargets = configurationLine.substring(i+8);
        
        st = new StringTokenizer(sTargets, "; ");
        
        Vector v = new Vector();
        while(st.hasMoreTokens())
            v.addElement(st.nextToken());
        if (v.size() == 0)
            throw new Exception("No targets in configuration line");
            
        String[] _targets = new String[v.size()];    
        for (int j=0; j