aboutsummaryrefslogtreecommitdiffstats
path: root/mail2news.py
diff options
context:
space:
mode:
Diffstat (limited to 'mail2news.py')
-rw-r--r--mail2news.py116
1 files changed, 38 insertions, 78 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()