-
Notifications
You must be signed in to change notification settings - Fork 3
/
gmailtosieve.py
executable file
·161 lines (136 loc) · 5.46 KB
/
gmailtosieve.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
#!/usr/bin/python
import sys
from xml.dom import minidom
class UnknownEntry(Exception):
pass
class UnhandledCase(Exception):
pass
GMAIL_PROPERTIES = {
#Criteria
'to' : 'c',
'from' : 'c',
'hasTheWord' : 'c',
'doesNotHaveTheWord' : 'c',
'subject' : 'c',
#Actions
'label' : 'a',
'shouldTrash' : 'a',
'shouldAlwaysMarkAsImportant' : 'a',
'shouldNeverSpam' : 'a',
'shouldMarkAsRead' : 'a',
'shouldArchive' : 'a',
#Ignore (What _are_ these?)
'sizeUnit' : 'i',
'sizeOperator' : 'i',
}
def getFilterCriteria(properties):
return {p:v for p,v in properties.iteritems() if p in GMAIL_PROPERTIES and GMAIL_PROPERTIES[p] == 'c'}
def getFilterActions(properties):
return {p:v for p,v in properties.iteritems() if p in GMAIL_PROPERTIES and GMAIL_PROPERTIES[p] == 'a'}
def getFilterUnknown(properties):
return {p:v for p,v in properties.iteritems() if p not in GMAIL_PROPERTIES}
def filterToSieve(properties):
criteria = getFilterCriteria(properties)
actions = getFilterActions(properties)
unknown = getFilterUnknown(properties)
if len(unknown) >= 1:
raise UnknownEntry("Identified the following unknown filter criteria:" + str(unknown))
folder = None
sieve_title = ""
sieve_script = "# rule:[XXX_REPLACEME_XXX]\n"
sieve_script += "if allof ("
#===================================================================================================
sieve_criteria = []
for c in criteria:
this_criteria = ""
#Simple From, To, or Subject Matching
if c in ["from", "to", "subject"]:
subcriteria = criteria[c].split(" OR ")
this_criteria += "header :contains \"" + c + "\" "
if len(subcriteria) > 1: this_criteria += "["
this_criteria += "\"" + "\",\"".join(subcriteria) + "\""
if len(subcriteria) > 1: this_criteria += "]"
#Match a Mailing List
elif c == "hasTheWord" and "list:" in criteria[c]:
list_id = criteria[c].replace("list:", "").strip("('\")")
this_criteria += "header :contains \"list-id\" \"" + list_id + "\""
elif c == "hasTheWord":
raise UnhandledCase("hasTheWord without a list: identifier")
#Match a missing word
elif c == "doesNotHaveTheWord":
subcriteria = criteria[c].split(" OR ")
this_criteria += "not body :text :contains "
if len(subcriteria) > 1: this_criteria += "["
this_criteria += "\"" + "\",\"".join(subcriteria) + "\""
if len(subcriteria) > 1: this_criteria += "]"
sieve_criteria.append(this_criteria)
sieve_script += ", ".join(sieve_criteria)
sieve_script += ")\n{\n"
#===================================================================================================
didAction = False
for a in actions:
if a == 'label':
didAction = True
folder = actions[a].replace(".", "-").replace("/", ".")
sieve_title = actions[a]
sieve_script += "\tfileinto \"" + folder + "\";\n"
elif a == 'shouldTrash':
didAction = True
sieve_script += "\tdiscard;\n"
elif a == 'shouldMarkAsRead':
didAction = True
sieve_script += "\taddflag \"\\\\Seen\";\n"
elif a == 'shouldAlwaysMarkAsImportant':
didAction = True
sieve_script += "\taddflag \"\\\\Flagged\";\n"
elif a == 'shouldArchive':
pass
elif a == 'shouldNeverSpam':
pass
if not didAction:
return "",""
sieve_script += "}\n"
if sieve_title:
sieve_script = sieve_script.replace("XXX_REPLACEME_XXX", sieve_title)
else:
sieve_script = sieve_script.replace("# rule:[XXX_REPLACEME_XXX]\n", "")
return sieve_script.encode('utf-8'), folder
#===================================================================================================
if __name__ == "__main__":
if len(sys.argv) < 4:
print "Usage: " + sys.argv[0] + " input:filters.xml output:filters.sieve output:folderscript.sh"
sys.exit()
inputfile = sys.argv[1]
xmldoc = minidom.parse(inputfile)
filters = []
for p in xmldoc.getElementsByTagName("entry"):
properties = {}
for n in p.childNodes:
if n.nodeName == "apps:property":
properties[n.getAttribute('name')] = n.getAttribute('value')
filters.append(properties)
sieveout = open(sys.argv[2], "w")
bashout = open(sys.argv[3], "w")
sieveout.write('require ["body","fileinto","imap4flags"];\n')
folders = set()
unhandled = 0
unhandled_criteria = set()
for f in filters:
try:
script, folder = filterToSieve(f)
sieveout.write(script)
if folder:
folders.add(folder)
except UnknownEntry :
unhandled += 1
unhandled_criteria.update(getFilterUnknown(f))
if len(folders) > 0:
bashout.write("#!/bin/bash\n")
for i in folders:
bashout.write('mkdir -p ".' + i + '/new"\n')
bashout.write('mkdir -p ".' + i + '/tmp"\n')
bashout.write('mkdir -p ".' + i + '/cur"\n')
bashout.write('touch ".' + i + '/maildirfolder"\n')
bashout.write('chown -R vmail:vmail .*')
if unhandled > 0:
print unhandled, "filters were unable to be processed. The following unknown attributes were seen:", list(unhandled_criteria)