001: package com.quadcap.pop3.client;
002:
003: /*
004: * Copyright 1997 by Stan Bailes and quadcap Software.
005: *
006: **/
007:
008: import java.io.File;
009: import java.io.FileOutputStream;
010: import java.io.InputStream;
011: import java.io.OutputStream;
012:
013: import java.util.List;
014: import java.util.Map;
015: import java.util.Properties;
016:
017: import com.quadcap.io.IO;
018:
019: import com.quadcap.util.Debug;
020:
021: /**
022: *
023: * @author Stan Bailes
024: */
025: public class MailSuck implements MessageHook {
026: boolean delete = false;
027: OutputStream os;
028:
029: /**
030: * Suck mail <host> <user> <passwd> <file>
031: */
032: public static void main(String[] args) {
033: MailSuck suck = new MailSuck();
034: try {
035: if (args.length == 1) {
036: suck.runMany(args[0]);
037: } else if (args.length == 4) {
038: Pop3Agent agent = new Pop3Agent();
039: Properties props = new Properties();
040: props.put("daysKeep", "0");
041: props.put("host", args[0]);
042: props.put("user", args[1]);
043: props.put("passwd", args[2]);
044: props.put("suck.outfile", args[3]);
045: props.put("delete", System.getProperty("suck.delete",
046: "false"));
047:
048: suck.init(props);
049: agent.run(props, suck);
050: }
051: } catch (Throwable t) {
052: Debug.print(t);
053: } finally {
054: suck.close();
055: }
056: }
057:
058: public void runMany(String accountsFile) throws Exception {
059: List agents = Pop3Service.getTuples(new File(accountsFile));
060: for (int i = 0; i < agents.size(); i++) {
061: try {
062: Map ai = (Map) agents.get(i);
063: Pop3Agent agent = new Pop3Agent();
064: Properties props = new Properties();
065: props.putAll(ai);
066: props.put("daysKeep", "0");
067: props.put("suck.outfile", ai.get("name") + ".log");
068: props.put("delete", System.getProperty("suck.delete",
069: "false"));
070: init(props);
071: Debug.println("Running : " + ai.get("name"));
072: agent.run(props, this );
073: close();
074: } catch (Throwable t) {
075: System.err.print(t.toString());
076: }
077: }
078: }
079:
080: /**
081: * Initialize the hook with its property set
082: */
083: public void init(Properties p) throws Exception {
084: os = new FileOutputStream(p.getProperty("suck.outfile",
085: "suck.out"), true);
086: delete = p.getProperty("delete", "false").equals("true");
087: }
088:
089: void close() {
090: try {
091: os.close();
092: } catch (Throwable t) {
093: } finally {
094: os = null;
095: }
096: }
097:
098: /**
099: * Return <code>true</code> if the call to
100: * <code>boolean passHeaders(Map headers)</code> will always return
101: * true, so the message receiver can avoid calling it.
102: */
103: public boolean passAllHeaders() {
104: return true;
105: }
106:
107: /**
108: * A hook first gets called with the parsed headers from the message.
109: * If the hook returns 'true' to this call, it will be called again
110: * to process the body using <code>sendMessage()</code>, below.
111: */
112: public boolean passHeaders(Map headers) {
113: return true;
114: }
115:
116: /**
117: * The hook is called to process the entire message (including headers)
118: * as an octet stream.
119: *
120: * @return <code>false</code> if the message is to be retained in
121: * the store, <code>true</code> to delete it.
122: */
123: public boolean passMessage(InputStream is) throws Exception {
124: IO.copyStream(is, os);
125: return delete;
126: }
127: }
|