emailscrapebasic.py 17.1 KB
Newer Older
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'''
	Need to check labels in Format Tab and tell user if they're wrong
	# Need to serialise all of this somehow
	Colours for different groups?
	oauth support?
'''

from arc.arcpreferences import PreferenceManager as PM
from arctool import ARCTool
from Default.emailfilter import EmailFilterTable, EmailFilterLogicValidator
from Default.glob import *
from Default.textedit import TextEditor
from arc.num2word import num2word
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import arc.arcclasses as arcclasses
import arc.arcgui as arcgui
import quopri
import re
21
22
import imaplib
import email
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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

class Plugin(arcclasses.Plugin):
	def __init__(self,package):
		super(Plugin,self).__init__(None,package)

		self.__name__ = "emailscrapebasic"
		# Required
		self.name = "Email Scrape (Basic)"
		self.authors = ["unonu"]
		self.version = (0,0,1)
		self.description =\
			"Finds a group of emails and re-presents their information."
		self.contexts = ['Date']

		self.emails = []
		self.groups = {}
		self.fetched = False
		self.groupColors = [] #https://en.wikipedia.org/wiki/Web_colors
		self.currentGroup = None
		self.items = {}
		self.fragments = {}

		self.igHeader = False
		self.igReplies= False
		self.igDup 	  = False
		self.igImages = False

		self.delim = ''

		self.contextFilters = []

	#needed
	def setupUi(self):
		#Filter
		self.widget.emailFilterTable = EmailFilterTable()
		self.widget.emailFilterTable.tableChanged.connect(
59
60
61
62
			lambda x: self.widget.fetchButton.setProperty('enabled',
				(x > 0
				or self.widget.contextCheck.isChecked()
				or self.widget.selectEdit.text() != ''))
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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
		)
		self.widget.emailFilterTable.tableChanged.connect(
				ARCTool.signalProfileChanged
		)

		self.widget.filterTableLayout.addWidget(self.widget.emailFilterTable)
		self.widget.fetchButton.clicked.connect(self.makeRequest)
		self.widget.logicBox.setValidator(EmailFilterLogicValidator())
		self.widget.selectEdit.textChanged.connect(
			lambda x: self.widget.fetchButton.setEnabled(
				x != '' or len(self.widget.emailFilterTable) > 0
			)
		)

		#Group
		self.widget.mailBrowser.setUndoRedoEnabled(True)
		self.widget.mailSelector.currentIndexChanged.connect(
			self.updateMailBrowser
		)
		self.widget.groupList.currentItemChanged.connect(self.setGroup)
		self.widget.groupList.itemChanged.connect(
			lambda x: self.renameGroup(self.currentGroup,x.text())
		)
		self.widget.groupList.itemActivated.connect(
			self.widget.groupList.editItem
		)
		self.widget.addGroup.setIcon(QIcon("icons/list-add.png"))
		self.widget.addGroup.clicked.connect(lambda: self.addGroup())
		self.widget.removeGroup.setIcon(QIcon("icons/list-remove.png"))
		self.widget.removeGroup.clicked.connect(lambda: self.removeGroup())
		self.widget.selectGroup.clicked.connect(lambda: self.selectGroup())
		self.widget.clearGroup.clicked.connect(lambda: self.clearGroup())
		self.widget.addSelection.clicked.connect(lambda: self.addToGroup())
		self.widget.removeSelection.clicked.connect(
			lambda: self.removeFromGroup()
		)
		self.widget.mailBrowser.copyAvailable.connect(
			lambda x: self.widget.selectionBox.setEnabled(
				x and self.widget.groupBox.isEnabled()
			)
		)
		# self.widget.groupList.currentTextChanged

		#Format
		self.widget.formatEdit = TextEditor()
		self.widget.formatLayout.addWidget(self.widget.formatEdit)

		#Options
		self.widget.contextCheck.stateChanged.connect(
			lambda x : self.widget.fetchButton.setProperty('enabled',x > 0)
		)
		self.widget.imageCheck.stateChanged.connect(
			lambda x: self.setIgImages(x > 0)
		)
		self.widget.delimeterEdit.textChanged.connect(
			lambda: self.updateDelim()
		)

	#call super
	def update(self):
		super(Plugin,self).update()

		#updated from extras
		if 'filters' in self.extras:
			self.widget.emailFilterTable.fromSerial(self.extras['filters'])
		if 'format' in self.extras:
			self.widget.formatEdit.setHtml(self.extras['format'])
		if 'groups' in self.extras:
			for g in self.extras['groups'].split(','):
				self.addGroup(g)

	#needed
	def storeOptions(self):
		self.options['logicBox'] =\
			(self.widget.logicBox.text(),'text')
		self.options['selectEdit'] =\
139
			(self.widget.selectEdit.text(),'text')
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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
		self.options['delimeterEdit'] =\
			(self.widget.delimeterEdit.text(), 'text')
		self.options['contextCheck'] =\
			(self.widget.contextCheck.isChecked(), 'checked')
		self.options['imageCheck'] =\
			(self.widget.imageCheck.isChecked(), 'checked')

		self.extras['filters'] = self.widget.emailFilterTable.serialize()
		self.extras['format'] = self.widget.formatEdit.toHtml()
		self.extras['groups'] = ','.join(self.groups.keys())

	#override
	def generate(self):
		if not self.fetched:
			r = self.makeRequest()
			if r < 0:
				return None
		doc = QTextDocument()
		cursor = QTextCursor(doc)

		self.constructItems()
		self.constructFragments()

		_c = _b = None
		for f in self.fragments:
			_c = cursor.charFormat()
			_b = cursor.blockFormat()
			cursor.insertFragment(QTextDocumentFragment(f))
			if self.delim != '':
				cursor.insertHtml('<br/><p>' + self.delim + '</p><br/>')
			cursor.insertBlock()
			cursor.setCharFormat(_c)
			cursor.setBlockFormat(_b)

		return doc

	def makeRequest(self):
		# make async? or at least talk to the user


		# Logic
		req, r, i = self.widget.emailFilterTable.getRequest(
			self.widget.logicBox.text()
		)

		if not req and r == 2:
			ARCTool.getStatusBar().showMessage(
				"Couldn't find filter label '%s'" %(i)
			)
			self.widget.logicBox.setSelection(
				self.widget.logicBox.text().find(i),len(i)
			)
			return -1

194
195
		if req == '':
			req = 'ALL'
196

Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
		# Context
		if self.widget.contextCheck.isChecked():
			b = ARCTool.getContext().getBegin("d-MMM-yyyy")
			e = ARCTool.getContext().getEnd()
			if b:
				req += ' SINCE %s' %(b)
			if e:
				e = e.addDays(1)
				e = e.toString("d-MMM-yyyy")
				req += ' BEFORE %s' %(e)

		print(req)

		protocol = imaplib.IMAP4_SSL if PM.getPreference('Default Package',
														 'emailssl')\
									 else imaplib.IMAP4

		with protocol(
			PM.getPreference('Default Package','emailserver'),
216
			int(PM.getPreference('Default Package','emailport') or 993)
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
217
218
219
220
221
		) as M:
		
			passDialog = arcgui.LoginDialog(self.widget)
			try:
				passDialog.credentials.connect(
222
					lambda x, y: M.login(x,y) if not(x==y=='') else print('')
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
223
				)
224
				passDialog.exec()
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
225
226
			except:
				ARCTool.getStatusBar().showMessage(
227
					"Couldn't login to IMAP server.", 5000
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
228
229
230
231
				)
				return -1

			try:
232
233
234
235
				ARCTool.getStatusBar().showMessage(
					"Selecting mailbox..."
				)
				mailbox = self.widget.selectEdit.text().replace('"','\\"')
236
237
				if ((mailbox == '' or mailbox.lower() =='inbox')
					and req == 'ALL'):
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
238
					ARCTool.getStatusBar().showMessage(
239
240
						"Can't select Inbox without at least one filter.",
						5000
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
241
242
					)
					return -1
243
244
				M.select('"'+mailbox+'"' if mailbox != '' else 'INBOX',
					readonly=True)
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
245
				try:
