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


package mfetcher;

//
// MailFetcher
//

//
// 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.io.*;
import java.util.*;


public class MailFetcher
    extends Thread
{
    
    public static final String version = "Mailfetcher 2.0.7c - 18.02.2002 - jmettraux@yahoo.com and wimh@users.sourceforget.net";
    public static final String copyright = 
        "Copyright (C) 1999 John Mettraux and Wim Hueskes\n"+
        "This is free software\n"+
        "It comes with absolutely no WARRANTY\n"+
        "You are welcome to redistribute it under certains conditions (http://www.gnu.org/copyleft/gpl.html)";
        
    public static String timeZoneModifier = "";
        // no modifier MailFetcher2 automatically states its report's date
        // with Great Britain's time (GMT) (Locale.UK)
    
//
// DATAS

    Vector popBoxes = new Vector();
    Report report = null;
    
    long sleepTime = 60 * 1000;
        // 60 seconds
        
    boolean verbose = false;  
    
    Log log;
    
    int totalMessagesFetched;
    int totalMessagesResent;
    
    String emergencyPath;

//
// CONSTRUCTORS

    public MailFetcher ()
    {
        this(null, null);
    }
    
    public MailFetcher (String logFileName, String emergencyPath)
    {
        super();
        if (logFileName == null)
            logFileName = "mfetcher.log";
        try {
            log = new Log(logFileName);
        } catch (IOException ie) {
            System.out.println("MailFetcher couldn't open log file '"+logFileName+"'");
        }
        this.emergencyPath = emergencyPath;
    }

//
// METHODS

    void parseConfigurationFile (String fileName)
        throws Exception
    {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        int popBoxesNb = 0;
        while(true) {
            
            String line = br.readLine();
            
            if (line == null)
                break;
                
            if (line.startsWith("#"))
                continue;
                
            if (line.startsWith("reportto")) {
                report = Report.parseReport(popBoxesNb, line);
                report.setVersion(version);
                continue;
            }
            
            PopBox pb = PopBox.parse(line);  
            popBoxesNb++;
            pb.addExceptionListener(new ExceptionListener () {
                public void processException (String account, Exception e)
                {
                    //
                    // FOR ALL EXCEPTIONS
                    if (report != null)
                        report.addMessage("Exception in popBox "+account+"\n"+e);
                    if (verbose) {
                        System.out.println("Exception in popBox "+account+"\n"+e);
                        //e.printStackTrace();
                    }
                    log.write("Exception for "+account+" : "+e);
                    
                    //
                    // SMTP EXCEPTION
                    if (e instanceof MessageException) {
                        MessageException me = (MessageException)e;
                        emergencySaveMessages(me.getMessages());
                    }
                }
            });
            pb.addReportListener(new ReportListener () {
                public void doReport 
                    (String account, int messagesFetched, int messagesResent, int targets)
                {
                    totalMessagesFetched += messagesFetched;
                    totalMessagesResent += messagesResent;
                    
                    if (report != null)
                        try {
                            report.addPopReport("fetched "+messagesFetched
                                +" resent "+messagesResent+" targets "+targets+" on "+account);
                        } catch (Exception e) {
                            if (verbose)
                                System.out.println("Couldn't send report :\n"+e);
                            log.write("Couldn't send report");    
                            if ( e instanceof MessageException) {
                                MessageException me = (MessageException)e;
                                emergencySaveMessages(me.getMessages());
                            }
                        }
                        
                    if (verbose) 
                        System.out.println("fetched "+messagesFetched
                                +" resent "+messagesResent+" targets "+targets+" on "+account);
                }
            });
            pb.setVersion(version);
                // leaving a trace on the mail...
            addPopBox(pb);
        }
        br.close();
    }

    void addPopBox (PopBox pb)
    {
        popBoxes.addElement(pb);
    }

    void emergencySaveSingleMessage (String[] message) {
        String[][] array = new String[1][];
        array[0] =  message;
        emergencySaveMessages(array);
    }

    void emergencySaveMessages (String[][] messages) {
        for (int messageNr=0; messageNr= args.length)
                    break;
                if (args[token].equals("-v") ||
                    args[token].equals("--verbose"))
                {
                    verbose = true;
                    continue;
                }
                if (args[token].equals("-d") ||
                    args[token].equals("--daemon"))
                {
                    daemon = true;
                    token++;
                    sleepTime = 1000 * Long.parseLong(args[token]);
                    continue;
                }
                if (args[token].equals("-f") ||
                    args[token].equals("--fetchconf"))
                {
                    token++;
                    fileName = args[token];
                    continue;
                }
                if (args[token].equals("-l") ||
                    args[token].equals("--logfile"))
                {
                    token++;
                    logFileName = args[token];
                    continue;
                }
                if (args[token].equals("-e") ||
                    args[token].equals("--emergency"))
                {
                    token++;
                    emergencyPath = args[token];
                    continue;
                }
                if (args[token].equals("-t") ||
                    args[token].equals("--timezone"))
                {
                    token++;
                    timeZoneModifier = args[token];
                    continue;
                }
                if (args[token].equals("-p") ||
                    args[token].equals("--pop3log"))
                {
                    token++;
                    if (args[token]=="-")
                        PopFetcher.commLog = new PrintWriter(System.out, true);
                    else
                        PopFetcher.commLog = new PrintWriter(new FileWriter(args[token], true));
                    continue;
                }
                if (args[token].equals("-s") ||
                    args[token].equals("--smtplog"))
                {
                    token++;
                    if (args[token]=="-")
                        SmtpMailer.commLog = new PrintWriter(System.out, true);
                    else
                        SmtpMailer.commLog = new PrintWriter(new FileWriter(args[token], true));
                    continue;
                }
                if (args[token].equals("-m") ||
                    args[token].equals("--messagelog"))
                {
                    token++;
                    if (args[token]=="-")
                        PopBox.messageLog = new PrintWriter(System.out, true);
                    else
                        PopBox.messageLog = new PrintWriter(new FileWriter(args[token], true));
                    continue;
                }
            }
        } catch (Exception e) {
            printUsage();
            return;
        }
        
        MailFetcher mf = new MailFetcher(logFileName, emergencyPath);
        mf.setVerbose(verbose);
        try {
            mf.parseConfigurationFile(fileName);
        } catch (Exception e) {
            System.out.println("Parsing Exception : "+e);
            return;
        }
        
        if (daemon) {
            mf.setSleepTime(sleepTime);
            mf.start();
        } else {
            mf.fetchMail();
        }

        closeLogFiles();
    }
    
    public static void flushLogFiles()
    {
        if (SmtpMailer.commLog != null)
            SmtpMailer.commLog.flush();
        if (PopFetcher.commLog != null)
            PopFetcher.commLog.flush();
        if (PopBox.messageLog != null)
            PopBox.messageLog.flush();
    }

    public static void closeLogFiles()
    {
        if (SmtpMailer.commLog != null)
            SmtpMailer.commLog.close();
        if (PopFetcher.commLog != null)
            PopFetcher.commLog.close();
        if (PopBox.messageLog != null)
            PopBox.messageLog.close();
    }

    public static void printUsage ()
    {
        System.out.println("\nUsage :\njava mfetcher.MailFetcher [options]");
        System.out.println("Options :");
        System.out.println("    -f / --fetchconf ");
        System.out.println("        reads list of account to fetch from another file");
        System.out.println("        than default mfetcher.cnf");
        System.out.println("    -v / --verbose");
        System.out.println("        displays all control messages");
        System.out.println("    -d / --daemon ");
        System.out.println("        run MailFetcher as a daemon");
        System.out.println("    -l / --logfile ");
        System.out.println("        work with another logfile than the default mfetcher.log");
        System.out.println("    -e / --emergency ");
        System.out.println("        saves undeliverable mail into the specified path\n");
        System.out.println("    -t / --timezone ");
        System.out.println("        specifies the timezone in which you use MailFetcher2");
        System.out.println("    -p / --pop3log ");
        System.out.println("        logs pop3 communications in file (use - for stdout)");
        System.out.println("    -s / --smtplog ");
        System.out.println("        logs smtp communications in file (use - for stdout)");
        System.out.println("    -m / --messagelog ");
        System.out.println("        logs message info in file (use - for stdout)");
        System.out.println("");
        System.out.println("mfetcher.cnf format:");
        System.out.println("# comments");
        System.out.println("pop3server  user  pass 

delete smtphost target "); System.out.println("pop3server ..... (etc)"); System.out.println("reportto from smtphost "); System.out.println(""); System.out.println("where:"); System.out.println(" = pop3 server (eg. pop3.provider.com)"); System.out.println(" = username on pop3server (eg. bill)"); System.out.println("

= password on pop3server (eg. IdfOWst)"); System.out.println(" = true or false, whether mail should be deleted on the pop3 server"); System.out.println(" = smtp server (eg. smtp.local.mycompany.com)"); System.out.println(" = emailaddres where the mail should go on the local smtp server"); } public void setSleepTime (long millis) { sleepTime = millis; } public void setVerbose (boolean v) { verbose = v; } }