From LQWiki
Jump to: navigation, search

normally the best to get started is to play a bit around with a simple script and alter it ..

a barebone structure of a xchat python script could be:

import xchat

# name <my@email>
# date 
# Released under the GPL
# prints hi on every channel message
# and uses the word parameters to redouble the intercepted msg
__module_name__ = "printHi"
__module_version__ = "1.0"
__module_description__ = "prints hi on channel message"

def on_message(word, word_eol, userdata):
	# prints in the tab the hook_print intercepted the chan msg
	xchat.prnt("hi - the user " + word[0] + " said " + word_eol[1])

	# could also be "return xchat.EAT_XCHAT" to not let xchat print the msg

# hooks into the application and waits to get executed on a chan msg
xchat.hook_print("Channel Message", on_message)

#the "Channel Message" parameter comes from the "Text Event" entrys
# .. also determines what gets passed to on_message (parameters word, word_eol, data)
# defined in ~/.xchat/pevents.conf with a display of the content accessible via a menu entry

#shows if the script got loaded
print "script loaded."

a nice way to test interactively the commands you want afterwards use in the script is to place them in the input field /py exec <pythonCommand> as example:

/py exec import xchat
/py exec xchat.prnt("tst")

to print from the script to another tab (as example to summary) is to use the context paradigm where the context needs to exist

in the previous prnt example the "current context" was returned by xchat.

/py exec xchat.command("query summary") 
/py exec myContext = xchat.find_context(channel='summary') 
/py exec myContext.prnt("tst")

(in the days before xchat2 there existed a perl method IRC::print_on_channel() that got now a much more general/nicer replacement with the context paradigm)