aboutsummaryrefslogtreecommitdiffstats
path: root/mail2news.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2014-12-25 02:45:47 +0100
committerMatěj Cepl <mcepl@cepl.eu>2014-12-25 03:11:31 +0100
commit0ac8a0944007d87cabc2d07d94bdeeb7670e7ec9 (patch)
tree1bae0a03e26aa40bdafca2eb8cf0f4a9a04adcfa /mail2news.py
parent8af6ecfe31a7767f1d76f36581b01ddd26fb3220 (diff)
downloadpygn-0ac8a0944007d87cabc2d07d94bdeeb7670e7ec9.tar.gz
Simplify mail2news.sendemail to use nntplib.post()
Diffstat (limited to 'mail2news.py')
-rw-r--r--mail2news.py90
1 files changed, 36 insertions, 54 deletions
diff --git a/mail2news.py b/mail2news.py
index 88c53ea..875e692 100644
--- a/mail2news.py
+++ b/mail2news.py
@@ -17,8 +17,9 @@ class mail2news is hopefully conform to rfc850.
from collections import OrderedDict
import email
import logging
+import os
import nntplib
-from os import getpid
+from StringIO import StringIO
from re import findall
from socket import gethostbyaddr, gethostname
import sys
@@ -32,22 +33,26 @@ VERSION = '0.9.9'
DESC = "The Python Gateway Script: news2mail mail2news gateway"
-class mail2news:
+class mail2news(object):
"""news to mail gateway class"""
- reader = None # mode reader
-# newsgroups = None # Newsgroups: local.test,local.moderated...
-# approved = None # Approved: kame@aragorn.lorien.org
- newsserver = 'localhost' # no comment :)
- port = 119
- user = None
- password = None
+ def __init__(self):
+ # newsgroups = None # Newsgroups: local.test,local.moderated...
+ # approved = None # Approved: kame@aragorn.lorien.org
+ if 'NNTPHOST' in os.environ:
+ self.newsserver = os.environ['NNTPHOST']
+ else:
+ self.newsserver = 'localhost'
+
+ self.port = 119
+ self.user = None
+ self.password = None
- hostname = gethostbyaddr(gethostname())[0]
+ self.hostname = gethostbyaddr(gethostname())[0]
- heads_dict, smtpheads, nntpheads = {}, {}, {}
- email, headers, body = [], [], []
- message = None
+ self.heads_dict, self.smtpheads, self.nntpheads = {}, {}, {}
+ self.headers = []
+ self.message = None
def readfile(self, opt):
@@ -151,7 +156,7 @@ class mail2news:
# If message-id is not present, I generate it
if 'Message-Id' not in self.heads_dict:
- msgid = '<pyg.%d@tuchailepuppapera.org>\n' % (getpid())
+ msgid = '<pyg.%d@tuchailepuppapera.org>\n' % (os.getpid())
self.heads_dict['Message-Id'] = msgid
except KeyError, message:
@@ -190,43 +195,20 @@ class mail2news:
return self.headers
def sendemail(self):
- """Talk to NNTP server and try to send email."""
- try:
- n = nntplib.NNTP(self.newsserver, self.port, self.user,
- self.password)
-
- if(self.reader):
- n.putline('mode reader')
- resp = n.getline()
- print resp
-
- resp = n.shortcmd('POST')
-
- # sett RFC977 2.4.2
- if resp[0] != '3':
- raise n.error_reply, str(resp)
-
- for line in self.headers:
- if not line:
- break
- if line[-1] == '\n':
- line = line[:-1]
- if line[:1] == '.':
- line = '.' + line
- n.putline(line)
-
- for line in self.body:
- if not line:
- break
- if line[-1] == '\n':
- line = line[:-1]
- if line[:1] == '.':
- line = '.' + line
- n.putline(line)
-
- n.putline('.')
- n.quit()
- return None
- except (nntplib.error_reply, nntplib.error_temp, nntplib.error_perm,
- nntplib.error_proto, nntplib.error_data), message:
- return 'NNTP: ' + str(message)
+ "Talk to NNTP server and try to send email."
+ # readermode must be True, otherwise we don't have POST command.
+ 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.quit()