diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2014-12-31 12:12:02 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2014-12-31 12:12:02 +0100 |
commit | 527e535279dcceeb6ffcf091bc9e8fc23b46f287 (patch) | |
tree | 5a2c6bbeed90ee47e4a13dcfa854df6513a775f2 /mail2news.py | |
parent | cdd9f1d6a872fbc20e368fc23ddaf98eb1ef3741 (diff) | |
download | pyg-527e535279dcceeb6ffcf091bc9e8fc23b46f287.tar.gz |
Remove the spaghetti code from the main scripts.
Diffstat (limited to 'mail2news.py')
-rw-r--r-- | mail2news.py | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/mail2news.py b/mail2news.py index 7461eb8..9b3ffc9 100644 --- a/mail2news.py +++ b/mail2news.py @@ -14,12 +14,12 @@ Gets news email and sends it via SMTP. class mail2news is hopefully conform to rfc850. """ +from StringIO import StringIO from collections import OrderedDict import email import logging -import os import nntplib -from StringIO import StringIO +import os from re import findall from socket import gethostbyaddr, gethostname import sys @@ -36,7 +36,7 @@ DESC = "The Python Gateway Script: news2mail mail2news gateway" class mail2news(object): """news to mail gateway class""" - def __init__(self, verbose=False): + def __init__(self, options): # newsgroups = None # Newsgroups: local.test,local.moderated... # approved = None # Approved: kame@aragorn.lorien.org if 'NNTPHOST' in os.environ: @@ -47,34 +47,37 @@ class mail2news(object): self.port = 119 self.user = None self.password = None - self.verbose = verbose - logging.debug('self.verbose = %s', verbose) + self.verbose = options.verbose + logging.debug('self.verbose = %s', self.verbose) self.hostname = gethostbyaddr(gethostname())[0] self.heads_dict, self.smtpheads, self.nntpheads = {}, {}, {} - self.message = None + self.message = self.__readfile(options) - def add_header(self, header, value): - if value: - self.message[header] = value.strip() + self.message['X-Gateway'] = 'pyg {0} {1}'.format(VERSION, DESC) - def readfile(self, opt): + def __add_header(self, header, value, msg=None): + if msg is None: + msg = self.message + if value: + msg[header] = value.strip() - self.message = email.message_from_file(sys.stdin) + def __readfile(self, opt): + message = email.message_from_file(sys.stdin) - if (len(self.message) == 0) \ - and self.message.get_payload().startswith('/'): - msg_file_name = self.message.get_payload().strip() - del self.message + if (len(message) == 0) \ + and message.get_payload().startswith('/'): + msg_file_name = message.get_payload().strip() + del message with open(msg_file_name, 'r') as msg_file: - self.message = email.message_from_file(msg_file) + message = email.message_from_file(msg_file) # introduce nntpheads - self.add_header('Newsgroups', opt.newsgroup) - self.add_header('Approved', opt.approver) + self.__add_header('Newsgroups', opt.newsgroup, message) + self.__add_header('Approved', opt.approver, message) - return 1 + return message def renameheads(self): """rename headers such as Resent-*: to X-Resent-*: @@ -152,20 +155,20 @@ class mail2news(object): logging.debug('heads_dict = %s', heads_dict) for k in head_set: if k in heads_dict: - self.add_header(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.add_header(k, heads_dict[k]) + self.__add_header(k, heads_dict[k]) for k in heads_dict: if k.startswith('X-'): - self.add_header(k, heads_dict[k]) + self.__add_header(k, heads_dict[k]) for k in heads_dict: if k.startswith('X-Resent-'): - self.add_header(k, heads_dict[k]) + self.__add_header(k, heads_dict[k]) def sendemail(self): "Talk to NNTP server and try to send email." |