forked from mailpile/Mailpile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
executable file
·1193 lines (1038 loc) · 40 KB
/
ui.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/python
#
# This file contains the UserInteraction and Session classes.
#
# The Session encapsulates settings and command results, allowing commands
# to be chanined in an interactive environment.
#
# The UserInteraction classes log the progress and performance of individual
# operations and assist with rendering the results in various formats (text,
# HTML, JSON, etc.).
#
###############################################################################
from collections import defaultdict
import datetime
import os
import random
import re
import sys
import traceback
import json
from lxml.html.clean import autolink_html
import jsontemplate
import mailpile.commands
from mailpile.util import *
from mailpile.search import MailIndex
class SuppressHtmlOutput(Exception):
pass
def default_dict(*args):
d = defaultdict(str)
for arg in args:
d.update(arg)
return d
class UserInteraction:
"""Log the progress and performance of individual operations"""
MAX_BUFFER_LEN = 150
MAX_WIDTH = 79
LOG_URGENT = 0
LOG_RESULT = 5
LOG_ERROR = 10
LOG_NOTIFY = 20
LOG_WARNING = 30
LOG_PROGRESS = 40
LOG_DEBUG = 50
LOG_ALL = 99
def __init__(self, log_parent=None):
self.log_parent = log_parent
self.log_buffer = []
self.log_buffering = False
self.log_level = self.LOG_ALL
self.interactive = False
self.time_tracking = [('Main', [])]
self.time_elapsed = 0.0
self.last_display = [self.LOG_PROGRESS, 0]
self.render_mode = 'text'
self.html_variables = {
'title': 'Mailpile',
'name': 'Chelsea Manning',
'csrf': '',
'even_odd': 'odd',
'mailpile_size': 0
}
# Logging
def _display_log(self, text, level=LOG_URGENT):
pad = ''
if self.last_display[0] in (self.LOG_PROGRESS, ):
pad = ' ' * max(0, min(self.MAX_WIDTH, self.MAX_WIDTH-len(text)))
sys.stderr.write('\r')
elif self.last_display[0] not in (self.LOG_RESULT, ):
sys.stderr.write('\n')
sys.stderr.write('%s%s' % (text.encode('utf-8'), pad))
self.last_display = [level, len(text)]
def clear_log(self):
self.log_buffer = []
def flush_log(self):
try:
while len(self.log_buffer) > 0:
level, message = self.log_buffer.pop(0)
if level <= self.log_level:
self._display_log(message, level)
except IndexError:
pass
def block(self):
self._display_log('')
self.log_buffering = True
def unblock(self):
self.log_buffering = False
self.last_display = [self.LOG_RESULT, 0]
self.flush_log()
def log(self, level, message):
if self.log_buffering:
self.log_buffer.append((level, message))
while len(self.log_buffer) > self.MAX_BUFFER_LEN:
self.log_buffer[0:(self.MAX_BUFFER_LEN/10)] = []
elif level <= self.log_level:
self._display_log(message, level)
def finish_command(self):
pass
def start_command(self):
pass
error = lambda self, msg: self.log(self.LOG_ERROR, msg)
notify = lambda self, msg: self.log(self.LOG_NOTIFY, msg)
warning = lambda self, msg: self.log(self.LOG_WARNING, msg)
progress = lambda self, msg: self.log(self.LOG_PROGRESS, msg)
debug = lambda self, msg: self.log(self.LOG_DEBUG, msg)
# Progress indication and performance tracking
times = property(lambda self: self.time_tracking[0][1])
def mark(self, action, percent=None):
"""Note that we are about to perform an action."""
self.progress(action)
self.times.append((time.time(), action))
def reset_marks(self, quiet=False):
"""This sequence of actions is complete."""
t = self.times
self.times = []
if t:
self.time_elapsed = elapsed = t[-1][0] - t[0][0]
if not quiet:
self.notify('Elapsed: %.3fs (%s)' % (elapsed, t[-1][1]))
return elapsed
else:
return 0
def mark_push(self, subtask):
"""We are beginnning a sub-sequence we want to track separately."""
self.time_tracking[:0] = [(subtask, [])]
def mark_pop(self, quiet=False):
"""Sub-task completed."""
elapsed = self.reset_marks(quiet=quiet)
if len(self.time_tracking) > 1:
subtask, times = self.time_tracking.pop(0)
self.mark('Completed %s in %.3fs' % (subtask, elapsed))
return elapsed
# Higher level command-related methods
def _display_result(self, result):
sys.stdout.write(result+'\n')
def start_command(self, cmd, args, kwargs):
self.flush_log()
self.mark('%s(%s)' % (cmd, ', '.join((args or []) + ['%s' % kwargs])))
def finish_command(self):
self.reset_marks()
def display_result(self, result):
"""Render command result objects to the user"""
self._display_log('', level=self.LOG_RESULT)
if self.render_mode == 'json':
return self._display_result(result.as_json())
elif self.render_mode in ('html', 'jhtml'):
return self._display_result(result.as_html())
elif self.render_mode == 'xml':
return self._display_result(result.as_xml())
elif self.render_mode == 'rss':
return self._display_result(result.as_rss())
else:
return self._display_result(unicode(result))
# Creating output files
DEFAULT_DATA_NAME_FMT = '%(msg_idx)s.%(count)s_%(att_name)s.%(att_ext)s'
DEFAULT_DATA_ATTRS = {
'msg_idx': 'file',
'mimetype': 'application/octet-stream',
'att_name': 'unnamed',
'att_ext': 'dat',
'rand': '0000'
}
DEFAULT_DATA_EXTS = {
# FIXME: Add more!
'text/plain': 'txt',
'text/html': 'html',
'image/gif': 'gif',
'image/jpeg': 'jpg',
'image/png': 'png'
}
def _make_data_filename(self, name_fmt, attributes):
return (name_fmt or self.DEFAULT_DATA_NAME_FMT) % attributes
def _make_data_attributes(self, attributes={}):
attrs = self.DEFAULT_DATA_ATTRS.copy()
attrs.update(attributes)
attrs['rand'] = '%4.4x' % random.randint(0, 0xffff)
if attrs['att_ext'] == self.DEFAULT_DATA_ATTRS['att_ext']:
if attrs['mimetype'] in self.DEFAULT_DATA_EXTS:
attrs['att_ext'] = self.DEFAULT_DATA_EXTS[attrs['mimetype']]
return attrs
def open_for_data(self, name_fmt=None, attributes={}):
filename = self._make_data_filename(name_fmt,
self._make_data_attributes(attributes))
return filename, open(filename, 'w')
# Rendering helpers for templating and such
def render_json(self, data):
"""Render data as JSON"""
from json import JSONEncoder
class NoFailEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, (list, dict, str, unicode, int, float, bool, type(None))):
return JSONEncoder.default(self, obj)
return "COMPLEXBLOB"
return json.dumps(data, indent=1, cls=NoFailEncoder)
def _html_template(self, config, tpl_names, elems=None):
for tpl_name in tpl_names:
try:
fn = '%s.html' % tpl_name
return config.open_file('html_template', fn)[1].read().decode('utf-8')
except (IOError, OSError, AttributeError):
pass
if elems:
return ('<div class="%s">\n ' % tpl_names[0].replace('/', '_') +
'\n '.join(['<span class="%s">{%s}</span>' % (e,e)
for e in elems]) +
'\n</div>')
else:
return '{data}'
def render_html(self, cfg, tpl_names, data):
"""Render data as HTML"""
return jsontemplate.expand(self._html_template(cfg, tpl_names,
elems=data.keys()), data,
undefined_str='')
def edit_messages(self, emails):
self.error('Sorry, this UI cannot edit messages.')
class HttpUserInteraction(UserInteraction):
def __init__(self, request, *args, **kwargs):
UserInteraction.__init__(self, *args, **kwargs)
self.request = request
self.logged = []
self.results = []
# Just buffer up rendered data
def _display_log(self, text, level=UserInteraction.LOG_URGENT):
self.logged.append((level, text))
def _display_result(self, result):
self.results.append(result)
# Stream raw data to the client on open_for_data
def open_for_data(self, name_fmt=None, attributes={}):
return 'HTTP Client', RawHttpResponder(self.request, attributes)
# Render to HTML/JSON/...
def _render_jhtml_response(self, config):
return json.dumps(default_dict(self.html_variables, {
'results': self.results,
'logged': self.logged,
}))
def _render_text_response(self, config):
return '%s\n%s' % (
'\n'.join([l[1] for l in self.logged]),
('\n%s\n' % ('=' * 79)).join(self.results)
)
def _render_html_response(self, config):
page = self._html_template(config, ['html/page'],
elems=['results', 'logged'])
quiet = Session(config)
quiet.ui = SilentInteraction()
while page.startswith('{# set'):
load, page = page.split('\n', 1)
_set, vname, _eq, mode, cmd = load.strip()[3:-2].split(None, 4)
cmd, arg = (' ' in cmd) and cmd.split(' ', 1) or (cmd, '')
quiet.ui.render_mode = mode
result = mailpile.commands.Action(quiet, cmd, arg)
self.html_variables[vname] = quiet.ui.display_result(result)
return jsontemplate.expand(page, default_dict(self.html_variables, {
'results': '\n'.join(['<div class="result">%s</div>' % r
for r in self.results]),
'logged': '\n'.join(['<p class="ll_%s">%s</p>' % l
for l in self.logged])
}), undefined_str='')
def render_response(self, config):
if self.render_mode == 'json':
return ('application/json', '[%s]' % ','.join(self.results))
elif self.render_mode == 'jhtml':
return ('application/json', self._render_jhtml_response(config))
elif self.render_mode == 'html':
return ('text/html', self._render_html_response(config))
else:
return ('text/plain', self._render_text_response(config))
def edit_messages(self, emails):
pass
def print_filters(self, args):
print args
return args
class BackgroundInteraction(UserInteraction):
# FIXME: This shouldn't be quite so silent...
def _display_log(self, text, level=UserInteraction.LOG_URGENT):
pass
class SilentInteraction(UserInteraction):
def _display_log(self, text, level=UserInteraction.LOG_URGENT):
pass
def _display_result(self, result):
return result
def edit_messages(self, emails):
pass
class BackgroundInteraction(SilentInteraction):
# FIXME: This shouldn't be quite so silent...
pass
class xxBaseUI(object):
WIDTH = 80
MAX_BUFFER_LEN = 150
interactive = False
buffering = False
def __init__(self):
self.buffered = []
def print_key(self, key, config): pass
def reset_marks(self, quiet=False): pass
def mark(self, progress): pass
def clear(self):
self.buffered = []
def flush(self):
try:
while len(self.buffered) > 0:
self.buffered.pop(0)()
except IndexError:
pass
def block(self):
self.buffering = True
def unblock(self):
self.flush()
self.buffering = False
def start_command(self, cmd, args, kwargs):
self.clear()
self.mark('%s(%s)' % (cmd, ', '.join((args or []) + ['%s' % kwargs])))
def finish_command(self):
self.reset_marks()
def say(self, text='', newline='\n', fd=sys.stdout):
if not fd:
fd = sys.stdout
def sayit():
fd.write(text.encode('utf-8')+newline)
fd.flush()
self.buffered.append(sayit)
while len(self.buffered) > self.MAX_BUFFER_LEN:
self.buffered[0:(self.MAX_BUFFER_LEN/10)] = []
if not self.buffering:
self.flush()
def notify(self, message):
self.say('%s%s' % (message, ' ' * (self.WIDTH-1-len(message))))
def warning(self, message):
self.say('Warning: %s%s' % (message, ' ' * (self.WIDTH-11-len(message))))
def error(self, message):
self.say('Error: %s%s' % (message, ' ' * (self.WIDTH-9-len(message))))
def print_filters(self, config):
w = int(self.WIDTH * 23/80)
ffmt = ' %%3.3s %%-%d.%ds %%-%d.%ds %%s' % (w, w, w-2, w-2)
self.say(ffmt % ('ID', ' Tags', 'Terms', ''))
for fid, terms, tags, comment in config.get_filters(filter_on=None):
self.say(ffmt % (
fid,
' '.join(['%s%s' % (t[0], config['tag'].get(t[1:], t[1:]))
for t in tags.split()]),
((terms == '*') and '(all new mail)' or
(terms == '@read') and '(read mail)' or terms or '(none)'),
comment or '(none)'
))
def display_messages(self, emails,
raw=False, sep='', fd=sys.stdout, context=True):
for email in emails:
if raw:
self.display_message(email, None, raw=True, sep=sep, fd=fd)
else:
tree = email.get_message_tree()
if context:
try:
try:
conversation = [int(m[0], 36) for m in tree['conversation']
if m[0] is not None]
except TypeError:
self.warning('Bad conversation: %s' % tree['conversation'])
conversation = [email.msg_idx]
self.display_results(email.index, conversation, [],
expand=[email], fd=fd)
except TypeError:
self.warning('No conversation, bad ID: %s' % email.msg_idx)
self.warning(traceback.format_exc())
else:
email.evaluate_pgp(tree, decrypt=True)
self.display_message(email, tree, raw=raw, sep=sep, fd=fd)
def _name(self, sender):
words = re.sub('["<>]', '', sender).split()
nomail = [w for w in words if not '@' in w]
if nomail: return ' '.join(nomail)
return ' '.join(words)
def _names(self, senders):
if len(senders) > 1:
return re.sub('["<>]', '', ', '.join([x.split()[0] for x in senders]))
return ', '.join([self._name(s) for s in senders])
def _compact(self, namelist, maxlen):
l = len(namelist)
while l > maxlen:
namelist = re.sub(', *[^, \.]+, *', ',,', namelist, 1)
if l == len(namelist): break
l = len(namelist)
namelist = re.sub(',,,+, *', ' .. ', namelist, 1)
return namelist
def display_message(self, email, tree, raw=False, sep='', fd=None):
if raw:
self.say(sep, fd=fd)
for line in email.get_file().readlines():
try:
line = line.decode('utf-8')
except UnicodeDecodeError:
try:
line = line.decode('iso-8859-1')
except:
line = '(MAILPILE DECODING FAILED)\n'
self.say(line, newline='', fd=fd)
else:
self.say(sep, fd=fd)
for hdr in ('From', 'Subject', 'Date', 'To', 'Cc'):
value = email.get(hdr, '')
if value:
self.say('%s: %s' % (hdr, value), fd=fd)
self.say('', fd=fd)
for part in tree['text_parts']:
if part['type'] == 'quote':
self.say('[quoted text]', fd=fd)
else:
self.say('%s' % part['data'], fd=fd, newline='')
if tree['attachments']:
self.say('', fd=fd)
for att in tree['attachments']:
desc = '%(count)s: %(filename)s (%(mimetype)s, %(length)s bytes)' % att
self.say(' [Attachment #%s]' % desc, fd=fd)
self.say('', fd=fd)
def edit_messages(self, emails):
self.say('Sorry, this UI cannot edit messages.')
def display_gpg_keys(self, data):
self.say('%s' % data)
class xxTextUI(xxBaseUI):
def __init__(self):
xxBaseUI.__init__(self)
self.times = []
def reset_marks(self, quiet=False):
t = self.times
self.times = []
if t:
if not quiet:
result = 'Elapsed: %.3fs (%s)' % (t[-1][0] - t[0][0], t[-1][1])
self.say('%s%s' % (result, ' ' * (self.WIDTH-1-len(result))))
return t[-1][0] - t[0][0]
else:
return 0
def mark(self, progress):
self.say(' %s%s\r' % (progress, ' ' * (self.WIDTH-3-len(progress))),
newline='', fd=sys.stderr)
self.times.append((time.time(), progress))
def display_vcard(self, vcard, compact=False):
if compact:
self.say('%s' % vcard)
else:
self.say('%s' % vcard.as_vCard())
def display_results(self, idx, results, terms,
start=0, end=None, num=None, expand=None,
fd=None):
if not results: return (0, 0)
num = num or 20
if end: start = end - num
if start > len(results): start = len(results)
if start < 0: start = 0
clen = max(3, len('%d' % len(results)))
cfmt = '%%%d.%ds' % (clen, clen)
count = 0
expand_ids = [e.msg_idx for e in (expand or [])]
for mid in results[start:start+num]:
count += 1
if expand and mid in expand_ids:
self.display_messages([expand[expand_ids.index(mid)]],
context=False, fd=fd);
else:
try:
msg_info = idx.get_msg_by_idx(mid)
msg_subj = msg_info[idx.MSG_SUBJECT]
if expand:
msg_from = [msg_info[idx.MSG_FROM]]
msg_date = [msg_info[idx.MSG_DATE]]
else:
conversation = idx.get_conversation(msg_info)
msg_from = [r[idx.MSG_FROM] for r in conversation]
msg_date = [r[idx.MSG_DATE] for r in conversation]
msg_from = msg_from or ['(no sender)']
msg_date = datetime.date.fromtimestamp(max([
int(d, 36) for d in msg_date]))
msg_tags = '<'.join(sorted([re.sub("^.*/", "", idx.config['tag'].get(t, t))
for t in idx.get_tags(msg_info=msg_info)]))
msg_tags = msg_tags and (' <%s' % msg_tags) or ' '
sfmt = '%%-%d.%ds%%s' % (41-(clen+len(msg_tags)),41-(clen+len(msg_tags)))
self.say((cfmt+' %4.4d-%2.2d-%2.2d %-25.25s '+sfmt
) % (start + count,
msg_date.year, msg_date.month, msg_date.day,
self._compact(self._names(msg_from), 25),
msg_subj, msg_tags),
fd=fd)
except (IndexError, ValueError):
self.say('-- (not in index: %s)' % mid)
self.mark(('Listed %d-%d of %d results'
) % (start+1, start+count, len(results)))
return (start, count)
def display_messages(self, emails, raw=False, sep=None, fd=None, context=True):
viewer = None
if not fd:
if self.interactive:
viewer = subprocess.Popen(['less'], stdin=subprocess.PIPE)
fd = viewer.stdin
else:
fd = sys.stdout
try:
xx.BaseUI.display_messages(self, emails,
raw=raw,
sep=(sep is None and ('_' * self.WIDTH) or sep),
fd=fd, context=context)
except IOError, e:
pass
if viewer:
fd.close()
viewer.wait()
def edit_messages(self, emails):
for email in emails:
try:
if email.is_editable():
es = email.get_editing_string().encode('utf-8')
tf = tempfile.NamedTemporaryFile(suffix='.txt')
tf.write(es)
tf.flush()
rv = subprocess.call(['edit', tf.name])
tf.seek(0, 0)
ns = tf.read()
tf.close()
if es != ns:
email.update_from_string(ns)
self.say('Message saved. Use the "mail" command to send it.')
else:
self.warning('Message unchanged.')
else:
self.error('That message cannot be edited.')
except:
self.warning('Editing failed!')
self.warning(traceback.format_exc())
class RawHttpResponder:
def __init__(self, request, attributes={}):
self.request = request
#
# FIXME: Security risks here, untrusted content may find its way into
# our raw HTTP headers etc.
#
mimetype = attributes.get('mimetype', 'application/octet-stream')
filename = attributes.get('filename', 'attachment.dat').replace('"', '')
length = attributes['length']
request.send_http_response(200, 'OK')
request.send_standard_headers(header_list=[
('Content-Length', length),
('Content-Disposition', 'attachment; filename="%s"' % filename)
], mimetype=mimetype)
def write(self, data):
self.request.wfile.write(data)
def close(self):
raise SuppressHtmlOutput()
class xxHttpUI(xxBaseUI):
def __init__(self, request):
xxBaseUI.__init__(self)
def set_postdata(self, postdata):
self.post_data = postdata
def set_querydata(self, querydata):
self.query_data = querydata
def open_for_data(self, name_fmt=None, attributes={}):
return 'HTTP Client', RawHttpResponder(self.request, attributes)
class xxJsonUI(xxHttpUI):
def __init__(self, request):
xxHttpUI.__init__(self, request)
self.request = request
self.clear()
def clear(self):
self.status_code = 200
self.buffered_results = []
self.buffered_loglines = []
self.buffered_json = {
"command": '',
"loglines": self.buffered_loglines,
"results": self.buffered_results
}
def say(self, text=[], newline=None, fd=None):
# Just suppress the progress indicator chitter chatter
if not text.endswith('\r'):
self.buffered_loglines.append(text.rstrip())
def error(self, message):
self.status_code = 500
return xxHttpUI.error(self, message)
def explain_msg_summary(self, info):
return {
'idx': info[0],
'id': info[1],
'from': info[2],
'subject': info[3],
'date': long(info[4], 36),
'tag_ids': info[5],
'url': '/=%s/%s/' % (info[0], info[1])
}
def display_gpg_keys(self, data):
self.buffered_json['results'] = self.buffered_results = data
def display_results(self, idx, results, terms,
start=0, end=0, num=0, expand=None,
fd=None):
if not results:
return (0, 0)
num = num or 50
if end: start = end - num
if start > len(results): start = len(results)
if start < 0: start = 0
count = 0
for mid in results[start:start+num]:
count += 1
msg_info = idx.get_msg_by_idx(mid)
result = self.explain_msg_summary([
msg_info[MailIndex.MSG_IDX],
msg_info[MailIndex.MSG_ID],
msg_info[MailIndex.MSG_FROM],
msg_info[MailIndex.MSG_SUBJECT],
msg_info[MailIndex.MSG_DATE],
msg_info[MailIndex.MSG_TAGS].split(','),
])
result['tags'] = sorted([idx.config['tag'].get(t,t)
for t in idx.get_tags(msg_info=msg_info)
if 'tag:%s' % t not in terms])
self.buffered_results.append(result)
return (start, count)
def display_messages(self, emails,
raw=False, sep='', fd=sys.stdout, context=True):
for email in emails:
# This doesn't do e-mail contexts...
tree = email.get_message_tree()
email.evaluate_pgp(tree, decrypt=True)
self.display_message(email, tree, raw=raw, sep=sep, fd=fd)
def prune_message_tree(self, tree):
pruned = {}
for k in tree:
if k not in ('headers_lc', 'summary', 'conversation', 'tags',
'attachments'):
pruned[k] = tree[k]
pruned['tag_ids'] = tree['tags']
pruned['summary'] = self.explain_msg_summary(tree['summary'])
pruned['conversation'] = [self.explain_msg_summary(c)
for c in tree['conversation']]
pruned['attachments'] = attachments = []
for a in tree.get('attachments', []):
att = {}
att.update(a)
del att['part']
attachments.append(att)
return pruned
def display_message(self, email, tree, raw=False, sep='', fd=None):
if raw:
for line in email.get_file().readlines():
try:
line = line.decode('utf-8')
except UnicodeDecodeError:
try:
line = line.decode('iso-8859-1')
except:
line = '(MAILPILE DECODING FAILED)\n'
self.say(line, newline='', fd=fd)
else:
self.buffered_results.append(self.prune_message_tree(tree))
def render_data(self, session, request_url, request_path):
message = json.dumps(self.buffered_json, indent=1)
return message, 'application/json'
def render(self, session, request_url, request_path):
message, mimetype = self.render_data(session, request_url, request_path)
self.request.send_http_response(self.status_code,
(self.status_code == 200) and "OK" or 'Error')
self.request.send_header('Content-Length', len(message or ''))
self.request.send_standard_headers(mimetype=mimetype, cachectrl='no-cache')
self.request.wfile.write(message)
self.request.log_request(self.status_code, message and len(message) or '-')
class xxXmlUI(xxJsonUI):
ROOT_NAME = 'xml'
ROOT_ATTRS = {'testing': True}
EXPLAIN_XML = True
BARE_LISTS = False
def esc(self, d):
d = unicode(d)
d = d.replace('&', '&').replace('>', '>').replace('<', '<')
return d.encode('utf-8')
def render_xml_data(self, data, name='', attrs={}, indent=''):
attrtext = ''
if type(data) == type(dict()):
data = self.render_xml_dict(data, indent=indent)+indent
dtype = 'dict'
elif type(data) == type(list()):
data = self.render_xml_list(data, name=name, indent=indent)+indent
dtype = 'list'
if self.BARE_LISTS:
return data
elif type(data) == type(set()):
data = self.render_xml_list(list(data), name=name, indent=indent)+indent
dtype = 'set'
if self.BARE_LISTS:
return data
else:
data = self.esc(data)
dtype = None
if '\n' in data:
attrtext += ' xml:space="preserve"'
if self.EXPLAIN_XML:
attrtext += dtype and (' type="%s"' % dtype) or ''
for attr in attrs:
attrtext += ' %s="%s"' % (attr, self.esc(attrs[attr]))
if data.strip():
return '%s<%s%s>%s</%s>' % (indent, name, attrtext, data, name)
else:
return '%s<%s%s/>' % (indent, name, attrtext)
def render_xml_list(self, lst, name='items', indent=''):
xml = ['']
if name.endswith('s'):
nh = name[:-1]
else:
nh = 'item'
for item in lst:
xml.append(self.render_xml_data(item, name=nh, indent=indent+' '))
return '\n'.join(xml)+'\n'
def render_xml_dict(self, dct, name='dict', indent=''):
xml = ['']
for name in dct.keys():
xml.append(self.render_xml_data(dct[name], name=name, indent=indent+' '))
return '\n'.join(xml)+'\n'
def render_data(self, session, request_url, request_path):
message = ('<?xml version="1.0"?>\n' +
self.render_xml_data(self.buffered_json,
name=self.ROOT_NAME,
attrs=self.ROOT_ATTRS))
return message, 'text/xml'
class xxRssUI(xxXmlUI):
ROOT_NAME = 'rss'
ROOT_ATTRS = {'version': '2.0'}
EXPLAIN_XML = False
BARE_LISTS = True
def clear(self):
xxXmlUI.clear(self)
self.buffered_json = {
"channel": {'items': self.buffered_results}
}
def explain_msg_summary(self, info):
summary = xxXmlUI.explain_msg_summary(self, info)
return {
'_id': summary['id'],
'title': summary['subject'],
'link': summary['url'],
'pubDate': summary['date']
}
def prune_message_tree(self, tree):
r = {}
r['items'] = [self.explain_msg_summary(c) for c in tree['conversation']]
for item in r['items']:
if item['_id'] == tree['id']:
item['description'] = 'FIXME: Insert text body here, w/o quotes?'
return r
def render_data(self, session, request_url, request_path):
# Reparent conversation list for single message
if (len(self.buffered_results) > 0
and 'items' in self.buffered_results[0]):
self.buffered_results = self.buffered_results[0]['items']
self.buffered_json['channel']['items'] = self.buffered_results
# Make URLs absolute
for item in self.buffered_results:
item['link'] = '%s%s' % (request_url, item['link'])
# Cleanup...
for r in self.buffered_results:
if 'tags' in r: del r['tags']
if '_id' in r: del r['_id']
# FIXME: Add channel info to buffered_json before rendering.
return (xxXmlUI.render_data(self, session, request_url, request_path)[0],
'application/rss+xml')
class xxHtmlUI(xxHttpUI):
WIDTH = 110
def __init__(self, request):
xxHttpUI.__init__(self, request)
self.buffered_html = []
self.request = request
def clear(self):
self.buffered_html = []
def say(self, text='', newline='\n', fd=None):
# Just suppress the progress indicator chitter chatter
if not text.endswith('\r'):
self.buffered_html.append(('text', text+newline))
def fmt(self, l):
return l[1].replace('&', '&').replace('>', '>').replace('<', '<')
def transform_text(self):
text = [self.fmt(l) for l in self.buffered_html if l[0] != 'html']
self.buffered_html = [l for l in self.buffered_html if l[0] == 'html']
self.buffered_html.append(('html', '<pre id="loglines">%s</pre>' % ''.join(text)))
def render(self, session, request_url, path):
config = session.config
index = config.get_index(session)
sidebar = ['<ul class="tag_list">']
tids = config.get('tag', {}).keys()
special = ['new', 'inbox', 'sent', 'drafts', 'spam', 'trash']
def tord(k):
tname = config['tag'][k]
if tname.lower() in special:
return '00000-%s-%s' % (special.index(tname.lower()), tname)
return tname
tids.sort(key=tord)
for tid in tids:
checked = ('tag:%s' % tid) in session.searched and ' checked' or ''
checked1 = checked and ' checked="checked"' or ''
tag_name = config.get('tag', {}).get(tid)
tag_new = index.STATS.get(tid, [0,0])[1]
sidebar.append((' <li id="tag_%s" class="%s">'
'<input type="checkbox" name="tag_%s"%s />'
' <a href="/%s/">%s</a>'
' <span class="tag_new %s">(<b>%s</b>)</span>'
'</li>') % (tid, checked, tid, checked1,
tag_name, tag_name,
tag_new and 'some' or 'none', tag_new))
sidebar.append('</ul>')
lastqpath = (path != '/' and path[1] not in ('=', '_') and path[:-1]
or '')
variables = {
'url': request_url,
'lastq': self.post_data.get('lq', self.query_data.get('q',
[lastqpath]))[0].strip().decode('utf-8'),
'csrf': self.request.csrf(),
'path': path
}
# FIXME: This title is dumb
title = 'The biggest pile of mail EVAR!'
self.request.send_full_response(self.render_page(config, variables,
title=title,
body=self.render_html(),
sidebar='\n'.join(sidebar)),
suppress_body=False)
def render_page(self, config, variables, body='', title='', sidebar=''):
tpl = config.get('path', {}).get(self.request.http_host(), 'html_template')
def r(part):
return config.open_file(tpl, 'html/%s.html' % part)[1].read() % variables
return ''.join([
r('head'), '<title>', title, '</title>',
r('body'), body,
r('sidebar'), sidebar,
r('tail')
])
def render_html(self):
self.transform_text()
html = ''.join([l[1] for l in self.buffered_html])
self.buffered_html = []
return html
def display_results(self, idx, results, terms,
start=0, end=None, num=None,
expand=None, fd=None):
if not results: return (0, 0)
num = num or 50
if end: start = end - num
if start > len(results): start = len(results)
if start < 0: start = 0
count = 0
nav = []
if start > 0:
bstart = max(1, start-num+1)
nav.append(('<a href="/?q=/search%s %s"><< page back</a>'
) % (bstart > 1 and (' @%d' % bstart) or '', ' '.join(terms)))
else:
nav.append('first page')
nav.append('(about %d results)' % len(results))
if start+num < len(results):
nav.append(('<a href="/?q=/search @%d %s">next page >></a>'
) % (start+num+1, ' '.join(terms)))
else:
nav.append('last page')
self.buffered_html.append(('html', ('<p id="rnavtop" class="rnav">%s '
' </p>\n') % ' '.join(nav)))
self.buffered_html.append(('html', '<table class="results" id="results">\n'))
expand_ids = [e.msg_idx for e in (expand or [])]
for mid in results[start:start+num]:
count += 1
try:
msg_info = idx.get_msg_by_idx(mid)
all_msg_tags = [idx.config['tag'].get(t,t)
for t in idx.get_tags(msg_info=msg_info)]