[Zope-Checkins] CVS: Zope/lib/python/Products/MailHost - MailHost.py:1.64.2.1 SendMailTag.py:1.13.2.1 Setup:NONE

Jens Vagelpohl jens@zope.com
Tue, 15 Jan 2002 10:31:06 -0500


Update of /cvs-repository/Zope/lib/python/Products/MailHost
In directory cvs.zope.org:/tmp/cvs-serv5782

Modified Files:
      Tag: Zope-2_5-branch
	MailHost.py SendMailTag.py 
Removed Files:
      Tag: Zope-2_5-branch
	Setup 
Log Message:
Merging changes to the MailHost package into the Zope-2_5-branch



=== Zope/lib/python/Products/MailHost/MailHost.py 1.64 => 1.64.2.1 ===
 __version__ = "$Revision$"[11:-2]
 
-from Globals import Persistent, DTMLFile, HTML, MessageDialog
+from Globals import Persistent, DTMLFile, InitializeClass
 from smtplib import SMTP
 from AccessControl.Role import RoleManager
 from operator import truth
 import Acquisition, sys, string, types, mimetools
 import OFS.SimpleItem, re, quopri, rfc822
-import Globals
 from cStringIO import StringIO
+from AccessControl import ClassSecurityInfo
+from AccessControl.Permissions import view_management_screens, \
+                                      use_mailhost_services
 
 smtpError = "SMTP Error"
 MailHostError = "MailHost Error"
 
 manage_addMailHostForm=DTMLFile('dtml/addMailHost_form', globals())
-def manage_addMailHost(self, id, title='', smtp_host=None,
-                       localhost='localhost', smtp_port=25,
-                       timeout=1.0, REQUEST=None):
+def manage_addMailHost( self, id, title='', smtp_host='localhost'
+                      , localhost='localhost', smtp_port=25
+                      , timeout=1.0, REQUEST=None ):
     ' add a MailHost into the system '
+    i = MailHost( id, title, smtp_host, smtp_port )   #create new mail host
+    self._setObject( id,i )   #register it
 
-    id=str(id)
-    title=str(title)
-    if smtp_host is not None:
-        smtp_host=str(smtp_host)
-    if type(smtp_port) is not type(1):
-        smtp_port=string.atoi(smtp_port)
-
-    i=MailHost()            #create new mail host
-    i.id=id                 #give it id
-    i.title=title           #title
-    i._init(smtp_host=smtp_host, smtp_port=smtp_port)
-
-    self=self.this()
-    self._setObject(id,i)   #register it
     if REQUEST is not None:
         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
 
@@ -58,7 +48,7 @@
     manage=manage_main=DTMLFile('dtml/manageMailHost', globals())
     manage_main._setName('manage_main')
     index_html=None
-    icon='misc_/MailHost/MHIcon'
+    security = ClassSecurityInfo()
 
     timeout=1.0
 
@@ -72,20 +62,22 @@
         +OFS.SimpleItem.Item.manage_options
         )
 
-    __ac_permissions__=(
-        ('View management screens', ('manage','manage_main')),
-        ('Change configuration', ('manage_makeChanges',)),
-        ('Use mailhost services',('',)),
-        )
-   
-    def __init__(self):
-        'nothing yet'
-        pass
 
+    def __init__( self, id='', title='', smtp_host='localhost', smtp_port=25 ):
+        """Initialize a new MailHost instance """
+        self.id = id
+        self.title = title
+        self.smtp_host = str( smtp_host )
+        self.smtp_port = str( smtp_port )
+
+
+    # staying for now... (backwards compatibility)
     def _init(self, smtp_host, smtp_port):
         self.smtp_host=smtp_host
         self.smtp_port=smtp_port
 
+
+    security.declareProtected( 'Change configuration', 'manage_makeChanges' )
     def manage_makeChanges(self,title,smtp_host,smtp_port, REQUEST=None):
         'make the changes'
 
@@ -97,12 +89,15 @@
         self.title=title
         self.smtp_host=smtp_host
         self.smtp_port=smtp_port
-        if REQUEST: return MessageDialog(
-            title  ='Changed %s' % self.__name__,
-            message='%s has been updated' % self.id,
-            action =REQUEST['URL2']+'/manage_main',
-            target ='manage_main')
+        if REQUEST is not None:
+            msg = 'MailHost %s updated' % self.id
+            return self.manage_main( self
+                                   , REQUEST
+                                   , manage_tabs_message=msg
+                                   )
     
+
+    security.declareProtected( use_mailhost_services, 'sendTemplate' )
     def sendTemplate(trueself, self, messageTemplate, 
                      statusTemplate=None, mto=None, mfrom=None,
                      encode=None, REQUEST=None):
@@ -117,8 +112,8 @@
             if not headers.has_key(requiredHeader):
                 raise MailHostError,"Message missing SMTP Header '%s'"\
                       % requiredHeader
-        mailserver = SMTP(trueself.smtp_host, trueself.smtp_port)
-        mailserver.sendmail(headers['from'], headers['to'], messageText)
+
+        self._send( headers, messageText )
 
         if not statusTemplate: return "SEND OK"
 
@@ -128,6 +123,8 @@
         except:
             return "SEND OK"
 
+
+    security.declareProtected( use_mailhost_services, 'send' )
     def send(self, messageText, mto=None, mfrom=None, subject=None,
              encode=None):
         headers = extractheaders(messageText)
@@ -155,16 +152,16 @@
                 raise MailHostError,"Message missing SMTP Header '%s'"\
                 % requiredHeader
         messageText=_encode(messageText, encode)
-        smtpserver = SMTP(self.smtp_host, self.smtp_port)
-        smtpserver.sendmail(headers['from'],headers['to'], messageText)
 
+        self._send( headers, messageText )
 
+
+    security.declareProtected( use_mailhost_services, 'scheduledSend' )
     def scheduledSend(self, messageText, mto=None, mfrom=None, subject=None,
                       encode=None):
         """Looks like the same function as send() - scheduledSend() is nowhere 
         used in Zope. No idea if it is still needed/used (ajung)
         """
-
         headers = extractheaders(messageText)
 
         if not headers['subject']:
@@ -182,17 +179,30 @@
                 raise MailHostError,"Message missing SMTP Header '%s'"\
                 % requiredHeader
         messageText=_encode(messageText, encode)
-        smtpserver = SMTP(self.smtp_host, self.smtp_port)
-        smtpserver.sendmail(headers['from'], headers['to'], messageText)
 
+        self._send( headers, messageText )
+
+
+    security.declareProtected( use_mailhost_services, 'simple_send' )
     def simple_send(self, mto, mfrom, subject, body):
         body="from: %s\nto: %s\nsubject: %s\n\n%s" % (
             mfrom, mto, subject, body)
-        mailserver = SMTP(self.smtp_host, self.smtp_port)
-        mailserver.sendmail(mfrom, mto, body)
+        headers = {}
+        headers['from'] = mfrom
+        headers['to'] = mto
+
+        self._send( headers, body )
+
+
+    security.declarePrivate( '_send' )
+    def _send( self, headers, body ):
+        """ Send the message """
+        smtpserver = SMTP( self.smtp_host, int( self.smtp_port ) )
+        smtpserver.sendmail( headers['from'], headers['to'], body )
+        smtpserver.quit()
 
         
-Globals.default__class_init__(MailBase)
+InitializeClass( MailBase )
 
 class MailHost(Persistent, MailBase):
     "persistent version"


=== Zope/lib/python/Products/MailHost/SendMailTag.py 1.13 => 1.13.2.1 ===
             mhost=md[self.mailhost]
         elif self.smtphost:
-            mhost=MailBase()
-            mhost._init(self.smtphost, self.port)
+            mhost=MailBase( smtp_host=self.smtphost, smtp_port=self.port )
 
         mhost.send(render_blocks(self.section.blocks, md),
                    self.mailto, self.mailfrom,

=== Removed File Zope/lib/python/Products/MailHost/Setup ===