This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
executable file
·329 lines (265 loc) · 10.3 KB
/
main.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
#!/usr/bin/python
# Modified by Joao Paulo Barraca <[email protected]>
import codecs
import getopt
import getpass
import logging
import messageinfo
import re
import sys
from Cheetah.Template import Template
import jwzthreading
import mail
import stats.base
import stats.bucket
import stats.group
import stats.table
def GetOptsMap():
opts, args = getopt.getopt(sys.argv[1:], "", [
# Standard options
"username=", "password=", "use_ssl", "server=", "maildir=", "mailboxpackage=",
# Other params
"filter_out=", "me=", "server_mailbox=",
# Development options
"record", "replay",
"max_messages=", "random_subset",
"skip_labels"])
if len(opts) == 0:
print "Usage: main.py --username=<login> --password=<password> --server=<server_address> [options]"
print "Main Parameters"
print "\t--username=<login>\t\tThe login to use when connecting to the server"
print "\t--password=<password>\t\tThe password to use when connecting to the server"
print "\t--server=<server_address>\tThe IP address or DNS name of the server"
print "\t--maildir=path\t\t\tRead emails from maildir folders"
print "\t--mailboxpackage=path\t\tRead emails from mailbox packages (Mail.app)"
print "\nOptions"
print "\t--filter_out=<filter>\t\tRegular expression to filter results"
print "\t--me=<address>\t\t\tYour email address"
print "\t--use_ssl\t\t\tConnect to server using SSL"
print "\t--server_mailbox=<inbox,mb1>\tOnly consider the given mailboxes (or label)"
print "\n"
sys.exit()
opts_map = {}
for name, value in opts:
opts_map[name[2:]] = value
if "maildir" in opts_map:
return opts_map
if "mailboxpackage" in opts_map:
return opts_map
assert "username" in opts_map
if "password" not in opts_map:
opts_map["password"] = getpass.getpass(
prompt="Password for %s: " % opts_map["username"])
print opts_map
assert "password" in opts_map
assert "server" in opts_map
return opts_map
def GetMessageInfos(opts):
if "maildir" in opts:
m = mail.MaildirInfo(opts["maildir"])
else:
if "mailboxpackage" in opts:
m = mail.MailBoxPackageInfo(opts["mailboxpackage"])
else:
m = mail.Mail(
opts["server"], "use_ssl" in opts, opts[
"username"], opts["password"],
"record" in opts, "replay" in opts,
"max_messages" in opts and int(opts["max_messages"]) or -1,
"random_subset" in opts,)
message_infos = []
server_mailbox = []
if "server_mailbox" in opts:
server_mailbox = opts['server_mailbox'].split(",")
# Then for each mailbox, see which messages are in it, and attach that to
# the mail info
# if "skip_labels" not in opts:
# Don't want to parse all these dates, since we already have them from the
# message infos above.
messageinfo.MessageInfo.SetParseDate(False)
for mailbox in m.GetMailboxes():
if len(server_mailbox) > 0:
if mailbox not in server_mailbox:
continue
m.SelectMailbox(mailbox)
mb_message_infos = m.GetMessageInfos()
for message_info in mb_message_infos:
message_info.AddMailbox(mailbox)
message_infos.extend(mb_message_infos)
logging.info("Mailbox had %d messages. Total=%d", len(
mb_message_infos), len(message_infos))
message_infos_by_id = dict(
[(mi.GetMessageId(), mi) for mi in message_infos])
messageinfo.MessageInfo.SetParseDate(True)
m.Logout()
# Filter out those that we're not interested in
if "filter_out" in opts:
message_infos = FilterMessageInfos(message_infos, opts["filter_out"])
# Tag messages as being from the user running the script
if "me" in opts:
logging.info("Identifying \"me\" messages")
me_addresses = [address.lower().strip()
for address in opts["me"].split(",")]
me_from_count = 0
me_to_count = 0
for message_info in message_infos:
name, address = message_info.GetSender()
for me_address in me_addresses:
if me_address == address:
message_info.is_from_me = True
me_from_count += 1
break
for name, address in message_info.GetRecipients():
for me_address in me_addresses:
if me_address == address:
message_info.is_to_me = True
me_to_count += 1
break
if message_info.is_to_me:
break
logging.info(" %d messages are from \"me\"" % me_from_count)
logging.info(" %d messages are to \"me\"" % me_to_count)
return message_infos
def FilterMessageInfos(message_infos, filter_param):
logging.info("Filtering messages")
remaining_message_infos = []
filters = []
raw_filters = filter_param.split(",")
for raw_filter in raw_filters:
operator, value = raw_filter.strip().split(":", 1)
filters.append([operator, value.lower()])
for message_info in message_infos:
filtered_out = False
for operator, operator_value in filters:
if operator == "to":
pairs = message_info.GetRecipients()
elif operator == "from":
pairs = [message_info.GetSender()]
elif operator == "list":
pairs = [message_info.GetListId()]
else:
raise AssertionError("unknown operator: %s" % operator)
values = [name and name.lower() or "" for name, address in pairs] + \
[address and address.lower() or "" for name, address in pairs]
for value in values:
if value.find(operator_value) != -1:
filtered_out = True
break
if filtered_out:
break
if not filtered_out:
remaining_message_infos.append(message_info)
logging.info(" %d messages remaining" % len(remaining_message_infos))
return remaining_message_infos
def ExtractThreads(message_infos):
thread_messages = []
for message_info in message_infos:
try:
thread_message = jwzthreading.make_message(message_info.headers)
except ValueError:
continue
if thread_message:
thread_message.message_info = message_info
thread_messages.append(thread_message)
thread_dict = jwzthreading.thread(thread_messages)
containers = []
for subject, container in thread_dict.items():
# jwzthreading is too aggressive in threading by subject and will combine
# distinct threads that happen to have the same subject. Split them up if
# we have a dummy container that has lots of children at the first
# level.
if container.is_dummy() and len(container.children) >= 10:
for child_container in container.children:
child_container.subject = subject
containers.append(child_container)
else:
container.subject = subject
containers.append(container)
return containers
def InitStats(date_range):
s = [
stats.base.TitleStat(date_range),
stats.group.StatTabGroup(
(
"Time",
stats.group.StatColumnGroup(
stats.bucket.DayOfWeekStat(),
stats.bucket.TimeOfDayStat(),
stats.bucket.YearStat(date_range),
),
stats.group.StatColumnGroup(
stats.group.MonthStatCollection(date_range),
stats.group.DayStatCollection(date_range),
),
),
(
"Size",
stats.group.StatColumnGroup(
stats.bucket.SizeBucketStat(),
stats.table.SizeTableStat(),
),
),
(
"People and Lists",
stats.group.StatColumnGroup(
stats.table.SenderTableStat(),
stats.group.SenderDistributionStatCollection(date_range),
),
stats.group.StatColumnGroup(
stats.table.RecipientTableStat(),
stats.group.RecipientDistributionStatCollection(
date_range),
),
stats.group.StatColumnGroup(
stats.table.ListIdTableStat(),
stats.group.ListDistributionStatCollection(date_range),
),
),
(
"Me",
stats.group.StatColumnGroup(
stats.table.MeRecipientTableStat(),
stats.group.MeRecipientDistributionStatCollection(
date_range),
),
stats.group.StatColumnGroup(
stats.table.MeSenderTableStat(),
stats.group.MeSenderDistributionStatCollection(date_range),
),
),
(
"Threads",
stats.group.StatColumnGroup(
stats.bucket.ThreadSizeBucketStat(),
stats.table.ThreadSizeTableStat(),
),
stats.group.StatColumnGroup(
stats.table.ThreadStarterTableStat(),
stats.table.ThreadListTableStat(),
)
)
)
]
return s
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(message)s")
logging.info("Initializing")
opts = GetOptsMap()
message_infos = GetMessageInfos(opts)
logging.info("Extracting threads")
threads = ExtractThreads(message_infos)
stats = InitStats(messageinfo.MessageInfo.GetDateRange())
logging.info("Generating stats")
for stat in stats:
stat.ProcessMessageInfos(message_infos, threads)
logging.info("Outputting HTML")
t = Template(
file="templates/index.tmpl",
searchList={
"stats": stats,
"host": re.sub("^.*@", "", opts.get("username", ''))
}
)
out = codecs.open("out/index.html", mode="w", encoding='utf-8')
out.write(unicode(t))
out.close()
logging.info("Done")