Mercurial > hg > emaildispatcher
comparison emaildispatcher/main.py @ 2:5559e0734c8c
discriminate on From address; give a real version
author | k0s <k0scist@gmail.com> |
---|---|
date | Mon, 18 Jan 2010 15:37:59 -0500 |
parents | 2c76525ce80b |
children |
comparison
equal
deleted
inserted
replaced
1:2c76525ce80b | 2:5559e0734c8c |
---|---|
22 parser.add_option('--list-handlers', dest='list_handlers', | 22 parser.add_option('--list-handlers', dest='list_handlers', |
23 action='store_true', default=False, | 23 action='store_true', default=False, |
24 help="list available handlers") | 24 help="list available handlers") |
25 options, args = parser.parse_args(args) | 25 options, args = parser.parse_args(args) |
26 | 26 |
27 # get the handlers | |
27 handlers = {} | 28 handlers = {} |
28 for handler in iter_entry_points('email.dispatchers'): | 29 for handler in iter_entry_points('email.dispatchers'): |
29 try: | 30 try: |
30 handlers[handler.name] = handler.load() | 31 handlers[handler.name] = handler.load() |
31 except: | 32 except: |
32 pass | 33 pass |
33 | |
34 if options.list_handlers: | 34 if options.list_handlers: |
35 for handler in sorted(handlers.keys()): | 35 for handler in sorted(handlers.keys()): |
36 print handler | 36 print handler |
37 sys.exit(0) | 37 sys.exit(0) |
38 | |
39 assert options.handler | 38 assert options.handler |
40 | 39 |
41 kwargs = dict([arg.split('=', 1) for arg in args if '=' in arg]) | 40 # get the message |
42 args = [arg for arg in args if '=' not in arg] | |
43 | |
44 handler = handlers[options.handler](*args, **kwargs) | |
45 | |
46 if options.file: | 41 if options.file: |
47 input = file(options.file).read() | 42 input = file(options.file).read() |
48 else: | 43 else: |
49 input = sys.stdin.read() | 44 input = sys.stdin.read() |
45 message = message_from_string(input) | |
50 | 46 |
51 message = message_from_string(input) | 47 # test to ensure correct From address |
48 if options.From: | |
49 if message['From'] not in options.From: | |
50 sys.exit(0) | |
51 | |
52 ### handle the message | |
53 # get arguments to handler | |
54 kwargs = dict([arg.split('=', 1) for arg in args if '=' in arg]) | |
55 args = [arg for arg in args if '=' not in arg] | |
56 | |
57 # dispatch the message | |
58 handler = handlers[options.handler](*args, **kwargs) | |
52 handler(message) | 59 handler(message) |
53 | 60 |
54 | 61 |
55 if __name__ == '__main__': | 62 if __name__ == '__main__': |
56 main() | 63 main() |