aboutsummaryrefslogtreecommitdiffstats
path: root/news2mail.py
diff options
context:
space:
mode:
Diffstat (limited to 'news2mail.py')
-rw-r--r--news2mail.py134
1 files changed, 9 insertions, 125 deletions
diff --git a/news2mail.py b/news2mail.py
index 29f4271..b63df46 100644
--- a/news2mail.py
+++ b/news2mail.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python3
"""News to mail gateway script. Copyright 2000 Cosimo Alfarano
Author: Cosimo Alfarano
@@ -23,27 +22,25 @@ normal (what pygs does) operations flow is:
Date:, normal headers ending with X-* and Resent-* headers.
"""
-import argparse
-import os
-import os.path
-import collections
+from __future__ import absolute_import
+from __future__ import print_function
+from collections import OrderedDict
import email
from mail2news import VERSION, DESC
import smtplib
-import socket
+from socket import gethostbyaddr, gethostname
import sys
import time
-import whitelist
# logging.basicConfig(level=logging.DEBUG)
class news2mail(object):
"""news to mail gateway class"""
- def __init__(self, options):
+ def __init__(self, verbose=False):
self.wlfile = None
self.logfile = None
- self.verbose = options.verbose
+ self.verbose = verbose
self.sender = ''
self.rcpt = ''
@@ -51,16 +48,11 @@ class news2mail(object):
self.smtpserver = 'localhost'
- self.hostname = socket.gethostbyaddr(socket.gethostname())[0]
+ self.hostname = gethostbyaddr(gethostname())[0]
self.heads_dict = {}
self.article, self.headers, self.body = [], [], []
-
- if options.input == '':
- self.message = self.__addheads(email.message_from_file(sys.stdin))
- else:
- with open(options.input, 'r') as inp_stream:
- self.message = self.__addheads(email.message_from_file(inp_stream))
+ self.message = self.__addheads(email.message_from_file(sys.stdin))
def __addheads(self, msg):
"""add new header like X-Gateway: Received:
@@ -141,7 +133,7 @@ class news2mail(object):
# put at top
header_set = ('Received', 'From', 'To', 'Subject', 'Date')
- heads_dict = collections.OrderedDict(self.message)
+ heads_dict = OrderedDict(self.message)
for hdr in self.message.keys():
del self.message[hdr]
@@ -178,111 +170,3 @@ class news2mail(object):
s.quit()
-def parse_cmdline(args):
- """
- set a dictionary with smtp new header in gw parameter (gw.smtpheads)
- return (test,verbose) boolean tuple
- """
- parser = argparse.ArgumentParser(
- description='pyg version %s - Copyright 2000 Cosimo Alfarano\n%s' %
- (VERSION, DESC))
-
- parser.add_argument('-H', '--smtpserver', default='')
- parser.add_argument('-s', '--sender', required=True, default='')
- parser.add_argument('-e', '--envelope', default='')
- parser.add_argument('-t', '--to', dest='rcpt', required=True)
- parser.add_argument('-w', '--wlfile')
- parser.add_argument('-i', '--input', default='')
- parser.add_argument('-l', '--logfile')
-
- parser.add_argument('-T', '--test',
- help='test mode (not send article via SMTP)',
- action='store_true')
- parser.add_argument('-v', '--verbose', help='verbose output',
- action='store_true')
-
- opts = parser.parse_args(args)
-
-# 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 opts.rcpt == '' or opts.sender == '':
- raise argparse.ArgumentError('missing command line option')
-
- if opts.envelope == '' and opts.sender != '':
- opts.envelope = opts.sender
-
- return opts
-
-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
- """
-
- """phase 1:
- check and set pyg's internal variables
- """
- out = ''
-
- # it returns only test, other parms are set directly in the actual
- # parameter
- args = parse_cmdline(args)
-
- n2m = news2mail(args)
- owner = None
-
- # check if n2m has some file prefercences set on commandline
- if args.wlfile is None:
- wl = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.whitelist'))
- else:
- wl = args.wlfile
-
- if args.logfile is None:
- log = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.log'))
- else:
- log = args.logfile
-
- wl = whitelist.whitelist(wl, log)
-
- """phase 2:
- check whitelist for user's permission
- """
-
- # make a first check of From: address
- owner = wl.checkfrom(n2m.message['From'])
- if owner is None:
- if sys.stdin.isatty() == 1 or args.test:
- out += str('"%s" is not in whitelist!' % (n2m.message['From'][:-1])) + '\n'
- else:
- wl.logmsg(n2m.nntpheads, wl.DENY)
-
- # if verbose, I want to print out headers, so I can't
- # exit now.
- if not args.verbose:
- sys.exit(1)
-
- # Reformat the message
- n2m.process_message()
-
- # prints formatted email message only (without send) if user wants
- if args.verbose:
- out += n2m.message.as_string() + '\n'
-
- if owner is None:
- sys.exit(1)
-
- """phase 4:
- open smtp connection and send e-mail
- """
-
- wl.logmsg(n2m.heads_dict, wl.ACCEPT, owner)
- if not args.test:
- n2m.sendarticle()
-
- if args.input == '':
- print(out)
- else:
- return out