📄 autoconfigure.py
字号:
ham_weight = -500 unsure_weight = -50 # leave judgement up to the rest of the rules rule = '# SpamBayes adjustments\n' \ 'if header "%s" contains "%s" weight %s\n' \ 'if header "%s" contains "%s" weight %s\n' \ 'if header "%s" contains "%s" wieght %s\n\n' % \ (header_name, spam_tag, spam_weight, header_name, unsure_tag, unsure_weight, header_name, ham_tag, ham_weight) rules_file = file(rules_filename, "a") rules_file.write(rule) rules_file.close() return resultsdef pocomail_accounts_filename(): if win32api is None: # If we don't have win32, then we don't know. return "" key = "Software\\Poco Systems Inc" pop_proxy = pop_proxy_port smtp_proxy = smtp_proxy_port try: reg = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, key) except pywintypes.error: # It seems that we don't have PocoMail return "" else: subkey_name = "%s\\%s" % (key, win32api.RegEnumKey(reg, 0)) reg = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, subkey_name) pocomail_path = win32api.RegQueryValueEx(reg, "Path")[0] return os.path.join(pocomail_path, "accounts.ini")def configure_pocomail(pocomail_accounts_file): if os.path.exists(pocomail_accounts_file): f = open(pocomail_accounts_file, "r") accountName = "" pocomail_accounts = { } # Builds the dictionary with all the existing accounts. for line in f.readlines(): line = line.rstrip('\n\r') if line == '': continue if line[0] == '[' and line[-1] == ']': accountName = line[1:-1] pocomail_accounts[accountName] = { } else: separator = line.find('=') optionName = line[:separator] optionValue = line[separator + 1:] if optionName == "POPServer": pop3 = optionValue.split(':') if len(pop3) == 1: pop3.append(110) server = "%s:%s" % tuple(pop3) proxy = pop_proxy pop_proxy = move_to_next_free_port(pop_proxy) if not server in options["pop3proxy", "remote_servers"]: options["pop3proxy", "remote_servers"] += (server,) options["pop3proxy", "listen_ports"] += (proxy,) else: serverIndex = 0 for remoteServer in options["pop3proxy", "remote_servers"]: if remoteServer == server: break serverIndex += 1 proxy = options["pop3proxy", "listen_ports"][serverIndex] optionValue = "%s:%s" % ('localhost', proxy) pocomail_accounts[accountName][optionName] = optionValue f.close() f = open(pocomail_accounts_file, "w") for accountName in pocomail_accounts.keys(): f.write('[' + accountName + ']\n') for optionName, optionValue in pocomail_accounts[accountName].items(): f.write("%s=%s\n" % (optionName, optionValue)) f.write('\n') f.close() options.update_file(optionsPathname) # Add a filter to pocomail pocomail_filters_file = os.path.join(pocomail_path, "filters.ini") if os.path.exists(pocomail_filters_file): f = open(pocomail_filters_file, "r") pocomail_filters = { } filterName = "" for line in f.readlines(): line = line.rstrip('\n\r') if line == '': continue if line[0] == '[' and line[-1] == ']': filterName = line[1:-1] pocomail_filters[filterName] = [] elif line[0] != '{': pocomail_filters[filterName].append(line) f.close() spamBayesFilter = 'spam,X-Spambayes-Classification,move,' \ '"Junk Mail",0,0,,,0,,,move,In,0,0,,0,,,' \ 'move,In,0,0,,0,,,move,In,0,0,,0,,,move,' \ 'In,0,0,,0,,,move,In,0,0,1,0' if pocomail_filters.has_key("Incoming") and \ spamBayesFilter not in pocomail_filters["Incoming"]: pocomail_filters["Incoming"].append(spamBayesFilter) f = open(pocomail_filters_file, "w") f.write('{ Filter list generated by PocoMail 3.01 (1661)' \ '- Licensed Version}\n') for filterName in pocomail_filters.keys(): f.write('\n[' + filterName + ']\n') for filter in pocomail_filters[filterName]: f.write(filter + '\n') f.close() return []def find_config_location(mailer): """Attempt to find the location of the config file for the given mailer, to pass to the configure_* scripts above.""" # Requires win32all to be available, until someone # fixes the function to look in the right places for *nix/Mac. if win32api is None: raise ImportError("win32 extensions required") if mailer in ["Outlook Express", ]: # Outlook Express can be configured without a # config location, because it's all in the registry. return "" windowsUserDirectory = shell.SHGetFolderPath(0,shellcon.CSIDL_APPDATA,0,0) potential_locations = \ {"Eudora" : ("%(wud)s%(sep)sQualcomm%(sep)sEudora",), "Mozilla" : \ ("%(wud)s%(sep)sMozilla%(sep)sProfiles%(sep)s%(user)s", "%(wud)s%(sep)sMozilla%(sep)sProfiles%(sep)sdefault",), "M2" : ("%(wud)s%(sep)sOpera%(sep)sOpera7",), "PocoMail" : (pocomail_accounts_filename(),), } # We try with the username that the user uses # for Windows, even though that might not be the same as their profile # names for mailers. We can get smarter later. username = win32api.GetUserName() loc_dict = {"sep" : os.sep, "wud" : windowsUserDirectory, "user" : username} for loc in potential_locations[mailer]: loc = loc % loc_dict if os.path.exists(loc): return loc return Nonedef configure(mailer): """Automatically configure the specified mailer and SpamBayes.""" loc = find_config_location(mailer) if loc is None: # Can't set it up, so do nothing. return funcs = {"Eudora" : configure_eudora, "Mozilla" : configure_mozilla, "M2" : configure_m2, "Outlook Express" : configure_outlook_express, "PocoMail" : configure_pocomail, } return funcs[mailer](loc)def is_installed(mailer): """Return True if we believe that the mailer is installed.""" # For the simpler mailers, we believe it is installed if the # configuration path can be found and exists. config_location = find_config_location(mailer) if config_location: if os.path.exists(config_location): return True return False # For the ones based in the registry, we have different # techniques. if mailer == "Outlook Express": if oe_mailbox.OEIsInstalled(): return True return False # If we don't know, guess that it isn't. return Falsedef offer_to_configure(mailer): """If the mailer appears to be installed, offer to set it up for SpamBayes (and SpamBayes for it).""" # At the moment, the test we use to check if the mailer is installed # is whether a valid path to the configuration file can be found. # This is ok, except for those that are setup in the registry - there # will always be a valid path, whether they are installed or not. if find_config_location(mailer) is not None: confirm_text = "Would you like %s setup for SpamBayes, and " \ "SpamBayes setup with your %s settings?\n" \ "(This is alpha software! We recommend that you " \ "only do this if you know how to re-setup %s " \ "if necessary.)" % (mailer, mailer, mailer) ans = MessageBox(confirm_text, "Configure?", win32con.MB_YESNO | win32con.MB_ICONQUESTION) if ans == win32con.IDYES: results = configure(mailer) if results is None: MessageBox("Configuration unsuccessful.", "Error", win32con.MB_OK | win32con.MB_ICONERROR) else: text = "Configuration complete.\n\n" + "\n".join(results) MessageBox(text, "Complete", win32con.MB_OK)def GetConsoleHwnd(): """Returns the window handle of the console window in which this script is running, or 0 if not running in a console window. This function is taken directly from Pythonwin\dllmain.cpp in the win32all source, ported to Python.""" # fetch current window title try: oldWindowTitle = win32api.GetConsoleTitle() except: return 0 # format a "unique" NewWindowTitle newWindowTitle = "%d/%d" % (win32api.GetTickCount(), win32api.GetCurrentProcessId()) # change current window title win32api.SetConsoleTitle(newWindowTitle) # ensure window title has been updated import time time.sleep(0.040) # look for NewWindowTitle hwndFound = win32gui.FindWindow(0, newWindowTitle) # restore original window title win32api.SetConsoleTitle(oldWindowTitle) return hwndFoundhwndOwner = GetConsoleHwnd()def MessageBox(message, title=None, style=win32con.MB_OK): return win32gui.MessageBox(hwndOwner, message, title, style)if __name__ == "__main__": pmail_ini_dir = "C:\\Program Files\\PMAIL\\MAIL\\ADMIN" for mailer in ["Eudora", "Mozilla", "M2", "Outlook Express", "PocoMail"]: #print find_config_location(mailer) #configure(mailer) offer_to_configure(mailer)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -