diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2019-09-30 10:37:52 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2019-09-30 10:37:52 +0200 |
commit | c60e11ceba4fc9ac017e8b1c6a2f1eab810f2dc7 (patch) | |
tree | 10ba0f4b0e1b63844ccb470a07b07331af580268 /mail2news.py | |
parent | 0efae60c88d21bcdca66d904cbc842b2e64ecfac (diff) | |
download | pyg-c60e11ceba4fc9ac017e8b1c6a2f1eab810f2dc7.tar.gz |
Basic restructuring of the code to use setuptools’ entry_points.
Diffstat (limited to 'mail2news.py')
-rw-r--r-- | mail2news.py | 84 |
1 files changed, 79 insertions, 5 deletions
diff --git a/mail2news.py b/mail2news.py index d6d8658..386305b 100644 --- a/mail2news.py +++ b/mail2news.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 """Mail to news gateway script. Copyright 2000 Cosimo Alfarano Author: Cosimo Alfarano @@ -98,7 +99,7 @@ class mail2news(object): # In rfc822 References: is considered, but many MUA doen't put it. if ('References' not in self.message) and \ ('In-Reply-To' in self.message): - print self.message['In-Reply-To'] + print(self.message['In-Reply-To']) # some MUA uses msgid without '<' '>' # ref = findall('([^\s<>\']+@[^\s<>;:\']+)', \ @@ -110,8 +111,8 @@ class mail2news(object): if(ref and len(ref)): self.message['References'] = '%s\n' % ref[0] - except KeyError, message: - print message + except KeyError as message: + print(message) def removeheads(self, heads=None): """remove headers like Xref: Path: Lines: @@ -137,8 +138,8 @@ class mail2news(object): msgid = '<pyg.%d@tuchailepuppapera.org>\n' % (os.getpid()) self.message['Message-Id'] = msgid - except KeyError, message: - print message + except KeyError as message: + print(message) def sortheads(self): """make list sorted by heads: From: To: Subject: first, @@ -183,3 +184,76 @@ class mail2news(object): server.post(StringIO(self.message.as_string())) server.quit() + +def parse_cmdline(): + parser = argparse.ArgumentParser( + description='%s version %s - Copyright 2000 Cosimo Alfarano\n%s' % + ('pyg', mail2news.VERSION, mail2news.DESC)) + + parser.add_argument('-s', '--newsserver', default='') + parser.add_argument('-a', '--approver', default='', + help="address of moderator/approver") + parser.add_argument('-n', '--newsgroup', default='', + help='newsgroup[s] (specified as comma separated ' + + 'without spaces list)', required=True) + parser.add_argument('-u', '--user', default='', + help='NNTP server user (for authentication)') + 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('-l', '--logfile') + + parser.add_argument('-T', '--test', action='store_true', + help='test mode (not send article via NNTP)') + parser.add_argument('-v', '--verbose', action='store_true', + help='verbose output ' + + '(usefull with -T option for debugging)') + + args = parser.parse_args() + + if not args.newsgroup: + raise argparse.ArgumentError('Error: Missing Newsgroups\n') + + return args + + +def main(): + """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 + """ + + try: + """phase 1: + check and set pyg's internal variables + """ + opt = parse_cmdline() + + m2n = mail2news.mail2news(opt) + owner = None + + """phase 3: + format rfc 822 headers from input article + """ + m2n.renameheads() # rename useless heads + m2n.removeheads() # remove other heads + + m2n.sortheads() # sort remaining heads :) + + if opt.verbose: + print(m2n.message.as_string()) + + logging.debug('m2n.payload = len %d', len(m2n.message.get_payload())) + if len(m2n.message.get_payload()) > 0: + # wl.logmsg(m2n.heads_dict,wl.ACCEPT,owner) + if not opt.test: + try: + resp = m2n.sendemail() + except nntplib.NNTPError as ex: + print(ex) + except KeyboardInterrupt: + print('Keyboard Interrupt') + sys.exit(0) |