-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
656 lines (559 loc) · 20.5 KB
/
index.html
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
<!--[[[cog
from tools.showcode import showcode, runscript, showfile
show_yaml = showcode
import re
]]]-->
<!--[[[end]]]-->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Building a Tool for Organizing IMAP Email</title>
<meta name="author" content="Doug Hellmann">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/dhellmann.css" id="theme">
<link rel="stylesheet" href="css/font-awesome.min.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/github.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>Building a Tool for<br/>Organizing IMAP Email</h2>
<h3>PyATL</h3>
<h4>Doug Hellmann</h4>
</section>
<!--
1. what is it?
1. I wanted to automatically sort the sent messages in my email account
2. needed a way to describe the rules for what to do with each message
1. who I sent it to
2. other header settings
2. running the app
1. parsing command line arguments
2. loading a configuration file
3. getting secret password
4. connecting to the IMAP server
1. started with imaplib, but IMAPClient library proved easier to use because it is a little higher level
5. processing the rules for filing the messages
1. factory pattern for converting rule specifications into Rule objects
2. querying for the messages
3. fetching and parsing the message
4. does the rule apply?
5. build the action & invoke it
1. factory pattern again
6. expunge()
3. data driven
1. YAML file to describe the account and the rules
2. separate rules for each mailbox
3. digression into IMAP mailbox naming
4. creating the rules
1. simple string and pattern matching rules
1. HeaderSubString
2. HeaderRegex
2. build up more complex rule classes using composition and inheritance
1. Or
2. And
3. Headers
5. creating the actions
1. Move
2. Trash
3. Delete
-->
<section>
<h2>What is imapautofiler?</h2>
<ul>
<li>I send too much email.</li>
<li>I don't like gmail.</li>
<li>I need to keep my sent email somewhat organized.</li>
<li>Automation FTW!</li>
</ul>
</section>
<section>
<h2>Design Considerations</h2>
<ul>
<li>Must support multiple email clients</li>
<li>Must be easy to add new rules without writing code</li>
<li>Reduce errors in specifying actions</li>
</ul>
</section>
<section>
<h2>Standalone Program</h2>
<pre class="lineselect_selectable" data-trim data-noescape>usage: imapautofiler [-h] [-c CONFIG_FILE] [--list-mailboxes]
optional arguments:
-h, --help show this help message and exit
-c CONFIG_FILE, --config-file CONFIG_FILE
--list-mailboxes instead of processing rules,
print a list of mailboxes
</pre>
</section>
<section>
<h2>Processing Overview</h2>
<ol>
<li>Read the configuration file.</li>
<li>Connect to the IMAP server.</li>
<li>For each mailbox in the config file,</li>
<li>For each message,</li>
<li>For each rule,</li>
<li>If the rule matches the message,</li>
<li>Run the associated action.</li>
</ol>
</section>
<section>
<h2>Configuring IMAP Server Connection</h2>
<!--[[[cog
show_yaml('imapautofiler/config.yml', lines=(2, 4))
]]]-->
<pre><code class="lineselect_selectable yml" data-trim data-noescape>server:
hostname: "mail.example.com"
username: "[email protected]"</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Connecting with IMAPClient</h2>
<!--[[[cog
showcode('imapautofiler/app.py', lines=(94, 105))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape> cfg = config.get_config(args.config_file)
conn = imapclient.IMAPClient(
cfg['server']['hostname'],
use_uid=True,
ssl=True,
)
username = cfg['server']['username']
password = cfg['server'].get('password')
if not password:
password = getpass.getpass(
'Password for {}:'.format(username))
conn.login(username, password)</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Rule Class</h2>
<p>How do the rules work?</p>
<!--[[[cog
showcode('imapautofiler/rules.py', lines=(33, 44))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class Rule(metaclass=abc.ABCMeta):
def __init__(self, rule_data, cfg):
self._data = rule_data
self._cfg = cfg
@abc.abstractmethod
def check(self):
raise NotImplementedError()
def get_action(self):
return self._data.get('action', {})</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Header Rule</h2>
<!--[[[cog
show_yaml('imapautofiler/config.yml', lines=(54, 59))
]]]-->
<pre><code class="lineselect_selectable yml" data-trim data-noescape> - headers:
- name: "subject"
substring: "[pyatl]"
action:
name: "move"
dest-mailbox: "INBOX.PyATL"</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>HeaderSubString Class</h2>
<!--[[[cog
showcode('imapautofiler/rules.py', lines=(100, 109))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class HeaderSubString(Rule):
def __init__(self, rule_data, cfg):
super().__init__(rule_data, cfg)
self._header_name = rule_data['name']
self._substring = rule_data['substring'].lower()
def check(self, message):
header_value = message.get(self._header_name, '').lower()
return (self._substring in header_value)</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Combination Rule: OR</h2>
<!--[[[cog
show_yaml('imapautofiler/config.yml', lines=(61, 71))
]]]-->
<pre><code class="lineselect_selectable yml" data-trim data-noescape> - or:
rules:
- headers:
- name: "to"
substring: "[email protected]"
- headers:
- name: "cc"
substring: "[email protected]"
action:
name: "move"
dest-mailbox: "INBOX.PyATL"</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Or Class</h2>
<!--[[[cog
showcode('imapautofiler/rules.py', lines=(47, 61))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class Or(Rule):
"True if any one of the sub-rules is true."
def __init__(self, rule_data, cfg):
super().__init__(rule_data, cfg)
self._sub_rules = [
factory(r, cfg)
for r in rule_data['or'].get('rules', [])
]
def check(self, message):
return any(
r.check(message)
for r in self._sub_rules
)</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Recipient Rule</h2>
<!--[[[cog
show_yaml('imapautofiler/config.yml', lines=(73, 77))
]]]-->
<pre><code class="lineselect_selectable yml" data-trim data-noescape> - recipient:
substring: "[email protected]"
action:
name: "move"
dest-mailbox: "INBOX.PyATL"</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Recipient Class</h2>
<!--[[[cog
showcode('imapautofiler/rules.py', lines=(64, 77))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class Recipient(Or):
"True if any recipient sub-rule matches."
def __init__(self, rule_data, cfg):
rules = []
for header in ['to', 'cc']:
header_data = {}
header_data.update(rule_data['recipient'])
header_data['name'] = header
rules.append({'headers': [header_data]})
rule_data['or'] = {
'rules': rules,
}
super().__init__(rule_data, cfg)</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Instantiating Rules</h2>
<!--[[[cog
showcode('imapautofiler/rules.py', lines=(17, 30))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>def factory(rule_data, cfg):
"""Create a rule processor.
Using the rule type, instantiate a rule processor that can check
the rule against a message.
"""
if 'or' in rule_data:
return Or(rule_data, cfg)
if 'headers' in rule_data:
return Headers(rule_data, cfg)
if 'recipient' in rule_data:
return Recipient(rule_data, cfg)
raise ValueError('Unknown rule type {!r}'.format(rule_data))</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Processing Rules</h2>
<!--[[[cog
showcode('imapautofiler/app.py', lines=(51, 62))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>def process_rules(cfg, debug, conn):
num_messages = 0
num_processed = 0
for mailbox in cfg['mailboxes']: # multiple mailboxes allowed
mailbox_name = mailbox['name']
conn.select_folder(mailbox_name)
mailbox_rules = [ # convert data to instances
rules.factory(r, cfg)
for r in mailbox['rules']
]</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Processing Rules</h2>
<!--[[[cog
showcode('imapautofiler/app.py', lines=(64, 78))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape> msg_ids = conn.search(['ALL'])
for msg_id in msg_ids:
num_messages += 1
message = get_message(conn, msg_id)
for rule in mailbox_rules:
if rule.check(message):
action = actions.factory(rule.get_action(), cfg)
action.invoke(conn, msg_id, message)
num_processed += 1
break
# Remove messages that we just moved.
conn.expunge()</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Rule Actions</h2>
<!--[[[cog
showcode('imapautofiler/app.py', lines=(64, 78), mark=(9, 10))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape> msg_ids = conn.search(['ALL'])
for msg_id in msg_ids:
num_messages += 1
message = get_message(conn, msg_id)
for rule in mailbox_rules:
if rule.check(message):
<mark> action = actions.factory(rule.get_action(), cfg)
</mark><mark> action.invoke(conn, msg_id, message)
</mark> num_processed += 1
break
# Remove messages that we just moved.
conn.expunge()</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Action Class</h2>
<!--[[[cog
showcode('imapautofiler/actions.py', lines=(30, 38))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class Action(metaclass=abc.ABCMeta):
def __init__(self, action_data, cfg):
self._data = action_data
self._cfg = cfg
@abc.abstractmethod
def invoke(self, conn, message_id, message):
raise NotImplementedError()</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Move Action</h2>
<!--[[[cog
show_yaml('imapautofiler/config.yml', lines=(57, 59))
]]]-->
<pre><code class="lineselect_selectable yml" data-trim data-noescape> action:
name: "move"
dest-mailbox: "INBOX.PyATL"</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Move Class</h2>
<!--[[[cog
showcode('imapautofiler/actions.py', lines=(41, 49))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class Move(Action):
def __init__(self, action_data, cfg):
super().__init__(action_data, cfg)
self._dest_mailbox = self._data.get('dest-mailbox')
def invoke(self, conn, message_id, message):
conn.copy([message_id], self._dest_mailbox)
conn.add_flags([message_id], [imapclient.DELETED])</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Trash Action</h2>
<!--[[[cog
show_yaml('imapautofiler/config.yml', lines=(150, 154))
]]]-->
<pre><code class="lineselect_selectable yml" data-trim data-noescape> - headers:
- name: to
substring: [email protected]
action:
name: "trash"</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Trash Class</h2>
<!--[[[cog
showcode('imapautofiler/actions.py', lines=(52, 59))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>class Trash(Move):
def __init__(self, action_data, cfg):
super().__init__(action_data, cfg)
if self._dest_mailbox is None:
self._dest_mailbox = cfg.get('trash-mailbox')
if self._dest_mailbox is None:
raise ValueError('no "trash-mailbox" set in config')</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Action Factory</h2>
<!--[[[cog
showcode('imapautofiler/actions.py', lines=(18, 27))
]]]-->
<pre><code class="lineselect_selectable py" data-trim data-noescape>def factory(action_data, cfg):
"Create an Action instance."
name = action_data.get('name')
if name == 'move':
return Move(action_data, cfg)
if name == 'delete':
return Delete(action_data, cfg)
if name == 'trash':
return Trash(action_data, cfg)
raise ValueError('unrecognized rule action {!r}'.format(action_data))</code></pre>
<!--[[[end]]]-->
</section>
<section>
<h2>Sample Run</h2>
<pre class="lineselect_selectable">$ imapautofiler
Move: 13704 (Re: livarot france - Google Search) to INBOX.Theresa
Trash: 13706 (Help Anne with theme work) to INBOX.Trash
Trash: 13707 (Fwd: Change in openstack/oslo.db[master]: Warn on URL without a drivername) to INBOX.Trash
Move: 13709 ([oslo][all][logging] logging debugging improvement work status) to INBOX.OpenStack.Dev List
Move: 13712 (Re: ) to INBOX.Personal
Trash: 13713 (Fwd: [Python-Dev] 2017 Python Language Summit coverage) to INBOX.Trash
Move: 13732 (Re: [Openstack-operators] [openstack-dev] [upgrades][skip-level][leapfrog] - RFC - Skipping releases when upgrading) to INBOX.OpenStack.Misc Lists
</pre>
</section>
<section>
<h2>Future Work</h2>
<ul>
<li>Date-based rules</li>
<li>Body rules (attachments, text content, etc.)</li>
<li>Multiple accounts per config file</li>
<li>Copy action</li>
<li>Transferring messages between accounts</li>
<li>Improved documentation</li>
</ul>
</section>
<section>
<h2>Resources</h2>
<p>
<a href="https://github.com/imapautofiler/imapautofiler">
<i class="fa fa-github"> </i>imapautofiler/imapautofiler
</a><br/>
<a href="http://imapautofiler.readthedocs.io/">
http://imapautofiler.readthedocs.io/
</a>
</p>
<p>
<a href="https://twitter.com/doughellmann">
<i class="fa fa-twitter"> </i>@doughellmann
</a>
</p>
<p><small>
<a href="https://github.com/imapautofiler/presentation-organize-email-imapautofiler">
<i class="fa fa-github"></i> imapautofiler/presentation-organize-email-imapautofiler
</a><br/>
<a href="https://doughellmann.com/presentations/organize-email-imapautofiler">
https://doughellmann.com/presentations/organize-email-imapautofiler
</a>
</small></p>
<p class="creativecommons">
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
<img alt="Creative Commons License" src="img/cc-by-80x15.png" />
</a>
<span> This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creativle Commons Attribution 4.0 International License</a>.</span>
</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/jquery-3.1.1.js"></script>
<script src="js/reveal.js"></script>
<script src="js/lineselect.js"></script>
<script>
function _prev_slide() {
if (!_reveal_navigation_active()) {
return;
}
console.log('reveal: prev');
Reveal.navigatePrev();
};
function _next_slide() {
if (!_reveal_navigation_active()) {
return;
}
console.log('reveal: next');
Reveal.navigateNext();
};
function _reveal_navigation_active () {
return !document.querySelector( '.reveal' ).classList.contains('reveal-nav-off');
};
function _toggle_reveal_navigation () {
if (_reveal_navigation_active()) {
console.log('reveal: turning off nav');
document.querySelector( '.reveal' ).classList.add('reveal-nav-off');
document.querySelector('aside.controls').classList.add('controls-off');
} else {
console.log('reveal: turning on nav');
document.querySelector( '.reveal' ).classList.remove('reveal-nav-off');
document.querySelector('aside.controls').classList.remove('controls-off');
};
};
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
keyboard: {
// Use Logitech presenter remote black screen button
// to control when we are changing slides vs. selected lines.
190: _toggle_reveal_navigation,
// p, page up
80: _prev_slide,
33: _prev_slide,
// n, page down
78: _next_slide,
34: _next_slide,
// h, left
72: _prev_slide,
37: _prev_slide,
// l, right
76: _next_slide,
39: _next_slide,
},
progress: true,
slideNumber: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
<script>
/* find all of the lineselect nodes and add spans for their lines */
$(function() {
$('.lineselect_selectable').each(function (i, e) {
var $e = $(e);
var h = $e.html();
h = h.replace(/\n\n/g, "\n \n");
h = h.replace(/\n/g, "</span>\n<span class='line'>");
h = "<span class='line'>" + h + "</span>";
$e.html(h);
});
/* enable lineselect for the line nodes */
$(".lineselect_selectable").each(function () {
$(this).lineselect({ lines: "span.line" });
});
});
</script>
</body>
</html>