Esempio n. 1
0
    def handler(self, **args):
        """ gets a random factoid """
        from irclib import nm_to_n, Event

        # Standard return_to_sender target, talk to who talks to us
        target = self.return_to_sender(args)

        factoids = FactoIds.getRandoms()
        if not factoids:
            return Event("privmsg", "", target, ["no factoid defined"])

        factoid = factoids[0]
        factoid_key = factoid[0]
        factoid_value = FactoIds.parseSar(factoid[1].lstrip())
        factoid_value = factoid_value.replace("$who", nm_to_n(args["source"]))

        return Event("privmsg", "", target, \
         [ "Random factoid: %s is %s" % (factoid_key,
          factoid_value.strip()) ])
Esempio n. 2
0
	def handler(self, **args):
		""" gets a random factoid """
		from irclib import nm_to_n, Event


		# Standard return_to_sender target, talk to who talks to us
		target = self.return_to_sender(args)

		factoids = FactoIds.getRandoms()
		if not factoids:
			return Event("privmsg", "", target, [ "no factoid defined" ])

		factoid = factoids[0]
		factoid_key = factoid[0]
		factoid_value = FactoIds.parseSar(factoid[1].lstrip())
		factoid_value = factoid_value.replace("$who", nm_to_n(args["source"]))

		return Event("privmsg", "", target, \
			[ "Random factoid: %s is %s" % (factoid_key,
				factoid_value.strip()) ])
Esempio n. 3
0
	def handler(self, **args):
		""" gets the factoid_value field """
		import time
		from irclib import Event, nm_to_n

		# Store the ref
# 		ref = args["ref"]()

		# If we were /msg'ed this, /msg it back, otherwise send it to the
		# channel
		# (set up here because there are some rather early returns for the
		# continue handler types and whatnot)
		target = self.return_to_sender(args)

		factoid_key = args["text"]
		factoid_key = self.strip_words(factoid_key, 1) # removes "moobot: "

		# By default we want to parse the factoid value after grabbing it from
		# the db, but certain flags along the way may set this flag so that we
		# don't do so (e.g., "literal").
		dont_parse = 0

		# If we are called with something like "moobot: literal foo", we want
		# to strip the "literal" from it as that is not part of the
		# factoid, unless of course nothing follows "literal", in which
		# case we would be looking for the factoid for "literal" 
		if len(args["text"].split(" ")) > 1 and \
			args["text"].split(" ")[1] == "literal":
				dont_parse = 1	# set for later, when we spit the value back
				factoid_key = self.strip_words(factoid_key, 1)

		# Strip trailing ?'s and !'s (so asking for "moobot: foo?!?!?!???" is
		# the same as just asking for "moobot: foo" 
		factoid_key = self.strip_punctuation(factoid_key)

		text = FactoIds.getValueByKey(factoid_key, args["source"])

		# If the factoid doesn't exist, simply continue trying
		# to match with other handlers using the continue event type 
		if not text:
			return Event("continue", "", target, [""])

		### The new length stuff
		# Here we check to see if the total message length would be greater
		# than irclib.MESSAGE_SIZE_LIMIT, and if so, split it up accordingly
		# and return the requested one, or print out a warning.

		# Message format:
		# nickname!username@localhost privmsg #channel_name :factoid_text
# 		self.debug("%s!%s@%s" % (ref.connection.ircname, 
# 			ref.connection.username, ref.connection.localhost))
# #		msg_length = len(text) + len(" privmsg # :") + len(target) + \
# #			len(ref.connection.nickname) + len(ref.connection.username) + \
# #			len(ref.connection.localhost)
# #		self.debug(msg_length)


		# by default we will just say something to the target, but if the
		# factoid contains an <action> tag, we will make it an action
		# eventtype 
		eventtype="privmsg"

		# If the person says something like "moobot: literal foo", we don't
		# want to parse the factoid, we just want to spit it back in its raw
		# form.  Otherwise, we want to replace parentheses and pipes as well
		# as see if we need to change the eventtype 
		if not dont_parse:  # awkward, but supports the more general case,
							# instead of having to set a flag for every case
							# where we DO want to parse, only set a flag where
							# we do NOT parse
			
			# Strip spaces from the left-hand side
			text = text.lstrip()

			# Parse parentheses and pipes to come up with one random string
			# from many choices specified in the factoid 
			text = FactoIds.parseSar(text)

			# Replace $who and $nick with the person requesting the factoid
			text = text.replace("$who", nm_to_n(args["source"]))
			if args.has_key("channel"):
				text = text.replace("$chan", args["channel"])
			text = text.replace("$nick", nm_to_n(args["source"]))

			# If the new string (after previous replacements) begins with
			# "<action>" or "<reply>" (case insensitive), then we strip them
			# and possibly change the eventtype if necessary.  Otherwise, we
			# simply say "foo is bar" back to the target. 
			if text[:8].lower() == "<action>":
				eventtype="action"
				text = text[8:]
			elif text[:7].lower() == "<reply>":
				text = text[7:]
			else:
				text = factoid_key + " is " + text
	
		return Event(eventtype, "", target, [ text.strip() ])