246
247
248
249
250
					ARCTool.getStatusBar().showMessage(
						"Searching %s..." %(
							mailbox if mailbox != '' else 'Inbox'
						)
					)
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
251
252
253
					typ, data = M.search(None, req)
				except protocol.error as e:
					ARCTool.getStatusBar().showMessage(
254
						"Couldn't search mailbox: "+str(e), 5000
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
255
256
					)
				else:
257
258
259
					ARCTool.getStatusBar().showMessage(
						"Fetching Messages..."
					)
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
260
261
262
263
264
265
266
267
268
					self.emails = []
					for num in data[0].split():
						typ, data = M.fetch(num, '(RFC822)')
						self.emails.append(
							email.message_from_bytes(data[0][1])
						)
					M.close()
			except protocol.error as e:
				ARCTool.getStatusBar().showMessage(
269
					"Couldn't select inbox: "+str(e), 5000
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
270
271
272
273
274
275
				)
				return -1

		ARCTool.getStatusBar().showMessage(
			"Fetched %d message%s" %(len(self.emails),
				'' if len(self.emails) == 1 else 's'
276
277
			),
			5000
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
278
279
280
281
282
283
284
285
286
287
		)
		self.fetched = True
		self.updateMailList()
		self.widget.setCurrentIndex(1)
		return 0

	def updateMailList(self):
		self.widget.mailSelector.clear()
		for m in self.emails:
			self.widget.mailSelector.addItem(
288
289
				quopri.decodestring(m.__getitem__('subject')).decode('utf-8')
				or 'No Subject'
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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
			)

	def updateMailBrowser(self):
		if len(self.emails) == 0:
			self.widget.mailBrowser.setPlainText('')
			self.widget.browserWidget.setEnabled(False)
			return
		self.widget.browserWidget.setEnabled(True)

		message = self.emails[self.widget.mailSelector.currentIndex()]
		self.widget.mailBrowser.setDocument(
			emailToQTD(message,not self.igImages)
		)

	def addGroup(self,name='Group'):
		# Assure no duplicates
		if name in self.groups:
			i = 1
			dup = ''
			while name+dup in self.groups:
				dup = ' ' + num2word(i).capitalize()
				i += 1
			name += dup
		# Map emails to list of position tupples with dist > 0
		self.groups[name] = {}
		item = QListWidgetItem(name)
		item.setFlags(item.flags() | Qt.ItemIsEditable)
		self.widget.groupList.addItem(item)
		self.widget.groupList.setCurrentRow(len(self.groups)-1)
		self.setGroup()
		# if a highlighted section exactly matches a range then remove that
		# whole range and only that range if no exact matches are found, then
		# remove from all partial matches (splitting as necessary) no
		# duplicates, no mistakes

	def setGroup(self):
		item = self.widget.groupList.currentItem()
		if item:
			self.currentGroup = item.text()
			self.widget.groupBox.setEnabled(True)
		else:
			self.widget.groupBox.setEnabled(False)

	def removeGroup(self,name=None):
		if not name:
			name = self.widget.groupList.takeItem(
				self.widget.groupList.currentRow()
			).text()
		if name not in self.groups: return
		del self.groups[name]

		self.widget.groupList.setCurrentRow(0)
		self.setGroup()

	def renameGroup(self,old,new):
		if new == old or old not in self.groups: return
		if new in self.groups or new == '':
			self.widget.groupList.currentItem().setText(old)
			return
		self.groups[new] = self.groups[old]
		del self.groups[old]
		self.setGroup()

	def addToGroup(self,name=None,beg=None,end=None):
		cursor = self.widget.mailBrowser.textCursor()
		if not beg:
			beg = cursor.selectionStart()
		if not end:
			end = cursor.selectionEnd()
		if beg == end : return

		# get the group name
		# Maybe the user might not want to create a new group? ehhhh w/e
		if not name:
			name = self.widget.groupList.currentItem().text()
		if name not in self.groups:
			self.groups[name] = {}

		email = self.emails[self.widget.mailSelector.currentIndex()]
		set = [None]
		if email in self.groups[name]:
			set = self.groups[name][email]
		else:
			self.groups[name][email] = set

		if (beg,end) in set: return

		# I'm taking out multiple sections in a group. uncomment to add back
		# set.append((beg,end)) <===\\
		set[0] = (beg,end) #    <===//

	def removeFromGroup(self,name=None,beg=None,end=None):
		cursor = self.widget.mailBrowser.textCursor()
		if not beg:
			beg = cursor.selectionStart()
		if not end:
			end = cursor.selectionEnd()
		if beg == end : return

		# get the group name
		if not name:
			name = self.widget.groupList.currentItem().text()
		# Maybe the user might not want to create a new group? eh w/e
		if name not in self.groups: return

		email = self.emails[self.widget.mailSelector.currentIndex()]
		if email not in self.groups[name]: return

		set = self.groups[name][email]

		if (beg,end) in set:
			set.remove((beg,end))
			return

		matches = []
		for s in set:
			if ((beg >= s[0] and beg < s[1]) or (end <= s[1] and end > s[0]) or
					(s[0] >= beg and s[1] <= end)):
				matches.append(s)

		for m in matches:
			set.remove(m)
			if beg-m[0] > 0:
				set.append((m[0],beg))
			if m[1]-end > 0:
				set.append((end,m[1]))

	def clearGroup(self,name=None):
		if not name:
			name = self.widget.groupList.currentItem().text()

		email = self.emails[self.widget.mailSelector.currentIndex()]
