| 1 |
/* |
| 2 |
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.util.*; |
| 12 |
import java.io.*; |
| 13 |
import jakarta.mail.*; |
| 14 |
import jakarta.mail.search.*; |
| 15 |
import jakarta.mail.internet.*; |
| 16 |
|
| 17 |
/** |
| 18 |
* Program to manage the [email protected] mailing list by keeping track |
| 19 |
* of which messages have been answered. Message that have been answered |
| 20 |
* have the ANSWERED flag set. Messages that are replies to other messages |
| 21 |
* have the FLAGGED flag set. <p> |
| 22 |
* |
| 23 |
* Note that this program operates on the log file directly and thus all |
| 24 |
* access to the log file using this program must be through the same |
| 25 |
* IMAP server so that the flags are handled consistently. <p> |
| 26 |
* |
| 27 |
* When run with no message number arguments it will list the unanswered |
| 28 |
* messages. The -u flag will cause it to first update the mailbox to |
| 29 |
* account for any recent replies. <p> |
| 30 |
* |
| 31 |
* When run with message number arguments it will mark those messages as |
| 32 |
* answered. This is useful for marking spam and other random messages |
| 33 |
* that will never be replied to. <p> |
| 34 |
* |
| 35 |
* The -a flag will cause it to mark all messages as answered. Useful for |
| 36 |
* flushing the ever accumulating spam. <p> |
| 37 |
* |
| 38 |
* @author Bill Shannon |
| 39 |
*/ |
| 40 |
|
| 41 |
public class answer { |
| 42 |
|
| 43 |
static String protocol = "imap"; |
| 44 |
static String host = "anybodys.sfbay"; |
| 45 |
static String user = "javamail"; |
| 46 |
static String password = "1javamail1"; |
| 47 |
static String mbox = |
| 48 |
"/net/anybodys.sfbay/export6/javamail/logs/javamail.log"; |
| 49 |
static String url = null; |
| 50 |
static boolean verbose = false; |
| 51 |
static boolean update = false; |
| 52 |
static boolean markAll = false; |
| 53 |
|
| 54 |
public static void main(String argv[]) { |
| 55 |
int optind; |
| 56 |
|
| 57 |
for (optind = 0; optind < argv.length; optind++) { |
| 58 |
if (argv[optind].equals("-T")) { |
| 59 |
protocol = argv[++optind]; |
| 60 |
} else if (argv[optind].equals("-H")) { |
| 61 |
host = argv[++optind]; |
| 62 |
} else if (argv[optind].equals("-U")) { |
| 63 |
user = argv[++optind]; |
| 64 |
} else if (argv[optind].equals("-P")) { |
| 65 |
password = argv[++optind]; |
| 66 |
} else if (argv[optind].equals("-v")) { |
| 67 |
verbose = true; |
| 68 |
} else if (argv[optind].equals("-f")) { |
| 69 |
mbox = argv[++optind]; |
| 70 |
} else if (argv[optind].equals("-L")) { |
| 71 |
url = argv[++optind]; |
| 72 |
} else if (argv[optind].equals("-u")) { |
| 73 |
update = true; |
| 74 |
} else if (argv[optind].equals("-a")) { |
| 75 |
markAll = true; |
| 76 |
update = true; |
| 77 |
} else if (argv[optind].equals("--")) { |
| 78 |
optind++; |
| 79 |
break; |
| 80 |
} else if (argv[optind].startsWith("-")) { |
| 81 |
System.out.println( |
| 82 |
"Usage: answer [-L url] [-T protocol] [-H host] [-U user] [-P password]\n" + |
| 83 |
"\t[-f mailbox] [-v] [-u] [-a] [msgno ...]"); |
| 84 |
System.exit(1); |
| 85 |
} else { |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
try { |
| 91 |
// Get a Properties object |
| 92 |
Properties props = System.getProperties(); |
| 93 |
|
| 94 |
// Get a Session object |
| 95 |
Session session = Session.getDefaultInstance(props, |
| 96 |
new TtyAuthenticator()); |
| 97 |
|
| 98 |
// Get a Store object |
| 99 |
Store store = null; |
| 100 |
if (url != null) { |
| 101 |
URLName urln = new URLName(url); |
| 102 |
store = session.getStore(urln); |
| 103 |
store.connect(); |
| 104 |
} else { |
| 105 |
if (protocol != null) |
| 106 |
store = session.getStore(protocol); |
| 107 |
else |
| 108 |
store = session.getStore(); |
| 109 |
|
| 110 |
// Connect |
| 111 |
if (host != null || user != null || password != null) |
| 112 |
store.connect(host, user, password); |
| 113 |
else |
| 114 |
store.connect(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
// Open the Folder |
| 119 |
|
| 120 |
Folder folder = store.getDefaultFolder(); |
| 121 |
if (folder == null) { |
| 122 |
System.out.println("No default folder"); |
| 123 |
System.exit(1); |
| 124 |
} |
| 125 |
|
| 126 |
folder = folder.getFolder(mbox); |
| 127 |
if (folder == null) { |
| 128 |
System.out.println("Invalid folder"); |
| 129 |
System.exit(1); |
| 130 |
} |
| 131 |
|
| 132 |
if (optind < argv.length || update) |
| 133 |
folder.open(Folder.READ_WRITE); |
| 134 |
else |
| 135 |
folder.open(Folder.READ_ONLY); |
| 136 |
int totalMessages = folder.getMessageCount(); |
| 137 |
|
| 138 |
if (totalMessages == 0) { |
| 139 |
System.out.println("Empty folder"); |
| 140 |
folder.close(false); |
| 141 |
store.close(); |
| 142 |
System.exit(1); |
| 143 |
} |
| 144 |
|
| 145 |
if (verbose) { |
| 146 |
int newMessages = folder.getNewMessageCount(); |
| 147 |
pv("Total messages = " + totalMessages); |
| 148 |
pv("New messages = " + newMessages); |
| 149 |
pv("-------------------------------"); |
| 150 |
} |
| 151 |
|
| 152 |
if (optind >= argv.length) { |
| 153 |
// get all messages that aren't answered or flagged |
| 154 |
Flags f = new Flags(); |
| 155 |
f.add(Flags.Flag.ANSWERED); |
| 156 |
f.add(Flags.Flag.FLAGGED); |
| 157 |
|
| 158 |
// Use a suitable FetchProfile |
| 159 |
FetchProfile fp = new FetchProfile(); |
| 160 |
fp.add(FetchProfile.Item.ENVELOPE); |
| 161 |
fp.add(FetchProfile.Item.FLAGS); |
| 162 |
|
| 163 |
Message[] msgs = folder.search(new FlagTerm(f, false)); |
| 164 |
folder.fetch(msgs, fp); |
| 165 |
|
| 166 |
if (update) { |
| 167 |
doUpdate(msgs); |
| 168 |
// re-fetch messages |
| 169 |
msgs = folder.search(new FlagTerm(f, false)); |
| 170 |
pv(""); |
| 171 |
pv(""); |
| 172 |
} |
| 173 |
|
| 174 |
System.out.println("Unanswered messages:"); |
| 175 |
for (int i = 0; i < msgs.length; i++) { |
| 176 |
Message msg = msgs[i]; |
| 177 |
System.out.println("--------------------------"); |
| 178 |
System.out.println("MESSAGE #" + |
| 179 |
msg.getMessageNumber() + ":"); |
| 180 |
Question q = new Question(msg); |
| 181 |
System.out.println(q); |
| 182 |
if (q.isReply()) |
| 183 |
System.out.println( |
| 184 |
"A reply to a message we haven't seen"); |
| 185 |
if (markAll) { |
| 186 |
int msgno = msg.getMessageNumber(); |
| 187 |
if (q.isReply()) { |
| 188 |
pv("Flagging message #" + msgno); |
| 189 |
msg.setFlag(Flags.Flag.FLAGGED, true); |
| 190 |
} else { |
| 191 |
pv("Answering message #" + msgno); |
| 192 |
msg.setFlag(Flags.Flag.ANSWERED, true); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
} else { |
| 198 |
for (int i = optind; i < argv.length; i++) { |
| 199 |
int msgno = Integer.parseInt(argv[i]); |
| 200 |
Message msg = folder.getMessage(msgno); |
| 201 |
Question q = new Question(msg); |
| 202 |
if (q.isReply()) { |
| 203 |
pv("Flagging message #" + msgno); |
| 204 |
msg.setFlag(Flags.Flag.FLAGGED, true); |
| 205 |
} else { |
| 206 |
pv("Answering message #" + msgno); |
| 207 |
msg.setFlag(Flags.Flag.ANSWERED, true); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
folder.close(false); |
| 213 |
store.close(); |
| 214 |
} catch (Exception ex) { |
| 215 |
System.out.println("Oops, got exception! " + ex.getMessage()); |
| 216 |
ex.printStackTrace(); |
| 217 |
System.exit(1); |
| 218 |
} |
| 219 |
System.exit(0); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Update the mailbox to account for any replies. |
| 224 |
*/ |
| 225 |
static void doUpdate(Message[] msgs) throws MessagingException { |
| 226 |
// a hash table of all the unanswered questions |
| 227 |
Hashtable qt = new Hashtable(); |
| 228 |
|
| 229 |
Address javamailAddress = new InternetAddress("[email protected]"); |
| 230 |
SearchTerm javamail = new OrTerm( |
| 231 |
new RecipientTerm(Message.RecipientType.TO, javamailAddress), |
| 232 |
new RecipientTerm(Message.RecipientType.CC, javamailAddress)); |
| 233 |
|
| 234 |
for (int i = 0; i < msgs.length; i++) { |
| 235 |
Message msg = msgs[i]; |
| 236 |
pv("--------------------------"); |
| 237 |
pv("MESSAGE #" + msg.getMessageNumber() + ":"); |
| 238 |
Question q = new Question(msg); |
| 239 |
pv(q.toString()); |
| 240 |
Question q0; |
| 241 |
if ((q0 = (Question)qt.get(q)) != null) { |
| 242 |
pv("Found in table"); |
| 243 |
if (q.isReply()) { |
| 244 |
Message qm = q0.getMessage(); |
| 245 |
if (!qm.isSet(Flags.Flag.ANSWERED)) { |
| 246 |
pv("Answered:"); |
| 247 |
pv(q0.toString()); |
| 248 |
qm.setFlag(Flags.Flag.ANSWERED, true); |
| 249 |
} |
| 250 |
pv("is reply, flagging it"); |
| 251 |
msg.setFlag(Flags.Flag.FLAGGED, true); |
| 252 |
} else { |
| 253 |
// a second copy of a message that's not a reply? |
| 254 |
// shouldn't happen. |
| 255 |
} |
| 256 |
} else { |
| 257 |
pv("NOT found in table"); |
| 258 |
if (q.isReply()) { |
| 259 |
// a reply to a message we haven't seen? |
| 260 |
pv("flagging, but no original msg"); |
| 261 |
msg.setFlag(Flags.Flag.FLAGGED, true); |
| 262 |
} else { |
| 263 |
// an original question, put it in the table |
| 264 |
qt.put(q, q); |
| 265 |
// if the message looks like spam, flag it |
| 266 |
if (!javamail.match(msg)) { |
| 267 |
pv("SPAM!!!"); |
| 268 |
msg.setFlag(Flags.Flag.FLAGGED, true); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Print verbose. |
| 277 |
*/ |
| 278 |
static void pv(String s) { |
| 279 |
if (verbose) |
| 280 |
System.out.println(s); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* This class represents a single "question" sent to [email protected], |
| 286 |
* or possibly a reply to such a question. |
| 287 |
*/ |
| 288 |
class Question { |
| 289 |
Message msg; |
| 290 |
Address sender; |
| 291 |
Address[] recipients; |
| 292 |
String subject; |
| 293 |
Date date; |
| 294 |
boolean reply = false; |
| 295 |
|
| 296 |
Question(Message msg) throws MessagingException { |
| 297 |
this.msg = msg; |
| 298 |
sender = msg.getReplyTo()[0]; // XXX - assume only one |
| 299 |
subject = msg.getSubject(); |
| 300 |
if (subject == null) |
| 301 |
subject = ""; |
| 302 |
else if (subject.regionMatches(true, 0, "Re: ", 0, 4)) { |
| 303 |
subject = subject.substring(4); |
| 304 |
reply = true; |
| 305 |
recipients = msg.getRecipients(Message.RecipientType.TO); |
| 306 |
} |
| 307 |
subject = subject.trim(); |
| 308 |
date = msg.getSentDate(); |
| 309 |
if (date == null) |
| 310 |
date = msg.getReceivedDate(); |
| 311 |
} |
| 312 |
|
| 313 |
public boolean isReply() { |
| 314 |
return reply; |
| 315 |
} |
| 316 |
|
| 317 |
public Message getMessage() { |
| 318 |
return msg; |
| 319 |
} |
| 320 |
|
| 321 |
public int hashCode() { |
| 322 |
return subject.hashCode(); |
| 323 |
} |
| 324 |
|
| 325 |
public boolean equals(Object obj) { |
| 326 |
if (!(obj instanceof Question)) |
| 327 |
return false; |
| 328 |
Question q = (Question)obj; |
| 329 |
if (this.reply == q.reply) |
| 330 |
return this.sender.equals(q.sender) && |
| 331 |
this.subject.equals(q.subject); |
| 332 |
|
| 333 |
/* |
| 334 |
* A reply is equal to a question if the sender of the question |
| 335 |
* is one of the recipients of the reply. |
| 336 |
*/ |
| 337 |
Question qq, qr; |
| 338 |
if (this.reply) { |
| 339 |
qq = q; |
| 340 |
qr = this; |
| 341 |
} else { |
| 342 |
qq = this; |
| 343 |
qr = q; |
| 344 |
} |
| 345 |
for (int i = 0; i < qr.recipients.length; i++) { |
| 346 |
if (qq.sender.equals(qr.recipients[i])) |
| 347 |
return true; |
| 348 |
} |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
public String toString() { |
| 353 |
return "Date: " + date + "\nFrom: " + sender + "\nSubject: " + subject; |
| 354 |
} |
| 355 |
} |