This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.groovy
executable file
·150 lines (129 loc) · 4.26 KB
/
delete.groovy
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
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
public class Delete {
private static final String DIR_PATH = "/Users/sarnobat/.gcal_task_warrior";
private static final File mTasksFileLatest = new File(DIR_PATH
+ "/tasks.json");
public static void main(String[] args) throws IOException,
NoSuchProviderException, MessagingException {
String itemToDelete = args[0];
JSONObject eventJson = getEventJson(itemToDelete, mTasksFileLatest);
String title = eventJson.getString("title");
System.out.println("Title:\t" + title);
Message msg = getMessage(title);
String messageIdToDelete = getMessageID(msg);
commit(itemToDelete, messageIdToDelete);
}
private static JSONObject getEventJson(String itemToDelete, String errands) {
JSONObject allErrandsJson = new JSONObject(errands);
JSONObject eventJson = (JSONObject) allErrandsJson.get(itemToDelete);
return eventJson;
}
// Still useful
private static JSONObject getEventJson(String itemToDelete,
File tasksFileLastDisplayed) throws IOException {
String errands = FileUtils.readFileToString(tasksFileLastDisplayed);
JSONObject eventJson = getEventJson(itemToDelete, errands);
return eventJson;
}
private static Message getMessage(String title)
throws NoSuchProviderException, MessagingException {
Message[] msgs = getMessages();
Message msg = null;
for (Message aMsg : msgs) {
if (aMsg.getSubject().equals(title)) {
msg = aMsg;
break;
}
}
if (msg == null) {
throw new RuntimeException();
}
return msg;
}
private static Message[] getMessages() throws NoSuchProviderException,
MessagingException {
Store theImapClient = connect();
Folder folder = theImapClient
.getFolder("3 - Urg - time sensitive");
folder.open(Folder.READ_WRITE);
Message[] msgs = folder.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add("X-mailer");
folder.fetch(msgs, fp);
return msgs;
}
private static String getMessageID(Message aMessage)
throws MessagingException {
Enumeration<?> allHeaders = aMessage.getAllHeaders();
String messageID = "<not found>";
while (allHeaders.hasMoreElements()) {
Header e = (Header) allHeaders.nextElement();
if (e.getName().equals("Message-ID")) {
messageID = e.getValue();
}
}
return messageID;
}
private static Store connect() throws NoSuchProviderException,
MessagingException {
Properties props = System.getProperties();
String password = System.getenv("GMAIL_PASSWORD");
if (password == null) {
throw new RuntimeException(
"Please specify your password by running export GMAIL_PASSWORD=mypassword groovy mail.groovy");
}
props.setProperty("mail.store.protocol", "imap");
Store theImapClient = Session.getInstance(props).getStore("imaps");
theImapClient.connect("imap.gmail.com", "sarnobat.hotmail@gmail.com",
password);
return theImapClient;
}
private static void commit(String itemToDelete,
final String messageIdToDelete) throws NoSuchProviderException,
MessagingException, IOException {
// All persistent changes are done right at the end, so that any
// exceptions can get thrown first.
new Thread() {
@Override
public void run() {
try {
deleteEmail(messageIdToDelete);
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
private static void deleteEmail(String messageIdToDelete)
throws NoSuchProviderException, MessagingException {
Message[] messages;
messages = getMessages();
for (Message aMessage : messages) {
String aMessageID = getMessageID(aMessage);
if (aMessageID.equals(messageIdToDelete)) {
aMessage.setFlag(Flags.Flag.DELETED, true);
System.out.println("Deleted email:\t" + aMessage.getSubject());
break;
}
}
}
}