422
423
		# self.groups[name][email] = []	   <===\\
		self.groups[name][email] = [None] #<===//
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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

	def selectGroup(self,name=None):
		if not name:
			name = self.widget.groupList.currentItem().text()

		email = self.emails[self.widget.mailSelector.currentIndex()]
		if email not in self.groups[name]: return

		set = self.groups[name][email]
		cursor = self.widget.mailBrowser.textCursor()
		self.widget.mailBrowser.setTextCursor(cursor)
		self.widget.mailBrowser.undo()
		cursor.beginEditBlock()
		highlight = QTextCharFormat()
		brush = QBrush(QColor('#00e34b'))
		highlight.setBackground(brush)
		for s in set:
			cursor.setPosition(s[0])
			cursor.setPosition(s[1],cursor.KeepAnchor)
			cursor.mergeCharFormat(highlight)
		cursor.endEditBlock()

	def constructItems(self):
		self.items = {}
		for g in self.groups:
			item = GlobItem()
			for message in self.groups[g]:
				doc = QTextDocument()

				for part in message.walk():
					typ = part.get_content_type()
					dis = part.get('Content-Disposition')
					if dis != 'attachment':
						if typ == 'text/plain':
							body = ''
							try:
								body = part.get_payload(decode=True).decode('utf-8')
							except UnicodeDecodeError:
462
								body = quopri.decodestring(part.get_payload()).decode('utf-8')
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
463
464
465
466
467
468
							doc.setPlainText(body)
						elif typ == 'text/html':
							body = ''
							try:
								body = part.get_payload(decode=True).decode('utf-8')
							except UnicodeDecodeError:
469
								body = quopri.decodestring(part.get_payload()).decode('utf-8')
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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
							doc.setHtml(body)
				curs = []
				for p in self.groups[g][message]:
					curs.append(QTextCursor(doc))
					curs[-1].setPosition(p[0])
					curs[-1].setPosition(p[1],curs[-1].KeepAnchor)
				if len(curs) > 0:
					item + Glob(*curs)
			self.items[g] = item

	def constructFragments(self):
		self.fragments = []
		ref = QTextDocument()
		for message in self.emails:
			doc = self.widget.formatEdit.document().clone()

			# for part in message.walk():
			# 	typ = part.get_content_type()
			# 	dis = part.get('Content-Disposition')
			# 	if dis != 'attachment':
			# 		if typ == 'text/plain':
			# 			ref.setPlainText(
			# 				part.get_payload(decode=True).decode('utf-8')
			# 			)
			# 		elif typ == 'text/html':
			# 			ref.setHtml(
			# 				part.get_payload(decode=True).decode('utf-8')
			# 			)

			body = ''
			resources = {}
			parts = [p for p in message.walk()]
			i=0
			for part in parts:
				typ, dis = part.get_content_type(), part.get('Content-Disposition')
				if part.is_multipart():
					for sp in reversed([s for s in part.walk()]):
						if sp not in parts:
							parts.insert(i+1,sp)
				i += 1

				# Prefer HTML
				if typ == 'text/html' and dis == None:
					try:
						body = part.get_payload(decode=True).decode('utf-8')
					except UnicodeDecodeError:
