-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchMail.java
More file actions
562 lines (480 loc) · 18.6 KB
/
SearchMail.java
File metadata and controls
562 lines (480 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package Jvakt;
/*
* 2024-07-09 V.57 Michael Ekdal Added more info when reporting in status to Jvakt server.
* 2023-11-08 V.56 Michael Ekdal Removed some System.out.println. They can cause problem in Linux when null.
* 2023-01-10 V.55 Michael Ekdal Added send of the status to Jvakt server
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import jakarta.mail.*;
import jakarta.mail.search.*;
import java.util.*;
import Jvakt.Message;
import java.io.*;
import java.net.InetAddress;
public class SearchMail {
static boolean msgFound = false;
static boolean msgFixat = false;
static boolean swHtml = false;
static boolean swHelp = false;
static String msgId;
static boolean swGoodMail = true;
static boolean swExMail = true;
static boolean swDormant = false;
static boolean swOKtot = false;
static String jvrc = "";
static String Sid = null;
static String Soptional = null;
static String Stype = null;
static String Sfrom = null;
static String Ssubject = null;
static String Sbody = null;
static boolean swfrom = false;
static boolean swsubject = false;
static boolean swbody = false;
static boolean swDelete = false;
static boolean swParfile = false;
static List<String> listToS;
// static String parFile = "SearchMail";
static String element;
static char[] parStatus; // Mark with a F when the a parameter line is matched with a mail
static String from;
static String subject;
static String body;
static int antal;
static int detached;
static String agent = null;
static String jvhost = "localhost";
static String jvport = "1956";
static int jvporti ;
static String uname;
static String pwd;
static String imaphost;
static String imapport;
static String imapssl;
static String imaprw;
static String imapFolder;
static String config = null;
static File configF;
static InetAddress inet;
public static void main(String[] args) throws IOException, FileNotFoundException {
String version = "SearchMail ";
version += getVersion()+".57";
for (int i=0; i<args.length; i++) {
if (args[i].equalsIgnoreCase("-config")) config = args[++i];
if (args[i].equalsIgnoreCase("-help")) swHelp = true;
if (args[i].equalsIgnoreCase("-?")) swHelp = true;
}
if (swHelp || args.length==0) {
System.out.println("--- " + version + " ---");
System.out.println("by Michael Ekdal Perstorp Sweden.\n");
System.out.println("Search for specific mails identified by the rows in he csv files");
System.out
.println("\nThe parameters and their meaning are:\n"
+ "\n-config \tThe directory of the input files. Like: \"-dir c:\\Temp\" "
+ "\n \t Jvakt.properties and SearchMail-01.csv will be needed."
);
System.exit(4);
}
if (config == null ) configF = new File("Jvakt.properties");
else configF = new File(config,"Jvakt.properties");
System.out.println("\n"+new Date()+" ----- Jvakt ---- Version: "+version);
System.out.println(new Date()+" -config file: "+configF);
Properties props = new Properties();
String provider = "imap";
int imapPort = 993;
Address[] adr;
try {
getProps(); // get Jvakt properties
readParFile(); // read parameter file
SendMsg jm = new SendMsg(jvhost, jvporti); // kollar om JvaktServer är tillgänglig.
jvrc = jm.open();
System.out.println(new Date()+" Jvakt server status: "+jvrc);
// if (jvrc.toLowerCase().startsWith("dormant")) swDormant = true;
if (jvrc.toLowerCase().startsWith("failed")) {
swDormant = true;
System.out.println(new Date()+" ----- Access to Jvakt failed!");
}
jm.close();
if (swDormant) {
// System.out.println(new Date()+" *** Jvakt in DORMANT mode or unreachable, GetSpecificImap4mail exiting *** ");
System.out.println(new Date()+" *** Jvakt unreachable, SearchMail exiting *** ");
System.exit(4);
}
imapPort = Integer.parseInt(imapport);
// Connect to the imap4 server
if (imapssl.startsWith("N")) {
props.put("mail.imap.ssl.enable", "false"); // plain
provider = "imap";
}
else {
props.put("mail.imap.ssl.enable", "true"); // secure
provider = "imaps";
}
props.put("mail.imap.ssl.trust", "*");
props.put("mail.imaps.ssl.trust", "*");
System.out.println(new Date()+" Initiating connection to imap4 server....");
System.out.println(new Date()+" Connection... " + imaphost+":"+imapport+" User: "+uname);
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(provider);
try {
store.connect(imaphost, imapPort, uname, pwd);
} catch (Exception e) {
System.out.println(new Date()+" Connection to " + imaphost+":"+imapport+" User "+uname+" failed!");
System.out.println(new Date()+" "+e);
System.exit(8);
}
System.out.println(new Date()+" Connection to imap4 server established.");
// Open the folder
System.out.println(new Date()+" Open folder: " + imapFolder);
Folder inbox = store.getFolder(imapFolder);
if (inbox == null) {
System.out.println(new Date()+" Mailbox folder "+imapFolder+" not found!");
System.exit(1);
}
if (imaprw.startsWith("Y")) inbox.open(Folder.READ_WRITE); // remove the marked messages from the server
else inbox.open(Folder.READ_ONLY);
jakarta.mail.Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
System.out.println(new Date()+" **** Number of rows -> " +listToS.size());
System.out.println(new Date()+" **** Number of mails-> " +messages.length);
// Reads all the unseen messages (mails)
for (int i = 0; i < messages.length; i++) {
adr = messages[i].getFrom();
from = null;
if (adr != null) {
for (int j = 0; j < adr.length; j++) {
from = adr[j].toString();
}
} else {
from = "unknown";
}
System.out.println("\n"+new Date()+" >>> Mail from From: " + from );
subject = messages[i].getSubject();
System.out.println(new Date()+" MimeType> " + messages[i].getContentType());
System.out.println(new Date()+" Subject: " + subject);
body = "";
swHtml = false;
Object o = null;
if (messages[i].isMimeType("text/plain") || messages[i].isMimeType("text/html") ) {
try {
o = messages[i].getContent();
body = (String)o;
} catch (IOException e) {
body = "";
System.out.println(new Date()+" The content of the body was not retrieved : " + e);
}
if (messages[i].isMimeType("text/html")) swHtml = true;
}
if (messages[i].getContentType().startsWith("multipart")) {
System.out.println(new Date()+" *** multipart body, will scan the parts... ");
Multipart mp = (Multipart)messages[i].getContent();
System.out.println(new Date()+" *** Number of body parts: " + mp.getCount());
for (int j=0, n=mp.getCount(); j<n; j++) {
Part part = mp.getBodyPart(j);
// System.out.println(new Date()+" *** Part "+j+" description : "+part.getDescription());
// System.out.println(new Date()+" *** Part "+j+" ContentType : "+part.getContentType());
// System.out.println(new Date()+" *** Part "+j+" Disposition : "+part.getDisposition());
if (part.getContentType().contains("text/")) {
if (body.length()<=0) body = part.getContent().toString();
else body += part.getContent().toString();
// System.out.println(new Date()+" *** Body Content : "+body);
}
// else if (part.getDisposition().contains("attachment")) {
// System.out.println(new Date()+" *** File name: "+part.getFileName());
// body = part.getContent().toString();
// System.out.println(new Date()+" *** Attachment Content : "+body);
// }
}
}
if (body == null) body = "null.";
//********************************
System.out.println(new Date()+" From -> " + from);
System.out.println(new Date()+" Subject-> " + subject);
// System.out.println(new Date()+" Body -> " + body);
// Checks the mail for a match of all the rows in the parameter file
for (int i2 = 0; i2 < listToS.size(); i2++) {
msgFixat = false;swfrom = false; swsubject = false; swbody = false;
swGoodMail = false; swExMail = false; msgFound = false;
Object object = listToS.get(i2);
element = (String) object;
System.out.println("*** ParRow * " + element); // Shows the parameter rows in action
// System.out.println("*** ParStatus * " + parStatus[i2]); // Shows the Status of the line. F indicates a previous match
String[] tab = null;
tab = element.split(";");
Sid = tab[0].trim();
Soptional = tab[1].toUpperCase().trim();
if (tab[2].toUpperCase().trim().startsWith("D")) swDelete = true;
else swDelete = false;
Stype = tab[3].toUpperCase().trim();
Sfrom = tab[4].trim();
Ssubject = tab[5].trim();
Sbody = tab[6].trim();
if (Stype.startsWith("G")) swGoodMail = true;
if (Soptional.startsWith("M")) swExMail = true;
if (Sfrom==null) swfrom=true;
if (Ssubject==null) swsubject=true;
if (Sbody==null) swbody=true;
// if (subject == null) subject = "null.";
if (from.toLowerCase().indexOf(Sfrom.toLowerCase()) >= 0) swfrom=true;
if (subject.toLowerCase().indexOf(Ssubject.toLowerCase()) >= 0) swsubject=true;
if (body.toLowerCase().indexOf(Sbody.toLowerCase()) >= 0) swbody=true;
if (swfrom && swsubject && swbody ) {
System.out.println(new Date()+" * Mail found!");
msgFixat = true;
msgFound = true;
}
if (msgFixat && imaprw.startsWith("Y")) {
if (swDelete) {
System.out.println(new Date()+" * Mark as DELETED ");
messages[i].setFlag(Flags.Flag.DELETED, true);
}
else {
System.out.println(new Date()+" * Mark as SEEN ");
messages[i].setFlag(Flags.Flag.SEEN, true);
}
}
else {
System.out.println("* Mail ignored ");
messages[i].setFlag(Flags.Flag.SEEN, false); // markera mailet som oläst
}
if (msgFound ) {
parStatus[i2]='F';
if (swGoodMail) {
if (swExMail) {
// System.out.println(new Date()+" Expected mail from: "+ from +" / "+ subject +" / "+ body );
System.out.println(new Date()+" Expected mail from: "+ from +" / "+ subject );
sendJv(Sid , "OK" , "R", "From: "+ from +" / "+ subject +" / "+ body );
}
else {
// System.out.println(new Date()+" Expected mail from: "+ from +" / "+ subject +" / "+ body );
System.out.println(new Date()+" Expected mail from: "+ from +" / "+ subject);
sendJv(Sid , "INFO" , "T", "From: "+ from +" / "+ subject +" / "+ body );
}
}
else {
// System.out.println(new Date()+" From: "+ from +" / "+ subject +" / "+ body );
System.out.println(new Date()+" From: "+ from +" / "+ subject );
sendJv(Sid , "ERR" , "T", "From: "+ from +" / "+ subject +" / "+ body );
}
}
if (msgFound ) break; // read next mail
}
}
// Close the connection mails are all read
if (imaprw.startsWith("Y")) inbox.close(true); // remove the marked messages from the server
else inbox.close(false);
store.close();
swOKtot=true;
}
catch (Exception ex) {
swOKtot=false;
ex.printStackTrace();
}
// Loop the parameters an find the ones not matching a mail and report it to Jvakt server.
System.out.println("\n"+new Date()+" **** Missing mail section ****");
for (int i2 = 0; i2 < listToS.size(); i2++) {
Object object = listToS.get(i2);
element = (String) object;
// System.out.println("*** ParStatus * " + parStatus[i2]);
System.out.println("*** ParRow * " + element);
if (parStatus[i2]!='F' ) {
swGoodMail = false; swExMail = false;
String[] tab = null;
tab = element.split(";");
Sid = tab[0].trim();
Soptional = tab[1].toUpperCase().trim();
if (tab[2].toUpperCase().trim().startsWith("D")) swDelete = true;
else swDelete = false;
Stype = tab[3].toUpperCase().trim();
Sfrom = tab[4].trim();
Ssubject = tab[5].trim();
Sbody = tab[6].trim();
if (Stype.startsWith("G")) swGoodMail = true;
if (Soptional.startsWith("M")) swExMail = true;
if (swGoodMail && swExMail) {
System.out.println(new Date()+" Expected mail from "+Sfrom+" not found.");
sendJv(Sid , "ERR" , "R", "Expected mail from "+Sfrom+" not found!");
}
else {
// System.out.println(new Date()+" No mail from "+Sfrom+" or "+Ssubject+" or "+Sbody+" found!");
System.out.println(new Date()+" No mail from "+Sfrom+" found, which is fine!");
sendJv(Sid , "OK" , "T", "No mail from "+Sfrom+" found, which is fine!");
}
}
}
if (swOKtot ) try {sendSTS(true);} catch (IOException e) { e.printStackTrace();}
else try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
} // slut på pgm
// sends status to the Jvakt server
static protected void sendSTS( boolean STS) throws IOException {
System.out.println("--- Sending STS to "+jvhost+":"+jvport);
Message jmsg = new Message();
SendMsg jm = new SendMsg(jvhost, jvporti);
// System.out.println(jm.open());
jm.open();
jmsg.setId("Jvakt-SearchMail");
if (STS) {
jmsg.setBody("The SearchMail program is working. "+configF.getCanonicalPath());
jmsg.setRptsts("OK");
}
else {
jmsg.setBody("The SearchMail program is not working! "+configF.getCanonicalPath());
jmsg.setRptsts("ERR");
}
jmsg.setType("T");
inet = InetAddress.getLocalHost();
// System.out.println("-- Inet: "+inet);
agent = inet.toString();
jmsg.setAgent(agent);
if (!jm.sendMsg(jmsg)) System.out.println("--- Rpt to Jvakt Failed for SearchMail ---");
jm.close();
}
static protected void sendJv( String ID, String STS, String type, String msg) throws IOException {
Message jmsg = new Message();
SendMsg jm = new SendMsg(jvhost, jvporti);
System.out.println(new Date()+" Jvakt server status: "+jm.open());
jmsg.setId(ID);
jmsg.setRptsts(STS);
jmsg.setBody(msg);
jmsg.setType(type);
jmsg.setAgent(agent);
jmsg.setPrio(30);
if (jm.sendMsg(jmsg)) {
System.out.println(new Date()+" -- Rpt to Jvakt server Delivered -- " + ID +" - "+ STS +" - "+ type +" - "+ agent);
msgFixat = true;
}
else System.out.println(new Date()+"-- Rpt to Jvakt server Failed --");
jm.close();
}
// public static boolean scanFile(String filename, InputStream in) throws IOException {
//
// String s;
//
// if (filename == null) {
// System.out.println("Original filename <null> ");
// return(false);
// }
//
// if (filename.startsWith("=?")) {
// System.out.println("Original filename: "+filename);
// filename = ("noname.pdf");
// }
//
// // File file = new File("D_"+filename);
// System.out.println("In file : "+in);
// BufferedReader in2 = new BufferedReader(new InputStreamReader(in));
// antal++;
// // if (!filename.toLowerCase().endsWith(".TXT")) {
// // System.out.println("Kept : "+filename+"\tFrom: " + from +"\tSubject: " + subject);
// // return(false); }
//
// // byte[] buf = new byte[1024];
// // int len;
// while ((s = in2.readLine()) != null) {
// if (s.length() == 0) continue;
// if (s.indexOf("PXP") >= 0) {
//// swSmq1PXP = true;
// System.out.println("** Found PXP in input stream!");
// }
// if (s.indexOf("Entries Displayed: 0 ") >= 0) {
// System.out.println("** Found Entries Displayed: 0 !");
//// swSmq2PCP = true;
// }
// if (s.indexOf("Queues Displayed: 0 ") >= 0) {
// System.out.println("** Found Queues Displayed: 0 !");
//// swSmq2PCP = true;
// }
// int offset = 0;
// while (s.indexOf("Transaction executing", offset) >= 0) {
//// Sm58PCPerr++;
// offset = s.indexOf("Transaction executing", offset);
// offset++;
//// System.out.println("** Found Transaction executing: " + Sm58PCPerr );
//// swSmq1PXP = false;
// }
// }
// in2.close();
// in.close();
// detached++;
// return(true);
// }
static void getProps() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(configF);
prop.load(input);
// get the property value and print it out
jvport = prop.getProperty("jvport");
jvhost = prop.getProperty("jvhost");
jvporti = Integer.parseInt(jvport);
uname = prop.getProperty("smtpuser");
pwd = prop.getProperty("smtppwd");
if (pwd.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(pwd.substring(3));
String decodedString = new String(decodedBytes);
pwd=decodedString;
}
imaphost = prop.getProperty("imaphost");
imapport = prop.getProperty("imapport");
imapssl = prop.getProperty("imapSSL");
imaprw = prop.getProperty("imapRW");
imapFolder = prop.getProperty("imapFolder");
// int imapporti = Integer.parseInt(imapport);
System.out.println(new Date()+" getProps jvport:" + jvport + " jvhost:"+jvhost) ;
} catch (IOException ex) {
ex.printStackTrace();
}
try {
inet = InetAddress.getLocalHost();
System.out.println(new Date()+" -- Inet: "+inet);
agent = inet.toString();
}
catch (Exception e) { System.out.println(e); }
}
static void readParFile() {
File[] listf;
DirFilter df;
String s;
// File dir = new File(".");
File dir;
String suf = ".csv";
String pos = "SearchMail";
int antal = 0;
if (config == null ) dir = new File(".");
else dir = new File(config);
// System.out.println("\n*** parameter file: "+" "+dir+ pos+" "+suf);
listToS = new ArrayList<String>(); // id:mailadress.
df = new DirFilter(suf, pos);
listf = dir.listFiles(df);
System.out.println("\n*** Number of parameter files found: "+ listf.length);
try {
BufferedReader in;
for (int i = 0; i < listf.length; i++) {
System.out.println("-- Importing parfile: "+listf[i]);
in = new BufferedReader(new FileReader(listf[i]));
while ((s = in.readLine()) != null) {
if (s.length() == 0) continue;
if (s.startsWith("#")) continue;
listToS.add(s);
antal++;
// System.out.println("-- add: "+s);
}
in.close();
}
} catch (Exception e) { System.out.println(e); }
System.out.println("-- Number of rows imported: "+ antal);
parStatus = new char[antal];
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
}