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

📄 autoconfigure.py

📁 用python实现的邮件过滤器
💻 PY
📖 第 1 页 / 共 3 页
字号:
        server = "%s:%s" % (server, port)        proxy_port = move_to_next_free_port(proxy_port)        port_pref = 'user_pref("mail.server.server%s.port", %s);' % \                    (num, proxy_port)        options["pop3proxy", "remote_servers"] += (server,)        options["pop3proxy", "listen_ports"] += (proxy_port,)        if old_port is None:            pref = "%s\n%s" % (pref, port_pref)        else:            save_prefs = save_prefs.replace(old_port, port_pref)        save_prefs = save_prefs.replace(old_pref, pref)        results.append("[%s] Proxy %s on localhost:%s" % \                       (num, server, proxy_port))    # Do the SMTP server.    # Mozilla recommends that only advanced users setup more than one,    # so we'll just set that one up.  Advanced users can setup SpamBayes    # themselves <wink>.    prefs = save_prefs    r = re.compile(r"user_pref\(\"mail.smtpserver.smtp(\d+).hostname\", \"([^\"]*)\"\);")    current_pos = 0    while True:        m = r.search(prefs[current_pos:])        if not m:            break        current_pos += m.end()        server_num = m.group(1)        server = m.group(2)        old_pref = 'user_pref("mail.smtpserver.smtp%s.hostname", ' \                   '"%s");' % (server_num, server)        new_pref = 'user_pref("mail.smtpserver.smtp%s.hostname", ' \                   '"127.0.0.1");' % (server_num,)        # Find the port        port_string = 'user_pref("mail.smtpserver.smtp%d.port", ' \                      % (server_num,)        port_loc = prefs.find(port_string)        if port_loc == -1:            port = "25"            old_port = None        else:            loc_plus_len = port_loc + len(port_string)            end_of_number = loc_plus_len + prefs[loc_plus_len:].index(')')            port = prefs[loc_plus_len : end_of_number]            old_port = 'user_pref("mail.smtpserver.smtp%s.port", %s);' % \                       (server_num, port)        smtp_accounts[server_num] = (new_pref, old_pref, old_port,                                     server, port)    proxy_port = smtp_proxy_port    for num, (pref, old_pref, old_port, server, port) in smtp_accounts.items():        server = "%s:%s" % (server, port)        proxy_port = move_to_next_free_port(proxy_port)        port_pref = 'user_pref("mail.smtpserver.smtp%s.port", %s);' % \                    (num, proxy_port)        options["smtpproxy", "remote_servers"] += (server,)        options["smtpproxy", "listen_ports"] += (proxy_port,)        if old_port is None:            pref = "%s\n%s" % (pref, port_pref)        else:            save_prefs = save_prefs.replace(old_port, port_pref)        save_prefs = save_prefs.replace(old_pref, pref)        results.append("[%s] Proxy %s on localhost:%s" % \                       (num, server, proxy_port))    prefs_file = file("%s%sprefs.js" % (config_location, os.sep), "w")    prefs_file.write(save_prefs)    prefs_file.close()    options.update_file(optionsPathname)    # Setup filtering rules.    # Assumes that the folders already exist!  I don't know how difficult    # it would be to create new Mozilla mail folders.    filter_filename = "%s%smsgFilterRules.dat" % (config_location, os.sep)    store_name = "" # how do we get this?    spam_folder_url = "mailbox:////%s//Junk%%20Mail" % (store_name,)    unsure_folder_url = "mailbox:////%s//Possible%%20Junk" % (store_name,)    header_name = options["Headers", "classification_header_name"]    spam_tag = options["Headers", "header_spam_string"]    unsure_tag = options["Headers", "header_unsure_string"]    rule = 'name="SpamBayes-Spam"\n' \           'enabled="yes"\n' \           'type="1"\n' \           'action="Move to folder"\n' \           'actionValue="%s"\n' \           'condition="OR (\"%s\",contains,%s)"\n' \           'name="SpamBayes-Unsure"\n' \           'enabled="yes"\n' \           'type="1"\n' \           'action="Move to folder"\n' \           'actionValue="%s"\n' \           'condition="OR (\"%s\",contains,%s)"\n' % \           (spam_folder_url, header_name, spam_tag,            unsure_folder_url, header_name, unsure_tag)    # This should now be written to the file, but I'm not sure how we    # determine which subdirectory it goes into - does it have to go    # into them all?    # We are assuming that a rules file already exists, otherwise there    # is a bit more to go at the top.    return resultsdef configure_m2(config_location):    """Configure M2 (Opera's mailer) to use the SpamBayes POP3 and SMTP    proxies, and configure SpamBayes to proxy the servers that M2 was    connecting to."""    ini_filename = os.path.join(config_location, "Mail", "accounts.ini")    ini_file = file(ini_filename, "r")    faked_up = StringIO.StringIO()    faked_up.write(";") # Missing at the start    faked_up.write(ini_file.read())    faked_up.seek(0)    ini_file.close()    c = ConfigParser.ConfigParser()    c.readfp(faked_up)    translate = {("Incoming Servername", "Incoming Port") : "pop3proxy",                 ("Outgoing Servername", "Outgoing Port") : "smtpproxy",                 }    pop_proxy = pop_proxy_port    smtp_proxy = smtp_proxy_port    results = []    for sect in c.sections():        if sect.startswith("Account") and sect != "Accounts":            if c.get(sect, "Incoming Protocol") == "POP":                for (m2_name, m2_port), us_name in translate.items():                    try:                        port = c.get(sect, m2_port)                    except ConfigParser.NoOptionError:                        port = None                    if us_name.lower()[:4] == "pop3":                        if port is None:                            port = 110                        pop_proxy = move_to_next_free_port(pop_proxy)                        proxy_port = pop_proxy                    else:                        if port is None:                            port = 25                        smtp_proxy = move_to_next_free_port(smtp_proxy)                        proxy_port = smtp_proxy                    server = "%s:%s" % (c.get(sect, m2_name), port)                    options[us_name, "remote_servers"] += (server,)                    options[us_name, "listen_ports"] += (proxy_port,)                    results.append("[%s] Proxy %s on localhost:%s" % \                                   (sect, server, proxy_port))                    c.set(sect, m2_name, "localhost")                    c.set(sect, m2_port, proxy_port)            elif c.get(sect, "Incoming Protocol") == "IMAP":                # Setup imapfilter instead                pass    out = file(ini_filename, "w")    c.write(out)    out.close()    options.update_file(optionsPathname)    # Setting up a filter in M2 is very simple, but I'm not sure what the    # right rule is - M2 doesn't move mail, it just displays a subset.    # If someone can describe the best all-purpose rule, I'll pop it in    # here.    return resultsdef configure_outlook_express(unused):    """Configure OE to use the SpamBayes POP3 and SMTP proxies, and    configure SpamBayes to proxy the servers that OE was connecting to."""    # Requires win32all to be available (or for someone to write a    # Mac version <wink>)    if win32api is None:        raise ImportError("win32 extensions required")    accounts = oe_mailbox.OEAccountKeys()    translate = {("POP3 Server", "POP3 Port") : "pop3proxy",                 ("SMTP Server", "SMTP Port") : "smtpproxy",                 }    pop_proxy = pop_proxy_port    smtp_proxy = smtp_proxy_port    results = []    for proto, subkey, account in accounts:        if proto == "POP3":            for (server_key, port_key), sect in translate.items():                if sect[:4] == "pop3":                    default_port = 110                    pop_proxy = move_to_next_free_port(pop_proxy)                    proxy = pop_proxy                else:                    default_port = 25                    smtp_proxy = move_to_next_free_port(smtp_proxy)                    proxy = smtp_proxy                if account.has_key(port_key):                    port = account[port_key][0]                else:                    port = default_port                server = "%s:%s" % (account[server_key][0], port)                options[sect, "remote_servers"] += (server,)                options[sect, "listen_ports"] += (proxy,)                win32api.RegSetValueEx(subkey, server_key, 0,                                       win32con.REG_SZ, "127.0.0.1")                win32api.RegSetValueEx(subkey, port_key, 0,                                       win32con.REG_SZ, str(proxy))                results.append("[%s] Proxy %s on localhost:%s" % \                               (account["Account Name"][0], server, proxy))        elif proto == "IMAP4":            # Setup imapfilter instead.            pass    options.update_file(optionsPathname)    # Outlook Express rules are done in much the same way.  Should one    # be set up to work with notate_to or notate_subject?  (and set that    # option, obviously)    return resultsdef configure_pegasus_mail(config_location):    """Configure Pegasus Mail to use the SpamBayes POP3 and SMTP proxies,    and configure SpamBayes to proxy the servers that Pegasus Mail was    connecting to."""    # We can't use ConfigParser here, as we want 'surgical' editing,    # so we want to use out OptionsClass.  There is the additional trouble    # that the Pegasus Mail config file doesn't have a section header.    pop_proxy = pop_proxy_port    smtp_proxy = smtp_proxy_port    results = []    for filename in os.listdir(config_location):        if filename.lower().startswith("pop") or filename.lower().startswith("smt"):            full_filename = os.path.join(config_location, filename)            working_filename = "%s.tmp" % (filename, )            shutil.copyfile(filename, working_filename)            c = OptionsClass.OptionsClass()            c.merge_file(working_filename)            server = "%s:%s" % (c.get("all", "host"), c.get("all", "port"))            if filename[:3] == "pop":                pop_proxy = move_to_next_free_port(pop_proxy)                proxy = pop_proxy                sect = "pop3proxy"            else:                smtp_proxy = move_to_next_free_port(smtp_proxy)                proxy = smtp_proxy                sect = "smtpproxy"            options[sect, "remote_servers"] += (server,)            options[sect, "listen_ports"] += (proxy,)            # Write in the new options!!            c.set("all", "host", "127.0.0.1")            c.set("all", "port", proxy)            c.update_file(working_filename)            results.append("[%s] Proxy %s on localhost:%s" % \                           (c.get("all", "title"), server, proxy))        elif filename.lower() == "IMAP.PM":            # Setup imapfilter instead.            pass    # Pegasus Mail has a 'weight' system for determining junk mail.    # The best plan would probably be to just add to this.  Something like:    rules_filename = os.path.join(config_location, "spambust.dat")    header_name = options["Headers", "classification_header_name"]    spam_tag = options["Headers", "header_spam_string"]    unsure_tag = options["Headers", "header_unsure_string"]    ham_tag = options["Headers", "header_ham_string"]    spam_weight = 500

⌨️ 快捷键说明

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