-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimapnotes.py
More file actions
executable file
·307 lines (261 loc) · 10.7 KB
/
Copy pathimapnotes.py
File metadata and controls
executable file
·307 lines (261 loc) · 10.7 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
#!/usr/bin/env python3.9
import os, sys, re, time, io, configparser, imaplib, html.parser, base64
import email.parser, email.header, email.utils
import tkinter, tkinter.font, tkinter.messagebox, PIL.Image, PIL.ImageTk
class HTMLNoteParser(html.parser.HTMLParser):
style_tag = ""
style_num = 0
textField = None
def __init__(self, textField):
html.parser.HTMLParser.__init__(self)
self.textField = textField
def getColor(self, cssvalue):
rgba_re = re.compile(r"rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})")
match = rgba_re.match(cssvalue)
if match is None:
return cssvalue
else:
return "#%02x%02x%02x" % tuple(map(int, match.group(1, 2, 3)))
def parseStyle(self, css):
style = {}
for setting in [x for x in css.split(";") if ":" in x]:
cssname, cssvalue = map(str.strip, setting.split(":", 1))
if cssname == "color":
style["foreground"] = self.getColor(cssvalue)
elif cssname == "background-color":
style["background"] = self.getColor(cssvalue)
self.textField.tag_configure(self.style_tag, style)
def handle_starttag(self, tag, attrs):
self.style_tag = "tag%d" % (self.style_num,)
self.style_num += 1
if tag == "strike":
font = tkinter.font.Font(overstrike=True)
self.textField.tag_configure(self.style_tag, font=font)
elif tag == "b":
font = tkinter.font.Font(weight="bold")
self.textField.tag_configure(self.style_tag, font=font)
elif tag == "i":
font = tkinter.font.Font(weight="italic")
self.textField.tag_configure(self.style_tag, font=font)
elif tag in ("br", "div", "p", "h1", "h2", "h3", "h4"):
self.textField.insert(tkinter.END, "\n")
for name, value in attrs:
if name == "style":
self.parseStyle(value)
def handle_endtag(self, tag):
if tag in ("div", "p", "h1", "h2", "h3", "h4"):
self.textField.insert(tkinter.END, "\n")
def handle_startendtag(self, tag, attrs):
if tag in ("br", "div", "p", "h1", "h2", "h3", "h4"):
self.textField.insert(tkinter.END, "\n")
def handle_data(self, data):
previous_style_tag = self.style_tag
self.textField.insert(tkinter.END, data.replace("\r", ""),
self.style_tag)
self.style_tag = previous_style_tag
def displayMessage(message):
if message.is_multipart():
for part in message.get_payload():
displayMessage(part)
else:
contenttype = message.get_content_type().lower()
body = message.get_payload(decode=True)
if contenttype.startswith("text/plain"):
textField.insert(tkinter.END, body.decode().replace("\r", ""))
elif contenttype.startswith("text/html"):
HTMLNoteParser(textField).feed(body.decode())
elif contenttype.startswith("image/"):
img = PIL.ImageTk.PhotoImage(data=body)
textField.image_create(tkinter.END, image=img)
imgCache.append(img)
else:
textField.insert(tkinter.END, "<cannot display " + contenttype +
">")
def displayNote(*args):
global noteChanged
if noteChanged:
index = listBox.index(tkinter.ACTIVE)
subject = textField.get(1.0, 2.0).strip().encode("utf-8")
body = textField.get(1.0, tkinter.END).encode("utf-8")
note = notes[len(notes) - index - 1]
note["message"].set_payload(body)
note["subject"] = subject
note["changed"] = True
listBox.delete(index)
listBox.insert(index, subject)
noteChanged = False
index = listBox.curselection()
if len(index) > 0:
textField.delete(1.0, tkinter.END)
for tag in textField.tag_names():
textField.tag_delete(tag)
index = index[0]
message = notes[len(notes) - index - 1]["message"]
imgCache = []
displayMessage(message)
deleteButton.config(state=tkinter.NORMAL)
textField.edit_modified(False)
else:
deleteButton.config(state=tkinter.DISABLED)
def newNote():
subject = "new note"
message = email.message_from_string("")
notes.append({
"uid": None,
"message": message,
"subject": subject,
"changed": False,
})
listBox.insert(0, subject)
listBox.selection_clear(0, tkinter.END)
listBox.selection_set(0)
listBox.activate(0)
listBox.event_generate("<<ListboxSelect>>")
def deleteNote():
index = listBox.index(tkinter.ACTIVE)
if index >= 0:
rindex = len(notes) - index - 1
message = notes[rindex]
listBox.delete(index)
notes.remove(message)
deletedNotes.append(message)
deleteButton.config(state=tkinter.DISABLED)
def set_header(message, header, value, replace=True):
if not header in message.keys():
message[header] = value
elif replace:
message.replace_header(header, value)
def imapConnect():
try:
config = configparser.ConfigParser()
config.read(os.path.expanduser("~/.imapnotes.ini"))
host = config.get("connection", "host")
if config.has_option("connection", "port"):
imap = imaplib.IMAP4_SSL(host, config.get("connection", "port"))
else:
imap = imaplib.IMAP4_SSL(host)
imap.login(config.get("connection", "user"),
config.get("connection", "pass"))
imap.select("Notes")
return imap
except Exception as e:
tkinter.messagebox.showerror("Connection failed",
"Cannot connect to IMAPS server:\n%s" % (str(e),))
sys.exit(1)
def imapNoop():
try:
imap.noop()
except Exception as e:
imapConnect()
def deleteUid(uid):
ret = imap.uid("COPY", uid, "Trash")
print("moved note to Trash:\nuid=%s\nret=%s\n" % (uid, ret))
ret = imap.uid("STORE", uid, "+FLAGS", "(\\Deleted)")
print("deleted note:\nuid=%s\nret=%s\n" % (uid, ret))
def saveNotes(*args):
displayNote(args)
imapNoop()
for note in [x for x in notes if x["changed"]]:
message = note["message"]
subject = email.header.Header(note["subject"], "utf-8")
rfc_now = email.utils.formatdate(localtime=True)
set_header(message, "Subject", subject)
set_header(message, "Content-Type", "text/plain; charset=utf-8")
set_header(message, "Content-Transfer-Encoding", "8bit")
set_header(message, "X-Uniform-Type-Identifier", "com.apple.mail-note")
set_header(message, "Message-ID", email.utils.make_msgid())
set_header(message, "Date", email.utils.formatdate(localtime=True))
set_header(message, "X-Mail-Created-Date", rfc_now, False)
set_header(message, "X-Universally-Unique-Identifier",
email.utils.make_msgid().split("@")[0][1:], False)
now = imaplib.Time2Internaldate(time.time())
ret = imap.append("Notes", "", now, message.as_bytes())
print("changed note:\nsubject=%s\nret=%s\n" % (subject,ret))
uid = note["uid"]
if not uid is None:
deleteUid(uid)
for note in deletedNotes:
uid = note["uid"]
if not uid is None:
deleteUid(uid)
print("expunged mailbox=%s\n" % (imap.expunge(),))
print("closed mailbox=%s\n" % (imap.close(),))
root.destroy()
def textModified(*args):
global noteChanged
if textField.edit_modified():
noteChanged = True
imap = imapConnect()
notes = []
deletedNotes = []
# search returns tuple with list
notes_numbers = imap.uid("search", None, "ALL")[1][0].decode().replace(" ", ",")
# imap fetch expects comma separated list
if len(notes_numbers) > 0:
notes_list = imap.uid("fetch", notes_numbers, "RFC822")
uid_re = re.compile(r"UID\s+(\d+)")
for part in notes_list[1]:
# imap fetch returns s.th. like:
# ('OK', [('1 (UID 1 RFC822 {519}', 'From: ...'), ')'])
if part == b")":
continue
match = uid_re.search(part[0].decode())
uid = None if match is None else match.group(1)
message = email.message_from_bytes(part[1])
subject = ""
raw_subject = message.get("subject")
for substring, charset in email.header.decode_header(raw_subject):
if not charset is None:
substring = substring.decode(charset)
subject += substring
notes.append({
"uid": uid,
"message": message,
"subject": subject,
"changed": False,
})
gifdata = base64.b64decode("""
R0lGODlhQABAAKECAAAAAPHKGf///////yH5BAEKAAIALAAAAABAAEAAAAL+lH+gy+0PI0C0Jolz
tvzqDypdtwTmiabqyqLLyJXtTLcvXMn1vt84ouMJWb6fIThMnopGXegJWIqMHin0Y6VWTVdQVitA
KpPMn3gsLOPO6N5Uy27T1LC43Pam2u8r+sjZpfEFp2AVKDGoV8h1iJHYtMjH40cSKVlDGWN5OZNp
scfJOAEGGuqZsxnalwcZJdoI8WgWCYsoChZGWxt7S5qqmnJKUcopDPQLLLuGnBxgvNWs8nzEnDyd
6+q8+6Bcp7vd0P33DQ44Spgd7cI6m67ei/4ezL7s/n5NfIlfDbxvr+7PULlsAV8NFFeJ4EBzuPJJ
KigPnqJ/0SBGtCgP4z0pet4oNoO4kKEvhSERaiK50OQnfqo0AuQ4zqM1mAlDYmhoc8PInBE6FAAA
Ow==
""")
root = tkinter.Tk()
gificon = tkinter.PhotoImage(data=gifdata)
root.tk.call('wm', 'iconphoto', root._w, gificon)
root.title("imapnotes")
root.protocol("WM_DELETE_WINDOW", saveNotes)
root.after(42000, imapNoop)
frameButtons = tkinter.Frame(root)
frameButtons.pack(fill=tkinter.BOTH, expand=1)
frameListAndText = tkinter.Frame(frameButtons)
frameListAndText.pack(side=tkinter.LEFT)
newButton = tkinter.Button(frameListAndText, text="New", command=newNote)
newButton.pack(side=tkinter.TOP, fill=tkinter.X)
deleteButton = tkinter.Button(frameListAndText, text="Delete",
command=deleteNote, state=tkinter.DISABLED)
deleteButton.pack(side=tkinter.TOP, fill=tkinter.X)
panedWindow = tkinter.PanedWindow(frameButtons, orient=tkinter.HORIZONTAL)
panedWindow.pack(fill=tkinter.BOTH, expand=1)
listBox = tkinter.Listbox(panedWindow)
vscroll = tkinter.Scrollbar(listBox, command=listBox.yview,
orient=tkinter.VERTICAL)
vscroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
listBox.config(yscrollcommand=vscroll.set)
panedWindow.add(listBox, width=300, height=400)
for note in notes:
listBox.insert(0, note["subject"])
listBox.bind("<<ListboxSelect>>", displayNote)
textField = tkinter.Text(panedWindow, undo=True, wrap=tkinter.WORD)
vscroll = tkinter.Scrollbar(textField, command=textField.yview,
orient=tkinter.VERTICAL)
vscroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
textField.config(yscrollcommand=vscroll.set)
textField.bind("<<Modified>>", textModified)
panedWindow.add(textField, width=500)
noteChanged = False
imgCache = []
root.mainloop()