diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2002-06-04 17:02:00 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2015-01-03 10:31:58 +0100 |
commit | 59eda1d0150e4b517a071c1e04ded96ff8f0f314 (patch) | |
tree | 47db347f3ff74e3a7fb23cef1cba2ed4a452aeaa /pygm2n | |
parent | a9d49c6a0fd9dbad859659d7f49901bc9101d790 (diff) | |
download | pygn-0.9.6.tar.gz |
pyg 0.9.6 from archive.debian.org0.9.6
Diffstat (limited to 'pygm2n')
-rwxr-xr-x | pygm2n | 248 |
1 files changed, 186 insertions, 62 deletions
@@ -1,5 +1,5 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- + """News to mail gateway script. Copyright 2000 Cosimo Alfarano Author: Cosimo Alfarano @@ -13,87 +13,211 @@ Thanks to md for this useful formula. Beer is beer. Gets news article and sends it via SMTP. """ -from __future__ import print_function -import argparse -import logging -import mail2news -import nntplib -import sys +import sys, os +import getopt +from string import split + +sys.path.append('/usr/lib/pyg') + +import pyginfo +import mail2news + + +def parse_cmdline(gw): + """Parses cmdline with getopt. and returns a dictionary with + smtp new header + """ + + opt, arg = None, None + test, verbose = 0, 0 +# retnull = (None, None) + retnull = None + + retval = { 'test': 0, + 'verbose': 0 } + + try: + opt, arg = getopt.getopt(sys.argv[1:],"a:s:n:u:p:P:hvVTM") + except (getopt.error), message: + print '%s: %s\n' % (sys.argv[0], message) + sys.exit(1) + + if len(sys.argv) == 1 or opt == []: + gw.smtpheads = None + return retnull + + for i in range(len(opt)): + if opt[i][0] == '-h': + gw.nntpheads = None + return retnull + elif opt[i][0] == '-v': + gw.nntpheads = None + return retnull + elif opt[i][0] == '-n': + gw.nntpheads['Newsgroups:'] = opt[i][1] + '\n' + elif opt[i][0] == '-a': + gw.nntpheads['Approved:'] = opt[i][1] + '\n' + elif opt[i][0] == '-s': + gw.newsserver = opt[i][1] + elif opt[i][0] == '-P': + gw.port = int(opt[i][1]) +# elif opt[i][0] == '-d': +# gw.debug = 1 + elif opt[i][0] == '-u': + gw.user = opt[i][1] + elif opt[i][0] == '-p': + gw.password = opt[i][1] + elif opt[i][0] == '-T': + retval['test'] = 1 + elif opt[i][0] == '-V': + retval['verbose'] = 1 + elif opt[i][0] == '-M': + gw.reader = 1 + + if not gw.nntpheads.has_key('Newsgroups:'): + print 'Error: Missing Newsgroups\n' + return retnull + + +# By rfc822 [Resent-]Sender: should be ever set, unless == From: +# (not this case). Should be a human, while [Resent-]From: may be a program. + +# if gw.rcpt == '' or gw.sender == '': +# print 'missing command line option' +# gw.smtpheads = None +# return retnull + +# if gw.envelope == '' and gw.sender != '': +# gw.smtpheads['Resent-From:'] = gw.sender + '\n' +# gw.envelope = gw.sender +# elif gw.envelope == -1: +# gw.smtpheads = None +# return retnull + + sys.argv[1:] = arg + +# return (test, verbose) + return retval + + +def usage(): + i = pyginfo.pygsinfo() + + print '%s version %s - Copyright 2000 Cosimo Alfarano' % (i.PROGNAME, i.VERSION) + print i.PROGDESC + ' - Mail to News' + print + print 'usage: %s -n newsgroup [-h] [-a approver] [-n newsgroup] [-T] [-V]' % split(sys.argv[0],'/')[-1] + print '-n newsgroup[s] (specified as comma separated without spaces list)' + print '-u user | -p passwword (for auth to newsserver)' + print '-a address of moderator/approver' + print '-s servername' +# print '-d for debug' + print '-h or -v for this info' + print '-T for test mode (not send article via NNTP)' + print '-V for verbose output (usefull with -T option for debugging)' + + + -# logging.basicConfig(level=logging.DEBUG) -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 """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 + 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() + """phase 1: + check and set pyg's internal variables + """ - m2n = mail2news.mail2news(opt) - owner = None + m2n = mail2news.mail2news() + owner = None - """phase 3: - format rfc 822 headers from input article - """ - m2n.renameheads() # rename useless heads - m2n.removeheads() # remove other heads + opt = parse_cmdline(m2n) + if(opt == None): + usage() + sys.exit(0) + + + # check if m2n has some file prefercences set on commandline +# if(m2n.wlfile == None): +# wl = os.environ['HOME'] + '/pyg.whitelist' +# else: +# wl = m2n.wlfile +# +# if(m2n.logfile == None): +# log = os.environ['HOME'] + '/pyg.log' +# else: +# log = m2n.logfile + + +# wl = whitelist.whitelist(wl,log) + + # reads stdin and parses article separating head from body + m2n.readfile() + m2n.parseemail() + + + +# for line in m2n.email: +# print line[:-1] + +# for line in m2n.smtpheads.keys(): +# print line +# print m2n.smtpheads[line][:-1] + + """phase 2: + check whitelist for user's permission + """ + + # make a first check of From: address +# owner = wl.checkfrom(m2n.nntpheads['From:']) +# if(owner == None): +# if(sys.stdin.isatty()==1 or test or verbose): +# print ('"%s" is not in whitelist!' % (m2n.nntpheads['From:'][:-1])) +# else: +# wl.logmsg(m2n.nntpheads,wl.DENY) +# sys.exit(1) + + """phase 3: + format rfc 822 headers from input article + """ + + m2n.mergeheads() # make unique dict from NNTP and SMTP dicts + + m2n.addheads() # add some important heads + m2n.renameheads() # rename useless heads + m2n.removeheads() # remove other heads + + m2n.sortheads() # sort remaining heads :) - m2n.sortheads() # sort remaining heads :) + if(opt['verbose']): + for line in m2n.headers: + print line[:-1] - if opt.verbose: - print(m2n.message.as_string()) + """phase 4: + open smtp connection and send e-mail + """ - 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) + if(len(m2n.headers) > 0 and len(m2n.body) > 0): +# wl.logmsg(m2n.heads_dict,wl.ACCEPT,owner) + if(not opt['test']): + resp = m2n.sendemail() + if resp: + print resp except KeyboardInterrupt: - print('Keyboard Interrupt') - sys.exit(0) + print 'Keyboard Interrupt' + sys.exit(0) |