-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-participants
executable file
·75 lines (61 loc) · 2.73 KB
/
scan-participants
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
#!/usr/bin/env python2.7
import os
import sys
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.message import MIMEMessage
import argparse
import gemlv.utils
import sqlite3 # sqlite3 >= 3.24.0
def warn(msg):
sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
def err(errno, msg):
warn(msg)
sys.exit(errno)
argparser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
argparser.add_argument('--our', metavar='HEADER_NAME', action='append', required=True, help="in which headers are our email addresses")
argparser.add_argument('--their', metavar='HEADER_NAME', action='append', required=True, help="in which headers are others email addresses")
argparser.add_argument('--print-only', action='store_true', help="only print the list of participants; no update database")
argparser.add_argument('--db-file', metavar='FILE', default=os.path.expanduser('~/Mail/.participants.db'), help="file to store data")
cliArgs = argparser.parse_args()
Email = email.message_from_file(sys.stdin)
msgid = Email.get('Message-ID')
if msgid is None:
err(1, "No Message-ID.")
key = msgid
addresses = {}
for kind in 'our', 'their':
addresses[kind] = []
headers = []
for hdr in getattr(cliArgs, kind):
headers.extend(Email.get_all(hdr, []))
addresses[kind] = [al.email for al in gemlv.utils.getaddresslines(headers)]
if len(addresses['our']) == 0:
err(3, "No \"our\" address found.")
if len(addresses['their']) == 0:
err(4, "No \"their\" address found.")
if cliArgs.print_only:
print "%s\t%s\t%s\n" % (key, ','.join(addresses['our']), ','.join(addresses['their'])),
sys.exit(0)
with sqlite3.connect(cliArgs.db_file) as db:
db.execute("CREATE TABLE IF NOT EXISTS processed_message (mid TEXT PRIMARY KEY ON CONFLICT IGNORE)")
db.execute("CREATE TABLE IF NOT EXISTS address (aid INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT UNIQUE)")
db.execute("CREATE TABLE IF NOT EXISTS contact (aid_our INTEGER, aid_their INTEGER, times INTEGER, PRIMARY KEY (aid_our, aid_their))")
if db.execute("SELECT 1 FROM processed_message WHERE mid = ?", [key]).fetchone():
warn("Record exists.")
else:
for address in addresses['our'] + addresses['their']:
db.execute("INSERT INTO address (address) VALUES (?) ON CONFLICT DO NOTHING", [address])
for addr_our in addresses['our']:
for addr_their in addresses['their']:
db.execute("""INSERT INTO contact (aid_our, aid_their, times)
VALUES (
(SELECT aid FROM address WHERE address = ?),
(SELECT aid FROM address WHERE address = ?),
1)
ON CONFLICT DO UPDATE SET times = times + 1
""",
[addr_our, addr_their])
db.execute("INSERT INTO processed_message (mid) VALUES (?)", [key])