diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | mail2news.py | 30 | ||||
-rw-r--r-- | news2mail.py | 8 | ||||
-rwxr-xr-x | pygm2n | 22 | ||||
-rwxr-xr-x | pygn2m | 20 |
5 files changed, 41 insertions, 40 deletions
@@ -4,3 +4,4 @@ wlp/lex.yy.c *.pyc wlp.so pyg.egg-info/ +dist/ diff --git a/mail2news.py b/mail2news.py index adce631..7461eb8 100644 --- a/mail2news.py +++ b/mail2news.py @@ -16,7 +16,7 @@ class mail2news is hopefully conform to rfc850. """ from collections import OrderedDict import email -#import logging +import logging import os import nntplib from StringIO import StringIO @@ -36,7 +36,7 @@ DESC = "The Python Gateway Script: news2mail mail2news gateway" class mail2news(object): """news to mail gateway class""" - def __init__(self): + def __init__(self, verbose=False): # newsgroups = None # Newsgroups: local.test,local.moderated... # approved = None # Approved: kame@aragorn.lorien.org if 'NNTPHOST' in os.environ: @@ -47,13 +47,18 @@ class mail2news(object): self.port = 119 self.user = None self.password = None + self.verbose = verbose + logging.debug('self.verbose = %s', verbose) self.hostname = gethostbyaddr(gethostname())[0] self.heads_dict, self.smtpheads, self.nntpheads = {}, {}, {} - self.headers = [] self.message = None + def add_header(self, header, value): + if value: + self.message[header] = value.strip() + def readfile(self, opt): self.message = email.message_from_file(sys.stdin) @@ -66,10 +71,8 @@ class mail2news(object): self.message = email.message_from_file(msg_file) # introduce nntpheads - if opt.newsgroup != '': - self.message['Newsgroups'] = opt.newsgroup - if opt.approver != '': - self.message['Approved'] = opt.approver + self.add_header('Newsgroups', opt.newsgroup) + self.add_header('Approved', opt.approver) return 1 @@ -146,22 +149,23 @@ class mail2news(object): head_set = ('Newsgroups', 'From', 'To', 'X-To', 'Cc', 'Subject', 'Date', 'Approved', 'References', 'Message-Id') + logging.debug('heads_dict = %s', heads_dict) for k in head_set: if k in heads_dict: - self.message[k] = heads_dict[k] + self.add_header(k, heads_dict[k]) for k in heads_dict: if not k.startswith('X-') and not k.startswith('X-Resent-') \ and k not in head_set: - self.message[k] = heads_dict[k] + self.add_header(k, heads_dict[k]) for k in heads_dict: if k.startswith('X-'): - self.message[k] = heads_dict[k] + self.add_header(k, heads_dict[k]) for k in heads_dict: if k.startswith('X-Resent-'): - self.message[k] = heads_dict[k] + self.add_header(k, heads_dict[k]) def sendemail(self): "Talk to NNTP server and try to send email." @@ -169,6 +173,10 @@ class mail2news(object): server = nntplib.NNTP(self.newsserver, self.port, self.user, self.password, readermode=True) + logging.debug('self.verbose = %s', self.verbose) + if self.verbose: + server.set_debuglevel(2) + server.post(StringIO(self.message.as_string())) server.quit() diff --git a/news2mail.py b/news2mail.py index b396bc2..7338fd8 100644 --- a/news2mail.py +++ b/news2mail.py @@ -36,7 +36,7 @@ from mail2news import VERSION, DESC class news2mail(object): """news to mail gateway class""" - def __init__(self): + def __init__(self, verbose=False): self.wlfile = None self.logfile = None @@ -50,11 +50,10 @@ class news2mail(object): self.heads_dict = {} self.article, self.headers, self.body = [], [], [] - self.message = None - - def readfile(self): self.message = email.message_from_file(sys.stdin) + self.verbose = verbose + def addheads(self): """add new header like X-Gateway: Received: """ @@ -154,6 +153,7 @@ class news2mail(object): def sendarticle(self): """Talk to SMTP server and try to send email.""" s = smtplib.SMTP(self.smtpserver) + s.set_debuglevel(self.verbose) s.sendmail(self.envelope, self.rcpt, self.message.as_string()) @@ -17,7 +17,8 @@ from __future__ import print_function import sys import nntplib - +import logging +logging.basicConfig(level=logging.DEBUG) import argparse import mail2news @@ -37,21 +38,18 @@ def parse_cmdline(): parser.add_argument('-n', '--newsgroup', default='', help='newsgroup[s] (specified as comma separated ' + 'without spaces list)', required=True) - parser.add_argument('-u', '--user', default='') + parser.add_argument('-u', '--user', default='', + help='NNTP server user (for authentication)') parser.add_argument('-p', '--password', default='', - help='password (for auth to newsserver)') + help='NNTP server password (for authentication)') parser.add_argument('-P', '--port', default='') - # WHAT IS THIS GOOD FOR? - # parser.add_argument('-M', '--reader', action='store_true') - parser.add_argument('-e', '--envellope', default='') - parser.add_argument('-t', '--to', dest='rcpt') parser.add_argument('-w', '--wlfile') 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', + parser.add_argument('-v', '--verbose', action='store_true', help='verbose output ' + '(usefull with -T option for debugging)') @@ -76,12 +74,11 @@ try: """phase 1: check and set pyg's internal variables """ + opt = parse_cmdline() - m2n = mail2news.mail2news() + m2n = mail2news.mail2news(verbose=opt.verbose) owner = None - opt = parse_cmdline() - # reads stdin and parses article separating head from body m2n.readfile(opt) # m2n.parseemail() @@ -104,7 +101,8 @@ try: if opt.verbose: print(m2n.message.as_string()) - if len(m2n.headers) > 0 and len(m2n.message.get_payload()) > 0: + 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: @@ -44,7 +44,7 @@ def parse_cmdline(): parser.add_argument('-T', '--test', help='test mode (not send article via SMTP)', action='store_true') - parser.add_argument('-V', '--verbose', help='verbose output', + parser.add_argument('-v', '--verbose', help='verbose output', action='store_true') opts = parser.parse_args() @@ -74,12 +74,12 @@ try: check and set pyg's internal variables """ - n2m = news2mail.news2mail() - owner = None - # 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: @@ -96,9 +96,6 @@ try: wl = whitelist.whitelist(wl, log) - # reads stdin and parses article separating head from body - n2m.readfile() - """phase 2: check whitelist for user's permission """ @@ -137,12 +134,9 @@ try: open smtp connection and send e-mail """ - if len(n2m.headers) > 0: - wl.logmsg(n2m.heads_dict, wl.ACCEPT, owner) - if not args.test: - n2m.sendarticle() - else: - print('Error: No Headers!!!') + wl.logmsg(n2m.heads_dict, wl.ACCEPT, owner) + if not args.test: + n2m.sendarticle() except KeyboardInterrupt: print('Keyboard Interrupt') |