-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcutepiesmtpdaemon_py3.py
executable file
·1490 lines (1154 loc) · 58.8 KB
/
cutepiesmtpdaemon_py3.py
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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: ascii -*-
import _thread
import asyncore
import base64
import datetime
import email
import glob
import html
import logging
import mailbox
import operator
import os
import pprint
import queue
import re
import socket
import subprocess
import sys
import threading
import traceback
from email.header import decode_header, Header
from email.utils import parsedate_tz, mktime_tz
from functools import wraps
from time import sleep
from time import time
import smtpd
import lxml as lxml
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from lxml.html.clean import Cleaner
import cutesmtp_icons
from valid_encodings import VALID_ENCODINGS
import pickle as pickle
__all__ = ['cutesmtp_icons',
'SmtpMailsinkServer',
'EmailParser']
DEBUG_SMTP = True
DEBUG_APP = False
DEFAULT_PORT = 1025
DISABLE_APPSTATE = False
APPNAME = 'Cute Pie SMTP Daemon'
VERSION = '0.17.3.2221 (pyqt5)'
DEFAULT_MBOX_PATH = 'mailbox.mbox'
POLLING_TIME_MILLISECS = 1000
PICKLE_FILE_NAME = "app_cache.appstate"
PICKLE_IS_LOADED = False
PICKLE_STATE_DIRTY = False
INDEX_HIDDEN_METADATA = 3 # hidden row for message storage
LOG_FILE_NAME = 'app.log'
LOG = None # type: logging.Logger
tableview_data = [] # type: list
def timed(f):
"""
Metrics for method execution
:param f: function
"""
@wraps(f)
def wrapper(*args, **kwds):
start = time()
result = f(*args, **kwds)
elapsed = time() - start
LOG.debug("method %s() took %.6f seconds to complete" % (f.__name__, elapsed))
return result
return wrapper
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.actionLogToFileEnabled = None
self.appdata_dir = None
self.attachments_dir = None
self.last_saved_sort_column = None
self.last_saved_sort_order = None
self.last_used_fileopen_folder = None
self.actionLogToFileEnabled = None
self.iconSwitch = QtGui.QIcon(':/icons/switch.png')
self.setWindowIcon(self.iconSwitch)
# window size and position
self.setGeometry(100, 150, 500, 660)
self.read_settings()
init_logging(log_file_dir=self.appdata_dir, log_to_file=self.is_log_file_enabled)
self.init_folders()
self.textEdit = QtWidgets.QTextEdit()
self.textEdit.setReadOnly(True)
self.setCentralWidget(self.textEdit)
self.createActions()
self.createMenus()
self.createToolBars()
self.createStatusBar()
self.createDockWindows()
self.createAttachmentsMenuItems()
self.setWindowTitle("%s - %s" % (APPNAME, self.mbox_path))
self.topDock.setWindowTitle(" SMTP status: localhost:%s OFF" % self.port)
self.setUnifiedTitleAndToolBarOnMac(True)
self.mailsync = None
self.attachment_buttons = None
self.attachment_icon = QtGui.QIcon(':/icons/attached.png')
if self.isSmtpAutostartEnabled:
self.toggle_smtp_server_state(start=True)
if self.is_toolbar_hidden:
self.actionToggleToolbar.setChecked(True)
self.on_toggle_toolbar()
if self.isHtmlCleaningEnabled:
self.actionCleanHtmlToggle.setChecked(True)
EmailParser.cleaning_is_enabled = self.isHtmlCleaningEnabled
self.pickle_storage_path = os.path.join(self.appdata_dir, PICKLE_FILE_NAME)
self.initialize_data() # here data init is happening
self.restore_column_sort_mode()
def restart_as_root(self):
euid = os.geteuid()
if euid != 0:
reply = QtWidgets.QMessageBox.question(self, APPNAME,
"Script not running as root. Restart with root permissions?",
QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
executable = getattr(sys, 'frozen', False) and sys.argv[0] or sys.executable
args = ['sudo', executable] + sys.argv + [os.environ]
# the next line replaces the currently-running process with the sudo
if sys.platform.startswith('darwin'):
executable = os.path.abspath(executable)
print(executable, type(executable))
os.execlpe('osascript',
'osascript',
'-e',
"do shell script \"python /Users/vio/projects/cutesmtpdaemon/cutepiesmtpdaemon.py\" with administrator privileges",
os.environ)
else:
os.execlpe('sudo', *args)
def init_folders(self):
userhome = os.path.expanduser("~")
self.appdata_dir = os.path.join(userhome, APPNAME)
self.attachments_dir = os.path.join(self.appdata_dir, "attachments")
create_folder_if_not_exists(self.appdata_dir)
create_folder_if_not_exists(self.attachments_dir)
if not self.mbox_path or not os.path.exists(self.mbox_path):
default_mbox_path = os.path.join(self.appdata_dir, DEFAULT_MBOX_PATH)
if not os.path.exists(default_mbox_path):
with (open(default_mbox_path, 'w')) as f:
pass
self.mbox_path = default_mbox_path
def print_(self):
selected_indexes = self.tableView.selectionModel().selectedRows()
if not len(selected_indexes):
QtWidgets.QMessageBox.warning(self, APPNAME,
"Please select a message first")
return
document = self.textEdit.document()
printer = QtPrintSupport.QPrinter()
dlg = QtPrintSupport.QPrintDialog(printer, self)
if dlg.exec_() != QtWidgets.QDialog.Accepted:
return
document.print_(printer)
self.statusBar().showMessage("Ready", 2000)
@timed
def on_open_raw_message(self, row_index=None, extension='txt'):
""" open raw message body in default text editor """
if not row_index:
selected_indexes = self.tableView.selectionModel().selectedRows()
if len(selected_indexes):
row_index = selected_indexes[0].row()
else:
show_gui_error("No message selected", "Please select a message first!")
return
meta_data = tableview_data[row_index][INDEX_HIDDEN_METADATA]
raw_body = meta_data.message
raw_message_path = os.path.join(self.appdata_dir, "message.tmp.%s.%s" % (time(), extension))
try:
with open(raw_message_path, 'wb') as attachmentFile:
attachmentFile.write(bytes(raw_body))
sleep(0.2)
except IOError as e:
show_gui_error(e, 'Cannot create file: %s' % raw_message_path)
return
self.start_file(raw_message_path)
def save_binary_file(self, caption=None, fname=None, bytes=None):
filename, __ = QtWidgets.QFileDialog.getSaveFileName(self,
caption, os.path.join(self.attachments_dir, fname), '*.*')
if not filename:
return
qFile = QtCore.QFile(filename)
if not qFile.open(QtCore.QFile.WriteOnly | QtCore.QFile.ReadWrite):
QtWidgets.QMessageBox.warning(self, APPNAME,
"Cannot create file: %s\n%s." % (filename, qFile.errorString()))
return
with open(filename, 'wb') as file_handle:
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
file_handle.write(bytes)
QtWidgets.QApplication.restoreOverrideCursor()
self.statusBar().showMessage("Saved '%s'" % filename, 2000)
def save_generated_html(self):
selected_indices = self.tableView.selectionModel().selectedRows()
if not len(selected_indices):
QtWidgets.QMessageBox.warning(self, APPNAME,
"Please select a message first")
return
filename, __ = QtWidgets.QFileDialog.getSaveFileName(self,
"Saving current message in HTML format...", '.',
"HTML (*.html *.htm)")
if not filename:
return
file_name = QtCore.QFile(filename)
if not file_name.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text):
QtWidgets.QMessageBox.warning(self, APPNAME,
"Cannot write file_name %s:\n%s." % (filename, file_name.errorString()))
return
out = QtCore.QTextStream(file_name)
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
out << self.textEdit.toHtml()
QtWidgets.QApplication.restoreOverrideCursor()
self.statusBar().showMessage("Saved '%s'" % filename, 2000)
def on_about(self):
QtWidgets.QMessageBox.about(self, "About",
"X-Mailer: %s<br/><br/>"
"X-Version: %s<br/><br/>"
"From: <a href='mailto:[email protected]?Subject=CutePieSmtpDaemon'>[email protected]</a><br/><br/>"
"Subject: For a moment, nothing happened. Then, after a second or so, nothing continued to happen...<br/><br/>"
"Cute SMPT Daemon is a fake SMTP server created for debugging and development purposes.<br/>"
"The app listens on localhost, intercepts email messages, and writes them to a standard Unix mailbox file.<br/>"
"The app can also open an existing Unix mailbox file "
"or raw email messages in EML/MSG format.<br/>"
"To strip styles and scripts from the HTML messages use Config > 'Enable HTML cleaning'<br/>"
"%s is capable of extracting and saving attachments from mailboxes or from EML/MSG files.<br/>"
"Running the SMTP server on port 25 requires root priveleges. To run as a regular user, set a port "
"higher than 1024, and configure your email clients to use that port.<br/><br/> "
"X-Dedicated-To: Douglas Adams" % (APPNAME, VERSION, APPNAME))
def createActions(self):
"""
# ICONS http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
SHORTCUTS MAPPING: http://pyqt.sourceforge.net/Docs/PyQt4/qkeysequence.html
"""
self.actionNewMboxFile = QtWidgets.QAction(QtGui.QIcon(':/icons/new.png'),
"Create &New mailbox", self,
shortcut=QtGui.QKeySequence.New,
statusTip="Create new mailbox file",
triggered=self.create_new_mbox)
self.actionNewMboxFile.setIconText("New")
self.actionSmtpToggle = QtWidgets.QAction(self.iconSwitch,
"Start/Stop SMTP", self,
shortcut=QtGui.QKeySequence.MoveToStartOfDocument,
statusTip="Start/Stop SMTP server",
triggered=self.toggle_smtp_server_state,
checkable=True)
self.actionSmtpToggle.setIconText("SMTP")
self.actionSmtpAutostartToggle = QtWidgets.QAction(self.iconSwitch,
"&Autostart SMTP server", self,
statusTip="Autostart SMTP server",
triggered=self.update_smtp_autostart_setting,
checkable=True)
self.actionSmtpAutostartToggle.setChecked(self.isSmtpAutostartEnabled)
self.actionSmtpAutostartToggle.setIconText("SMTP autostart")
self.actionSaveHtml = QtWidgets.QAction(QtGui.QIcon(':/icons/device.png'),
"&Save HTML for selected message", self,
shortcut=QtGui.QKeySequence.Save,
statusTip="Save rendered HTML for the selected message",
triggered=self.save_generated_html)
self.actionSaveHtml.setIconText("Save")
self.actionShowRowMessage = QtWidgets.QAction(
"Open &raw message as text...", self,
shortcut=QtGui.QKeySequence.Forward,
statusTip="Open raw email message as text",
triggered=lambda: self.on_open_raw_message(extension='txt'))
self.actionToggleToolbar = QtWidgets.QAction(
"Hide toolbar...", self,
shortcut=QtGui.QKeySequence.Bold,
statusTip="Show or hide toolbar",
triggered=self.on_toggle_toolbar,
checkable=True)
self.actionLogToFileEnabled = QtWidgets.QAction(
"Enable logging to file", self,
statusTip="Enable logging to file",
triggered=self.on_logging_enabled,
checkable=True)
self.actionLogToFileEnabled.setChecked(self.is_log_file_enabled)
self.actionOpenMessage = QtWidgets.QAction("Open message with default &email app", self,
shortcut=QtGui.QKeySequence.InsertParagraphSeparator,
statusTip="Open message with default email app",
triggered=lambda: self.on_open_raw_message(extension='eml'))
self.actionPrint = QtWidgets.QAction(QtGui.QIcon(':/icons/printer.png'),
"&Print selected message...", self,
shortcut=QtGui.QKeySequence.Print,
statusTip="Print the selected message",
triggered=self.print_)
self.actionPrint.setIconText("Print")
self.quitAct = QtWidgets.QAction("&Quit", self,
statusTip="Quit the application", triggered=self.close)
self.actionAbout = QtWidgets.QAction("X-About", self,
statusTip="Show the About box",
triggered=self.on_about)
# self.aboutAct = QtWidgets.QAction("About this app", self,
# statusTip="Show the application's About box",
# triggered=self.on_about)
self.actionSetPort = QtWidgets.QAction("SMTP port", self,
statusTip="Set SMTP port",
triggered=self.set_port)
self.actionCleanHtmlToggle = QtWidgets.QAction("Enable HTML cleaning", self,
statusTip="Enabling will remove all styling from the markup",
checkable=True,
triggered=self.update_html_clean_setting)
self.actionCleanHtmlToggle.setChecked(self.isHtmlCleaningEnabled)
# ACTIONS FOR ATTACHMENT CONTEXT MENU
self.actionAttachmentSave = QtWidgets.QAction("Save &attachment", self,
statusTip="Save attachment to file",
triggered=self.on_attachment_context_menu_selection)
self.actionAttachmentOpen = QtWidgets.QAction("Open attachment with associated program", self,
statusTip="Open attachment with system default application",
triggered=self.on_attachment_context_menu_selection)
self.actionAttachmentOpenAsText = QtWidgets.QAction("Open attachment as text", self,
statusTip="Open attachment as plain text",
triggered=self.on_attachment_context_menu_selection)
self.actionOpenMboxFile = QtWidgets.QAction(QtGui.QIcon(':/icons/open.png'),
"Open Mailbox", self,
shortcut=QtGui.QKeySequence.Open,
statusTip="Open mailbox file",
triggered=self.on_open_file)
self.actionOpenMboxFile.setIconText("Open")
def set_port(self):
int_value, ok = QtWidgets.QInputDialog.getInt(self,
"SMTP port", "Enter SMTP port:", self.port)
if ok:
self.port = int_value
self.statusBar().showMessage('Set SMTP port to %s' % self.port)
self.topDock.setWindowTitle(" SMTP status: localhost:%s OFF" % self.port)
def on_logging_enabled(self):
if self.actionLogToFileEnabled.isChecked():
QtWidgets.QMessageBox.information(self, APPNAME, "Enabled logging to {}".format(
os.path.join(self.appdata_dir, LOG_FILE_NAME)))
def on_open_file(self, filePath=None):
global PICKLE_STATE_DIRTY
if not filePath:
filePath, __ = QtWidgets.QFileDialog.getOpenFileName(self,
"Select a Mailbox/EML/MSG file...",
self.last_used_fileopen_folder or os.path.dirname(
self.mbox_path),
"All Files (*)")
if filePath:
self.last_used_fileopen_folder = os.path.dirname(filePath)
if filePath.lower().endswith('.eml') or filePath.lower().endswith('.msg'):
with open(filePath, 'r') as fh:
msg = email.message_from_file(fh)
self.add_single_email_item(msg, True)
fh.seek(0)
QtWidgets.QMessageBox.information(self, APPNAME, "Single email message selected."
"It will be appended to the current mailbox.")
mbox_write_item(self.mbox_path, msg['From'], fh.read())
PICKLE_STATE_DIRTY = True
return
self.textEdit.setHtml("")
self.mbox_path = filePath
self.write_settings()
self.statusBar().showMessage('Mailbox: %s' % self.mbox_path)
del tableview_data[:]
self.parse_email_items()
PICKLE_STATE_DIRTY = True
self.setWindowTitle("%s - %s" % (APPNAME, self.mbox_path))
def update_html_clean_setting(self):
self.isHtmlCleaningEnabled = self.actionCleanHtmlToggle.isChecked()
EmailParser.cleaning_is_enabled = self.isHtmlCleaningEnabled
self.write_settings()
def on_toggle_toolbar(self):
self.is_toolbar_hidden = self.actionToggleToolbar.isChecked()
self.toolBar.setVisible(not self.is_toolbar_hidden)
def update_smtp_autostart_setting(self):
self.isSmtpAutostartEnabled = self.actionSmtpAutostartToggle.isChecked()
self.write_settings()
def toggle_smtp_server_state(self, start=False):
if self.port < 1024 and os.name != 'nt' and os.geteuid() != 0:
QtWidgets.QMessageBox.warning(self, "Not running as root",
"You must be root in order to run the SMTP server on port %s (current UID=%s).\n"
"To run as regular user set a port higher than 1024 in the Config menu" % (
self.port, os.geteuid()))
self.statusBar().showMessage('Failed starting SMTP server')
return
if start:
self.queueSmtpResult = queue.Queue()
try:
self.mailsync = SmtpMailsink(host="0.0.0.0", port=self.port, mailboxFilePath=self.mbox_path,
mailQueue=self.queueSmtpResult)
except PortAlreadyInUseException as e:
QtWidgets.QMessageBox.warning(None, APPNAME,
"SMTP port %d is already in use\n\n%s" % (self.port, str(e)))
return
self.mailsync.start()
self.topDock.setWindowTitle(" [email protected]:%s ON" % self.port)
# set polling for new messages every 1 sec
self.poller = EmailPoller()
self.poller.set_queue(self.queueSmtpResult)
self.poller.set_handler(self.add_single_email_item)
self.poller.start()
self.actionSmtpToggle.setChecked(True)
elif self.mailsync and self.mailsync.isAlive():
self.mailsync.stop()
self.topDock.setWindowTitle(" [email protected]:%s OFF" % self.port)
self.poller.stop()
# self.actionSmtpToggle.setIconText("&Start SMTP")
def createMenus(self):
self.setMenuBar(QtWidgets.QMenuBar())
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.actionNewMboxFile)
self.fileMenu.addAction(self.actionOpenMboxFile)
# self.fileMenu.addAction(self.actionSmtpToggle)
self.fileMenu.addAction(self.actionPrint)
self.fileMenu.addAction(self.quitAct)
self.editMenu = self.menuBar().addMenu("&Edit")
self.editMenu.addAction(self.actionSaveHtml)
self.editMenu.addAction(self.actionOpenMessage)
self.editMenu.addAction(self.actionShowRowMessage)
self.viewMenu = self.menuBar().addMenu("&View")
self.viewMenu.addAction(self.actionToggleToolbar)
self.smtpMenu = self.menuBar().addMenu("&SMTP")
self.smtpMenu.addAction(self.actionSmtpToggle)
self.configMenu = self.menuBar().addMenu("&Config")
self.configMenu.addAction(self.actionSetPort)
self.configMenu.addAction(self.actionSmtpAutostartToggle)
self.configMenu.addAction(self.actionCleanHtmlToggle)
self.configMenu.addAction(self.actionLogToFileEnabled)
self.helpMenu = self.menuBar().addMenu("&Help")
self.helpMenu.addAction(self.actionAbout)
def createAttachmentsMenuItems(self):
# ATTACHMENT RIGHT-CLICK CONTEXT MENU
self.attachmentContextMenu = QtWidgets.QMenu(self)
self.attachmentContextMenu.addAction(self.actionAttachmentOpen)
self.attachmentContextMenu.addAction(self.actionAttachmentOpenAsText)
self.attachmentContextMenu.addAction(self.actionAttachmentSave)
def createToolBars(self):
self.toolBar = self.addToolBar("&File")
self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.toolBar.addAction(self.actionNewMboxFile)
self.toolBar.addAction(self.actionOpenMboxFile)
self.toolBar.addAction(self.actionSmtpToggle)
self.toolBar.addAction(self.actionSaveHtml)
self.toolBar.addAction(self.actionPrint)
def createStatusBar(self):
self.statusBar().showMessage("Ready")
def createDockWindows(self):
self.topDock = QtWidgets.QDockWidget(self)
self.topDock.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable | QtWidgets.QDockWidget.DockWidgetFloatable)
self.topDock.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea
| QtCore.Qt.RightDockWidgetArea
| QtCore.Qt.TopDockWidgetArea
| QtCore.Qt.BottomDockWidgetArea)
self.tableView = EmailTableView()
self.tableView.addAction(self.actionSaveHtml)
self.tableView.addAction(self.actionShowRowMessage)
self.tableView.addAction(self.actionOpenMessage)
self.tableView.setAppWindowHandle(self)
self.tableView.setSelectionMode(QtWidgets.QTableView.SingleSelection)
self.topDock.setWindowTitle(" %s" % self.mbox_path)
self.topDock.setWidget(self.tableView)
self.tableView.selectionModel().selectionChanged.connect(self.onListItemSelect)
self.tableView.doubleClicked.connect(self.onListItemDoubleClick)
# self.tableView.connect(self.tableView, QtCore.SIGNAL("doubleClicked(QtCore.QModelIndex)"),
# self.tableView, QtCore.SLOT("self.onListItemDoubleClick(QtCore.QModelIndex)"))
self.addDockWidget(QtCore.Qt.TopDockWidgetArea, self.topDock)
self.bottomDock = QtWidgets.QDockWidget("", self)
self.bottomDock.setFeatures(QtWidgets.QDockWidget.DockWidgetVerticalTitleBar)
self.bottomDock.setTitleBarWidget(QtWidgets.QWidget(self.bottomDock))
# self.bottomDock.setFeatures(QtWidgets.QDockWidget.DockWidgetClosable)
self.bottomDock.setAllowedAreas(QtCore.Qt.BottomDockWidgetArea | QtCore.Qt.RightDockWidgetArea)
## hide initially
self.bottomDock.hide()
self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, self.bottomDock)
@timed
def initialize_data(self):
global tableview_data, PICKLE_IS_LOADED
if not DISABLE_APPSTATE and os.path.exists(self.pickle_storage_path):
LOG.debug('Appstate file found at %s, loading...' % self.pickle_storage_path)
try:
self.tableView.tableModel.sendSignalLayoutAboutToBeChanged()
listview_table_data_ = restore_state(open(self.pickle_storage_path, 'rb'))
tableview_data[:] = listview_table_data_[
:] # don't assign directly, need to keep the existing reference!
self.tableView.tableModel.sendSignalLayoutChanged()
PICKLE_IS_LOADED = True
except Exception as e:
LOG.error("Error initializing data", exc_info=e)
if not PICKLE_IS_LOADED:
LOG.info('Appstate not found or invalid, parsing data...')
try:
self.parse_email_items()
except Exception as e:
show_gui_error(e, 'Failed parsing mailbox!')
# self.tableView.resizeColumnsToContents()
def restore_column_sort_mode(self):
if len(tableview_data):
if self.last_saved_sort_column and self.last_saved_sort_order:
self.tableView.tableModel.sort(self.last_saved_sort_column, self.last_saved_sort_order)
def create_new_mbox(self):
suggested_filename = os.path.join(self.appdata_dir, 'mymailbox.mbox')
filename, __ = QtWidgets.QFileDialog.getSaveFileName(self,
"Create a new mailbox file", suggested_filename,
"Mailbox (*.mbox)")
if not filename:
return
with open(filename, 'wb') as new_file:
pass
if os.path.exists(filename):
self.on_open_file(filePath=filename)
def clearLayout(self, layout):
while layout.count():
child = layout.takeAt(0)
if child.widget() is not None:
child.widget().deleteLater()
elif child.layout() is not None:
self.clearLayout(child.layout())
def onListItemDoubleClick(self, qModelIndex):
self.on_open_raw_message(row_index=qModelIndex.row(), extension='eml')
# print 'double-clicked', qModelIndex.row()
def onListItemSelect(self, selected):
"""an item in the listbox has been clicked/selected
:param selected bool
"""
self.bottomDock.hide()
self.attachment_buttons = []
rowIndex = selected.first().top()
# TODO: clear layout
# self.clearLayout(self.bottomDock)
meta_data = tableview_data[rowIndex][INDEX_HIDDEN_METADATA]
email_message = meta_data.message
headers = meta_data.headers
email_body, attachments = EmailParser.parse_email_body(email_message)
if not email_body:
email_body = '<pre>[invalid message body]</pre><hr/>\n' + email_message.as_string()
html_headers = []
for header in list(headers.keys()):
header_value = headers[header]
if isinstance(header_value, str):
if header in ("From", "To"):
header_value = html.escape(header_value)
html_headers.append('<b>%s</b>: %s' % (header, header_value))
htmlheaders_div = '''<div style="font-size:10pt; color:#888;">
{0}</div><hr/>'''.format("<br/>\n".join(html_headers))
self.textEdit.setHtml(htmlheaders_div + email_body)
if attachments and len(attachments):
self.bottomDockWidgetContents = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(self.bottomDockWidgetContents) # :type layout: QtWidgets.QVBoxLayout
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.setAlignment(QtCore.Qt.AlignTop)
self.bottomDockLayout = QtWidgets.QHBoxLayout()
for attachIdx, attachmnt in enumerate(attachments):
button = QtWidgets.QPushButton(attachmnt.filename, None)
self.attachment_buttons.append(button)
button.setMenu(self.attachmentContextMenu)
button.setIcon(self.attachment_icon)
button.setIconSize(QtCore.QSize(16, 16))
button.attachment = attachmnt
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
button.setSizePolicy(sizePolicy)
layout.addWidget(button)
scrollarea = QtWidgets.QScrollArea()
scrollarea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scrollarea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scrollarea.setWidgetResizable(True)
scrollarea.setWidget(self.bottomDockWidgetContents)
self.bottomDock.setWidget(scrollarea)
self.bottomDock.show()
self.bottomDock.setMinimumHeight(32)
def on_attachment_context_menu_selection(self):
"""
Triggered when an item is selected in the rich-click context menu on an attachment button
"""
action = self.sender()
widgets = self.bottomDockWidgetContents.children()
selected_button = None
# TODO: Find a cleaner way to get the target without looping
# SEARCH FOR THE PRESSED BUTTON
for widget in widgets:
if isinstance(widget, QtWidgets.QPushButton):
if widget.isDown():
selected_button = widget
break
if selected_button:
if action == self.actionAttachmentOpen:
self.attachment_button_click_handler(selected_button)
if action == self.actionAttachmentOpenAsText:
self.attachment_button_click_handler(selected_button, ".txt")
if action == self.actionAttachmentSave:
attachment = selected_button.attachment
if attachment:
self.save_binary_file(caption='Saved attached file',
fname=attachment.filename,
bytes=attachment.binary_data)
def attachment_button_click_handler(self, widget=None, appendExtension=""):
"""
#:type attachments: Attachment
# :type attachIdx: int
"""
# PySide: Connecting Multiple Widgets to the Same Slot - The Mouse Vs. The Python
# http://www.blog.pythonlibrary.org/2013/04/10/pyside-connecting-multiple-widgets-to-the-same-slot/
target = self.sender()
if isinstance(target, QtWidgets.QPushButton):
button = target
else:
button = widget
if not button:
raise ValueError('Ouch! Expecting a button!')
attachment = button.attachment
attachment_file_path = os.path.join(self.appdata_dir, attachment.filename) + appendExtension
try:
with open(attachment_file_path, 'wb') as attachmentFile:
attachmentFile.write(attachment.binary_data)
self.start_file(attachment_file_path)
except Exception as e:
show_gui_error(e, error_text="Failed writing file " + attachment_file_path)
def start_file(self, filepath):
"""
Launches a file in platform-independent way
"""
if sys.platform.startswith('darwin'):
subprocess.call(('open', filepath))
elif os.name == 'nt':
os.startfile(filepath) ## only available on windowses
elif os.name == 'posix':
subprocess.call(('xdg-open', filepath))
def read_settings(self):
self.settings = QtCore.QSettings(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, "xh", APPNAME)
pos = self.settings.value("pos", QtCore.QPoint(200, 200))
size = self.settings.value("size", QtCore.QSize(600, 400))
self.resize(size)
self.move(pos)
self.port = self.settings.contains('port') and self.settings.value("port", type=int) or DEFAULT_PORT
self.mbox_path = self.settings.contains('mbox_path') and self.settings.value("mbox_path")
self.isSmtpAutostartEnabled = self.settings.contains('smtp_autostart') and self.settings.value(
"smtp_autostart", type=bool) or False
self.isHtmlCleaningEnabled = self.settings.contains('clean_html') and self.settings.value(
"clean_html", type=bool) or False
self.last_saved_sort_column = self.settings.contains('last_saved_sort_column') and self.settings.value(
"last_saved_sort_column", type=int) or None
self.last_saved_sort_order = self.settings.contains('last_saved_sort_order') and self.settings.value(
"last_saved_sort_order", type=int) or None
self.is_toolbar_hidden = self.settings.contains('is_toolbar_hidden') and self.settings.value(
"is_toolbar_hidden", type=bool) or False
self.is_log_file_enabled = self.settings.contains('is_log_file_enabled') and self.settings.value(
"is_log_file_enabled", type=bool) or False
def write_settings(self):
settings = QtCore.QSettings(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, "xh", APPNAME)
settings.setValue("pos", self.pos())
settings.setValue("size", self.size())
settings.setValue("port", self.port)
settings.setValue("mbox_path", str(self.mbox_path))
settings.setValue("smtp_autostart", self.isSmtpAutostartEnabled)
settings.setValue("clean_html", self.isHtmlCleaningEnabled)
settings.setValue("last_saved_sort_column", self.last_saved_sort_column)
settings.setValue("last_saved_sort_order", self.last_saved_sort_order)
settings.setValue("is_toolbar_hidden", self.is_toolbar_hidden)
settings.setValue("is_log_file_enabled", self.actionLogToFileEnabled.isChecked())
settings.sync()
def closeEvent(self, event):
if self.mailsync and self.mailsync.isAlive():
self.mailsync.stop()
if self.tableView.tableModel.last_saved_sort_column and self.tableView.tableModel.last_saved_sort_order:
self.last_saved_sort_column = self.tableView.tableModel.last_saved_sort_column
self.last_saved_sort_order = self.tableView.tableModel.last_saved_sort_order
LOG.info('closeEvent: Saving settings...')
self.write_settings()
LOG.info('Cleanup...')
delete_temp_files(self.appdata_dir, '*.tmp.*')
if not DISABLE_APPSTATE:
self.save_appstate()
return
quit_msg = "Are you sure you want to exit the program?"
reply = QtWidgets.QMessageBox.question(self, 'Message',
quit_msg, QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
self.write_settings()
event.accept()
QtWidgets.QApplication.instance().quit()
else:
event.ignore()
@timed
def save_appstate(self):
if PICKLE_IS_LOADED and not PICKLE_STATE_DIRTY:
# avoid resaving appstate if it did not change
LOG.info('App state data unchanged. Skipping saving appstate!')
return
LOG.info('Serializing appstate to file %s', self.pickle_storage_path)
fhandle = open(self.pickle_storage_path, 'wb')
save_state(tableview_data, fhandle)
def add_single_email_item(self, email_message, isSendUpdateModelSignal=True):
"""
This method is called both from bulk parsing, and single message parsing which required
the update signals to be sent
"""
headers = EmailParser.parse_email_headers(email_message)
if isSendUpdateModelSignal:
self.tableView.tableModel.sendSignalLayoutAboutToBeChanged()
# special treatment for the date column, since it needs to be sortable
# therefore using a QTableWidgetItem for it
timestamp = headers.get('timestamp', 0)
if isinstance(timestamp, float):
timestamp = int(timestamp)
qDate = QtCore.QDateTime.fromTime_t(timestamp)
tableview_data.append((headers.get('From', '[empty]'),
qDate,
headers.get('Subject', '[empty]'),
ItemMetaData(email_message, headers)
)
)
if isSendUpdateModelSignal:
self.tableView.tableModel.sendSignalLayoutChanged()
def parse_email_items(self):
"""
start parser thread in a separate thread
"""
_thread.start_new_thread(self.parse_email_items_task, ())
@timed
def parse_email_items_task(self):
mbox = mailbox.mbox(self.mbox_path)
num_total = 0
# table model update start
self.tableView.tableModel.sendSignalLayoutAboutToBeChanged()
for count, email_message in enumerate(mbox):
try:
self.add_single_email_item(email_message)
except Exception as e:
LOG.error('Error parsing message %s. Skipping..', pprint.pformat(email_message), exc_info=e)
if count % 100 == 0:
LOG.info('%d', count)
self.topDock.setWindowTitle("Loading messages: %s..." % count)
num_total = count
##### magic model update end
self.tableView.tableModel.sendSignalLayoutChanged()
LOG.info('Total messages parsed: %d', num_total)
self.topDock.setWindowTitle("Total messages: %s" % num_total)
class EmailPoller:
"""
:type queue: Queue.Queue
"""
def __init__(self):
self.timer = QtCore.QTimer()
self.timer.setInterval(POLLING_TIME_MILLISECS)
self.timer.timeout.connect(self.check_for_new_items)
self.queue = None
self.handler = None
def start(self):
self.timer.start()
def stop(self):
self.timer.stop()
def set_queue(self, queue=None):
self.queue = queue
def set_handler(self, handler):
self.handler = handler
# @QtCore.pyqtSlot()
def check_for_new_items(self):
global PICKLE_STATE_DIRTY
if not self.queue:
raise Exception("You forgot to set a Queue object")
if not self.handler:
raise Exception("You forgot to set a handler")
if not self.queue.empty():
raw_email = self.queue.get()
if raw_email:
raw_email = re.sub("""<style.*?</style>""", '', raw_email)
emailMessage = email.message_from_string(raw_email)
self.handler(emailMessage, isSendUpdateModelSignal=True)
PICKLE_STATE_DIRTY = True
class EmailParser:
cleaning_is_enabled = False
@staticmethod
def parse_email_body(email_message):
if not email_message:
return None, None
attachments = []
body_plain = ''
body_html = ''
body_image = ''
body_attacments_info = ''
mainbodydata = ''
already_have_html = False
if email_message.is_multipart():
for part in email_message.walk():
# print part.get_content_type(), part.is_multipart(), len(part.get_payload())
charset = part.get_content_charset()
if charset and charset.lower() in VALID_ENCODINGS:
charset = None
content_type = part.get_content_type()
if 'text/html' in content_type: