Commit ceb9dd9c authored by Jeffrey Shell's avatar Jeffrey Shell

first step in some new cleanups.

parent 4b9c9a7d
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
from Globals import Persistent, HTMLFile, HTML, MessageDialog 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
import Acquisition, sys, regex, string, types from operator import truth
import Acquisition, sys, ts_regex, string, types
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
#$Id: MailHost.py,v 1.28 1998/04/22 20:43:03 jeffrey Exp $ #$Id: MailHost.py,v 1.29 1998/04/30 19:04:49 jeffrey Exp $
__version__ = "$Revision: 1.28 $"[11:-2] __version__ = "$Revision: 1.29 $"[11:-2]
smtpError = "SMTP Error" smtpError = "SMTP Error"
MailHostError = "MailHost Error" MailHostError = "MailHost Error"
...@@ -37,11 +38,9 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -37,11 +38,9 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
timeout=1.0 timeout=1.0
manage_options=({'icon':'', 'label':'Edit', manage_options=({'icon':'', 'label':'Edit',
'action':'manage_main', 'target':'manage_main', 'action':'manage_main', 'target':'manage_main'},
},
{'icon':'', 'label':'Security', {'icon':'', 'label':'Security',
'action':'manage_access', 'target':'manage_main', 'action':'manage_access', 'target':'manage_main'},
},
) )
__ac_permissions__=( __ac_permissions__=(
...@@ -86,7 +85,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -86,7 +85,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
headers, message = newDecapitate(messageText) headers, message = newDecapitate(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', 'subject'): for requiredHeader in ('to', 'from'):
if not headers.has_key(requiredHeader): if not headers.has_key(requiredHeader):
raise MailHostError,"Message missing SMTP Header '%s'"\ raise MailHostError,"Message missing SMTP Header '%s'"\
% requiredHeader % requiredHeader
...@@ -96,7 +95,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -96,7 +95,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
(trueself.smtpHost, trueself.smtpPort, (trueself.smtpHost, trueself.smtpPort,
trueself.localHost, trueself.timeout, trueself.localHost, trueself.timeout,
headers['from'], headers['to'], headers['from'], headers['to'],
headers['subject'], messageText headers['subject'] or 'No Subject', messageText
), ),
threadsafe=1 threadsafe=1
)) ))
...@@ -110,38 +109,51 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -110,38 +109,51 @@ 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):
if subject: messageText="subject: %s\n%s" % (subject, messageText)
headers, message = newDecapitate(messageText) headers, message = newDecapitate(messageText)
if not headers['subject']:
messageText="subject: %s\n%s" % (subject or '[No Subject]',
messageText)
if mto: if mto:
if type(mto) is type('s'): if type(mto) is type('s'):
mto=map(string.strip, string.split(mto,',')) mto=map(string.strip, string.split(mto,','))
headers['to'] = mto headers['to'] = filter(truth, mto)
if mfrom: headers['from'] = mfrom if mfrom:
for requiredHeader in ('to', 'from', 'subject'): headers['from'] = mfrom
for requiredHeader in ('to', 'from'):
if not headers.has_key(requiredHeader): if not headers.has_key(requiredHeader):
raise MailHostError,"Message missing SMTP Header '%s'"\ raise MailHostError,"Message missing SMTP Header '%s'"\
% requiredHeader % requiredHeader
SendMail(self.smtpHost, self.smtpPort, self.localHost, sm=SendMail(self.smtpHost, self.smtpPort, self.localHost, self.timeout)
self.timeout).send( sm.send(mfrom=headers['from'], mto=headers['to'],
mfrom=headers['from'], mto=headers['to'], subj=headers['subject'] or 'No Subject',
subj=headers['subject'], body=messageText) body=messageText)
def scheduledSend(self, messageText, mto=None, mfrom=None, subject=None): def scheduledSend(self, messageText, mto=None, mfrom=None, subject=None):
if subject: messageText="subject: %s\n%s" % (subject, messageText)
headers, message = newDecapitate(messageText) headers, message = newDecapitate(messageText)
if mto: headers['to'] = mto
if mfrom: headers['from'] = mfrom if not headers['subject']:
for requiredHeader in ('to', 'from', 'subject'): messageText="subject: %s\n%s" % (subject or '[No Subject]',
if not headers.has_key(requiredHeader): messageText)
raise MailHostError,"Message missing SMTP Header '%s'"\ if mto:
% requiredHeader if type(mto) is type('s'):
mto=map(string.strip, string.split(mto,','))
headers['to'] = filter(truth, mto)
if mfrom:
headers['from'] = mfrom
for requiredHeader in ('to', 'from'):
if not headers.has_key(requiredHeader):
raise MailHostError,"Message missing SMTP Header '%s'"\
% requiredHeader
Globals.Scheduler.schedule(OneTimeEvent( Globals.Scheduler.schedule(OneTimeEvent(
Send, Send,
(self.smtpHost, self.smtpPort, self.localHost, self.timeout, (self.smtpHost, self.smtpPort, self.localHost, self.timeout,
headers['from'], headers['to'], headers['from'], headers['to'],
headers['subject'], messageText headers['subject'] or 'No Subject', messageText
), ),
threadsafe=1 threadsafe=1
)) ))
...@@ -197,7 +209,7 @@ class SendMail: ...@@ -197,7 +209,7 @@ class SendMail:
% (code, line) % (code, line)
return 1 return 1
def send(self, mfrom, mto, subj, body): def send(self, mfrom, mto, subj='No Subject', body='Blank Message'):
self.conn.send("mail from:<%s>\n" % mfrom) self.conn.send("mail from:<%s>\n" % mfrom)
self._check() self._check()
if type(mto) in [types.ListType, types.TupleType]: if type(mto) in [types.ListType, types.TupleType]:
...@@ -217,33 +229,43 @@ class SendMail: ...@@ -217,33 +229,43 @@ class SendMail:
self.conn.send("quit\n") self.conn.send("quit\n")
self.conn.close() self.conn.close()
def newDecapitate(message): def newDecapitate(message, req_headers=['to','from','subject']):
blank_re =regex.compile('^[%s]+$' % string.whitespace) blank_re =ts_regex.compile('^[%s]+$' % string.whitespace)
header_re=regex.symcomp('^\(<headerName>[^\0- <>:]+\):\(<headerText>.*\)$') header_re=ts_regex.symcomp(
space_re =regex.compile('^[%s]+' % string.whitespace) '^\(<headerName>[^\0- <>:]+\):\(<headerText>.*\)$',
ts_regex.casefold)
space_re =ts_regex.compile('^[%s]+' % string.whitespace)
linecount=0; headerDict={};curHeader={} # initialize namespace, blank out required headers
maxwell=map(lambda x: string.rstrip(x),string.split(message,'\n')) linecount=0; hd={};curHeader={}
for rh in req_headers: hd[rh]=''
maxwell=map(string.rstrip, string.split(message,'\n'))
for line in maxwell: for line in maxwell:
if not line: break if not line:
if blank_re.match(line) >= 0: break break
if header_re.match(line) >=0: if blank_re.match(line) >= 0:
break
if header_re.match(line) >= 0:
curHeader=string.lower(header_re.group('headerName')) curHeader=string.lower(header_re.group('headerName'))
headerDict[curHeader] =\ hd[curHeader] =\
string.strip(header_re.group('headerText')) string.strip(header_re.group('headerText'))
elif space_re.match(line)>=0: elif space_re.match(line) >= 0:
headerDict[curHeader] = "%s %s" % (headerDict[curHeader], line) hd[curHeader] = "%s %s" % (hd[curHeader], line)
linecount=linecount+1 linecount=linecount+1
if headerDict.has_key('to'): hd['to']=map(string.strip, string.split(hd['to'], ','))
headerDict['to']=map(
lambda x: string.strip(x), if hd.has_key('cc'):
string.split(headerDict['to'], ',') 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') body=string.join(maxwell[linecount:],'\n')
return headerDict, body return hd, body
def decapitate(message, **kw): def decapitate(message, **kw):
#left behind for bw-compatibility #left behind for bw-compatibility
...@@ -257,6 +279,9 @@ __init__.need_license=1 ...@@ -257,6 +279,9 @@ __init__.need_license=1
#################################################################### ####################################################################
# #
#$Log: MailHost.py,v $ #$Log: MailHost.py,v $
#Revision 1.29 1998/04/30 19:04:49 jeffrey
#first step in some new cleanups.
#
#Revision 1.28 1998/04/22 20:43:03 jeffrey #Revision 1.28 1998/04/22 20:43:03 jeffrey
#comma-delimeted strings can now be sent to the .send() method for the #comma-delimeted strings can now be sent to the .send() method for the
#'mto' parameter. MailHost appropriately breaks them into multiple #'mto' parameter. MailHost appropriately breaks them into multiple
......
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