From: Andreas J. <aj...@us...> - 2004-09-06 15:08:44
|
Update of /cvsroot/collective/PloneCollectorNG/email_submission In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17386 Modified Files: Tag: pcng-1_2-branch smtp2pcng.py Log Message: refactored code for commercial addons Index: smtp2pcng.py =================================================================== RCS file: /cvsroot/collective/PloneCollectorNG/email_submission/smtp2pcng.py,v retrieving revision 1.17 retrieving revision 1.17.6.1 diff -u -d -r1.17 -r1.17.6.1 --- smtp2pcng.py 19 Apr 2004 09:59:50 -0000 1.17 +++ smtp2pcng.py 6 Sep 2004 15:08:32 -0000 1.17.6.1 @@ -12,7 +12,7 @@ """ Gateway to submit issues through email to a PloneCollectorNG instance """ -import sys, os, logging, base64, logging, time, re +import sys, os, base64, logging, time, re import httplib, urllib, urlparse from ConfigParser import ConfigParser from cStringIO import StringIO @@ -20,7 +20,9 @@ import email from email.Header import decode_header -CFG_FILE = '.smtp2pcng.cfg' +from Result import Result +from util import getLogger, getConfiguration + MAX_LENGTH = 32768 # Spool directories @@ -32,64 +34,8 @@ if not os.path.exists(d): os.makedirs(d) -# Logger stuff -LOG = logging.getLogger('pcng') -hdlr = logging.FileHandler('smtp2pcng.log') -hdlr1 = logging.StreamHandler() -formatter = logging.Formatter('%(asctime)s %(levelname)s %(process)s %(message)s') -hdlr.setFormatter(formatter) -hdlr1.setFormatter(formatter) -LOG.addHandler(hdlr) -LOG.addHandler(hdlr1) -LOG.setLevel(logging.DEBUG) - -# Configuration -CFG_LOCATIONS = (os.getcwd(), os.path.expanduser('~')) -config = ConfigParser() -files = [] -for loc in CFG_LOCATIONS: - cfg_name = os.path.join(loc, CFG_FILE) - if not os.path.exists(cfg_name): - LOG.warn('No configuration file %s found' % cfg_name) - else: - LOG.debug('Reading configuration file %s ' % cfg_name) - files.append(cfg_name) - -if not files: - LOG.warn('No suitables configuration files found') -else: - config.read(files) - - -class Result: - - def __init__(self): - self.attachments = [] - self.key = '' - self.collector_abr = '' - self.issue_id = '' - - def addAttachment(self, data, mimetype, filename): - self.attachments.append( (data, mimetype, filename)) - - def getAttachments(self): - return self.attachments - - def toXML(self): - IO = StringIO() - IO.write('<?xml version="1.0" encoding="utf-8"?>\n') - IO.write('<issue>\n') - for a in ('sendername', 'senderaddress', 'reply_to', 'subject', 'body', 'key', 'issue_id', 'collector_abr'): - IO.write('<%s>%s</%s>\n' % (a, getattr(self, a), a)) - - for a in self.getAttachments(): - IO.write('<attachment mimetype="%s" filename="%s">\n' % a[1:]) - IO.write(base64.encodestring(a[0])) - IO.write('</attachment>\n') - - IO.write('</issue>\n') - return IO.getvalue() - +LOG = getLogger() +config = getConfiguration() def parse_mail(options): @@ -124,7 +70,15 @@ if len(text) > options.max_length: raise RuntimeError('Message size exceeds allowed length of %d bytes' % options.max_length) + return process_email_from_text(text) + + +def process_email_from_text(text): + """ process an email represented as plain text """ + + # convert to Python internal message format msg = email.message_from_string(text) + R = Result() R.original_message = text |