diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2014-12-28 01:00:36 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2014-12-28 01:04:31 +0100 |
commit | 8e8a89e3dc6d6da2e9f230e316f4c3372013bb85 (patch) | |
tree | 15c9dc3367154319340ee6e3e368fdfefdacd627 | |
parent | aac353539738870f9ec7f89f66cc2f44f93129cb (diff) | |
download | pyg-8e8a89e3dc6d6da2e9f230e316f4c3372013bb85.tar.gz |
Work directly on self.message in mail2news.
-rw-r--r-- | mail2news.py | 116 | ||||
-rwxr-xr-x | pygm2n | 11 | ||||
-rwxr-xr-x | test/test_wlp.py | 5 |
3 files changed, 46 insertions, 86 deletions
diff --git a/mail2news.py b/mail2news.py index 875e692..c9ed55f 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 @@ -67,30 +67,12 @@ class mail2news(object): # introduce nntpheads if opt.newsgroup != '': - self.nntpheads['Newsgroups'] = opt.newsgroup + self.message['Newsgroups'] = opt.newsgroup if opt.approver != '': - self.nntpheads['Approved'] = opt.approver + self.message['Approved'] = opt.approver return 1 - @staticmethod - def puthead(from_dict, out_list, key): - """private, x-form dict entries in out_list entries - """ - if key in from_dict: - out_list.append(key + ': ' + from_dict.get(key)) - else: - return 0 - return 1 - - def mergeheads(self): - """make a unique headers dictionary from NNTP and SMTP - single headers dictionaries.""" - - self.heads_dict = OrderedDict(self.message) - self.heads_dict.update(self.nntpheads) - return self.heads_dict - def renameheads(self): """rename headers such as Resent-*: to X-Resent-*: @@ -99,34 +81,32 @@ class mail2news(object): """ try: - for key in self.heads_dict.keys(): - if(key[:7] in ['Resent-']): - if ('X-' + key) in self.heads_dict: - self.heads_dict['X-Original-' + key] = \ - self.heads_dict['X-' + key] - self.heads_dict['X-' + key] = self.heads_dict[key] - del self.heads_dict[key] + for key in self.message.keys(): + if key.startswith('Resent-'): + if ('X-' + key) in self.message: + self.message['X-Original-' + key] = \ + self.message['X-' + key] + self.message['X-' + key] = self.message[key] + del self.message[key] # In rfc822 References: is considered, but many MUA doen't put it. - if ('References' not in self.heads_dict) and \ - ('In-Reply-To' in self.heads_dict): - print self.heads_dict['In-Reply-To'] + if ('References' not in self.message) and \ + ('In-Reply-To' in self.message): + print self.message['In-Reply-To'] # some MUA uses msgid without '<' '>' # ref = findall('([^\s<>\']+@[^\s<>;:\']+)', \ # but I prefer use RFC standards ref = findall('(<[^<>]+@[^<>]+>)', - self.heads_dict['In-Reply-To']) + self.message['In-Reply-To']) # if found, keep first element that seems a Msg-ID. if(ref and len(ref)): - self.heads_dict['References'] = '%s\n' % ref[0] + self.message['References'] = '%s\n' % ref[0] except KeyError, message: print message - return self.heads_dict - def removeheads(self, heads=None): """remove headers like Xref: Path: Lines: """ @@ -136,63 +116,52 @@ class mail2news(object): rmheads = ['Received', 'From ', 'NNTP-Posting-Host', 'X-Trace', 'X-Compliants-To', 'NNTP-Posting-Date'] - if(heads): + if heads: rmheads.append(heads) for head in rmheads: - if head in self.heads_dict: - del self.heads_dict[head] - - if 'Message-Id' not in self.heads_dict: - if 'Message-id' in self.heads_dict: - self.heads_dict['Message-Id'] = \ - self.heads_dict['Message-id'] - del(self.heads_dict['Message-id']) - - if 'Message-ID' in self.heads_dict: - self.heads_dict['Message-Id'] = \ - self.heads_dict['Message-ID'] - del(self.heads_dict['Message-ID']) + if head in self.message: + del self.message[head] - # If message-id is not present, I generate it - if 'Message-Id' not in self.heads_dict: - msgid = '<pyg.%d@tuchailepuppapera.org>\n' % (os.getpid()) - self.heads_dict['Message-Id'] = msgid + if 'Message-Id' in self.message: + msgid = self.message['Message-Id'] + del self.message['Message-Id'] + self.message['Message-Id'] = msgid + else: + msgid = '<pyg.%d@tuchailepuppapera.org>\n' % (os.getpid()) + self.message['Message-Id'] = msgid except KeyError, message: print message - return self.heads_dict - def sortheads(self): """make list sorted by heads: From: To: Subject: first, others, X-*, X-Resent-* last""" + heads_dict = OrderedDict(self.message) + for hdr in self.message.keys(): + del self.message[hdr] + # put at top head_set = ('Newsgroups', 'From', 'To', 'X-To', 'Cc', 'Subject', 'Date', 'Approved', 'References', 'Message-Id') - logging.debug('self.heads_dict = %s', self.heads_dict) - for k in head_set: - self.puthead(self.heads_dict, self.headers, k) + if k in heads_dict: + self.message[k] = heads_dict[k] - for k in self.heads_dict.keys(): + for k in heads_dict: if not k.startswith('X-') and not k.startswith('X-Resent-') \ and k not in head_set: - self.puthead(self.heads_dict, self.headers, k) + self.message[k] = heads_dict[k] - for k in self.heads_dict.keys(): + for k in heads_dict: if k.startswith('X-'): - self.puthead(self.heads_dict, self.headers, k) + self.message[k] = heads_dict[k] - for k in self.heads_dict.keys(): + for k in heads_dict: if k.startswith('X-Resent-'): - self.puthead(self.heads_dict, self.headers, k) - - logging.debug('self.headers = %s', self.headers) - - return self.headers + self.message[k] = heads_dict[k] def sendemail(self): "Talk to NNTP server and try to send email." @@ -200,15 +169,6 @@ class mail2news(object): server = nntplib.NNTP(self.newsserver, self.port, self.user, self.password, readermode=True) - msg = self.message.copy() - for hdr in msg.keys(): - del msg[hdr] - - for key, value in self.headers.items: - msg.add_header(key, value) - - str_file = StringIO(msg.as_string()) - - server.post(str_file) + server.post(StringIO(self.message.as_string())) server.quit() @@ -95,9 +95,7 @@ try: format rfc 822 headers from input article """ - m2n.mergeheads() # make unique dict from NNTP and SMTP dicts - - m2n.heads_dict['X-Gateway'] = 'pyg {0} {1}'.format( + m2n.message['X-Gateway'] = 'pyg {0} {1}'.format( mail2news.VERSION, mail2news.DESC) m2n.renameheads() # rename useless heads m2n.removeheads() # remove other heads @@ -105,8 +103,7 @@ try: m2n.sortheads() # sort remaining heads :) if opt.verbose: - for line in m2n.headers: - print(line) + print(m2n.message.as_string()) """phase 4: open smtp connection and send e-mail @@ -117,8 +114,8 @@ try: if not opt.test: try: resp = m2n.sendemail() - except nntplib.NNTPError, message: - print(message) + except nntplib.NNTPError as ex: + print(ex) except KeyboardInterrupt: print('Keyboard Interrupt') diff --git a/test/test_wlp.py b/test/test_wlp.py index 62b2551..759dffe 100755 --- a/test/test_wlp.py +++ b/test/test_wlp.py @@ -52,8 +52,11 @@ Return-Path: <pyg@localhost> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: Mutt/1.2.5i -X-Multiline: this header probably broke RFC, but is frequent. +X-Multiline: this header probably broke RFC, but is frequent. X-Gateway: pyg %s %s + +one line test + """ % (mail2news.VERSION, mail2news.DESC) def test_m2n(self): |