⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smtpproxy.py

📁 用python实现的邮件过滤器
💻 PY
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/env python"""A SMTP proxy to train a Spambayes database.You point SMTP Proxy at your SMTP server(s) and configure your emailclient(s) to send mail through the proxy (i.e. usually this means you uselocalhost as the outgoing server).To setup, enter appropriate values in your Spambayes configuration file inthe "SMTP Proxy" section (in particular: "remote_servers", "listen_ports",and "use_cached_message").  This configuration can also be carried out viathe web user interface offered by POP3 Proxy and IMAP Filter.To use, simply forward/bounce mail that you wish to train to theappropriate address (defaults to spambayes_spam@localhost andspambayes_ham@localhost).  All other mail is sent normally.(Note that IMAP Filter and POP3 Proxy users should not execute this script;launching of SMTP Proxy will be taken care of by those applicatons).There are two main forms of operation.  With both, mail to two(user-configurable) email addresses is intercepted by the proxy (and is*not* sent to the SMTP server) and used as training data for a Spambayesdatabase.  All other mail is simply relayed to the SMTP server.If the "use_cached_message" option is False, the proxy uses the messagesent as training data.  This option is suitable for those not usingPOP3 Proxy or IMAP Filter, or for those that are confident that theirmailer will forward/bounce messages in an unaltered form.If the "use_cached_message" option is True, the proxy examines the messagefor a unique spambayes identification number.  It then tries to find thismessage in the pop3proxy caches and on the imap servers.  It then retrievesthe message from the cache/server and uses *this* as the training data.This method is suitable for those using POP3 Proxy and/or IMAP Filter, andavoids any potential problems with the mailer altering messages beforeforwarding/bouncing them.To use, enter the required SMTP server data in your configuration file andrun sb_server.py"""# This module is part of the spambayes project, which is Copyright 2002-3# The Python Software Foundation and is covered by the Python Software# Foundation license.__author__ = "Tony Meyer <ta-meyer@ihug.co.nz>"__credits__ = "Tim Stone, all the Spambayes folk."try:    True, Falseexcept NameError:    # Maintain compatibility with Python 2.2    True, False = 1, 0todo = """ o It would be nice if spam/ham could be bulk forwarded to the proxy,   rather than one by one.  This would require separating the different   messages and extracting the correct ids.  Simply changing to find   *all* the ids in a message, rather than stopping after one *might*   work, but I don't really know.  Richie Hindle suggested something along   these lines back in September '02. o Suggestions?Testing: o Test with as many clients as possible to check that the   id is correctly extracted from the forwarded/bounced message.MUA information:A '*' in the Header column signifies that the smtpproxy can extractthe id from the headers only.  A '*' in the Body column signifies thatthe smtpproxy can extract the id from the body of the message, if itis there.                                                        Header  Body*** Windows 2000 MUAs ***Eudora 5.2 Forward                                         *     *Eudora 5.2 Redirect                                              *Netscape Messenger (4.7) Forward (inline)                  *     *Netscape Messenger (4.7) Forward (quoted) Plain                  *Netscape Messenger (4.7) Forward (quoted) HTML                   *Netscape Messenger (4.7) Forward (quoted) Plain & HTML           *Netscape Messenger (4.7) Forward (attachment) Plain        *     *Netscape Messenger (4.7) Forward (attachment) HTML         *     *Netscape Messenger (4.7) Forward (attachment) Plain & HTML *     *Outlook Express 6 Forward HTML (Base64)                          *Outlook Express 6 Forward HTML (None)                            *Outlook Express 6 Forward HTML (QP)                              *Outlook Express 6 Forward Plain (Base64)                         *Outlook Express 6 Forward Plain (None)                           *Outlook Express 6 Forward Plain (QP)                             *Outlook Express 6 Forward Plain (uuencoded)                      *http://www.endymion.com/products/mailman Forward                     *M2 (Opera Mailer 7.01) Forward                                   *M2 (Opera Mailer 7.01) Redirect                            *     *The Bat! 1.62i Forward (RFC Headers not visible)                 *The Bat! 1.62i Forward (RFC Headers visible)               *     *The Bat! 1.62i Redirect                                          *The Bat! 1.62i Alternative Forward                         *     *The Bat! 1.62i Custom Template                             *     *AllegroMail 2.5.0.2 Forward                                      *AllegroMail 2.5.0.2 Redirect                                     *PocoMail 2.6.3 Bounce                                            *PocoMail 2.6.3 Bounce                                            *Pegasus Mail 4.02 Forward (all headers option set)         *     *Pegasus Mail 4.02 Forward (all headers option not set)           *Calypso 3 Forward                                                *Calypso 3 Redirect                                         *     *Becky! 2.05.10 Forward                                           *Becky! 2.05.10 Redirect                                          *Becky! 2.05.10 Redirect as attachment                      *     *Mozilla Mail 1.2.1 Forward (attachment)                    *     *Mozilla Mail 1.2.1 Forward (inline, plain)                 *1    *Mozilla Mail 1.2.1 Forward (inline, plain & html)          *1    *Mozilla Mail 1.2.1 Forward (inline, html)                  *1    **1 The header method will only work if auto-include original messageis set, and if view all headers is true."""import stringimport reimport socketimport asyncoreimport asynchatimport getoptimport sysimport osimport emailfrom spambayes import Dibblerfrom spambayes import storagefrom spambayes import messagefrom spambayes.tokenizer import textpartsfrom spambayes.tokenizer import try_to_repair_damaged_base64from spambayes.Options import optionsfrom sb_server import _addressPortStr, ServerLineReaderfrom sb_server import _addressAndPortclass SMTPProxyBase(Dibbler.BrighterAsyncChat):    """An async dispatcher that understands SMTP and proxies to a SMTP    server, calling `self.onTransaction(command, args)` for each    transaction.    self.onTransaction() should return the command to pass to    the proxied server - the command can be the verbatim command or a    processed version of it.  The special command 'KILL' kills it (passing    a 'QUIT' command to the server).    """    def __init__(self, clientSocket, serverName, serverPort):        Dibbler.BrighterAsyncChat.__init__(self, clientSocket)        self.request = ''        self.set_terminator('\r\n')        self.command = ''           # The SMTP command being processed...        self.args = ''              # ...and its arguments        self.isClosing = False      # Has the server closed the socket?        self.inData = False        self.data = []        self.blockData = False        if not self.onIncomingConnection(clientSocket):            # We must refuse this connection, so pass an error back            # to the mail client.            self.push("421 Connection not allowed\r\n")            self.close_when_done()            return        self.serverSocket = ServerLineReader(serverName, serverPort,                                             self.onServerLine)    def onIncomingConnection(self, clientSocket):        """Checks the security settings."""        # Stolen from UserInterface.py        remoteIP = clientSocket.getpeername()[0]        trustedIPs = options["smtpproxy", "allow_remote_connections"]        if trustedIPs == "*" or remoteIP == clientSocket.getsockname()[0]:            return True        trustedIPs = trustedIPs.replace('.', '\.').replace('*', '([01]?\d\d?|2[04]\d|25[0-5])')        for trusted in trustedIPs.split(','):            if re.search("^" + trusted + "$", remoteIP):                return True        return False    def onTransaction(self, command, args):        """Overide this.  Takes the raw command and returns the (possibly        processed) command to pass to the email client."""        raise NotImplementedError    def onProcessData(self, data):        """Overide this.  Takes the raw data and returns the (possibly        processed) data to pass back to the email client."""        raise NotImplementedError    def onServerLine(self, line):        """A line of response has been received from the SMTP server."""        # Has the server closed its end of the socket?        if not line:            self.isClosing = True        # We don't process the return, just echo the response.        self.push(line)        self.onResponse()    def collect_incoming_data(self, data):        """Asynchat override."""        self.request = self.request + data    def found_terminator(self):        """Asynchat override."""        verb = self.request.strip().upper()        if verb == 'KILL':            self.socket.shutdown(2)            self.close()            raise SystemExit        if self.request.strip() == '':            # Someone just hit the Enter key.            self.command = self.args = ''        else:            # A proper command.            if self.request[:10].upper() == "MAIL FROM:":                splitCommand = self.request.split(":", 1)            elif self.request[:8].upper() == "RCPT TO:":                splitCommand = self.request.split(":", 1)            else:                splitCommand = self.request.strip().split(None, 1)            self.command = splitCommand[0]            self.args = splitCommand[1:]        if self.inData == True:            self.data.append(self.request + '\r\n')            if self.request == ".":                self.inData = False                cooked = self.onProcessData("".join(self.data))                self.data = []                if self.blockData == False:                    self.serverSocket.push(cooked)                else:                    self.push("250 OK\r\n")        else:            cooked = self.onTransaction(self.command, self.args)            if cooked is not None:                self.serverSocket.push(cooked + '\r\n')        self.command = self.args = self.request = ''    def onResponse(self):        # If onServerLine() decided that the server has closed its        # socket, close this one when the response has been sent.        if self.isClosing:            self.close_when_done()        # Reset.        self.command = ''        self.args = ''        self.isClosing = Falseclass BayesSMTPProxyListener(Dibbler.Listener):    """Listens for incoming email client connections and spins off

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -