Beispiel #1
0
def processChannelMessage(dictionary, ial):
	"""
	This function was specified in loadMe() as the handler for "Channel Message"
	and "Channel Message Local" Event Dictionaries. As such, it will be called
	each time one of these Event Dictionaries is passed through PyRC's plugin
	chain.
	
	@type dictionary: dict
	@param dictionary: The Event Dictionary to be processed.
	@type ial: function
	@param ial: A reference to the IRC Abstraction Layer's interface.
	
	@rtype: dict|None
	@return: Nothing, at least in this case. A Raise Event Event Dictionary
	    could be returned if this function needed to alter the way in which
	    PyRC's plugin chain processed this event.
	"""
	handling_data = pyrc.handleChannelMessage(dictionary, _ALLOWED_SOURCES)
	#The function above returns None if the event isn't from an allowed source.
	#In Python, anything that can be considered a form of NoneType, such as an
	#empty string, empty container, 0, False, and None, can be matched with the
	#following 'if not' syntax.
	if not handling_data:
		return
		
	#This applies the regular expression specified above to the received string
	#to find out if it is a request for 8-Squall.
	match = _RESPONSE_REGEXP.match(dictionary['message'])
	if match:
		(user_name, local) = handling_data #This breaks the tuple into a string and a bool. Indexing would work, too, but this is more clear.
		
		message = None #This specifies a variable used to store the message
		               #string when it is generated. It is declared here because
					   #any variables declared in a sub-scope, like an 'if' or
					   #anything else that gets indented, save for 'try's, is
					   #valid only within that sub-scope, and this value is
					   #needed later.
		if match.group(1) != ':' and random.randint(0, 2) == 2:
			#8-Squall mode is used only one third of the time. The user may use
			#a colon, as in '8-Squall: x?' to force 8-Ball mode.
			message = user_name + ", 8-Squall speaks: " + random.choice(_RESPONSES_8SQUALL)
		else:
			message = user_name + ", 8-Squall's 8-Ball has concluded: " + random.choice(_RESPONSES_8BALL)
			
		#This causes PyRC to send the response to the IRC network. Easy.
		pyrc.respondToChannelMessage(message, dictionary)
Beispiel #2
0
def processChannelMessage(dictionary, ial):
	if not pyrc.handleChannelMessage(dictionary, _ALLOWED_SOURCES):
		return
		
	match = _CALC_REGEXP.match(dictionary['message'])
	if match:
		message = match.group(1).lower()
		if message == 'list':
			session = calc.Session()
			pyrc.respondToChannelMessage("Built-ins: %s | %s" % (', '.join(session.listFunctions()), ', '.join(session.listVariables())), dictionary)
		elif message == 'help':
			pyrc.respondToChannelMessage("Usage: '!calc <variable|function|equation>[;...]|list' Order does not matter.", dictionary)
		else:
			try:
				session = calc.Session(str(match.group(1)))
				(variables, equations) = session.evaluate()
				
				if equations:
					for (equation, value) in equations:
						try:
							i_value = int(value)
							if i_value == value:
								value = i_value
						except:
							pass
						pyrc.respondToChannelMessage("%s = %s" % (equation, value), dictionary)
				else:
					pyrc.respondToChannelMessage("No expressions provided.", dictionary)
			except calc.Error, e:
				pyrc.respondToChannelMessage("%s: %s" % (e.__class__.__name__, e), dictionary)
			except Exception, e:
				pyrc.respondToChannelMessage("%s: %s" % (e.__class__.__name__, e), dictionary)