00001
package com.quadcap.pop3.client;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.BufferedInputStream;
00042
import java.io.BufferedOutputStream;
00043
import java.io.BufferedReader;
00044
import java.io.File;
00045
import java.io.FileInputStream;
00046
import java.io.FileOutputStream;
00047
import java.io.InputStreamReader;
00048
import java.io.ObjectInputStream;
00049
import java.io.ObjectOutputStream;
00050
00051
import java.util.ArrayList;
00052
import java.util.HashMap;
00053
import java.util.Map;
00054
import java.util.Properties;
00055
00056
import com.quadcap.util.text.Text;
00057
00058
import com.quadcap.server.ServiceImpl;
00059
import com.quadcap.server.ServiceContainer;
00060
00061
import com.quadcap.util.Debug;
00062
00063
00064
00065
00066
00067
00068 public class Pop3Service extends ServiceImpl {
00069 Thread
thread;
00070 boolean terminate =
false;
00071 Properties
props;
00072 Object
lock =
new Object();
00073
00074 public void init(
ServiceContainer c, Properties p)
00075
throws Exception
00076 {
00077 super.init(c, p);
00078
props = getProperties();
00079
final long sleepMs = 1000L *
00080 Integer.parseInt(
props.getProperty(
"interval",
"60"));
00081
terminate =
false;
00082
thread =
new Thread() {
00083
public void run() {
00084
while (!
terminate) {
00085
try {
00086
runAgent();
00087 }
catch (Throwable t) {
00088
Debug.print(t);
00089 }
00090
try { Thread.sleep(sleepMs); }
catch (Throwable t) {}
00091 }
00092 }
00093 };
00094
thread.start();
00095 }
00096
00097 static ArrayList
getTuples(File tf)
throws Exception {
00098 ArrayList a =
new ArrayList();
00099 FileInputStream fi =
new FileInputStream(tf);
00100 HashMap object = null;
00101
try {
00102 InputStreamReader isr =
new InputStreamReader(fi);
00103 BufferedReader r =
new BufferedReader(isr);
00104 StringBuffer buf =
new StringBuffer();
00105 String line;
00106
boolean haveBuffer =
false;
00107
while ((line = r.readLine()) != null) {
00108
if (line.length() > 0) {
00109
if (!haveBuffer) {
00110 buf.replace(0, buf.length(), line.trim());
00111 haveBuffer =
true;
00112 }
else {
00113
if (Character.isWhitespace(line.charAt(0))) {
00114 buf.append(
' ');
00115 buf.append(line.trim());
00116 }
else {
00117 object =
addAttr(object, buf.toString());
00118 buf.replace(0, buf.length(), line.trim());
00119 }
00120 }
00121 }
else {
00122
if (object != null) {
00123
if (haveBuffer) {
00124 object =
addAttr(object, buf.toString());
00125 haveBuffer =
false;
00126 }
00127 a.add(object);
00128 object = null;
00129 }
00130 }
00131 }
00132
if (haveBuffer) {
00133 object =
addAttr(object, buf.toString());
00134 }
00135
if (object != null) a.add(object);
00136
return a;
00137 } finally {
00138 fi.close();
00139 }
00140 }
00141
00142 static final HashMap
addAttr(HashMap h, String s) {
00143
if (h == null) h =
new HashMap();
00144 String[] sv =
Text.extractN(s,
"*:*");
00145
if (sv == null || sv.length != 2 || sv[0] == null || sv[1] == null) {
00146
00147 }
else {
00148 h.put(sv[0].trim(), sv[1].trim());
00149 }
00150
return h;
00151 }
00152
00153 public void stop() {
00154
terminate =
true;
00155
synchronized (
lock) {
00156
try {
thread.interrupt(); }
catch (Throwable t) {}
00157 }
00158 }
00159
00160 public void runAgent() throws Exception {
00161
synchronized (
lock) {
00162 File accountsFile =
new File(
props.getProperty(
"accounts"));
00163 ArrayList agents = getTuples(accountsFile);
00164 String hooker =
props.getProperty(
"hook");
00165 Class hookerClass = Class.forName(hooker);
00166
MessageHook hook = (
MessageHook)hookerClass.newInstance();
00167 hook.
init(
props);
00168
for (
int i = 0; i < agents.size(); i++) {
00169 Map ai = (Map)agents.get(i);
00170
Pop3Agent agent =
readAgent(ai);
00171
if (agent != null) {
00172 Properties p =
new Properties();
00173 p.putAll(
props);
00174 p.putAll(ai);
00175 agent.
run(p, hook);
00176
writeAgent(ai, agent);
00177 }
00178 }
00179 }
00180 }
00181
00182 public Pop3Agent readAgent(Map ai) {
00183
try {
00184 String name = ai.get(
"name").toString();
00185 String fileName =
props.getProperty(
"savefile",
"pop3." +
00186 name +
".save");
00187 File file =
new File(fileName);
00188
Pop3Agent agent =
new Pop3Agent();
00189
if (file.exists()) {
00190 FileInputStream fis =
new FileInputStream(fileName);
00191
try {
00192 BufferedInputStream bis =
new BufferedInputStream(fis);
00193 agent.
read(bis);
00194 } finally {
00195 fis.close();
00196 }
00197 }
00198
return agent;
00199 }
catch (Throwable t) {
00200
Debug.print(t);
00201
return null;
00202 }
00203 }
00204
00205 public void writeAgent(Map ai,
Pop3Agent agent) {
00206 FileOutputStream fos = null;
00207
try {
00208 String name = ai.get(
"name").toString();
00209 String fileName =
props.getProperty(
"savefile",
"pop3." +
00210 name +
".save");
00211 fos =
new FileOutputStream(fileName);
00212 BufferedOutputStream bos =
new BufferedOutputStream(fos);
00213 agent.
write(bos);
00214 bos.flush();
00215 }
catch (Throwable t) {
00216
Debug.print(t);
00217 } finally {
00218
if (fos != null) {
00219
try { fos.close(); }
catch (Throwable t) {}
00220 }
00221 }
00222 }
00223 }