[Zope-Checkins] CVS: Zope3/lib/python/Zope/Server/SMTP - ISMTPCommandHandler.py:1.1.2.1 SMTPServer.py:1.1.2.1 SMTPServerChannel.py:1.1.2.1 SMTPSpamFilter.py:1.1.2.1 SMTPStatusMessages.py:1.1.2.1 SMTPUtilities.py:1.1.2.1 SpamData.py:1.1.2.1 __init__.py:1.1.2.1

Stephan Richter srichter@cbu.edu
Fri, 5 Apr 2002 12:19:54 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Server/SMTP
In directory cvs.zope.org:/tmp/cvs-serv18691/SMTP

Added Files:
      Tag: Zope3-Server-Branch
	ISMTPCommandHandler.py SMTPServer.py SMTPServerChannel.py 
	SMTPSpamFilter.py SMTPStatusMessages.py SMTPUtilities.py 
	SpamData.py __init__.py 
Log Message:
First cut of SMTP. Same as for POP3. I have only transcribed shick! code to
our model. Since the shiks! code depends on SQL, there is quiet a bit of 
stuff to write to make it work for us. 
But at least all commands and status messages are covered now.


=== Added File Zope3/lib/python/Zope/Server/SMTP/ISMTPCommandHandler.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: ISMTPCommandHandler.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""

from Interface import Interface

class ISMTPCommandHandler(Interface):
    """This interface defines all the SMTP commands that are supported by the
       server.

       Every command takes the args as first arguments, since it is
       responsible for parsing the rest of the input (which is usually
       easy).
    """

    def cmd_data(args):
        """DATA (DATA)

        The receiver treats the lines following the command as mail
        data from the sender.  This command causes the mail data
        from this command to be appended to the mail data buffer.
        The mail data may contain any of the 128 ASCII character
        codes.

        The mail data is terminated by a line containing only a
        period, that is the character sequence "<CRLF>.<CRLF>" (see
        Section 4.5.2 on Transparency).  This is the end of mail
        data indication.

        The end of mail data indication requires that the receiver
        must now process the stored mail transaction information.
        This processing consumes the information in the reverse-path
        buffer, the forward-path buffer, and the mail data buffer,
        and on the completion of this command these buffers are
        cleared.  If the processing is successful the receiver must
        send an OK reply.  If the processing fails completely the
        receiver must send a failure reply.

        When the receiver-SMTP accepts a message either for relaying
        or for final delivery it inserts at the beginning of the
        mail data a time stamp line.  The time stamp line indicates
        the identity of the host that sent the message, and the
        identity of the host that received the message (and is
        inserting this time stamp), and the date and time the
        message was received.  Relayed messages will have multiple
        time stamp lines.

        When the receiver-SMTP makes the "final delivery" of a
        message it inserts at the beginning of the mail data a
        return path line.  The return path line preserves the
        information in the <reverse-path> from the MAIL command.
        Here, final delivery means the message leaves the SMTP
        world.  Normally, this would mean it has been delivered to
        the destination user, but in some cases it may be further
        processed and transmitted by another mail system.

           It is possible for the mailbox in the return path be
           different from the actual sender's mailbox, for example,
           if error responses are to be delivered a special error
           handling mailbox rather than the message senders.

        The preceding two paragraphs imply that the final mail data
        will begin with a  return path line, followed by one or more
        time stamp lines.  These lines will be followed by the mail
        data header and body [2].  See Example 8.

        Special mention is needed of the response and further action
        required when the processing following the end of mail data
        indication is partially successful.  This could arise if
        after accepting several recipients and the mail data, the
        receiver-SMTP finds that the mail data can be successfully
        delivered to some of the recipients, but it cannot be to
        others (for example, due to mailbox space allocation
        problems).  In such a situation, the response to the DATA
        command must be an OK reply.  But, the receiver-SMTP must
        compose and send an "undeliverable mail" notification
        message to the originator of the message.  Either a single
        notification which lists all of the recipients that failed
        to get the message, or separate notification messages must
        be sent for each failed recipient (see Example 7).  All
        undeliverable mail notification messages are sent using the
        MAIL command (even if they result from processing a SEND,
        SOML, or SAML command)."""


    def cmd_ehlo(args):
        """Extended Greeting.
        
           See http://www.faqs.org/rfcs/rfc2821.html for details
        """

    def cmd_expn(args):
        """EXPAND (EXPN)

        This command asks the receiver to confirm that the argument
        identifies a mailing list, and if so, to return the
        membership of that list.  The full name of the users (if
        known) and the fully specified mailboxes are returned in a
        multiline reply.

        This command has no effect on any of the reverse-path
        buffer, the forward-path buffer, or the mail data buffer.
        """

    def cmd_helo(args):
        """HELLO (HELO)

        This command is used to identify the sender-SMTP to the
        receiver-SMTP.  The argument field contains the host name of
        the sender-SMTP.

        The receiver-SMTP identifies itself to the sender-SMTP in
        the connection greeting reply, and in the response to this
        command.

        This command and an OK reply to it confirm that both the
        sender-SMTP and the receiver-SMTP are in the initial state,
        that is, there is no transaction in progress and all state
        tables and buffers are cleared.
        """

    def cmd_help(args):
        """HELP (HELP)

        This command causes the receiver to send helpful information
        to the sender of the HELP command.  The command may take an
        argument (e.g., any command name) and return more specific
        information as a response.

        This command has no effect on any of the reverse-path
        buffer, the forward-path buffer, or the mail data buffer.
        """

    def cmd_mail(args):
        """MAIL (MAIL)

        MAIL FROM:<reverse-path> <CRLF>

        This command is used to initiate a mail transaction in which
        the mail data is delivered to one or more mailboxes.  The
        argument field contains a reverse-path.

        The reverse-path consists of an optional list of hosts and
        the sender mailbox.  When the list of hosts is present, it
        is a "reverse" source route and indicates that the mail was
        relayed through each host on the list (the first host in the
        list was the most recent relay).  This list is used as a
        source route to return non-delivery notices to the sender.
        As each relay host adds itself to the beginning of the list,
        it must use its name as known in the IPCE to which it is
        relaying the mail rather than the IPCE from which the mail
        came (if they are different).  In some types of error
        reporting messages (for example, undeliverable mail
        notifications) the reverse-path may be null (see Example 7).

        This command clears the reverse-path buffer, the
        forward-path buffer, and the mail data buffer; and inserts
        the reverse-path information from this command into the
        reverse-path buffer.
        """

    def cmd_noop(args):
        """NOOP (NOOP)

        This command does not affect any parameters or previously
        entered commands.  It specifies no action other than that
        the receiver send an OK reply.

        This command has no effect on any of the reverse-path
        buffer, the forward-path buffer, or the mail data buffer.
        """
        
    def cmd_quit(args):
        """QUIT (QUIT)

        This command specifies that the receiver must send an OK
        reply, and then close the transmission channel.

        The receiver should not close the transmission channel until
        it receives and replies to a QUIT command (even if there was
        an error).  The sender should not close the transmission
        channel until it send a QUIT command and receives the reply
        (even if there was an error response to a previous command).
        If the connection is closed prematurely the receiver should
        act as if a RSET command had been received (canceling any
        pending transaction, but not undoing any previously
        completed transaction), the sender should act as if the
        command or transaction in progress had received a temporary
        error (4xx).
        """

    def cmd_rcpt(args):
        """RECIPIENT (RCPT)

        This command is used to identify an individual recipient of
        the mail data; multiple recipients are specified by multiple
        use of this command.

        The forward-path consists of an optional list of hosts and a
        required destination mailbox.  When the list of hosts is
        present, it is a source route and indicates that the mail
        must be relayed to the next host on the list.  If the
        receiver-SMTP does not implement the relay function it may
        user the same reply it would for an unknown local user
        (550).

        When mail is relayed, the relay host must remove itself from
        the beginning forward-path and put itself at the beginning
        of the reverse-path.  When mail reaches its ultimate
        destination (the forward-path contains only a destination
        mailbox), the receiver-SMTP inserts it into the destination
        mailbox in accordance with its host mail conventions.

        For example, mail received at relay host A with arguments

              FROM:<USERX@HOSTY.ARPA>
              TO:<@HOSTA.ARPA,@HOSTB.ARPA:USERC@HOSTD.ARPA>

           will be relayed on to host B with arguments

              FROM:<@HOSTA.ARPA:USERX@HOSTY.ARPA>
              TO:<@HOSTB.ARPA:USERC@HOSTD.ARPA>.

        This command causes its forward-path argument to be appended
        to the forward-path buffer.
        """

    def cmd_rset(args):
        """RESET (RSET)

        This command specifies that the current mail transaction is
        to be aborted.  Any stored sender, recipients, and mail data
        must be discarded, and all buffers and state tables cleared.
        The receiver must send an OK reply.
        """

    def cmd_vrfy(args):
        """VERIFY (VRFY)

        This command asks the receiver to confirm that the argument
        identifies a user.  If it is a user name, the full name of
        the user (if known) and the fully specified mailbox are
        returned.

        This command has no effect on any of the reverse-path
        buffer, the forward-path buffer, or the mail data buffer.
        """