516
						body = quopri.decodestring(part.get_payload()).decode('utf-8')
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
517
518
519
520
				elif typ == 'text/plain' and dis == None and body == '':
					try:
						body = part.get_payload(decode=True).decode('utf-8')
					except UnicodeDecodeError:
521
						body = quopri.decodestring(part.get_payload()).decode('utf-8')
Cage, Marshall Andrew's avatar
Cage, Marshall Andrew committed
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
				elif typ[:typ.find('/')] == 'image' and self.igImages:
					# Embed image
					dis = dis.split(';',1)
					if dis[0] == 'inline':
						# print(part.get('Content-ID'))
						# print("\nimage:",part.get_filename())
						resources['cid:'+part.get('Content-ID')[1:-1]] =\
							part.get_payload(decode=True)
			ref.setHtml(body)

			for group in self.groups:
				i=0
				cur = doc.find('$%s$'%(group),0,
							   options=doc.FindCaseSensitively
				)
				# print('find $%s$: %s' %(group, 'FAIL' if cur.isNull() else 'OK'))
				text, ok, perc = self.items[group].getText(ref)
				# print('find starting at %d: %s %f' %(i,text,perc))
				while not cur.isNull():
					# Use the GlobItem to get text from the ref document
					if not ok:
						cur.insertHtml(
							('<span style="color:red; font-weight:bold">%s '\
							+ '[%0.1f%% certainty]</span>') %(text,perc*100))
					elif perc < .6:
						cur.insertHtml(
							('<span style="color:red">%s [%0.1f%% certainty]<'\
							+ '/span>') %(text,perc*100))
					else:
						cur.insertText(text)

					i = cur.selectionEnd()
					cur = doc.find('$%s$'%(group),cur,
								   options=doc.FindCaseSensitively)
			# 		print('looking for next match')
			# 	print('i is now',i)
			# print('appended fragment',doc.toPlainText())
			self.fragments.append(doc)

	def setIgImages(self, b):
		self.igImages = b
		self.updateMailBrowser()

	def updateDelim(self):
		self.delim = self.widget.delimeterEdit.text()

def emailToQTD(message,showImages=True):
	body = ''
	resources = {}
	
	parts = [p for p in message.walk()]
	i=0
	for part in parts:
		typ, dis = part.get_content_type(), part.get('Content-Disposition')
		if part.is_multipart():
			for sp in reversed([s for s in part.walk()]):
				if sp not in parts:
					parts.insert(i+1,sp)
		i += 1

		# Prefer HTML
		if typ == 'text/html' and dis == None:
			try:
				body = part.get_payload(decode=True).decode('utf-8')
			except UnicodeDecodeError:
				body = str(quopri.decodestring(part.get_payload()))
		elif typ == 'text/plain' and dis == None and body == '':
			try:
				body = part.get_payload(decode=True).decode('utf-8')
			except UnicodeDecodeError:
				body = str(quopri.decodestring(part.get_payload()))
		elif typ[:typ.find('/')] == 'image' and showImages:
			# Embed image
			dis = dis.split(';',1)
			if dis[0] == 'inline':
				# print(part.get('Content-ID'))
				# print("\nimage:",part.get_filename())
				resources['cid:'+part.get('Content-ID')[1:-1]] =\
					part.get_payload(decode=True)
			
	doc = QTextDocument()
	doc.setHtml(body)
	# print(body)
	for r in resources:
		img = QImage()
		img.loadFromData(resources[r])
		doc.addResource(doc.ImageResource, QUrl(r), img)

	return doc