KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > pop3 > client > MailSuck


1 package com.quadcap.pop3.client;
2
3 /*
4  * Copyright 1997 by Stan Bailes and quadcap Software.
5  *
6  **/

7
8 import java.io.File JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import com.quadcap.io.IO;
18
19 import com.quadcap.util.Debug;
20
21 /**
22  *
23  * @author Stan Bailes
24  */

25 public class MailSuck implements MessageHook {
26     boolean delete = false;
27     OutputStream JavaDoc os;
28     
29     /**
30      * Suck mail <host> <user> <passwd> <file>
31      */

32     public static void main(String JavaDoc[] args) {
33         MailSuck suck = new MailSuck();
34         try {
35             if (args.length == 1) {
36                 suck.runMany(args[0]);
37             } else if (args.length == 4) {
38                 Pop3Agent agent = new Pop3Agent();
39                 Properties JavaDoc props = new Properties JavaDoc();
40                 props.put("daysKeep", "0");
41                 props.put("host", args[0]);
42                 props.put("user", args[1]);
43                 props.put("passwd", args[2]);
44                 props.put("suck.outfile", args[3]);
45                 props.put("delete",
46                           System.getProperty("suck.delete", "false"));
47
48                 suck.init(props);
49                 agent.run(props, suck);
50             }
51         } catch (Throwable JavaDoc t) {
52             Debug.print(t);
53         } finally {
54             suck.close();
55         }
56     }
57
58     public void runMany(String JavaDoc accountsFile) throws Exception JavaDoc {
59         List JavaDoc agents = Pop3Service.getTuples(new File JavaDoc(accountsFile));
60         for (int i = 0; i < agents.size(); i++) {
61             try {
62                 Map JavaDoc ai = (Map JavaDoc)agents.get(i);
63                 Pop3Agent agent = new Pop3Agent();
64                 Properties JavaDoc props = new Properties JavaDoc();
65                 props.putAll(ai);
66                 props.put("daysKeep", "0");
67                 props.put("suck.outfile", ai.get("name") + ".log");
68                 props.put("delete",
69                           System.getProperty("suck.delete", "false"));
70                 init(props);
71                 Debug.println("Running : " + ai.get("name"));
72                 agent.run(props, this);
73                 close();
74             } catch (Throwable JavaDoc t) {
75                 System.err.print(t.toString());
76             }
77         }
78     }
79         
80     /**
81      * Initialize the hook with its property set
82      */

83     public void init(Properties JavaDoc p) throws Exception JavaDoc {
84         os = new FileOutputStream JavaDoc(
85             p.getProperty("suck.outfile", "suck.out"),
86             true);
87         delete = p.getProperty("delete", "false").equals("true");
88     }
89
90     void close() {
91         try {
92             os.close();
93         } catch (Throwable JavaDoc t) {
94         } finally {
95             os = null;
96         }
97     }
98
99     /**
100      * Return <code>true</code> if the call to
101      * <code>boolean passHeaders(Map headers)</code> will always return
102      * true, so the message receiver can avoid calling it.
103      */

104     public boolean passAllHeaders() {
105         return true;
106     }
107
108     /**
109      * A hook first gets called with the parsed headers from the message.
110      * If the hook returns 'true' to this call, it will be called again
111      * to process the body using <code>sendMessage()</code>, below.
112      */

113     public boolean passHeaders(Map JavaDoc headers) {
114         return true;
115     }
116
117     /**
118      * The hook is called to process the entire message (including headers)
119      * as an octet stream.
120      *
121      * @return <code>false</code> if the message is to be retained in
122      * the store, <code>true</code> to delete it.
123      */

124     public boolean passMessage(InputStream JavaDoc is) throws Exception JavaDoc {
125         IO.copyStream(is, os);
126         return delete;
127     }
128 }
129
Popular Tags