1 7 8 import java.util.*; 9 import java.text.*; 10 import java.io.*; 11 import javax.mail.*; 12 import javax.mail.search.*; 13 import javax.mail.internet.*; 14 15 28 29 public class msgsperweek { 30 31 static String protocol = "imap"; 32 static String host = "anybodys.sfbay"; 33 static String user = "javamail"; 34 static String password = "1javamail1"; 35 static String mbox = 36 "/net/anybodys.sfbay/export6/javamail/logs/javamail.log"; 37 static String url = null; 38 static boolean verbose = false; 39 static boolean doPop3 = false; 40 static Calendar cal = null; 41 42 public static void main(String argv[]) { 43 int optind; 44 45 for (optind = 0; optind < argv.length; optind++) { 46 if (argv[optind].equals("-T")) { 47 protocol = argv[++optind]; 48 } else if (argv[optind].equals("-H")) { 49 host = argv[++optind]; 50 } else if (argv[optind].equals("-U")) { 51 user = argv[++optind]; 52 } else if (argv[optind].equals("-P")) { 53 password = argv[++optind]; 54 } else if (argv[optind].equals("-v")) { 55 verbose = true; 56 } else if (argv[optind].equals("-f")) { 57 mbox = argv[++optind]; 58 } else if (argv[optind].equals("-L")) { 59 url = argv[++optind]; 60 } else if (argv[optind].equals("-p")) { 61 doPop3 = true; 62 } else if (argv[optind].equals("--")) { 63 optind++; 64 break; 65 } else if (argv[optind].startsWith("-")) { 66 System.out.println( 67 "Usage: msgperweek [-L url] [-T protocol] [-H host] [-U user] [-P password]\n"+ 68 "\t[-f mailbox] [-v] [-p]"); 69 System.exit(1); 70 } else { 71 break; 72 } 73 } 74 75 System.out.println("msgsperweek: Generating stats, please wait..."); 76 77 cal = Calendar.getInstance(); 78 79 try { 80 Properties props = System.getProperties(); 82 83 Session session = Session.getDefaultInstance(props, 85 new TtyAuthenticator()); 86 87 90 Store store = null; 92 if (url != null) { 93 URLName urln = new URLName(url); 94 store = session.getStore(urln); 95 store.connect(); 96 } else { 97 if (protocol != null) 98 store = session.getStore(protocol); 99 else 100 store = session.getStore(); 101 102 if (host != null || user != null || password != null) 104 store.connect(host, user, password); 105 else 106 store.connect(); 107 } 108 109 110 112 Folder folder = store.getDefaultFolder(); 113 if (folder == null) { 114 System.out.println("No default folder"); 115 System.exit(1); 116 } 117 118 folder = folder.getFolder(mbox); 119 if (folder == null) { 120 System.out.println("Invalid folder"); 121 System.exit(1); 122 } 123 124 folder.open(Folder.READ_ONLY); 125 int totalMessages = folder.getMessageCount(); 126 127 if (totalMessages == 0) { 128 System.out.println("Empty folder"); 129 folder.close(false); 130 store.close(); 131 System.exit(1); 132 } 133 134 if (verbose) { 135 int newMessages = folder.getNewMessageCount(); 136 pv("Total messages = " + totalMessages); 137 pv("New messages = " + newMessages); 138 pv("-------------------------------"); 139 } 140 141 FetchProfile fp = new FetchProfile(); 143 fp.add(FetchProfile.Item.ENVELOPE); 144 145 SearchTerm term = new RecipientTerm(Message.RecipientType.TO, 146 new InternetAddress("javamail@sun.com")); 147 Message[] msgs = folder.search(term); 148 folder.fetch(msgs, fp); 149 150 Hashtable wks = new Hashtable(); 151 int totalCount = 0; 152 BodyTerm pop3Search = new BodyTerm("pop3"); 153 int pop3Requests = 0; 154 155 for (int i = 0; i < msgs.length; i++) { 157 Address[] recs=msgs[i].getRecipients(Message.RecipientType.TO); 161 if (recs.length > 1) 162 continue; 163 164 Date d = msgs[i].getSentDate(); 166 if (d == null) 167 d = msgs[i].getReceivedDate(); 168 cal.setTime(d); 169 170 int week = cal.get(Calendar.WEEK_OF_YEAR); 172 int yr = cal.get(Calendar.YEAR); 173 String wkInYr = week + " " + yr; 174 175 String num = (String )wks.get(wkInYr); 177 totalCount++; 178 if (num == null) 179 wks.put(wkInYr, "1"); 180 else { 181 int count = Integer.parseInt(num); 182 count++; 183 Integer str = new Integer (count); 184 wks.put(wkInYr, str.toString()); 185 } 186 187 if (doPop3 && pop3Search.match(msgs[i])) 189 pop3Requests++; 190 } 191 192 int startWk = 40; 194 int startYr = 1997; 195 Calendar now = Calendar.getInstance(); 196 now.setTime(new Date()); 197 cal.set(startYr, 1, 1); 198 cal.set(Calendar.WEEK_OF_YEAR, startWk); 199 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 200 DateFormat df = new SimpleDateFormat("E MMM dd yyyy"); 201 int totalWks = 0; 202 203 System.out.println("\nNumber of new messages a week sent to javamail@Sun.COM by external JavaMail\nusers. This does not include our answers and follow-up mail."); 204 for (;;) { 205 if (cal.after(now)) 206 break; 207 else { 208 totalWks++; 209 String wkInYr = (cal.get(Calendar.WEEK_OF_YEAR)) + 210 " " + 211 (cal.get(Calendar.YEAR)); 212 Object o = wks.get(wkInYr); 213 if (o != null) { 214 String i = (String )o; 215 System.out.println(" week of " + 216 df.format(cal.getTime()) + 217 ": " + 218 i + " messages"); 219 } 220 221 cal.add(Calendar.WEEK_OF_YEAR, 1); 223 } 224 } 225 System.out.println("------------------"); 226 System.out.println("Total messages received: " + totalCount); 227 System.out.println("Average messages/week: " + 228 totalCount/totalWks); 229 if (doPop3) 230 System.out.println("Total POP3 requests: " + pop3Requests); 231 folder.close(false); 232 store.close(); 233 } catch (Exception ex) { 234 System.out.println("Oops, got exception! " + ex.getMessage()); 235 ex.printStackTrace(); 236 } 237 System.exit(1); 238 } 239 240 241 244 static void pv(String s) { 245 if (verbose) 246 System.out.println(s); 247 } 248 } 249 | Popular Tags |