From 0ac8a0944007d87cabc2d07d94bdeeb7670e7ec9 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 25 Dec 2014 02:45:47 +0100 Subject: Simplify mail2news.sendemail to use nntplib.post() --- mail2news.py | 90 ++++++++++++++++++++++++------------------------------------ 1 file changed, 36 insertions(+), 54 deletions(-) (limited to 'mail2news.py') 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 = '\n' % (getpid()) + msgid = '\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() -- cgit