This repository has been archived by the owner on Dec 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maillist-script
executable file
·445 lines (365 loc) · 13.8 KB
/
maillist-script
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
#!/usr/bin/env python
# TODO: CLEAN UP
#
# 2 types of files
# * ruleset file - has body just like virtual file ( start with @), comments are
# not copied over
# * entry file - one username per line
# * addon file - specify directory and it goes through the directory looking for
# all entries that end in the name of the file, and list them as expansions of
# the addon
# * directory - includes the non-ruleset non-addon files inside the directory as the expansions
# * aliases_ruleset - @@
# expansion is dest and people it goes to
# TODO: MENTION ALL OPTIONS ARE MUTUALLY EXCLUSIVE
# TODO: SEARCH IN ALIASES_OUTPUT file as well
import os
import sys
from optparse import OptionParser
MAILLISTS_DIR = "/home/hkn/compserv/new-maillists"
VIRTUAL_OUTPUT = "/home/hkn/compserv/virtual.sample"
ALIASES_OUTPUT = "/home/hkn/compserv/aliases.sample"
ACTUAL_VIRTUAL = '/etc/postfix/virtual'
ACTUAL_ALIASES = '/etc/aliases'
ENTRIES_PATH = set([])
def error_exit(str):
print "[ERROR] %s" % str
sys.exit(1)
def clean_lines(lines):
"""
lines - List of strings; each string is a line.
Removes commented lines starting with # or has just whitespace
"""
new_lines = []
for line in lines:
cleaned_line = line.strip()
if len(cleaned_line) > 0 and cleaned_line[0] != "#":
new_lines.append(cleaned_line)
return new_lines
def list_files(dirpath):
"""
Reads the list of files stored in the dirpath and returns a tuple:
(entries, rulesets, addons, directories).
"""
global ENTRIES_PATH
files = os.listdir(dirpath)
entries = []
rulesets = []
aliases_rulesets = []
addons = []
directories = []
for file in files:
if file[0:2] == "@@":
aliases_rulesets.append(file)
elif file[0] == "@":
rulesets.append(file)
elif file[0] == "\\":
addons.append(file)
elif os.path.isdir(os.path.join(dirpath, file)):
directories.append(file)
else:
entries.append(file)
ENTRIES_PATH.add(os.path.join(dirpath, file))
return (entries, rulesets, aliases_rulesets, addons, directories)
def read_entry(entry_path):
"""
entry - full path to the entry file
return - dictionary
"""
def update_aliases(target, expansion, aliases):
if target in aliases.keys():
aliases[target].append(expansion)
else:
aliases[target] = [expansion]
entry = os.path.basename(entry_path)
f = open(entry_path)
addresses = []
aliases = {}
lines = clean_lines(f.readlines())
for line in lines:
if line[0] != "@":
addresses.append(line)
else:
aliases_target = entry + "-aliases"
expansion = line[1:].strip()
update_aliases(aliases_target, expansion, aliases)
addresses.append(aliases_target)
# If entry file is empty, print error and exit
if not addresses:
error_exit("Following entry had no expansions: %s" % entry)
return ({entry: addresses}, aliases)
def read_ruleset(ruleset_path):
"""
ruleset - full path to the ruleset entry
return - dictionary
"""
ruleset = ruleset_path
f = open(ruleset_path)
virtual = {}
lines = clean_lines(f.readlines())
for line in lines:
target = line.split()[0]
# TODO REDO BASED ON "," NOT SPACE
expansions = "".join(line.split()[1:])
expansions_list = [expansion for expansion in
expansions.split(",") if len(expansion) > 0]
virtual[target] = expansions_list
return (virtual, {})
def read_aliases_ruleset(aliases_ruleset_path):
"""
ruleset
"""
### TODO
aliases_ruleset = aliases_ruleset_path
f = open(aliases_ruleset_path)
aliases = {}
lines = clean_lines(f.readlines())
for line in lines:
target = line.split(":")[0].strip()
expansions = ":".join(line.split(":")[1:])
expansions_list = [expansion.strip() for expansion in
expansions.split(",") if len(expansion.strip()) > 0]
aliases[target] = expansions_list
# TODO
return ({}, aliases)
def read_directory(directory_path):
"""
directory - full path to the directory
return - dictionary
"""
directory = os.path.basename(directory_path)
entries, rulesets, aliases_rulesets, addons, directories = list_files(directory_path)
if not entries and not directories:
error_exit("Following directory had no expansions: %s" % directory)
return ({directory: entries + directories}, {})
def read_addon(addon_path, dir_virtual):
"""
addon - full path to the addon
"""
addon = os.path.basename(addon_path)[1:]
addresses = []
f = open(addon_path)
lines = clean_lines(f.readlines())
for line in lines:
if line[0] == "@":
to_search = line[1:].strip()
possible_expansions = dir_virtual[to_search].keys()
for possible_expansion in possible_expansions:
if possible_expansion.find(addon) != -1:
addresses.append(possible_expansion)
else:
addresses.append(line)
if not addresses:
error_exit("Following addon had no expansions: %s" % addon)
return ({addon: addresses}, {})
def fill_table(dirpath):
"""
Opens the given dirpath and returns an virtual table containing all the
virtual form the entries, rulesets, addons, and directories in that
dirpath. The given dirpath has to be the full path to the directory.
"""
#global MAILLISTS_DIR
entries, rulesets, aliases_rulesets, addons, directories = list_files(dirpath)
virtual = {}
dir_virtual = {}
aliases = {}
# vrules = rules for the virtual file
# arules = rules for the aliases file
for entry in entries:
vrules, arules = read_entry(os.path.join(dirpath, entry))
virtual.update(vrules)
aliases.update(arules)
for ruleset in rulesets:
vrules, arules = read_ruleset(os.path.join(dirpath, ruleset))
virtual.update(vrules)
aliases.update(arules)
for aliases_ruleset in aliases_rulesets:
vrules, arules = read_aliases_ruleset(os.path.join(dirpath,
aliases_ruleset))
virtual.update(vrules)
aliases.update(arules)
for directory in directories:
vrules, arules = read_directory(os.path.join(dirpath, directory))
dir_vrules, dir_arules = fill_table(os.path.join(dirpath, directory))
dir_virtual[directory] = dir_vrules
virtual.update(vrules)
aliases.update(arules)
virtual.update(dir_virtual[directory])
aliases.update(dir_arules)
for addon in addons:
vrules, arules = read_addon(os.path.join(dirpath, addon), dir_virtual)
virtual.update(vrules)
aliases.update(arules)
return virtual, aliases
def parse_options():
parser = OptionParser()
parser.add_option("-l", action="store_true", dest="list", default=False,
help="list all mailing list targets")
parser.add_option("-r", action="store_true", dest="recursive",
default=False,
help="make expansion or reverse expansion recursive")
parser.add_option("-e", dest="target", metavar="target",
help="expand target")
parser.add_option("-b", dest="expansion", metavar="expansion",
help="reverse expand expansion; find targets expansion belongs to")
parser.add_option("-a", action="store_true", dest="aliases", default=False,
help="do given action for the aliases file")
parser.add_option("-i", dest="to_insert", metavar="email entry", nargs=2,
help="inserts the email to the given target. Works only with " +
"virtual file.")
parser.add_option("-d", dest="to_delete", metavar="email entry", nargs=2,
help="deletes the email from the given target. This doesn't " +
"actually delete the entry but only comments it out. Works only " +
"with virtual file.")
parser.add_option("-z", action="store_true", dest="real_sync",
default=False, help="syncs directly to the actual file instead" +
"of syncing to the test aliases and virtual file.")
options, args = parser.parse_args()
return (options, args)
def list_targets(table):
# table = aliases or virtual table
to_print = table.keys()
to_print.sort()
print "\n".join(to_print)
def expand(to_lookup, recursive, table):
# table = aliases or virtual table list
table_keys = table.keys()
def shallow_expand(target):
if target in table_keys:
return table[target]
else:
return []
def recursive_expand(target):
expansions = shallow_expand(target)
unflattened = [recursive_expand(expansion) for expansion in expansions]
flattened = reduce(list.__add__, unflattened) if unflattened else []
return flattened + expansions
if to_lookup in table_keys:
if recursive:
to_print = recursive_expand(to_lookup)
to_print = list(set(to_print))
else:
to_print = shallow_expand(to_lookup)
to_print.sort()
print "\n".join(to_print)
else:
error_exit("Could not find target: %s" % to_lookup)
def reverse_expand(to_lookup, recursive, table):
# table = aliases or virtual table list
all_expansions = reduce(list.__add__, table.values()) if table.values() else []
table_items = table.items()
def shallow_reverse_expand(expansion):
if expansion in all_expansions:
targets = [target for target, expansions in table_items
if expansion in expansions]
return targets
else:
return []
def recursive_reverse_expand(expansion):
targets = shallow_reverse_expand(expansion)
unflattened = [recursive_reverse_expand(target) for target in targets]
flattened = reduce(list.__add__, unflattened) if unflattened else []
return flattened + targets
if to_lookup in all_expansions:
if recursive:
to_print = recursive_reverse_expand(to_lookup)
to_print = list(set(to_print))
else:
to_print = shallow_reverse_expand(to_lookup)
to_print.sort()
print "\n".join(to_print)
else:
error_exit("Could not find expansion: %s" % to_lookup)
def insert_email(email, entry):
global ENTRIES_PATH
email = email.strip()
entry_path = ""
for path in ENTRIES_PATH:
if entry == os.path.basename(path):
entry_path = path
if entry_path == "":
error_exit("Could not find entry file: %s" % entry)
f = open(entry_path, 'a+')
lines = f.readlines()
for line in lines:
if email == line.strip():
error_exit("Following email already exists in entry: %s" % email)
last_line = lines[len(lines)-1]
if last_line[len(last_line)-1] != '\n':
# If last character isn't a newline
f.write('\n')
f.write(email + '\n')
f.close()
def delete_email(email, entry):
global ENTRIES_PATH
email = email.strip()
entry_path = ""
for path in ENTRIES_PATH:
if entry == os.path.basename(path):
entry_path = path
if entry_path == "":
error_exit("Could not find entry file: %s" % entry)
f = open(entry_path, 'w+')
lines = f.readlines()
email_index = -1
for line in lines:
if email == line.strip():
email_index = lines.index(line)
if email_index == -1:
error_exit("Following email doesn't exists in entry: %s" % email)
lines[email_index] = '#' + lines[email_index]
f.writelines(lines)
def init():
global MAILLISTS_DIR
virtual, aliases = fill_table(MAILLISTS_DIR)
for target in virtual.keys():
if not virtual[target]:
error_exit("Following target has no expansion: %s" % target)
for target in aliases.keys():
if not aliases[target]:
error_exit("Following target has no expansion: %s" % target)
return virtual, aliases
def main():
virtual, aliases = init()
options, args = parse_options()
if options.list:
table = aliases if options.aliases else virtual
list_targets(table)
elif options.target != None:
table = aliases if options.aliases else virtual
expand(options.target, options.recursive, table)
elif options.expansion != None:
table = aliases if options.aliases else virtual
reverse_expand(options.expansion, options.recursive, table)
elif options.to_insert != None:
email, entry = options.to_insert
insert_email(email, entry)
elif options.to_delete != None:
email, entry = options.to_delete
delete_email(email, entry)
elif options.real_sync:
global ACTUAL_VIRTUAL
actual_virtual = open(ACTUAL_VIRTUAL, 'w')
for target in virtual.keys():
expansions = ", ".join(virtual[target])
actual_virtual.write("%s\t\t\t%s\n" % (target, expansions))
global ACTUAL_ALIASES
actual_aliases = open(ACTUAL_ALIASES, 'w')
for target in aliases.keys():
expansions = ", ".join(aliases[target])
actual_aliases.write("%s:\t\t\t%s\n" % (target, expansions))
os.system("postmap " + ACTUAL_VIRTUAL)
os.system("newaliases")
else:
global VIRTUAL_OUTPUT
virtual_file = open(VIRTUAL_OUTPUT, "w")
global ALIASES_OUTPUT
aliases_file = open(ALIASES_OUTPUT, "w")
for target in virtual.keys():
expansions = ", ".join(virtual[target])
virtual_file.write("%s\t\t\t%s\n" % (target, expansions))
for target in aliases.keys():
expansions = ", ".join(aliases[target])
aliases_file.write("%s:\t\t\t%s\n" % (target, expansions))
if __name__ == "__main__":
main()