aboutsummaryrefslogtreecommitdiffstats
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
parent8af6ecfe31a7767f1d76f36581b01ddd26fb3220 (diff)
downloadpygn-0ac8a0944007d87cabc2d07d94bdeeb7670e7ec9.tar.gz
Simplify mail2news.sendemail to use nntplib.post()
-rw-r--r--mail2news.py90
-rwxr-xr-xpygm2n13
2 files changed, 44 insertions, 59 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()
diff --git a/pygm2n b/pygm2n
index 2dcd21e..9c4e069 100755
--- a/pygm2n
+++ b/pygm2n
@@ -16,6 +16,7 @@ Gets news article and sends it via SMTP.
from __future__ import print_function
import sys
+import nntplib
import argparse
@@ -40,7 +41,8 @@ def parse_cmdline():
parser.add_argument('-p', '--password', default='',
help='password (for auth to newsserver)')
parser.add_argument('-P', '--port', default='')
- parser.add_argument('-M', '--reader', default='')
+ # WHAT IS THIS GOOD FOR?
+ # parser.add_argument('-M', '--reader', action='store_true')
parser.add_argument('-e', '--envellope', default='')
parser.add_argument('-t', '--to', dest='rcpt')
@@ -110,12 +112,13 @@ try:
open smtp connection and send e-mail
"""
- if len(m2n.headers) > 0 and len(m2n.body) > 0:
+ if len(m2n.headers) > 0 and len(m2n.message.get_payload()) > 0:
# wl.logmsg(m2n.heads_dict,wl.ACCEPT,owner)
if not opt.test:
- resp = m2n.sendemail()
- if resp:
- print(resp)
+ try:
+ resp = m2n.sendemail()
+ except nntplib.NNTPError, message:
+ print(message)
except KeyboardInterrupt:
print('Keyboard Interrupt')