示例#1
0
	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"])
示例#2
0
文件: factoids.py 项目: oink/BadGirl
    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"])
示例#3
0
	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"])
示例#4
0
文件: factoids.py 项目: oink/BadGirl
    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"])