aboutsummaryrefslogtreecommitdiffstats
path: root/mail2news.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2020-07-01 16:16:44 +0200
committerMatěj Cepl <mcepl@cepl.eu>2020-07-01 16:19:16 +0200
commitd5fc1faf927a5ff91f3416eb97014c0eadb14742 (patch)
tree2cb34082a4df13685c2ee0f46e1360cfdf4a8e88 /mail2news.py
parent19388fa2e3e6b02e7d7fd5c0b5dc9ee0cd9ef100 (diff)
downloadpygn-d5fc1faf927a5ff91f3416eb97014c0eadb14742.tar.gz
Sync with 0.10.1 release on PyPI0.10.1
Port to Python 3.
Diffstat (limited to 'mail2news.py')
-rw-r--r--mail2news.py104
1 files changed, 9 insertions, 95 deletions
diff --git a/mail2news.py b/mail2news.py
index 6e60d4c..34cfed8 100644
--- a/mail2news.py
+++ b/mail2news.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python3
"""Mail to news gateway script. Copyright 2000 Cosimo Alfarano
Author: Cosimo Alfarano
@@ -15,8 +14,9 @@ Gets news email and sends it via SMTP.
class mail2news is hopefully conform to rfc850.
"""
-import argparse
-from io import StringIO
+from __future__ import absolute_import
+from __future__ import print_function
+import io
from collections import OrderedDict
import email
import logging
@@ -27,11 +27,11 @@ from socket import gethostbyaddr, gethostname
import sys
-# logging.basicConfig(level=logging.DEBUG)
+#logging.basicConfig(level=logging.DEBUG)
# This is the single source of Truth
# Yes, it is awkward to have it assymetrically here
# and not in news2mail as well.
-VERSION = '0.9.14'
+VERSION = '0.10.1'
DESC = "The Python Gateway Script: news2mail mail2news gateway"
@@ -55,11 +55,7 @@ class mail2news(object):
self.hostname = gethostbyaddr(gethostname())[0]
self.heads_dict, self.smtpheads, self.nntpheads = {}, {}, {}
- 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 = self.__readfile(options)
self.message['X-Gateway'] = 'pyg {0} {1}'.format(VERSION, DESC)
@@ -69,8 +65,8 @@ class mail2news(object):
if value:
msg[header] = value.strip()
- def __readfile(self, opt, input_stream):
- message = email.message_from_file(input_stream)
+ def __readfile(self, opt):
+ message = email.message_from_file(sys.stdin)
if (len(message) == 0) \
and message.get_payload().startswith('/'):
@@ -186,88 +182,6 @@ class mail2news(object):
if self.verbose:
server.set_debuglevel(2)
- data = [x.encode() for x in self.message.as_string().split('\n')]
- logging.debug('data = type %s', type(data))
- server.post(data)
+ server.post(io.BytesIO(self.message.as_bytes()))
server.quit()
-
-def parse_cmdline(args):
- parser = argparse.ArgumentParser(
- description='%s version %s - Copyright 2000 Cosimo Alfarano\n%s' %
- ('pyg', VERSION, 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', '--envelope', default='')
- parser.add_argument('-i', '--input', 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(args)
-
- if not args.newsgroup:
- raise argparse.ArgumentError('Error: Missing Newsgroups\n')
-
- return args
-
-
-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(args)
-
- m2n = 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:
- out += m2n.message.as_string() + '\n'
-
- 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:
- logging.exception(ex)
- except KeyboardInterrupt:
- logging.error('Keyboard Interrupt')
- sys.exit(0)
-
- if opt.input == '':
- print(out)
- else:
- return out