diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2014-12-31 12:12:02 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2014-12-31 12:12:02 +0100 |
commit | 527e535279dcceeb6ffcf091bc9e8fc23b46f287 (patch) | |
tree | 5a2c6bbeed90ee47e4a13dcfa854df6513a775f2 /pygn2m | |
parent | cdd9f1d6a872fbc20e368fc23ddaf98eb1ef3741 (diff) | |
download | pygn-527e535279dcceeb6ffcf091bc9e8fc23b46f287.tar.gz |
Remove the spaghetti code from the main scripts.
Diffstat (limited to 'pygn2m')
-rwxr-xr-x | pygn2m | 117 |
1 files changed, 50 insertions, 67 deletions
@@ -14,15 +14,13 @@ Thanks to md for this useful formula. Beer is beer. Gets news article from stdin and sends it via SMTP. """ from __future__ import print_function -import sys -import os -import argparse - -sys.path.append('/usr/lib/pyg') -import whitelist -import news2mail +import argparse from mail2news import VERSION, DESC +import news2mail +import os +import sys +import whitelist def parse_cmdline(): @@ -68,76 +66,61 @@ def parse_cmdline(): 4) open smtp connection and send e-mail """ -try: - - """phase 1: - check and set pyg's internal variables - """ - - # it returns only test, other parms are set directly in the actual - # parameter - args = parse_cmdline() - - n2m = news2mail.news2mail(verbose=args.verbose) - owner = None - - # check if n2m has some file prefercences set on commandline - if args.wlfile is None: - wl = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.whitelist')) - else: - wl = args.wlfile - - if args.logfile is None: - log = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.log')) - else: - log = args.logfile +"""phase 1: +check and set pyg's internal variables +""" -# print 'using %s %s\n' % (wl,log) +# it returns only test, other parms are set directly in the actual +# parameter +args = parse_cmdline() - wl = whitelist.whitelist(wl, log) +n2m = news2mail.news2mail(verbose=args.verbose) +owner = None - """phase 2: - check whitelist for user's permission - """ +# check if n2m has some file prefercences set on commandline +if args.wlfile is None: + wl = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.whitelist')) +else: + wl = args.wlfile - # make a first check of From: address - owner = wl.checkfrom(n2m.message['From']) - if owner is None: - if sys.stdin.isatty() == 1 or args.test: - print ('"%s" is not in whitelist!' % (n2m.message['From'][:-1])) - else: - wl.logmsg(n2m.nntpheads, wl.DENY) - - # if verbose, I want to print out headers, so I can't - # exit now. - if not args.verbose: - sys.exit(1) - - """phase 3: - format rfc 822 headers from input article - """ +if args.logfile is None: + log = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.log')) +else: + log = args.logfile - n2m.addheads() # add some important heads - n2m.renameheads() # rename useless heads - n2m.removeheads() # remove other heads +wl = whitelist.whitelist(wl, log) - n2m.sortheads() # sort remaining heads :) +"""phase 2: +check whitelist for user's permission +""" - # prints formatted email message only (without send) if user wants - if args.verbose: - print(n2m.message.as_string()) +# make a first check of From: address +owner = wl.checkfrom(n2m.message['From']) +if owner is None: + if sys.stdin.isatty() == 1 or args.test: + print ('"%s" is not in whitelist!' % (n2m.message['From'][:-1])) + else: + wl.logmsg(n2m.nntpheads, wl.DENY) - if owner is None: + # if verbose, I want to print out headers, so I can't + # exit now. + if not args.verbose: sys.exit(1) - """phase 4: - open smtp connection and send e-mail - """ +# Reformat the message +n2m.process_message() - wl.logmsg(n2m.heads_dict, wl.ACCEPT, owner) - if not args.test: - n2m.sendarticle() +# prints formatted email message only (without send) if user wants +if args.verbose: + print(n2m.message.as_string()) -except KeyboardInterrupt: - print('Keyboard Interrupt') +if owner is None: sys.exit(1) + +"""phase 4: +open smtp connection and send e-mail +""" + +wl.logmsg(n2m.heads_dict, wl.ACCEPT, owner) +if not args.test: + n2m.sendarticle() |