aboutsummaryrefslogtreecommitdiffstats
path: root/mail2news.py
diff options
context:
space:
mode:
Diffstat (limited to 'mail2news.py')
-rw-r--r--mail2news.py30
1 files changed, 19 insertions, 11 deletions
diff --git a/mail2news.py b/mail2news.py
index adce631..7461eb8 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
@@ -36,7 +36,7 @@ DESC = "The Python Gateway Script: news2mail mail2news gateway"
class mail2news(object):
"""news to mail gateway class"""
- def __init__(self):
+ def __init__(self, verbose=False):
# newsgroups = None # Newsgroups: local.test,local.moderated...
# approved = None # Approved: kame@aragorn.lorien.org
if 'NNTPHOST' in os.environ:
@@ -47,13 +47,18 @@ class mail2news(object):
self.port = 119
self.user = None
self.password = None
+ self.verbose = verbose
+ logging.debug('self.verbose = %s', verbose)
self.hostname = gethostbyaddr(gethostname())[0]
self.heads_dict, self.smtpheads, self.nntpheads = {}, {}, {}
- self.headers = []
self.message = None
+ def add_header(self, header, value):
+ if value:
+ self.message[header] = value.strip()
+
def readfile(self, opt):
self.message = email.message_from_file(sys.stdin)
@@ -66,10 +71,8 @@ class mail2news(object):
self.message = email.message_from_file(msg_file)
# introduce nntpheads
- if opt.newsgroup != '':
- self.message['Newsgroups'] = opt.newsgroup
- if opt.approver != '':
- self.message['Approved'] = opt.approver
+ self.add_header('Newsgroups', opt.newsgroup)
+ self.add_header('Approved', opt.approver)
return 1
@@ -146,22 +149,23 @@ class mail2news(object):
head_set = ('Newsgroups', 'From', 'To', 'X-To', 'Cc', 'Subject',
'Date', 'Approved', 'References', 'Message-Id')
+ logging.debug('heads_dict = %s', heads_dict)
for k in head_set:
if k in heads_dict:
- self.message[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.message[k] = heads_dict[k]
+ self.add_header(k, heads_dict[k])
for k in heads_dict:
if k.startswith('X-'):
- self.message[k] = heads_dict[k]
+ self.add_header(k, heads_dict[k])
for k in heads_dict:
if k.startswith('X-Resent-'):
- self.message[k] = heads_dict[k]
+ self.add_header(k, heads_dict[k])
def sendemail(self):
"Talk to NNTP server and try to send email."
@@ -169,6 +173,10 @@ class mail2news(object):
server = nntplib.NNTP(self.newsserver, self.port, self.user,
self.password, readermode=True)
+ logging.debug('self.verbose = %s', self.verbose)
+ if self.verbose:
+ server.set_debuglevel(2)
+
server.post(StringIO(self.message.as_string()))
server.quit()