📄 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<messages.length; messageNr++) { // // mail couldn't be resent String emergencyFileName = emergencyPath+"message"; File emergencyFile = null; int i=0; while(true) { emergencyFile = new File(emergencyFileName+i+".txt"); if (emergencyFile.exists()) { i++; } else { break; } } try { PrintWriter pw = new PrintWriter(new FileWriter(emergencyFile)); for (int j=0; j<messages[messageNr].length; j++) { pw.println(messages[messageNr][j]); } pw.close(); if (report != null) report.addMessage("Message saved in "+emergencyFile.getAbsolutePath()); if (verbose) System.out.println("Message saved in "+emergencyFile.getAbsolutePath()); log.write("Message saved in "+emergencyFile.getAbsolutePath()); } catch (IOException ie) { System.out.println("Couldn't write to emergency file " +emergencyFile.getAbsolutePath()); } } } //// PUBLIC METHODS public void fetchMail () { totalMessagesFetched = 0; totalMessagesResent = 0; for (int i=0; i<popBoxes.size(); i++) { //System.out.println("PopBox #"+i); ((PopBox)popBoxes.elementAt(i)).run(); // no concurrent mailfetching... } if (verbose) { System.out.println("Total : fetched "+totalMessagesFetched +" resent "+totalMessagesResent); } log.write("Total : fetched "+totalMessagesFetched +" resent "+totalMessagesResent); flushLogFiles(); } public void run () { while(true) { if (verbose) System.out.println("Fetching mail..."); fetchMail(); if (verbose) System.out.println("Daemon goes to sleep for "+(sleepTime/1000)+" seconds."); try { sleep(sleepTime); } catch (InterruptedException ie) { } } } public static void main (String[] args) { if (args.length == 0) { printUsage(); return; } boolean verbose = false; boolean daemon = false; long sleepTime = 300; // 5 minutes default String fileName = "mfetcher.cnf"; String logFileName = null; String emergencyPath = ""; int token = -1; try { while (true) { token++; if (token >= 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 <fileName>"); 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 <sleep_time_in_seconds>"); System.out.println(" run MailFetcher as a daemon"); System.out.println(" -l / --logfile <fileName>"); System.out.println(" work with another logfile than the default mfetcher.log"); System.out.println(" -e / --emergency <path>"); System.out.println(" saves undeliverable mail into the specified path\n"); System.out.println(" -t / --timezone <modifier>"); System.out.println(" specifies the timezone in which you use MailFetcher2"); System.out.println(" -p / --pop3log <fileName>"); System.out.println(" logs pop3 communications in file (use - for stdout)"); System.out.println(" -s / --smtplog <fileName>"); System.out.println(" logs smtp communications in file (use - for stdout)"); System.out.println(" -m / --messagelog <fileName>"); 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 <p3> user <u> pass <p> delete <d> smtphost <s> target <m>"); System.out.println("pop3server ..... (etc)"); System.out.println("reportto <m> from <m> smtphost <s>"); System.out.println(""); System.out.println("where:"); System.out.println(" <p3> = pop3 server (eg. pop3.provider.com)"); System.out.println(" <u> = username on pop3server (eg. bill)"); System.out.println(" <p> = password on pop3server (eg. IdfOWst)"); System.out.println(" <d> = true or false, whether mail should be deleted on the pop3 server"); System.out.println(" <s> = smtp server (eg. smtp.local.mycompany.com)"); System.out.println(" <m> = 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -