-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPurgeImap4Msgs.java
More file actions
337 lines (286 loc) · 10.6 KB
/
PurgeImap4Msgs.java
File metadata and controls
337 lines (286 loc) · 10.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
package Jvakt;
/*
* 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 jakarta.mail.search.SearchTerm;
import java.util.*;
import java.io.*;
import java.net.InetAddress;
public class PurgeImap4Msgs {
// static boolean newMsgId = false;
static boolean msgFixat = false;
static boolean swHtml = false;
static boolean swHelp = false;
static String msgId;
static boolean swSunet = false;
static boolean swSerious = false;
static String from;
static String subject;
static String body;
static String Pfrom = null;
static String Psubject = null;
static String Pbody = null;
static boolean swPfrom = true;
static boolean swPsubject = true;
static boolean swPbody = true;
static int antal = 0;
static int antalDmarked = 0;
static int detached;
static String agent = null;
static String jvhost = "localhost";
static String jvport = "1956";
static int port ;
static String uname;
static String pwd;
static String imaphost;
static String imapport;
static String imapssl;
static String imaprw;
static String imapcopy;
static String imapdelete;
static String imapexpunge = "Y";
static String imapFolder;
static String imapFolderc;
static String config = null;
static File configF;
static InetAddress inet;
public static void main(String[] args) throws IOException, FileNotFoundException {
String version = "PurgeImap4Msgs ";
version += getVersion()+".54";
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) {
System.out.println("--- PurgeImap4Msgs --- ");
System.out.println("by Michael Ekdal Perstorp Sweden.\n");
System.out.println("Analyses mail directed to a mailbox folder");
System.exit(4);
}
if (config == null ) configF = new File("PurgeImap4Msgs.properties");
else configF = new File(config,"PurgeImap4Msgs.properties");
System.out.println("----- Jvakt: "+new Date()+" Version: "+version);
System.out.println("-config file: "+configF);
Properties props = new Properties();
String provider = "imap"; // plain
int imapPort = 993; // secure
// int imapPort = 143; // plain
Address[] adr;
// String[] hdr;
// String value;
// String c;
// boolean swFinns;
// System.out.println("\n*** Jvakt.PurgeImap4Msgs starting --- " + new Date());
try {
getProps(); // get Jvakt properties
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("Initiating connection to imap4 server....");
System.out.println("Connection... " + imaphost+":"+imapport+" User: "+uname);
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(provider);
//store.connect(host, username, password);
try {
store.connect(imaphost, imapPort, uname, pwd);
} catch (Exception e) {
System.out.println("Connection to " + imaphost+":"+imapport+" User "+uname+" Pass "+pwd+" failed!");
System.out.println(e);
System.exit(8);
}
System.out.println("Connection to imap4 server established.");
// Open the folder
System.out.println("Open folder: " + imapFolder);
Folder inbox = store.getFolder(imapFolder);
if (inbox == null) {
System.out.println("Mailbox folder souce is "+imapFolder+" not found!");
System.exit(1);
}
Folder rensat = null;
if (imapcopy.startsWith("Y")) {
rensat = store.getFolder(imapFolderc);
if (rensat == null) {
System.out.println("Mailbox folder target is "+imapFolderc+" 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);
// if (imaprw.startsWith("Y")) rensat.open(Folder.READ_WRITE); // remove the marked messages from the server
// else rensat.open(Folder.READ_ONLY);
// creates a search criterion
SearchTerm searchCondition = new SearchTerm() {
private static final long serialVersionUID = 5298994639594655420L;
@Override
// public boolean match(javax.mail.Message message) {
public boolean match(jakarta.mail.Message message) {
try {
if (Pfrom != null) {
swPfrom = false;
Address[] adr;
adr = message.getFrom();
from = null;
if (adr != null) {
// System.out.println("adr null ");
for (int j = 0; j < adr.length; j++) {
from = adr[j].toString();
}
} else {
from = "unknown";
}
if (from.contains(Pfrom)) swPfrom = true;
}
if (Psubject != null) {
swPsubject = false;
if (message.getSubject() != null) {
if (message.getSubject().contains(Psubject)) swPsubject = true;
}
}
if (Pbody != null) {
swPbody = false;
Object o = null;
if (message.getSubject() != null) {
if (message.isMimeType("text/plain") || message.isMimeType("text/html") ) {
try {
o = message.getContent();
body = (String)o;
} catch (IOException e) {
body = "";
}
if (message.isMimeType("text/html")) swHtml = true;
}
// if (body == null) subject = "null."; 2013-05-23
if (body == null) body = "null.";
if (body.contains(Pbody)) swPbody=true;
}
}
if (swPfrom && swPsubject && swPbody) {
return true;
}
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
};
// Get the messages from the server
// Fetch all messages from inbox folder
// javax.mail.Message[] messages = inbox.getMessages();
// Fetch unseen messages from inbox folder
// javax.mail.Message[] messages = inbox.search(searchCondition);
jakarta.mail.Message[] messages = inbox.search(searchCondition);
// javax.mail.Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
// System.out.println("\n------------ NewMessageCount " + inbox.getNewMessageCount() + " ------------");
// System.out.println("------------ UnreadMessageCount " + inbox.getUnreadMessageCount() + " ------------");
System.out.println("--- Number of mails: " +messages.length +" to purge...");
if (messages.length > 0 ) {
if (imaprw.startsWith("Y")) {
if (imapcopy.startsWith("Y")) {
System.out.println("\n*** Copy messages " + new Date());
inbox.copyMessages(messages, rensat);
}
if (imapdelete.startsWith("Y")) {
System.out.println("\n*** Delete marking messages... " + new Date());
// inbox.setFlags(messages, new Flags(Flags.Flag.DELETED), true); // kan ge timeout i stora mailboxes
for (int i = 0; i < messages.length; i++) {
messages[i].setFlag(Flags.Flag.DELETED, true); // markera mailet för deletion
antal++; antalDmarked++;
if (antal > 5000 ) {
// System.out.println("* expunge...");
// if (imapexpunge.startsWith("Y")) inbox.expunge();
inbox.exists();
antal = 0;
}
// System.out.println("* Mark as DELETED ");
}
System.out.println("\n*** Delete-marked messages: "+antalDmarked+" " + new Date());
}
}
}
// Close the connection
System.out.println("\n*** Close connection " + new Date());
if (imaprw.startsWith("Y")) {
if (imapexpunge.startsWith("Y")) {
System.out.println("--- Close with expunge..." + new Date());
inbox.close(true); // remove the marked messages from the server (expunge)
}
else inbox.close(false);
}
else inbox.close(false);
store.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("\n*** Jvakt.PurgeImap4Msgs * Finished --- " + new Date());
} // slut på pgm
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
port = Integer.parseInt(jvport);
uname = prop.getProperty("imapuser");
pwd = prop.getProperty("imappwd");
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");
imapcopy = prop.getProperty("imapCopy");
imapdelete = prop.getProperty("imapDelete");
imapexpunge = prop.getProperty("imapExpunge");
imapFolder = prop.getProperty("imapFolder");
imapFolderc = prop.getProperty("imapFolderC");
Pfrom = prop.getProperty("From");
Psubject = prop.getProperty("Subject");
Pbody = prop.getProperty("Body");
// int imapporti = Integer.parseInt(imapport);
System.out.println("Search criteria used during open phase: ") ;
System.out.println("Pfrom : "+Pfrom) ;
System.out.println("Psubject : "+Psubject) ;
System.out.println("Pbody : "+Pbody) ;
System.out.println("Source Folder: "+imapFolder) ;
System.out.println("Target Folder: "+imapFolderc) ;
System.out.println("Copy: "+imapcopy +" Copy: "+imapdelete +" Expunge: "+imapexpunge) ;
} catch (IOException ex) {
ex.printStackTrace();
}
// try {
// inet = InetAddress.getLocalHost();
// System.out.println("-- Inet: "+inet);
// agent = inet.toString();
// }
// catch (Exception e) { System.out.println(e); }
}
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;
}
}