=== Added File Zope3/lib/python/Zope/Server/SMTP/SMTPServer.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: SMTPServer.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""
import asyncore
from FTPServerChannel import FTPServerChannel
from Zope.Server.ServerBase import ServerBase
from Zope.Server.Counter import Counter

from Zope.Server.VFS.UnixFileSystem import UnixFileSystem 
from Zope.Server.Authentication.DictionaryAuthentication import \
     DictionaryAuthentication


class SMTPServer(ServerBase):
    """Generic FTP Server"""
    
    channel_class = SMTPServerChannel
    SERVER_IDENT = 'Zope.Server.SMTPServer'


    relay_smtp_server_name = 'mail.cbu.edu'
    storage = UnixFileSystem('/opt/ZopeMail')
    auth_source = DictionaryAuthentication({'foo': 'bar'})
    refresh_relay_rules = 1
    relay_rules = []
    allow_unknown_receiver_default = 1
    allow_unknown_sender_default = 1
    admin_account = ""
    default_local_domain = ""
    unknown_account = ""
    ip_address_range = None
    

    def __init__(self, ip, port, task_dispatcher=None, adj=None, start=1,
                 hit_log=None, verbose=0, socket_map=None):
        super(SMTPServer, self).__init__(ip, port, task_dispatcher,
                                         adj, start, hit_log,
                                         verbose, socket_map)
        
        self.counter = Counter()
        

if __name__ == '__main__':
    from Zope.Server.TaskThreads import ThreadedTaskDispatcher
    td = ThreadedTaskDispatcher()
    td.setThreadCount(4)
    SMTPServer('', 8025, task_dispatcher=td)
    try:
        while 1:
            asyncore.poll(5)
            print 'active channels:', SMTPServerChannel.active_channels
    except KeyboardInterrupt:
        print 'shutting down...'
        td.shutdown()


=== Added File Zope3/lib/python/Zope/Server/SMTP/SMTPServerChannel.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: SMTPServerChannel.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""

from Zope.Server.ServerChannelBase import ServerChannelBase
from SMTPimport status_msgs
from SMTPTask import SMTPTask

from ISMTPCommandHandler import ISMTPCommandHandler 


class SMTPServerChannel(LineServerChannel):
    """The SMTP Server Channel represents a connection to a particular
       client. We can therefore store information here."""

    __implements__ = ISMTPCommandHandler

    # Commands that are run in a separate thread
    thread_commands = ('cmd_mail', 'cmd_vrfy', 'cmd_data')

    # Define the authentication status of the channel. Note that only the
    # "special commands" can be executed without having authenticated.
    authenticated = 1

    # Define the reply code for an unrecognized command
    unknown_reply = (500, 0)

    # Define the status messages
    status_messages = status_msgs


    def __init__(self, server, conn, addr, adj=None, socket_map=None):
        super(SMTPServerChannel, self).__init__(server, conn, addr,
                                               adj, socket_map)
        
        self._sender_host = None

        self.terminated = 0
        self.receiving_data = 0
        
        self.reply(220, 0, self.server.server_name)


    ############################################################
    # Implementation methods for interface
    # Zope.Server.SMTP.ISMTPCommandHandler

    def cmd_data(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'

        if not self.is_local_connection and not self.canSendMessage():
            self.terminated = 1
            return self.reply(551)

        self.receiving_data = 1
        self.reply(354)

    def cmd_ehlo(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        return cmd_helo(args)

    def cmd_expn(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        self.reply(550)

    def cmd_helo(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        args = args.strip()
        if len(args):
            self.sender_host = args
        self.reply(250)

    def cmd_help(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        self.reply(214)

    def cmd_mail(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        args = args.split()
        if args[1].upper() != 'FROM:':
            return self.reply(501)

        if not self.isSenderAllowed(args[2]):
            return self.reply(551)

        self.message["FROM"] = request[2]
        self.reply(250)

    def cmd_noop(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        self.reply(250)

    def cmd_quit(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        self.reply(221)
        self.close_when_done()

    def cmd_rcpt(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        args = args.split()
        if args[1].upper() != 'TO:':
            return self.reply(501)

        if not self.isReceiverAllowed(args[2]):
            return self.reply(551)

        self.message["TO"].append(request[2])
        self.reply(250)

    def cmd_rset(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        self.resetMessage()
        self.reply(250)
        

    def cmd_vrfy(self, args):
        'See Zope.Server.SMTP.ISMTPCommandHandler.ISMTPCommandHandler'
        name = self.getExpandedName(args)
        if name:
            self.reply(250, 1, name)
        else:
            self.reply(550, 1)

    #
    ############################################################


    def resetMessage(self):        
        self.message = {'FROM':None, 'TO': [], 'MSG': []}
        

    def getExpandedName(self,name):
        # THIS HAS NOT YET BEEN TESTED :)
        
        if name[0] == '<' and name[-1] == '>':
            name = name[1:-1]
        
        # step 1: look up address directly"
        result = self.getUID()
        
        if result:
            userid = result[0][0]
            # ok, its a directly known address
            friendlyname = self.getFriendlyName(result)
            if friendlyname:
                return "%s <%s>" % (friendlyname[0][0], name)
            print "Warning, didn't find user for id %s" % str(userid)
            return "<%s>" % name

        # step 2: look up aliases
        result = 1
        # maildb.DbQuery("SELECT AID FROM aliases WHERE NAME='%s'" % name)

        if result:
            addressid = result
            result = '2', 'srichter@cbu.edu'
            # maildb.DbQuery("SELECT UID,MAIL FROM addresses
            # WHERE ID=%d" % addressid)
            
            if result:
                userid = result[0][0]
                mailaddr = result[0][1]
                
                # ok, its a directly known address
                friendlyname = 'Stephan Richter'
                # maildb.DbQuery("SELECT FRIENDLYNAME FROM users
                # WHERE ID=%d" % userid)
                if friendlyname:
                    return "%s <%s>" % (friendlyname,mailaddr)

                print "Warning, didn't find user for id %s" % str(userid)

            return "%s <%s>" % (name,mailaddr)


    def isReceiverAllowed(self,address):
        global RELAY_RULES, ALLOW_UNKNOWN_RECEIVER_DEFAULT

        if address[0] == '<' or address[-1] == '>':
            address = address[1:-1]

        # New in Version 0.4: If the sender is a local address, it must
        # come from a local IP, otherwise access is denied
        if (self.strict_relay_test and self.ip_address_range and
            not self.is_local_connection):
            is_local_address = 1

            # maildb.DbQuery("SELECT COUNT(*) FROM addresses WHERE "\
            # "UID<>-1 AND MAIL='%s'" %
            # maildb.EscapeSqlString(address))[0]

            if is_local_address[0]:
                print "Target address '%s' is local." % address
            else:
                print "Attempt to send message from '%s' to '%s' denied." % (
                    str(self.message["FROM"]), address)
                self.SendAdminMail(None, """Warning, an attempt has been
                made to send a message from '%s' to '%s'. The access
                has been denied.""" % (str(self.message["FROM"]), address))
                return 0
        
        self.RefreshRelayRules()
        
        # set default
        allow = ALLOW_UNKNOWN_RECEIVER_DEFAULT
        
        # enumerate all rules. they are priority-sorted, with higher
        # priorities comming later
        for rule in RELAY_RULES:
            
            # if this rule applies to the given address
            if fnmatch.fnmatch(address,rule[0]):
                
                # let it apply. further rules will modify this. 
                allow = rule[2]
                
                print "Rule '%s' applies to '%s': %s receiving." % (
                    rule,address,allow and "allow" or "deny")

                if not allow:
                    # self.SendAdminMail(None, """Warning, an attempt
                    # has been made to send a message from '%s' to
                    # '%s'. The domain '%s' is disallowed on this
                    # host. The access has been denied.""" %
                    # (str(self.message["FROM"]), address, rule))
                    return 0

        return allow


    def isSenderAllowed(self,address):

        if address[0] == '<' or address[-1] == '>':
            address = address[1:-1]

        # New in Version 0.4: If the sender is a local address, it must
        # come from a local IP, otherwise access is denied
        if (self.strict_relay_test and self.ip_address_range and
            not self.is_local_connection):
            # XXX: Is local address?
            is_local_address = 1 # self.isLocalAccess()

            if is_local_address[0]:
                print ("Error, sender '%s' has a local e-mail address, but " +
                       "comes from a remote IP '%s'" % (
                    address, self.ip_address_string))
                # self.SendAdminMail(None, """Warning, an attempt has
                # been made to send a message from IP '%s' with the
                # local e-mail address '%s'. The access has been
                # denied.""" % (self.ip_address_string, address))
                return 0
            else:
                # print "Sender '%s' is not a known local address, so
                # use of remote IP "\ "'%s' is possible." % (address,
                # self.ip_address_string)
                pass

        self.refreshRelayRules()
        
        # set default
        allow = self.allow_unknown_sender_default
        
        # enumerate all rules. they are priority-sorted, with higher
        # priorities comming later
        for rule in self.relay_rules:
           
            # if this rule applies to the given address
            if fnmatch.fnmatch(address,rule[0]):
                
                # let it apply. further rules will modify this. 
                allow = rule[1]
                
                # print "Rule '%s' applies to '%s': %s sending." %
                # (rule,address,allow and "allow" or "deny")

                if not allow:
                    # self.SendAdminMail(None, """Warning, an attempt
                    # has been made to send a message from '%s' to
                    # '%s'. The domain '%s' is disallowed on this
                    # host. The access has been denied.""" %
                    # (str(self.message["FROM"]), address, rule))
                    return 0

        return allow


    def refreshRelayRules(self):
        if self.refresh_relay_rules or not self.relay_rules:
            self.relay_rules = maildb.DbQuery("SELECT PATTERN,\
            CANSEND, CANRECEIVE FROM relayrules ORDER BY PRIO")


    def canSendMessage(self):
        # step 1: test if this is a message from a local user, or from
        # an outside user
        sender_id, sender_cansend = self.getAddressID(self.message['FROM'])

        # if the sender is a local mailbox, the message can be sent
        if sender_id and sender_cansend:
            return 1

        # it is a message from somebody outside
        can_process_message, remote_receivers = 1, 0
        
        for receiver in self.message['TO']:
            rid, rcs = self.getAddressID(receiver)

            if self.strict_relay_test:
                # if strict testing is enabled, ALL receivers must be local
                if not rid:
                    return 0

                continue

            elif rid:
                # if strict testing is disabled, at least one reciever
                # must be local
                return 1

            remote_receivers = 1

        if remote_receivers:
            can_process_message = 0
            assert (not self.strict_relay_test)

        return can_process_message


    def getAddressID(self,address):
        if address[0] == '<' or address[-1] == '>':
            address = address[1:-1]

        # GK wegen der DVG
        if address.find("'") >= 0:
            address = maildb.EscapeSqlString(address)

        row = maildb.DbQuery("SELECT UID FROM addresses WHERE MAIL='%s'" %
                             address )
        if len(row):
            if row[0][0] == -1:
                # this is an imported address to validate, but not to
                # indicate a local user

                return (0,0)
            return (row[0][0],1)

        if DomainOfAddress(address).lower() == DEFAULT_LOCAL_DOMAIN.lower():
            row = maildb.DbQuery("SELECT UID FROM addresses WHERE MAIL='%s'"
                                 % UNKNOWN_ACCOUNT)
            if len(row):
                return (row[0][0],1)          

        return (0,0)


=== Added File Zope3/lib/python/Zope/Server/SMTP/SMTPSpamFilter.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: SMTPSpamFilter.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""

import spam_data

def isSpamSubjectLine(subject):
    subject = subject.lower()
    reload(spam_data)

    weight = float(reduce(lambda x,y:x+y,map(ord,subject)))/len(subject)
    if weight > 128:
        print "Weight %.2f indicates spam mail." % weight
        return 1

    for token_tuple in spam_data.subject_tokens:
        found = 0
        for token in token_tuple:
            if subject.find(token) >= 0:
                found += 1
        if found == len(token_tuple):
            print "Tokens '%s' indicate spam mail." % str(token_tuple).strip()
            return 1

    tokens = subject.split()
    try:
        isdigit = int(tokens[-1])
        isdigit = len(tokens[-1]) > 3
    except:
        isdigit = 0
    if isdigit:
        print "Integer as last token indicates spam mail."
        return 1

    return 0


def checkSpamMail(lines):
    index = -1
    for line in lines:
        index += 1
        if line.strip() == "": break

        s = line.find(':')
        if s < 0: continue
        
        tokens = (line[:s],line[s:])
        keyword = tokens[0].lower()
        if keyword == 'subject':
            if IsSpamSubjectLine(tokens[1]):
                print "SPAM SUBJECT: "+tokens[1].strip()
                new_subject_line = line[:9] + "[SPAM] " + line[9:]
                lines[index] = new_subject_line
            else:
                print "NOT SPAM SUBJECT: "+ tokens[1].strip()
            break
    return lines


=== Added File Zope3/lib/python/Zope/Server/SMTP/SMTPStatusMessages.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: SMTPStatusMessages.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""


status_msgs = {
    214: ("Help not available. RTFM!",),
    220: ("Zope Service ready",),
    221: ("SHICKS! Service closing transmission channel",),
    250: ('OK',
          '%s',              # Username and location
          'Zope 3.0 ready.', # =
          'SIZE',            # |
          'VRFY',            # |
          'HELP',),          # +--> These are all for HELO
    251: ("User not local; will forward to <forward-path>",),
    354: ("Start mail input; end with <CRLF>.<CRLF>",),
    421: ("Zope Service not available",),
    450: ("Requested mail action not taken: mailbox unavailable",),
    500: ('Syntax error, command unrecognized"',),
    501: ("Syntax error in parameters or arguments",),
    502: ("Command not implemented",),
    503: (" Bad sequence of commands",),
    504: ("Command parameter not implemented",),
    550: (" Requested action not taken: mailbox unknown",
          'String does not match anything.'),
    551: ("ACCESS DENIED.",),  
    554: ("Transaction failed",),
    }



=== Added File Zope3/lib/python/Zope/Server/SMTP/SMTPUtilities.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: SMTPUtilities.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""

def getLongIpAddress(ipaddr):
    """ """
    tokens = ipaddr.split(".")

    if len(tokens) != 4:
        raise "ERROR, IP-Address '%s' invalid" % ipaddr

    tokens = map(long,tokens)

    return ( tokens[0] * (256 * 256 * 256) + tokens[1] * (256 * 256) +
             tokens[2] * (256) + tokens[3] )


def decodeValidIpRanges(data):
    """ """
    data = data.split(",")

    for i in xrange(len(data)):
        item = map(GetLongIpAddress,data[i].split("-"))
        if len(item) == 1:
            item.append(item[0])
        elif len(item) > 2:
            raise "ERROR, IP-Addressrange '%s' invalid" % data[i]
        data[i] = tuple(item)
    return tuple(data)


def domainOfAddress(address):
    x = address.find('@')
    if x >= 0:
        return address[x+1:]
    return ""


def splitMailHeader(s):
    result = []
    startindex = -1
    index = 0
    while index < len(s):
        c = s[index]
        if c == ' ':
            # ok, split
            if startindex != -1:
                result.append(s[startindex:index])
                startindex = -1
        elif c == ':':
            # ok, split
            if startindex != -1:
                result.append(s[startindex:index+1])
                startindex = -1
            # ok, split *including* this character
        elif startindex == -1:
            startindex = index
        index += 1
    if startindex != -1:
        result.append(s[startindex:])
    return result

import spam_data


=== Added File Zope3/lib/python/Zope/Server/SMTP/SpamData.py ===
subject_tokens = [ ["!!",], ["!","free"], ["!","your"], ["!","you"], ["hosting"], ["money"], ["business"],
                   ["adult"], ["your","site"], ["-","$"], ["only",":"], ["!","website"], ["advertisement"],
                   ["software","free"],[".","$"],["your","web"],["!","this"],["link"],["search"],["home"],["amateur"],
                   ["more"],["engines"],["!","get"],["cash"],["marketing"],["sex"],["$$"],["!",".."],[".","free"],
                   ["!","software"],["million"],[":","ad"],["accept"],["cards"],["your site"],["webmaster"],
                   ["your","?"],["to","$"],["message"],["scouting"],["get","your"],["live"],["phone"],["!","site"],
                   ["your","free"],["!","now"],["send"],["$","free"],["-",".."],["mlm"],["advertising"],["you","..."],
                   ["your","website"],["!","new"],["$$$"],["....."],["do","you"],["??"],["!","hot"],["you","i"],["offer"],
                   ["your","traffic"],["your","$"],["in","$"],["don't"],["increase"],["know"],["per"],["income"],
                   ["fuckin"],["stock","alert"],["fortune"],["babes","love"],["hardcore"],["hello!"],["christmas"],
                   ["*~*~*"],["equities"],["lesbian"],["huge","cock"],["pussy"],["investment"],["vhs","dvd"],
                   ["growth","potential"],["crazy"],["$$$"],["investor","alert"],["tax","break"],["web","design"],
                   ["geschlecht"],["download"],["dildo"],["yen","market"],["prescription"],["adv:"],["adv."],
                   ["lolitas"],["domain"],["national"],["guide"],["celebrities"],["xxx"],["lawyer"],["great"],
                   ["huge"],["need","help"],["take","hard"],["video"],["interest"],["bulk"],["porn"],["breast"],
                   ["junk"],["holiday"],["merchant"],["anything","anyone"],["alleine?"],["erwachsen"],["tax"],
                   ["iso 9000"],["squirrel"],["attraction"],["..."],["hetero"],["amazing"],["blond"],["debt"],
                   ["believe"],["pocket"],["traffic"],["special","report"],["fellowship"],["stock"],["cigar"],
                   ["abbildung"],["casino"],["urgent"],["info","requested"],["vacation"],["free","fun"],
                   ["nude","pics"],["girls"],["naked"],["this","really","worked"],["sick","shit"]]


=== Added File Zope3/lib/python/Zope/Server/SMTP/__init__.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: __init__.py,v 1.1.2.1 2002/04/05 17:19:53 srichter Exp $
"""