Esempio n. 4
0
    def handler(self, **args):
        """ gets the factoid_value field """
        import time
        from irclib import Event, nm_to_n

        # Store the ref
        # 		ref = args["ref"]()

        # If we were /msg'ed this, /msg it back, otherwise send it to the
        # channel
        # (set up here because there are some rather early returns for the
        # continue handler types and whatnot)
        target = self.return_to_sender(args)

        factoid_key = args["text"]
        factoid_key = self.strip_words(factoid_key, 1)  # removes "moobot: "

        # By default we want to parse the factoid value after grabbing it from
        # the db, but certain flags along the way may set this flag so that we
        # don't do so (e.g., "literal").
        dont_parse = 0

        # If we are called with something like "moobot: literal foo", we want
        # to strip the "literal" from it as that is not part of the
        # factoid, unless of course nothing follows "literal", in which
        # case we would be looking for the factoid for "literal"
        if len(args["text"].split(" ")) > 1 and \
         args["text"].split(" ")[1] == "literal":
            dont_parse = 1  # set for later, when we spit the value back
            factoid_key = self.strip_words(factoid_key, 1)

        # Strip trailing ?'s and !'s (so asking for "moobot: foo?!?!?!???" is
        # the same as just asking for "moobot: foo"
        factoid_key = self.strip_punctuation(factoid_key)

        text = FactoIds.getValueByKey(factoid_key, args["source"])

        # If the factoid doesn't exist, simply continue trying
        # to match with other handlers using the continue event type
        if not text:
            return Event("continue", "", target, [""])

        ### The new length stuff
        # Here we check to see if the total message length would be greater
        # than irclib.MESSAGE_SIZE_LIMIT, and if so, split it up accordingly
        # and return the requested one, or print out a warning.

        # Message format:
        # nickname!username@localhost privmsg #channel_name :factoid_text


# 		self.debug("%s!%s@%s" % (ref.connection.ircname,
# 			ref.connection.username, ref.connection.localhost))
# #		msg_length = len(text) + len(" privmsg # :") + len(target) + \
# #			len(ref.connection.nickname) + len(ref.connection.username) + \
# #			len(ref.connection.localhost)
# #		self.debug(msg_length)

# by default we will just say something to the target, but if the
# factoid contains an <action> tag, we will make it an action
# eventtype
        eventtype = "privmsg"

        # If the person says something like "moobot: literal foo", we don't
        # want to parse the factoid, we just want to spit it back in its raw
        # form.  Otherwise, we want to replace parentheses and pipes as well
        # as see if we need to change the eventtype
        if not dont_parse:  # awkward, but supports the more general case,
            # instead of having to set a flag for every case
            # where we DO want to parse, only set a flag where
            # we do NOT parse

            # Strip spaces from the left-hand side
            text = text.lstrip()

            # Parse parentheses and pipes to come up with one random string
            # from many choices specified in the factoid
            text = FactoIds.parseSar(text)

            # Replace $who and $nick with the person requesting the factoid
            text = text.replace("$who", nm_to_n(args["source"]))
            if args.has_key("channel"):
                text = text.replace("$chan", args["channel"])
            text = text.replace("$nick", nm_to_n(args["source"]))

            # If the new string (after previous replacements) begins with
            # "<action>" or "<reply>" (case insensitive), then we strip them
            # and possibly change the eventtype if necessary.  Otherwise, we
            # simply say "foo is bar" back to the target.
            if text[:8].lower() == "<action>":
                eventtype = "action"
                text = text[8:]
            elif text[:7].lower() == "<reply>":
                text = text[7:]
            else:
                text = factoid_key + " is " + text

        return Event(eventtype, "", target, [text.strip()])