def handler(self, **args): """Allows someone to add material onto a factoid (that isn't locked) by saying "foo is also bar", for example""" import priv from irclib import Event from time import time target = self.return_to_sender(args) # args["text"] should look something like: # "moobot: foo is also bar blatz qux" # Grab the factoid to change: factoid_key = self.strip_words(args["text"], 1).split(" is ")[0] # Grab the stuff to tack on: to_add = self.strip_words(args["text"], 1).split(" is also ")[1] # Check if the factoid is locked or not locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: message = "Factoid '%s' does not exist." % factoid_key return Event("privmsg", "", target, [message]) if priv.checkPriv(args["source"], "delete_priv") == 0 and ( locked_by != "" and locked_by != args["source"]): message = "You do not have permission to delete factoid '%s." % factoid_key return Event("privmsg", "", target, [message]) # Since we don't have delete_priv, we just delete and recreate the factoid orig_factoid = FactoIds.getValueByKey(factoid_key) new_factoid = orig_factoid + ", or " + to_add FactoIds.update(factoid_key, new_factoid, args["source"]) return Event("privmsg", "", target, ["ok"])
def handler(self, **args): """Allows someone to add material onto a factoid (that isn't locked) by saying "foo is also bar", for example""" import priv from irclib import Event from time import time target = self.return_to_sender(args) # args["text"] should look something like: # "moobot: foo is also bar blatz qux" # Grab the factoid to change: factoid_key = self.strip_words(args["text"], 1).split(" is ")[0] # Grab the stuff to tack on: to_add = self.strip_words(args["text"], 1).split(" is also ")[1] # Check if the factoid is locked or not locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: message = "Factoid '%s' does not exist." % factoid_key return Event("privmsg", "", target, [message]) if priv.checkPriv(args["source"], "delete_priv") == 0 and (locked_by != "" and locked_by != args["source"]): message = "You do not have permission to delete factoid '%s." % factoid_key return Event("privmsg", "", target, [message]) # Since we don't have delete_priv, we just delete and recreate the factoid orig_factoid = FactoIds.getValueByKey(factoid_key) new_factoid = orig_factoid + ", or " + to_add FactoIds.update(factoid_key, new_factoid, args["source"]) return Event("privmsg", "", target, ["ok"])
def handler(self, **args): """ replaces an existing factoid (no factoid is text> """ import priv from irclib import Event from time import time target = self.return_to_sender(args) # Strip the bot name and "no" from the factoid factoid_text = self.strip_words(args["text"], 2) # Separate the factoid_keu (the word(s) before "is") from the # factoid value (anything after the first "is") data = factoid_text.split(" is ", 1) factoid_key = data[0] factoid_value = data[1] locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: message = "Factoid '" + factoid_key + "' does not exist." return Event("privmsg", "", target, [message]) # Check if they can modify factoids, and if they can modify THIS # particular factoid (ie, it's not locked) if priv.checkPriv(args["source"], "delete_priv") == 0 and locked_by != "": return Event("privmsg","", target, [ "Factoid \"%s\" locked by %s" % (factoid_key, locked_by)]) FactoIds.replace(factoid_key, factoid_value, args["source"]) return Event("privmsg", "", target, [ "ok" ])
def handler(self, **args): """ replaces an existing factoid (no factoid is text> """ import priv from irclib import Event from time import time target = self.return_to_sender(args) # Strip the bot name and "no" from the factoid factoid_text = self.strip_words(args["text"], 2) # Separate the factoid_keu (the word(s) before "is") from the # factoid value (anything after the first "is") data = factoid_text.split(" is ", 1) factoid_key = data[0] factoid_value = data[1] locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: message = "Factoid '" + factoid_key + "' does not exist." return Event("privmsg", "", target, [message]) # Check if they can modify factoids, and if they can modify THIS # particular factoid (ie, it's not locked) if priv.checkPriv(args["source"], "delete_priv") == 0 and locked_by != "": return Event( "privmsg", "", target, ["Factoid \"%s\" locked by %s" % (factoid_key, locked_by)]) FactoIds.replace(factoid_key, factoid_value, args["source"]) return Event("privmsg", "", target, ["ok"])
def handler(self, **args): """ lock and unlock factoids """ import priv, time from irclib import Event, nm_to_n # Change the target based on how we were called target = self.return_to_sender(args) # Strip off the first word and then take the first word of # what's left to get our action, lock or unlock action = self.strip_words(args["text"], 1).split(" ", 1)[0] # Every word after the first two is the factoid key factoid_key = self.strip_words(args["text"], 2) if action == "lock": # We allow people to lock their own factoid, or, if they have # lock_priv, whatever they want if factoid_key == nm_to_n(args["source"]) \ or priv.checkPriv(args["source"], "lock_priv") == 1: FactoIds.lock(factoid_key, args["source"]) msg = "Factoid \"%s\" locked." % factoid_key else: msg = "You can't lock this factoid." elif action == "unlock": # For unlocking, first check who locked the factoid we are # talking about locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: msg = "Factoid \"%s\" not found." % factoid_key else: # if we aren't locked, tell them, otherwise, check if the factoid # is for the requester's nick or if they have lock_priv (which # allows unlocking as well) if not locked_by: msg = "Factoid \"%s\" is not locked." % factoid_key elif locked_by != args["source"] and not priv.checkPriv( args["source"], "lock_priv"): msg = "You cannot unlock this factoid." else: FactoIds.unlock(factoid_key, args["source"]) msg = "Factoid \"%s\" unlocked." % factoid_key return Event("privmsg", "", target, [msg])
def handler(self, **args): """ lock and unlock factoids """ import priv, time from irclib import Event, nm_to_n # Change the target based on how we were called target = self.return_to_sender(args) # Strip off the first word and then take the first word of # what's left to get our action, lock or unlock action = self.strip_words(args["text"], 1).split(" ", 1)[0] # Every word after the first two is the factoid key factoid_key = self.strip_words(args["text"], 2) if action == "lock": # We allow people to lock their own factoid, or, if they have # lock_priv, whatever they want if factoid_key == nm_to_n(args["source"]) \ or priv.checkPriv(args["source"], "lock_priv") == 1: FactoIds.lock(factoid_key, args["source"]) msg = "Factoid \"%s\" locked." % factoid_key else: msg = "You can't lock this factoid." elif action == "unlock": # For unlocking, first check who locked the factoid we are # talking about locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: msg = "Factoid \"%s\" not found." % factoid_key else: # if we aren't locked, tell them, otherwise, check if the factoid # is for the requester's nick or if they have lock_priv (which # allows unlocking as well) if not locked_by: msg = "Factoid \"%s\" is not locked." % factoid_key elif locked_by != args["source"] and not priv.checkPriv(args["source"], "lock_priv"): msg = "You cannot unlock this factoid." else: FactoIds.unlock(factoid_key, args["source"]) msg = "Factoid \"%s\" unlocked." % factoid_key return Event("privmsg", "", target, [ msg ])
def handler(self, **args): "Remove a factoid" import priv from irclib import Event # If we got this via a /msg, /msg back, otherwise back to the channel # it goes target = self.return_to_sender(args) # We are normally called with "moobot: forget foo", but the factoid key # is just the part after "moobot: forget", so we strip that here factoid_key = self.strip_words(args["text"], 2) requester = args["source"] locked_by = FactoIds.getLockedBy(factoid_key) created_by = FactoIds.getCreatedBy(factoid_key) if locked_by == None or created_by == None: msg = "Factoid \"%s\" does not exist." % factoid_key return Event("privmsg", "", target, [ msg ]) self.debug("created_by = ", created_by) self.debug("requester = ", requester) # If the factoid is locked, or the person doesn't have the right to # delete things anyway, tell them they can't. ... unless they created # it. if locked_by: msg = "Factoid \"%s\" is locked." % factoid_key return Event("privmsg", "", target, [ msg ]) if (priv.checkPriv(requester, "delete_priv") == 0) \ and (created_by != requester): msg = "You do not have delete privileges." return Event("privmsg", "", target, [ msg ]) # If we made it here, they can delete it. So delete it, and tell # them. FactoIds.delete(factoid_key) msg = "Factoid \"%s\" deleted." % factoid_key return Event("privmsg", "", target, [ msg ])
def handler(self, **args): "Remove a factoid" import priv from irclib import Event # If we got this via a /msg, /msg back, otherwise back to the channel # it goes target = self.return_to_sender(args) # We are normally called with "moobot: forget foo", but the factoid key # is just the part after "moobot: forget", so we strip that here factoid_key = self.strip_words(args["text"], 2) requester = args["source"] locked_by = FactoIds.getLockedBy(factoid_key) created_by = FactoIds.getCreatedBy(factoid_key) if locked_by == None or created_by == None: msg = "Factoid \"%s\" does not exist." % factoid_key return Event("privmsg", "", target, [msg]) self.debug("created_by = ", created_by) self.debug("requester = ", requester) # If the factoid is locked, or the person doesn't have the right to # delete things anyway, tell them they can't. ... unless they created # it. if locked_by: msg = "Factoid \"%s\" is locked." % factoid_key return Event("privmsg", "", target, [msg]) if (priv.checkPriv(requester, "delete_priv") == 0) \ and (created_by != requester): msg = "You do not have delete privileges." return Event("privmsg", "", target, [msg]) # If we made it here, they can delete it. So delete it, and tell # them. FactoIds.delete(factoid_key) msg = "Factoid \"%s\" deleted." % factoid_key return Event("privmsg", "", target, [msg])
def handler(self, **args): """Allows someone to alter material in a factoid by using a regular expression. Invoked like: "moobot: foo =~ s/moo/bar/".""" import priv, re, time from irclib import Event target = self.return_to_sender(args) # Grab the factoid to change: # first, drop the bot name factoid_key = " ".join(args["text"].split()[1:]) # Now split on " =~ " and take the left half factoid_key = factoid_key.split(" =~ ", 1)[0] locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: message = "Factoid '%s' does not exist." % factoid_key return Event("privmsg", "", target, [message]) # Check for the appropriate privilege (delete_priv, even # though we aren't deleting - we are modifying, and there # is no modify priv) if priv.checkPriv(args["source"], "delete_priv") == 0 and (locked_by != "" and locked_by != args["source"]): message = "You do not have permission to modify factoid '%s." % factoid_key return Event("privmsg", "", target, [message]) # get the original factoid value factoid = FactoIds.getValueByKey(factoid_key) # Grab the regex(es) to apply # First split on =~, then split on spaces for the RHS regexes_str = args["text"].split(" =~ ", 1)[1] # Now we can't split just on spaces, because the regex itself # can have spaces in it. We gotta grab each one individually # because a regex to get them all (potentially each having # unique separators, like "s/foo/bar blatz/g s:moo:mooooo!:i # s%this%makes it hard to parse%". # # The basic algorithm is: # 1 - find out the separator character (2nd) # 2 - find the index of the third occurrence of this character # 3 - find the next space, chop off everything before it and # append it to the regex_list regex_list = [] # regex for removing leading spaces, compiling it first lead_spaces = re.compile("^\s+") while len(regexes_str) > 2: # Strip leading spaces first (so regexes with many spaces # between them don't mess things up when we assume the separator # is the second character) regexes_str = lead_spaces.sub("", regexes_str) # Separator = 2nd char separator = regexes_str[1] # Find it once, then use that as a low range # NOTE - if there is garbage at any point, ignore everything # after it second_sep = regexes_str.find(separator, 2) if second_sep == 0: break third_sep = regexes_str.find(separator, second_sep+1) if third_sep == 0: break # now the space space_index = regexes_str.find(" ", third_sep) if space_index == -1: # no space found regex_list.append(regexes_str) break else: regex_list.append(regexes_str[:space_index]) regexes_str = regexes_str[space_index:] # apply each of the regexes in order # For now we are assuming all of the regexes are search and replace # s/foo/bar/[gi] ACCEPTABLE_PREFIXES = "sy" ACCEPTABLE_SUFFIXES = "gi" for regex_string in regex_list: # Split the regex into parts - strictly, we split on the second # character, as s:foo:bar: is valid as well try: parts = regex_string.split(regex_string[1]) except IndexError: break # If we don't get four parts (even if the last is empty) # then it's not a valid regex - chastise them ... also, # if it's not one of the valid prefixes/suffixes, chastise them :) if len(parts) != 4 or \ parts[0] not in ACCEPTABLE_PREFIXES: message = "Invalid regular expression: " + regex_string return Event("privmsg", "", target, [message]) for letter in parts[3]: if letter not in ACCEPTABLE_SUFFIXES: message = "Invalid regular expression: " + regex_string return Event("privmsg", "", target, [message]) # Get some flags before we compile a regex if "g" in parts[3]: count = 0 else: count = 1 if "i" in parts[3]: case_sensitive = 0 else: case_sensitive = 1 # Make a regex object for the first pattern try: re_str = parts[1] if case_sensitive: regex = re.compile(re_str) else: regex = re.compile(re_str, re.I) except re.error: message = "Invalid regular expression: " + regex_string return Event("privmsg", "", target, [message]) # Actually perform the transformation if parts[0] == "s": factoid = regex.sub(parts[2], factoid) elif parts[0] == "y": message = "This regex not yet supported. Sorry :(" return Event("privmsg", "", target, [message]) # When all the regexes are applied, store the factoid again # with the new date as the modification date. FactoIds.update(factoid_key, factoid, args["source"]) return Event("privmsg", "", target, ["ok"])
def handler(self, **args): """Allows someone to alter material in a factoid by using a regular expression. Invoked like: "moobot: foo =~ s/moo/bar/".""" import priv, re, time from irclib import Event target = self.return_to_sender(args) # Grab the factoid to change: # first, drop the bot name factoid_key = " ".join(args["text"].split()[1:]) # Now split on " =~ " and take the left half factoid_key = factoid_key.split(" =~ ", 1)[0] locked_by = FactoIds.getLockedBy(factoid_key) if locked_by == None: message = "Factoid '%s' does not exist." % factoid_key return Event("privmsg", "", target, [message]) # Check for the appropriate privilege (delete_priv, even # though we aren't deleting - we are modifying, and there # is no modify priv) if priv.checkPriv(args["source"], "delete_priv") == 0 and ( locked_by != "" and locked_by != args["source"]): message = "You do not have permission to modify factoid '%s." % factoid_key return Event("privmsg", "", target, [message]) # get the original factoid value factoid = FactoIds.getValueByKey(factoid_key) # Grab the regex(es) to apply # First split on =~, then split on spaces for the RHS regexes_str = args["text"].split(" =~ ", 1)[1] # Now we can't split just on spaces, because the regex itself # can have spaces in it. We gotta grab each one individually # because a regex to get them all (potentially each having # unique separators, like "s/foo/bar blatz/g s:moo:mooooo!:i # s%this%makes it hard to parse%". # # The basic algorithm is: # 1 - find out the separator character (2nd) # 2 - find the index of the third occurrence of this character # 3 - find the next space, chop off everything before it and # append it to the regex_list regex_list = [] # regex for removing leading spaces, compiling it first lead_spaces = re.compile("^\s+") while len(regexes_str) > 2: # Strip leading spaces first (so regexes with many spaces # between them don't mess things up when we assume the separator # is the second character) regexes_str = lead_spaces.sub("", regexes_str) # Separator = 2nd char separator = regexes_str[1] # Find it once, then use that as a low range # NOTE - if there is garbage at any point, ignore everything # after it second_sep = regexes_str.find(separator, 2) if second_sep == 0: break third_sep = regexes_str.find(separator, second_sep + 1) if third_sep == 0: break # now the space space_index = regexes_str.find(" ", third_sep) if space_index == -1: # no space found regex_list.append(regexes_str) break else: regex_list.append(regexes_str[:space_index]) regexes_str = regexes_str[space_index:] # apply each of the regexes in order # For now we are assuming all of the regexes are search and replace # s/foo/bar/[gi] ACCEPTABLE_PREFIXES = "sy" ACCEPTABLE_SUFFIXES = "gi" for regex_string in regex_list: # Split the regex into parts - strictly, we split on the second # character, as s:foo:bar: is valid as well try: parts = regex_string.split(regex_string[1]) except IndexError: break # If we don't get four parts (even if the last is empty) # then it's not a valid regex - chastise them ... also, # if it's not one of the valid prefixes/suffixes, chastise them :) if len(parts) != 4 or \ parts[0] not in ACCEPTABLE_PREFIXES: message = "Invalid regular expression: " + regex_string return Event("privmsg", "", target, [message]) for letter in parts[3]: if letter not in ACCEPTABLE_SUFFIXES: message = "Invalid regular expression: " + regex_string return Event("privmsg", "", target, [message]) # Get some flags before we compile a regex if "g" in parts[3]: count = 0 else: count = 1 if "i" in parts[3]: case_sensitive = 0 else: case_sensitive = 1 # Make a regex object for the first pattern try: re_str = parts[1] if case_sensitive: regex = re.compile(re_str) else: regex = re.compile(re_str, re.I) except re.error: message = "Invalid regular expression: " + regex_string return Event("privmsg", "", target, [message]) # Actually perform the transformation if parts[0] == "s": factoid = regex.sub(parts[2], factoid) elif parts[0] == "y": message = "This regex not yet supported. Sorry :(" return Event("privmsg", "", target, [message]) # When all the regexes are applied, store the factoid again # with the new date as the modification date. FactoIds.update(factoid_key, factoid, args["source"]) return Event("privmsg", "", target, ["ok"])