class SieveTest(unittest.TestCase): def setUp(self): self.parser = Parser() def __checkCompilation(self, script, result): self.assertEqual(self.parser.parse(script), result) def compilation_ok(self, script, **kwargs): self.__checkCompilation(script, True, **kwargs) def compilation_ko(self, script): self.__checkCompilation(script, False) def representation_is(self, content): target = six.StringIO() self.parser.dump(target) repr_ = target.getvalue() target.close() self.assertEqual(repr_, content.lstrip()) def sieve_is(self, content): filtersset = FiltersSet("Testfilterset") filtersset.from_parser_result(self.parser) target = six.StringIO() filtersset.tosieve(target) repr_ = target.getvalue() target.close() self.assertEqual(repr_, content)
class SieveTest(unittest.TestCase): def setUp(self): self.parser = Parser() def __checkCompilation(self, script, result): self.assertEqual(self.parser.parse(script), result) def compilation_ok(self, script): self.__checkCompilation(script, True) def compilation_ko(self, script): self.__checkCompilation(script, False) def representation_is(self, content): target = six.StringIO() self.parser.dump(target) repr_ = target.getvalue() target.close() self.assertEqual(repr_, content.lstrip()) def sieve_is(self, content): filtersset = FiltersSet("Testfilterset") filtersset.from_parser_result(self.parser) target = six.StringIO() filtersset.tosieve(target) repr_ = target.getvalue() target.close() self.assertEqual(repr_, content)
def test_002_parse_tests(self): from sievelib.parser import Parser sieve_parser = Parser(debug=True) i = 0 for sieve_str in sieve_scripts: i += 1 result = sieve_parser.parse(sieve_str) if not result: print "Sieve line: %r" % (sieve_parser.lexer.text.split('\n')[(sieve_parser.lexer.text[:sieve_parser.lexer.pos].count('\n'))]) raise Exception, "Failed parsing Sieve script #%d: %s" % (i, sieve_parser.error)
def test_002_parse_tests(self): from sievelib.parser import Parser sieve_parser = Parser(debug=True) i = 0 for sieve_str in sieve_scripts: i += 1 result = sieve_parser.parse(sieve_str) if not result: print "Sieve line: %r" % (sieve_parser.lexer.text.split('\n')[(sieve_parser.lexer.text[:sieve_parser.lexer.pos].count('\n'))]) raise Exception("Failed parsing Sieve script #%d: %s" % (i, sieve_parser.error))
def copy_sieve(base_path): sieve_path = os.path.expanduser("~/sieve/default.sieve") os.system("cat sieve/??-*.sieve > /tmp/default.sieve") p = Parser() with(open("/tmp/default.sieve")) as f: if p.parse(f.read()): print "parse OK" shutil.copy2("/tmp/default.sieve", sieve_path) else: print "parser error" print p.error
def getscript(self, name, format="raw"): content = self.msc.getscript(name) if content is None: raise SieveClientError(self.msc.errmsg) if format == "raw": return content p = Parser() if not p.parse(content): print "Parse error????" return None fs = FiltersSet(name) fs.from_parser_result(p) return fs
def main(): (config, nSec) = loadImapConfig() (SERVER, USER, PASSWORD, RULES, INBOX, FOLDER) = readImapConfig(config) # Make connections to server # Sieve client connection c = Client(SERVER) if not c.connect(USER, PASSWORD, starttls=True, authmech="PLAIN"): print("Connection failed") return 0 else: print(c.listscripts()) M = makeConnection(SERVER, USER, PASSWORD) PASSWORD = "******" M.select() end = "" while (not end): # Could we move this parsing part out of the while? script = c.getscript('sieve-script') p = Parser() p.parse(script) (rules, more) = extractActions(p) # We are going to filter based on one message msg = selectMessage(M) (keyword, filterCond) = selectHeaderAuto(M, msg) actions = selectAction(p, M) # actions[0][1] contains the rule selector # print("actions ", actions[0][1]) # print(rules[actions[0][1].strip('"')]) # For a manual selection option? # header= selectHeader() # keyword = selectKeyword(header) # Eliminate # conditions = [] # conditions.append((keyword, ":contains", filterCond)) #print("filtercond", filterCond) newActions = addRule(rules, more, keyword, filterCond, actions) #print("nA",newActions) #print("nA 0",newActions[0][0][2][0].tosieve()) #print("nA 0") fs = constructFilterSet(newActions) sieveContent = io.StringIO() # fs.tosieve(open(FILE_SIEVE, 'w')) # fs.tosieve() # sys.exit() fs.tosieve(sieveContent) #import time #time.sleep(5) # Let's do a backup of the old sieve script name = time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) res = c.putscript(name+'sogo', script) print("res",res) # Now we can put the new sieve filters in place # fSieve = open(FILE_SIEVE, 'r') # if not c.putscript('sogo', fSieve.read()): #print(sieveContent.getvalue()) if not c.putscript('sogo', sieveContent.getvalue()): print("fail!") # Let's start the git backup repo = Repo(repoDir) index = repo.index print("listscripts",c.listscripts()) listScripts=c.listscripts() print("listscripts",listScripts) if (listScripts != None): listScripts=listScripts[1] listScripts.sort() print("listscripts",c.listscripts()) print(listScripts[0]) # script = listScripts[-1] # The last one sieveFile=c.getscript('sogo') file=open(repoDir+repoFile,'w') file.write(sieveFile) file.close() index.add(['*']) index.commit(name+'sogo') if len(listScripts)>6: # We will keep the last five ones (plus the active one) numScripts = len(listScripts) - 6 i = 0 while numScripts > 0: script = listScripts[i] c.deletescript(script) i = i + 1 numScripts = numScripts - 1 end = input("More rules? (empty to continue) ")
def addToSieve(msg=""): config = loadImapConfig()[0] (SERVER, USER, PASSWORD, RULES, INBOX, FOLDER) = readImapConfig(config) # Make connections to server # Sieve client connection c = Client(SERVER) if not c.connect(USER, PASSWORD, starttls=True, authmech="PLAIN"): print("Connection failed") return 0 M = makeConnection(SERVER, USER, PASSWORD) PASSWORD = "******" M.select() #end = "" #while (not end): # Could we move this parsing part out of the while? script = c.getscript('sieve-script') p = Parser() p.parse(script) #print("p.result",p.result) (rules, more) = extractActions(p) # We are going to filter based on one message if not msg: msg = selectMessage(M) (keyword, filterCond) = selectHeaderAuto(M, msg) actions = selectAction(p, M) # actions[0][1] contains the rule selector # print("actions ", actions[0][1]) # print(rules[actions[0][1].strip('"')]) # For a manual selection option? # header= selectHeader() # keyword = selectKeyword(header) # Eliminate # conditions = [] # conditions.append((keyword, ":contains", filterCond)) #print("filtercond", filterCond) newActions = addRule(rules, more, keyword, filterCond, actions) #print("nA",newActions) #print("nA 0",newActions[0][0][2][0].tosieve()) #print("nA 0") (fs, moreSieve) = constructFilterSet(newActions) sieveContent = io.StringIO() # We need to add the require in order to use the body section sieveContent.write('require ["body"];\n') # fs.tosieve(open(FILE_SIEVE, 'w')) #fs.tosieve() #print(moreSieve) #sys.exit() print(USER) fs.tosieve(sieveContent) sieveContent.write(moreSieve) with open(os.path.expanduser('~'+USER)+'/sieve/body.sieve') as f: sieveContent.write(f.read()) print(sieveContent.getvalue()) #"""#Filter: #if anyof (body :raw :contains "puntoclick.info") { # fileinto "Spam"; # stop; #}""") #import time #time.sleep(5) # Let's do a backup of the old sieve script name = time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) res = c.putscript(name+'sogo', script) print("res",res) # Now we can put the new sieve filters in place # fSieve = open(FILE_SIEVE, 'r') # if not c.putscript('sogo', fSieve.read()): #print(sieveContent.getvalue()) if not c.putscript('sieve-script', sieveContent.getvalue()): print("fail!") # Let's start the git backup repo = Repo(repoDir) index = repo.index print("listscripts",c.listscripts()) listScripts=c.listscripts() print("listscripts",listScripts) if (listScripts != None): listScripts=listScripts[1] listScripts.sort() print("listscripts",c.listscripts()) print(listScripts[0]) # script = listScripts[-1] # The last one sieveFile=c.getscript('sieve-script') file=open(repoDir+repoFile,'w') file.write(sieveFile) file.close() index.add(['*']) index.commit(name+'sogo') if len(listScripts)>6: # We will keep the last five ones (plus the active one) numScripts = len(listScripts) - 6 i = 0 while numScripts > 0: script = listScripts[i] c.deletescript(script) i = i + 1 numScripts = numScripts - 1
def get_filter_vars(data): filter_match_types = { "match_any_flowing_rules": "anyof", "match_all_flowing_rules": "allof", "match_all": "all", } filter_sections = { "subject": "subject", "from": "from", "to": "to", "cc": "cc", "to_or_cc": "to_or_cc", "size": "size", "header": "header", "body": "body" } filter_conditions = { "is": ":is", "contains": ":contains", "matches": ":matches", "matches_regex": ":regex", "is_under": ":over", "is_over": ":under", } filter_negative_conditions = { "is_not": ":is", "does_not_contain": ":contains", "does_not_match": ":matches", "does_not_match_regex": ":regex" } filter_section_conditions = { "subject": lambda x: [x], "from": lambda x: [x], "to": lambda x: [x], "cc": lambda x: [x], "to_or_cc": lambda x: [x], "size": lambda x: [x], "header": lambda x: [x], "body": lambda x: [":text", x] } filter_actions = { "discard_message": "discard", "keep_message": "keep", "stop_processing_filter": "stop", "forward_message_to": "redirect", "send_reject_message": "reject", "file_message_in": "fileinto" } action_value_map = { "forward_message_to": "to_email", "send_reject_message": "message", "file_message_in": "folder" } filters = data.value raw_filter = "" fs = FiltersSet("cow") filters = sorted(filters, key=lambda f: f['order']) for s_filter in filters: # range(len(filters.keys())): # filter= filters[i] filter_name = s_filter.get("name") enabled = True # s_filter.get("enabled") TODO :: rules = s_filter.get("conditions") actions = s_filter.get("actions") matchtype = filter_match_types[s_filter.get("incoming_message")] builded_conditions = [] for rule in rules: # range(len(rules.keys())): # rule = rules[rule_number] negated = False section = filter_sections[rule["selector"]] if "not" in rule["condition"]: negated = True condition = filter_negative_conditions[rule["condition"]] else: condition = filter_conditions[rule["condition"]] condition = filter_section_conditions[section](condition) if negated: section = "not{}".format(section) value = rule["value"] builded_conditions.append((section, *condition, value)) builded_actions = [] actions = sorted(actions, key=lambda f: f['order']) for action in actions: # range(len(actions.keys())): # action = actions[action_number] param = "" if action["type"] in action_value_map.keys(): param = action[action_value_map[action["type"]]] # if value_map=[] builded_actions.append((filter_actions[action["type"]], param)) if matchtype == "all": p = Parser() raw = 'require ["reject","fileinto","imap4flags"];' for builded_action in builded_actions: name = builded_action[0] param = builded_action[1] if param: raw += "\n{} \"{}\";".format(name, param) else: raw += "\n{};".format(name) p.parse(raw) fs.from_parser_result(p) else: fs.addfilter(name=filter_name, conditions=builded_conditions, actions=builded_actions, matchtype=matchtype) requirements = fs.requires io = StringIO() fs.tosieve(io) raw_sieve = io.getvalue() if requirements: raw_sieve = '\n'.join(raw_sieve.splitlines()[2:]) if "not" in raw_sieve: for key in filter_sections.keys(): raw_sieve = raw_sieve.replace("not" + key, key) return [raw_sieve, requirements]
def main(): (config, nSec) = loadImapConfig() (SERVER, USER, PASSWORD, RULES, INBOX, FOLDER) = readImapConfig(config) # Make connections to server # Sieve client connection c = Client(SERVER) if not c.connect(USER, PASSWORD, starttls=True, authmech="PLAIN"): print("Connection failed") return 0 else: print(c.listscripts()) M = makeConnection(SERVER, USER, PASSWORD) PASSWORD = "******" M.select() end = "" while (not end): # Could we move this parsing part out of the while? script = c.getscript('sieve-script') p = Parser() p.parse(script) (rules, more) = extractActions(p) # We are going to filter based on one message msg = selectMessage(M) (keyword, filterCond) = selectHeaderAuto(M, msg) actions = selectAction(p, M) # actions[0][1] contains the rule selector # print("actions ", actions[0][1]) # print(rules[actions[0][1].strip('"')]) # For a manual selection option? # header= selectHeader() # keyword = selectKeyword(header) # Eliminate # conditions = [] # conditions.append((keyword, ":contains", filterCond)) #print("filtercond", filterCond) newActions = addRule(rules, more, keyword, filterCond, actions) #print("nA",newActions) #print("nA 0",newActions[0][0][2][0].tosieve()) #print("nA 0") fs = constructFilterSet(newActions) sieveContent = io.StringIO() # fs.tosieve(open(FILE_SIEVE, 'w')) # fs.tosieve() # sys.exit() fs.tosieve(sieveContent) #import time #time.sleep(5) # Let's do a backup of the old sieve script name = time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) res = c.putscript(name + 'sogo', script) print("res", res) # Now we can put the new sieve filters in place # fSieve = open(FILE_SIEVE, 'r') # if not c.putscript('sogo', fSieve.read()): #print(sieveContent.getvalue()) if not c.putscript('sogo', sieveContent.getvalue()): print("fail!") # Let's start the git backup repo = Repo(repoDir) index = repo.index print("listscripts", c.listscripts()) listScripts = c.listscripts() print("listscripts", listScripts) if (listScripts != None): listScripts = listScripts[1] listScripts.sort() print("listscripts", c.listscripts()) print(listScripts[0]) # script = listScripts[-1] # The last one sieveFile = c.getscript('sogo') file = open(repoDir + repoFile, 'w') file.write(sieveFile) file.close() index.add(['*']) index.commit(name + 'sogo') if len(listScripts) > 6: # We will keep the last five ones (plus the active one) numScripts = len(listScripts) - 6 i = 0 while numScripts > 0: script = listScripts[i] c.deletescript(script) i = i + 1 numScripts = numScripts - 1 end = input("More rules? (empty to continue) ")
def addToSieve(msg=""): config = loadImapConfig()[0] (SERVER, USER, PASSWORD, RULES, INBOX, FOLDER) = readImapConfig(config) # Make connections to server # Sieve client connection c = Client(SERVER) if not c.connect(USER, PASSWORD, starttls=True, authmech="PLAIN"): print("Connection failed") return 0 M = makeConnection(SERVER, USER, PASSWORD) PASSWORD = "******" M.select() #end = "" #while (not end): # Could we move this parsing part out of the while? script = c.getscript('sieve-script') p = Parser() p.parse(script) #print("p.result",p.result) (rules, more) = extractActions(p) # We are going to filter based on one message if not msg: msg = selectMessage(M) (keyword, filterCond) = selectHeaderAuto(M, msg) actions = selectAction(p, M) # actions[0][1] contains the rule selector # print("actions ", actions[0][1]) # print(rules[actions[0][1].strip('"')]) # For a manual selection option? # header= selectHeader() # keyword = selectKeyword(header) # Eliminate # conditions = [] # conditions.append((keyword, ":contains", filterCond)) #print("filtercond", filterCond) newActions = addRule(rules, more, keyword, filterCond, actions) #print("nA",newActions) #print("nA 0",newActions[0][0][2][0].tosieve()) #print("nA 0") (fs, moreSieve) = constructFilterSet(newActions) sieveContent = io.StringIO() # We need to add the require in order to use the body section sieveContent.write('require ["body"];\n') # fs.tosieve(open(FILE_SIEVE, 'w')) #fs.tosieve() #print(moreSieve) #sys.exit() print(USER) fs.tosieve(sieveContent) sieveContent.write(moreSieve) with open(os.path.expanduser('~' + USER) + '/sieve/body.sieve') as f: sieveContent.write(f.read()) print(sieveContent.getvalue()) #"""#Filter: #if anyof (body :raw :contains "puntoclick.info") { # fileinto "Spam"; # stop; #}""") #import time #time.sleep(5) # Let's do a backup of the old sieve script name = time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) res = c.putscript(name + 'sogo', script) print("res", res) # Now we can put the new sieve filters in place # fSieve = open(FILE_SIEVE, 'r') # if not c.putscript('sogo', fSieve.read()): #print(sieveContent.getvalue()) if not c.putscript('sieve-script', sieveContent.getvalue()): print("fail!") # Let's start the git backup repo = Repo(repoDir) index = repo.index print("listscripts", c.listscripts()) listScripts = c.listscripts() print("listscripts", listScripts) if (listScripts != None): listScripts = listScripts[1] listScripts.sort() print("listscripts", c.listscripts()) print(listScripts[0]) # script = listScripts[-1] # The last one sieveFile = c.getscript('sieve-script') file = open(repoDir + repoFile, 'w') file.write(sieveFile) file.close() index.add(['*']) index.commit(name + 'sogo') if len(listScripts) > 6: # We will keep the last five ones (plus the active one) numScripts = len(listScripts) - 6 i = 0 while numScripts > 0: script = listScripts[i] c.deletescript(script) i = i + 1 numScripts = numScripts - 1
from sievelib.parser import Parser import sys p = Parser() input = sys.stdin.read().strip() if input == "": print("🤔 File was empty") exit(1) result = p.parse(input) if not result: print("☠️ Error: " + p.error) exit(2) print("👍 File looks good") exit(0)