Commit cb905439 authored by Jeffrey Shell's avatar Jeffrey Shell

started using rfc822 module to parse headers

parent fba55b08
...@@ -4,14 +4,15 @@ from Globals import Persistent, HTMLFile, HTML, MessageDialog ...@@ -4,14 +4,15 @@ from Globals import Persistent, HTMLFile, HTML, MessageDialog
from socket import *; from select import select from socket import *; from select import select
from AccessControl.Role import RoleManager from AccessControl.Role import RoleManager
from operator import truth from operator import truth
import Acquisition, sys, ts_regex, string, types import Acquisition, sys, ts_regex, string, types, rfc822
import OFS.SimpleItem import OFS.SimpleItem
import Globals import Globals
from Scheduler.OneTimeEvent import OneTimeEvent from Scheduler.OneTimeEvent import OneTimeEvent
from ImageFile import ImageFile from ImageFile import ImageFile
from cStringIO import StringIO
#$Id: MailHost.py,v 1.29 1998/04/30 19:04:49 jeffrey Exp $ #$Id: MailHost.py,v 1.30 1998/05/07 19:55:27 jeffrey Exp $
__version__ = "$Revision: 1.29 $"[11:-2] __version__ = "$Revision: 1.30 $"[11:-2]
smtpError = "SMTP Error" smtpError = "SMTP Error"
MailHostError = "MailHost Error" MailHostError = "MailHost Error"
...@@ -82,7 +83,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -82,7 +83,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
mtemplate = getattr(self, messageTemplate) mtemplate = getattr(self, messageTemplate)
messageText = mtemplate(self, trueself.REQUEST) messageText = mtemplate(self, trueself.REQUEST)
headers, message = newDecapitate(messageText) headers, message = decapitate(messageText)
if mto: headers['to'] = mto if mto: headers['to'] = mto
if mfrom: headers['from'] = mfrom if mfrom: headers['from'] = mfrom
for requiredHeader in ('to', 'from'): for requiredHeader in ('to', 'from'):
...@@ -109,7 +110,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -109,7 +110,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
return "SEND OK" return "SEND OK"
def send(self, messageText, mto=None, mfrom=None, subject=None): def send(self, messageText, mto=None, mfrom=None, subject=None):
headers, message = newDecapitate(messageText) headers, message = decapitate(messageText)
if not headers['subject']: if not headers['subject']:
messageText="subject: %s\n%s" % (subject or '[No Subject]', messageText="subject: %s\n%s" % (subject or '[No Subject]',
...@@ -132,7 +133,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -132,7 +133,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
body=messageText) body=messageText)
def scheduledSend(self, messageText, mto=None, mfrom=None, subject=None): def scheduledSend(self, messageText, mto=None, mfrom=None, subject=None):
headers, message = newDecapitate(messageText) headers, message = decapitate(messageText)
if not headers['subject']: if not headers['subject']:
messageText="subject: %s\n%s" % (subject or '[No Subject]', messageText="subject: %s\n%s" % (subject or '[No Subject]',
...@@ -229,48 +230,23 @@ class SendMail: ...@@ -229,48 +230,23 @@ class SendMail:
self.conn.send("quit\n") self.conn.send("quit\n")
self.conn.close() self.conn.close()
def newDecapitate(message, req_headers=['to','from','subject']): def decapitate(message):
blank_re =ts_regex.compile('^[%s]+$' % string.whitespace) # split message into headers / body
header_re=ts_regex.symcomp( mfile=StringIO(message)
'^\(<headerName>[^\0- <>:]+\):\(<headerText>.*\)$', mo=rfc822.Message(mfile)
ts_regex.casefold)
space_re =ts_regex.compile('^[%s]+' % string.whitespace)
# initialize namespace, blank out required headers hd={}
linecount=0; hd={};curHeader={} hd['to']=[]
for rh in req_headers: hd[rh]='' for header in (mo.getaddrlist('to'),
maxwell=map(string.rstrip, string.split(message,'\n')) mo.getaddrlist('cc')):
if not header: continue
for name, addr in header:
hd['to'].append(addr)
for line in maxwell: hd['from']=mo.getaddr('from')[1]
if not line: hd['subject']=mo.getheader('subject') or "No Subject"
break
if blank_re.match(line) >= 0:
break
if header_re.match(line) >= 0:
curHeader=string.lower(header_re.group('headerName'))
hd[curHeader] =\
string.strip(header_re.group('headerText'))
elif space_re.match(line) >= 0:
hd[curHeader] = "%s %s" % (hd[curHeader], line)
linecount=linecount+1
hd['to']=map(string.strip, string.split(hd['to'], ','))
if hd.has_key('cc'):
hd['cc']=map(string.strip,
string.split(hd['cc'], ','))
hd['to']=hd['to'] + hd['cc']
# clean out empty strings
hd['to']=filter(truth, hd['to'])
body=string.join(maxwell[linecount:],'\n')
return hd, body
def decapitate(message, **kw):
#left behind for bw-compatibility
return newDecapitate(message)
return hd, mfile.read()
import __init__ import __init__
__init__.need_license=1 __init__.need_license=1
...@@ -279,6 +255,9 @@ __init__.need_license=1 ...@@ -279,6 +255,9 @@ __init__.need_license=1
#################################################################### ####################################################################
# #
#$Log: MailHost.py,v $ #$Log: MailHost.py,v $
#Revision 1.30 1998/05/07 19:55:27 jeffrey
#started using rfc822 module to parse headers
#
#Revision 1.29 1998/04/30 19:04:49 jeffrey #Revision 1.29 1998/04/30 19:04:49 jeffrey
#first step in some new cleanups. #first step in some new cleanups.
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment