1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """ This module contains functions that work directly on an online
22 ImapServer/ImapMailbox.
23 """
24 import email
25 from ProcImap.Utils.Processing import put_through_pager
26
27 DEFAULT_PAGER = 'less'
28
29
31 """ Display a stripped down version of the message with UID in
32 a pager. The displayed message will contain the the header
33 fields set in 'headerfields', and the first BODY section
34 (which is usually the human-readable part)
35
36 headerfields defaults to ['Date', 'From', 'To', 'Subject']
37 contrary to the declaration.
38 """
39 header = mailbox.get_header(uid)
40 result = ''
41
42 body = ''
43 (code, data) = mailbox._server.uid('fetch', uid, '(BODY[1])')
44 if code == 'OK':
45 body = data[0][1]
46
47 if headerfields is None:
48 headerfields = ['Date', 'From', 'To', 'Subject']
49 for field in headerfields:
50 if header.has_key(field):
51 result += "%s: %s\n" % (field, header[field])
52 result += "\n"
53 result += body
54 put_through_pager(result, pager)
55
56
57 -def summary(mailbox, uids, printout=True, printuid=True):
58 """ generates lines showing some basic information about the messages
59 with the supplied uids. Non-existing UIDs in the list are
60 silently ignored.
61
62 If printout is True, the lines are printed out as they
63 are generated, and the function returns nothing, otherwise,
64 nothing is printed out and the function returns a list of
65 generated lines.
66
67 The summary contains
68 1) an index (the uid if printuid=True)
69 2) the from name (or from address), truncated
70 3) the date of the message
71 4) the subject, truncated
72
73 Each line has the indicated fields truncated so that it is at
74 most 79 characters wide.
75 """
76 counter = 0
77 result = []
78 if isinstance(uids, (str, int)):
79 uids = [uids]
80 for uid in uids:
81 try:
82 header = mailbox.get_header(uid)
83 except:
84 continue
85 counter += 1
86 index = counter
87 if printuid:
88 index = str(uid)
89 index = "%2s" % index
90 (from_name, address) = email.utils.parseaddr(header['From'])
91 if from_name == '':
92 from_name = address
93 date = str(header['Date'])
94 datetuple = email.utils.parsedate_tz(date)
95 date = date[:16].ljust(16, " ")
96 if datetuple is not None:
97 date = "%02i/%02i/%04i %02i:%02i" \
98 % tuple([datetuple[i] for i in (1,2,0,3,4)])
99 subject = str(header['Subject'])
100 subject = ' '.join([s for (s, c) in \
101 email.header.decode_header(subject)])
102 length_from = 25-len(index)
103 length_subject = 35
104 generated_line = "%s %s %s %s" \
105 % (index,
106 from_name[:length_from].ljust(length_from, " "),
107 date,
108 subject[:length_subject].ljust(length_subject, " "))
109 if printout:
110 print generated_line
111 else:
112 result.append(generated_line)
113 if not printout:
114 return result
115