Tag: python

Code :: PleasurePoker

Posted by - April 25, 08

This is going to be a day late, but wow that was a long day. Fridays are code days. Personally, I think Fridays are a bit slow in the anime blogosphere, I mean its Friday, time to shed those weekly ambitions and let is ride. It goes without saying that I don’t expect much reading or feedback on these posts, there won’t be pretty pictures. It goes without saying there should be a disclaimer on any code I bring out on Fridays, as it is merely prototype-level stuff. Of course, I wouldn’t mind touching up some hacks if there is a need for it… personally I can work with hacks [and meta-hacks]. Hopefully, the relations to anime, or animeblogging, will be apparent.

Pleasure Poking and Bongo Party

Today’s code is quite simple/redundant, and for anyone that doesn’t want to look at code, I’ll just say what it does. It is a python XChat script (yes, that crazy stuff), which merely listens for the triggers: !bongos or !pokeme, and does a /me command based on the calling nick. Lame right?

pleasure

This has no intention of being an end script, but it does give some insight into useful XChat events, in case anyone wants to do this sort of thing, but doesn’t want to spend 4 hours looking at the documentation. Here is the code:


__module_name__ = "BongoPoke"
__module_version__ = "0.0"
__module_description__ = "Shakes bongos, and pokes for pleasurez"

import xchat, re

def pleasurepoke(word, word_eol, userdata):
	if xchat.get_info('nick')=='PleasurePoker' and re.search("^!pokeme$", word[1])!=None:
		xchat.command("me pokes "+ word[0] +" with pleasurez *ehew*")
		return xchat.EAT_ALL
	return xchat.EAT_NONE

def bongos( word ):
	if re.search("^!bongos$", word[1] ) != None:
		xchat.command("me shakes the bongos for "+ word[0]  +" ~()~");
		return 1
	else:
		return None

def echo_cb(nickchan):
	def f(word, word_eol, userdata):
		if xchat.get_info('nick')+xchat.get_info('channel')==nickchan:
		    res=bongos( word )
		    if res == 1:
		    	return xchat.EAT_ALL
		    else:
		    	return xchat.EAT_NONE
	return f

xchat.prnt("Loading module for " + xchat.get_info('nick') + " in " + xchat.get_info('channel') )
xchat.hook_print( "Channel Message", echo_cb( xchat.get_info('nick')+xchat.get_info('channel') ) )
xchat.hook_print( "Channel Message", pleasurepoke )

The Technicals

So here we have two handlers, pleasurepoke and bongos, which merely do some checking of the word data (state) and execute the xchat.command “me” (action command, we all know this one, the one to not use is say). The third function, echo_cb, is slightly more complex, it creates a function f, based on nickchan and returns the function f, the handler (here it is passing stuff to bongos). In this way, echo_cb is a mini “function factory” which abstracts the flow control that chooses to execute the handler or not. ( This simply means we can restrict the handlers to certain channels, nicks, servers, etc )

Well, I think it is straightforward, so I’ll stop here. Also, some of this is repeated from a previous XChat python post. One step closer to being l33t in all those anime IRC channels right!? This is pretty general use though :p

Ryan A

is un-XChat haxor

Posted by - April 9, 08

Well, for those who were in the #animeblogger IRC earlier today, I apologize for activating a test echo script in XChat, which apparently went ballistic (I wasn’t physically watching the channel and was totally unaware). The trouble was that this script was activated on another server window and in another [empty] channel, but it was handling every event, no matter the server. I just want to put my two-cents out there for anyone who is going to write a python module for XChat, and what NOT to do.

Teh Scriptz

echo like a monkey:

__module_name__ = "testing"
__module_version__ = "0.0"
__module_description__ = "Test mod"

import xchat

def echo_cb(word, word_eol, userdata):
	# DO STUFF HERE, I was doing xchat.command('say ' + word[1])
	return xchat.EAT_NONE

xchat.hook_print("Channel Message", echo_cb)

In the above code, I added a hook (event handler) to the print, for Channel Message. This only means, echo_cb is going to be called on the channel message event, great. The earlier problem arose because echo_cb doesn’t check it’s context, it just goes. I was under the impression that the plugin was context partitioned. Rather, xchat-python is global, so any loaded module will handle any event in xchat. This is good I guess, but I really wish I would have known about that (it isn’t in the doc).

a better method:

For those that may test stuff out, here is a better way to create handlers, so that they stay in “context” and not run amok.

__module_name__ = "testing"
__module_version__ = "0.0"
__module_description__ = "Test mod"

import xchat

def echo_cb(nick,channel):
	def f(word, word_eol, userdata):
		if xchat.get_info('channel')==channel and  xchat.get_info('nick')==nick:
		#DO STUFF HERE
		return xchat.EAT_NONE
	return f

xchat.prnt("Loading module for " + xchat.get_info('nick'))
xchat.hook_print( "Channel Message", echo_cb( 'YOUR NICK', "#CHANNEL TO ACTIVATE" ) )

What this manages is a small part of declarative programming; I am calling echo_cb with a nick and channel name, and echo_cb is returning the handler (f is an inner function being returned). The good news is that even on any channel message event, that if statement will prevent action unless it is from the channel specified and you are using the nick specified.

Had I used the second of these methods, there would not have been a problem since I wasn’t activating the module in AB or on IrcHighway. So, a good lesson.

Ryan A

ps. I wanted to write this earlier, but alas, school, which also made me miss the two UEFA Champions League matches ;.;