| 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.text.*; |
| 13 |
import java.io.*; |
| 14 |
import jakarta.mail.*; |
| 15 |
import jakarta.mail.search.*; |
| 16 |
import jakarta.mail.internet.*; |
| 17 |
|
| 18 |
/** |
| 19 |
* Program that generates stats about new messages received per week |
| 20 |
* by the [email protected] mailing list. It tracks *new* messages only, |
| 21 |
* the rule being that the message has a single addressee: |
| 22 |
* "[email protected]". Also searches each message body for the "pop3" |
| 23 |
* string and counts its occurrence. |
| 24 |
* |
| 25 |
* Note that this program operates on the log file directly and thus all |
| 26 |
* access to the log file using this program must be through the same |
| 27 |
* IMAP server so that the flags are handled consistently. |
| 28 |
* |
| 29 |
* @author Max Spivak |
| 30 |
*/ |
| 31 |
|
| 32 |
public class msgsperweek { |
| 33 |
|
| 34 |
static String protocol = "imap"; |
| 35 |
static String host = "anybodys.sfbay"; |
| 36 |
static String user = "javamail"; |
| 37 |
static String password = "1javamail1"; |
| 38 |
static String mbox = |
| 39 |
"/net/anybodys.sfbay/export6/javamail/logs/javamail.log"; |
| 40 |
static String url = null; |
| 41 |
static boolean verbose = false; |
| 42 |
static boolean doPop3 = false; |
| 43 |
static Calendar cal = null; |
| 44 |
|
| 45 |
public static void main(String argv[]) { |
| 46 |
int optind; |
| 47 |
|
| 48 |
for (optind = 0; optind < argv.length; optind++) { |
| 49 |
if (argv[optind].equals("-T")) { |
| 50 |
protocol = argv[++optind]; |
| 51 |
} else if (argv[optind].equals("-H")) { |
| 52 |
host = argv[++optind]; |
| 53 |
} else if (argv[optind].equals("-U")) { |
| 54 |
user = argv[++optind]; |
| 55 |
} else if (argv[optind].equals("-P")) { |
| 56 |
password = argv[++optind]; |
| 57 |
} else if (argv[optind].equals("-v")) { |
| 58 |
verbose = true; |
| 59 |
} else if (argv[optind].equals("-f")) { |
| 60 |
mbox = argv[++optind]; |
| 61 |
} else if (argv[optind].equals("-L")) { |
| 62 |
url = argv[++optind]; |
| 63 |
} else if (argv[optind].equals("-p")) { |
| 64 |
doPop3 = true; |
| 65 |
} else if (argv[optind].equals("--")) { |
| 66 |
optind++; |
| 67 |
break; |
| 68 |
} else if (argv[optind].startsWith("-")) { |
| 69 |
System.out.println( |
| 70 |
"Usage: msgperweek [-L url] [-T protocol] [-H host] [-U user] [-P password]\n"+ |
| 71 |
"\t[-f mailbox] [-v] [-p]"); |
| 72 |
System.exit(1); |
| 73 |
} else { |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
System.out.println("msgsperweek: Generating stats, please wait..."); |
| 79 |
|
| 80 |
cal = Calendar.getInstance(); |
| 81 |
|
| 82 |
try { |
| 83 |
// Get a Properties object |
| 84 |
Properties props = System.getProperties(); |
| 85 |
|
| 86 |
// Get a Session object |
| 87 |
Session session = Session.getDefaultInstance(props, |
| 88 |
new TtyAuthenticator()); |
| 89 |
|
| 90 |
//if (verbose) |
| 91 |
//session.setDebug(true); |
| 92 |
|
| 93 |
// Get a Store object |
| 94 |
Store store = null; |
| 95 |
if (url != null) { |
| 96 |
URLName urln = new URLName(url); |
| 97 |
store = session.getStore(urln); |
| 98 |
store.connect(); |
| 99 |
} else { |
| 100 |
if (protocol != null) |
| 101 |
store = session.getStore(protocol); |
| 102 |
else |
| 103 |
store = session.getStore(); |
| 104 |
|
| 105 |
// Connect |
| 106 |
if (host != null || user != null || password != null) |
| 107 |
store.connect(host, user, password); |
| 108 |
else |
| 109 |
store.connect(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
// Open the Folder |
| 114 |
|
| 115 |
Folder folder = store.getDefaultFolder(); |
| 116 |
if (folder == null) { |
| 117 |
System.out.println("No default folder"); |
| 118 |
System.exit(1); |
| 119 |
} |
| 120 |
|
| 121 |
folder = folder.getFolder(mbox); |
| 122 |
if (folder == null) { |
| 123 |
System.out.println("Invalid folder"); |
| 124 |
System.exit(1); |
| 125 |
} |
| 126 |
|
| 127 |
folder.open(Folder.READ_ONLY); |
| 128 |
int totalMessages = folder.getMessageCount(); |
| 129 |
|
| 130 |
if (totalMessages == 0) { |
| 131 |
System.out.println("Empty folder"); |
| 132 |
folder.close(false); |
| 133 |
store.close(); |
| 134 |
System.exit(1); |
| 135 |
} |
| 136 |
|
| 137 |
if (verbose) { |
| 138 |
int newMessages = folder.getNewMessageCount(); |
| 139 |
pv("Total messages = " + totalMessages); |
| 140 |
pv("New messages = " + newMessages); |
| 141 |
pv("-------------------------------"); |
| 142 |
} |
| 143 |
|
| 144 |
// Use a suitable FetchProfile |
| 145 |
FetchProfile fp = new FetchProfile(); |
| 146 |
fp.add(FetchProfile.Item.ENVELOPE); |
| 147 |
|
| 148 |
SearchTerm term = new RecipientTerm(Message.RecipientType.TO, |
| 149 |
new InternetAddress("[email protected]")); |
| 150 |
Message[] msgs = folder.search(term); |
| 151 |
folder.fetch(msgs, fp); |
| 152 |
|
| 153 |
Hashtable wks = new Hashtable(); |
| 154 |
int totalCount = 0; |
| 155 |
BodyTerm pop3Search = new BodyTerm("pop3"); |
| 156 |
int pop3Requests = 0; |
| 157 |
|
| 158 |
// go through each msg and count it if it's an incoming msg |
| 159 |
for (int i = 0; i < msgs.length; i++) { |
| 160 |
// make sure we only have a single addressee: |
| 161 |
// [email protected], which means it's a new message |
| 162 |
// from outside of Sun |
| 163 |
Address[] recs=msgs[i].getRecipients(Message.RecipientType.TO); |
| 164 |
if (recs.length > 1) |
| 165 |
continue; |
| 166 |
|
| 167 |
// get msgs date |
| 168 |
Date d = msgs[i].getSentDate(); |
| 169 |
if (d == null) |
| 170 |
d = msgs[i].getReceivedDate(); |
| 171 |
cal.setTime(d); |
| 172 |
|
| 173 |
// figure out what week it is |
| 174 |
int week = cal.get(Calendar.WEEK_OF_YEAR); |
| 175 |
int yr = cal.get(Calendar.YEAR); |
| 176 |
String wkInYr = week + " " + yr; |
| 177 |
|
| 178 |
// increment that week's count |
| 179 |
String num = (String)wks.get(wkInYr); |
| 180 |
totalCount++; |
| 181 |
if (num == null) |
| 182 |
wks.put(wkInYr, "1"); |
| 183 |
else { |
| 184 |
int count = Integer.parseInt(num); |
| 185 |
count++; |
| 186 |
Integer str = new Integer(count); |
| 187 |
wks.put(wkInYr, str.toString()); |
| 188 |
} |
| 189 |
|
| 190 |
// check for pop3 requests |
| 191 |
if (doPop3 && pop3Search.match(msgs[i])) |
| 192 |
pop3Requests++; |
| 193 |
} |
| 194 |
|
| 195 |
// print out the statistics |
| 196 |
int startWk = 40; |
| 197 |
int startYr = 1997; |
| 198 |
Calendar now = Calendar.getInstance(); |
| 199 |
now.setTime(new Date()); |
| 200 |
cal.set(startYr, 1, 1); |
| 201 |
cal.set(Calendar.WEEK_OF_YEAR, startWk); |
| 202 |
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); |
| 203 |
DateFormat df = new SimpleDateFormat("E MMM dd yyyy"); |
| 204 |
int totalWks = 0; |
| 205 |
|
| 206 |
System.out.println("\nNumber of new messages a week sent to [email protected] by external JavaMail\nusers. This does not include our answers and follow-up mail."); |
| 207 |
for (;;) { |
| 208 |
if (cal.after(now)) |
| 209 |
break; |
| 210 |
else { |
| 211 |
totalWks++; |
| 212 |
String wkInYr = (cal.get(Calendar.WEEK_OF_YEAR)) + |
| 213 |
" " + |
| 214 |
(cal.get(Calendar.YEAR)); |
| 215 |
Object o = wks.get(wkInYr); |
| 216 |
if (o != null) { |
| 217 |
String i = (String)o; |
| 218 |
System.out.println(" week of " + |
| 219 |
df.format(cal.getTime()) + |
| 220 |
": " + |
| 221 |
i + " messages"); |
| 222 |
} |
| 223 |
|
| 224 |
// increment to next week |
| 225 |
cal.add(Calendar.WEEK_OF_YEAR, 1); |
| 226 |
} |
| 227 |
} |
| 228 |
System.out.println("------------------"); |
| 229 |
System.out.println("Total messages received: " + totalCount); |
| 230 |
System.out.println("Average messages/week: " + |
| 231 |
totalCount/totalWks); |
| 232 |
if (doPop3) |
| 233 |
System.out.println("Total POP3 requests: " + pop3Requests); |
| 234 |
folder.close(false); |
| 235 |
store.close(); |
| 236 |
} catch (Exception ex) { |
| 237 |
System.out.println("Oops, got exception! " + ex.getMessage()); |
| 238 |
ex.printStackTrace(); |
| 239 |
} |
| 240 |
System.exit(1); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* Print verbose. |
| 246 |
*/ |
| 247 |
static void pv(String s) { |
| 248 |
if (verbose) |
| 249 |
System.out.println(s); |
| 250 |
} |
| 251 |
} |