diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2019-10-01 14:44:02 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2019-10-01 14:44:02 +0200 |
commit | d6b4d3860c4b36d35b9a8a96dcdf49093a139650 (patch) | |
tree | 980bea91eda072344f47c29f7311ac9922c14497 /mail2news.py | |
parent | c60e11ceba4fc9ac017e8b1c6a2f1eab810f2dc7 (diff) | |
download | pyg-d6b4d3860c4b36d35b9a8a96dcdf49093a139650.tar.gz |
A ways better.
Tentatively fixes #7
Diffstat (limited to 'mail2news.py')
-rw-r--r-- | mail2news.py | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/mail2news.py b/mail2news.py index 386305b..dd4758c 100644 --- a/mail2news.py +++ b/mail2news.py @@ -15,7 +15,8 @@ Gets news email and sends it via SMTP. class mail2news is hopefully conform to rfc850. """ -from StringIO import StringIO +import argparse +from io import StringIO from collections import OrderedDict import email import logging @@ -54,7 +55,11 @@ class mail2news(object): self.hostname = gethostbyaddr(gethostname())[0] self.heads_dict, self.smtpheads, self.nntpheads = {}, {}, {} - self.message = self.__readfile(options) + if options.input == '': + self.message = self.__readfile(options, sys.stdin) + else: + with open(options.input, 'r') as inp_stream: + self.message = self.__readfile(options, inp_stream) self.message['X-Gateway'] = 'pyg {0} {1}'.format(VERSION, DESC) @@ -64,8 +69,8 @@ class mail2news(object): if value: msg[header] = value.strip() - def __readfile(self, opt): - message = email.message_from_file(sys.stdin) + def __readfile(self, opt, input_stream): + message = email.message_from_file(input_stream) if (len(message) == 0) \ and message.get_payload().startswith('/'): @@ -185,10 +190,10 @@ class mail2news(object): server.quit() -def parse_cmdline(): +def parse_cmdline(args): parser = argparse.ArgumentParser( description='%s version %s - Copyright 2000 Cosimo Alfarano\n%s' % - ('pyg', mail2news.VERSION, mail2news.DESC)) + ('pyg', VERSION, DESC)) parser.add_argument('-s', '--newsserver', default='') parser.add_argument('-a', '--approver', default='', @@ -201,7 +206,8 @@ def parse_cmdline(): parser.add_argument('-p', '--password', default='', help='NNTP server password (for authentication)') parser.add_argument('-P', '--port', default='') - parser.add_argument('-e', '--envellope', default='') + parser.add_argument('-e', '--envelope', default='') + parser.add_argument('-i', '--input', default='') parser.add_argument('-l', '--logfile') parser.add_argument('-T', '--test', action='store_true', @@ -210,7 +216,7 @@ def parse_cmdline(): help='verbose output ' + '(usefull with -T option for debugging)') - args = parser.parse_args() + args = parser.parse_args(args) if not args.newsgroup: raise argparse.ArgumentError('Error: Missing Newsgroups\n') @@ -218,21 +224,22 @@ def parse_cmdline(): return args -def main(): +def main(args=sys.argv[1:]): """main is structured in 4 phases: 1) check and set pyg's internal variables 2) check whitelist for users' permission 3) format rfc 822 headers from input article 4) open smtp connection and send e-mail """ + out = '' try: """phase 1: check and set pyg's internal variables """ - opt = parse_cmdline() + opt = parse_cmdline(args) - m2n = mail2news.mail2news(opt) + m2n = mail2news(opt) owner = None """phase 3: @@ -244,7 +251,7 @@ def main(): m2n.sortheads() # sort remaining heads :) if opt.verbose: - print(m2n.message.as_string()) + out += m2n.message.as_string() + '\n' logging.debug('m2n.payload = len %d', len(m2n.message.get_payload())) if len(m2n.message.get_payload()) > 0: @@ -253,7 +260,12 @@ def main(): try: resp = m2n.sendemail() except nntplib.NNTPError as ex: - print(ex) + logging.exception(ex) except KeyboardInterrupt: - print('Keyboard Interrupt') + logging.error('Keyboard Interrupt') sys.exit(0) + + if opt.input == '': + print(out) + else: + return out |