XChat 2.0 Plugin Interface

This interface is still under development and may be changed!
Latest version of this document is available at: http://xchat.org/docs/plugin20.html

Information:

Introduction
Sample plugin
What is word and word_eol?
Lists and fields

Functions:

xchat_hook_command
xchat_hook_print
xchat_hook_server
xchat_hook_timer
xchat_unhook

xchat_command
xchat_commandf
xchat_print
xchat_printf

xchat_find_context
xchat_get_context
xchat_get_info
xchat_set_context

xchat_nickcmp

xchat_list_get
xchat_list_free
xchat_list_fields
xchat_list_next
xchat_list_str
xchat_list_int

Introduction

Plugins for XChat are written in C. The interface aims to keep 100% binary compatability. This means that if you upgrade XChat, you will not need to recompile your plugins, they'll continue to work. The interface doesn't depend on any structures and offsets, so compiler versions shouldn't have an impact either. The only real requirement of an XChat plugin, is that it define a "xchat_plugin_init" symbol. This is your entry point function, see the example below. You should make all your global variables and functions static, so that a symbol is not exported. There is probably no harm in exporting these symbols, but they are not necessary. Plugins are compiled as shared objects (.so files), for example:
	gcc -Wall -O1 -shared myplugin.c -o myplugin.so

Sample plugin

This simple plugin adds one extra command: /LS. When executed with a parameter, it will execute /exec ls <parameter>. When executed without any paramaters it will execute /exec ls. All XChat plugins must define a "xchat_plugin_init" function! Defining a xchat_plugin_deinit is optional.

#include "xchat-plugin.h"

static xchat_plugin *ph;	/* plugin handle */

static int ls_cb(char *word[], char *word_eol[], void *userdata)
{
	char *dir;

	dir = word[2];	/* the directory is the 1st arg (2nd word) */
	if(dir[0])
		xchat_commandf(ph, "exec ls %s", dir);
	else
		xchat_command(ph, "exec ls");

	return EAT_ALL;	/* eat this command so xchat and other plugins can't process it */
}

int xchat_plugin_init(xchat_plugin *plugin_handle,
                      char **plugin_name,
                      char **plugin_desc,
                      char **plugin_version,
                      char *arg)
{
	/* we need to save this for use with any xchat_* functions */
	ph = plugin_handle;

	*plugin_name = "LS plugin";
	*plugin_desc = "Adds an /LS command to give a directory listing";
	*plugin_version = "0.1";

	xchat_hook_command(ph, "ls", PRI_NORM, ls_cb, "/ls [dir], Does a directory listing", 0);
	xchat_print(ph, "LS Plugin loaded successfully!\n");

	return 1;       /* return 1 for success */
}

What's word and word_eol?

They are arrays of strings. They contain the parameters the user entered for the particular command. For example, if you executed:
/command NICK hi there

word[1] is command
word[2] is NICK
word[3] is hi
word[4] is there

word_eol[1] is command NICK hi there
word_eol[2] is NICK hi there
word_eol[3] is hi there
word_eol[4] is there
These arrays are simply provided for your convenience. You are NOT allowed to alter them. Both arrays are limited to 32 elements (index 31).


Lists and Fields

Lists of information (DCCs, Channels, Userlist etc) can be retreived with xchat_list_get. All fields are READ ONLY and must be copied if needed for a long time after calling xchat_list_str. The types of lists and fields available are:
"channels" - list of channels, querys and their servers.
NameDescriptionType
channelChannel or query namestring
context(xchat_context *) pointer. Can be used with xchat_set_contextstring
serverServer name to which this channel belongsstring
"dcc" - list of DCC file transfers. Fields:
NameDescriptionType
cpsBytes per second (speed)int
destfileDestination full pathnamestring
fileFile namestring
nickNickname of person who the file is from/tostring
posBytes sent/receivedint
resumePoint at which this file was resumed (or zero if it was not resumed)int
sizeFile size in bytesint
statusDCC Status: 0-Queued 1-Active 2-Failed 3-Done 4-Connecting 5-Abortedint
typeDCC Type: 0-Send 1-Receive 2-ChatRecv 3-ChatSendint
"ignore" - current ignore list.
NameDescriptionType
maskIgnore mask. .e.g: *!*@*.aol.comstring
flagsBit field of flags. 0=Private 1=Notice 2=Channel 3=Ctcp
4=Invite 5=UnIgnore 6=NoSave
int
"users" - list of users in the current channel.
NameDescriptionType
nickNick namestring
hostHost name in the form: user@host (or NULL if not known).string
prefixPrefix character, .e.g: @ or +. Points to a single char.string
Example:
{
	xchat_list *list;

	list = xchat_list_get(ph, "dcc");

	if(list)
	{
		xchat_print(ph, "--- DCC LIST ------------------\n"
				"File  To/From   KB/s   Position\n");

		while(xchat_list_next(ph, list))
		{
			xchat_printf(ph, "%6s %10s %.2f  %d\n",
				 xchat_list_str(ph, list, "file"),
				 xchat_list_str(ph, list, "nick"),
				 xchat_list_int(ph, list, "cps") / 1024,
				 xchat_list_int(ph, list, "pos"));
		}

		xchat_list_free(ph, list);
	}
}



 xchat_hook_command() 

Prototype: xchat_hook *xchat_hook_command(xchat_plugin *ph, char *name, int pri, xchat_cmd_cb *callb, char *help_text, void *userdata);

Description: Adds a new /command.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
name: Name of the command (without the forward slash).
pri: Priority of this command. Use PRI_NORM.
callb: Callback function. This will be called when the user executes the given command name.
help_text: String of text to display when the user executes /help for this command. May be NULL if you're lazy.
userdata: Pointer passed to the callback function.
Returns: Pointer to the hook. Can be passed to xchat_unhook.

Example:
static int onotice_cb(char *word[], char *word_eol[], void *userdata)
{
	if(word_eol[2][0] == 0)
	{
		xchat_printf(ph, "Second arg must be the message!\n");
		return EAT_ALL;
	}

	xchat_commandf(ph, "NOTICE @%s :%s", xchat_get_info(ph, "channel"), word_eol[2]);
	return EAT_ALL;
}

xchat_hook_command(ph, "ONOTICE", PRI_NORM, onotice_cb, "/ONOTICE <message> Sends a notice to all ops", NULL);

 xchat_hook_print() 

Prototype: xchat_hook *xchat_hook_print(xchat_plugin *ph, char *name, int pri, xchat_print_cb *callb, void *userdata);

Description: Registers a function to trap any print events. The event names may be any available in the "Edit Event Texts" window.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
name: Name of the print event (as in Edit Event Texts Window).
pri: Priority of this command. Use PRI_NORM.
callb: Callback function. This will be called when this event name is printed.
userdata: Pointer passed to the callback function.
Returns: Pointer to the hook. Can be passed to xchat_unhook.

Example:
static int youpart_cb(char *word[], void *userdata)
{
	xchat_printf(ph, "You have left channel %s\n", word[3]);
	return EAT_XCHAT;	/* dont let xchat do its normal printing */
}

xchat_hook_print(ph, "You Part", PRI_NORM, youpart_cb, NULL);

 xchat_hook_server() 

Prototype: xchat_hook *xchat_hook_server(xchat_plugin *ph, char *name, int pri, xchat_serv_cb *callb, void *userdata);

Description: Registers a function to be called when a certain server event occurs. You can use this to trap PRIVMSG, NOTICE, PART, a server numeric etc...

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
name: Name of the server event.
pri: Priority of this command. Use PRI_NORM.
callb: Callback function. This will be called when this event is received from the server.
userdata: Pointer passed to the callback function.
Returns: Pointer to the hook. Can be passed to xchat_unhook.

Example:
static int kick_cb(char *word[], char *word_eol[], void *userdata)
{
	xchat_printf(ph, "%s was kicked from %s (reason=%s)\n", word[4], word[3], word_eol[5]);
	return EAT_NONE;	/* don't eat this event, let other plugins and xchat see it too */
}

xchat_hook_server(ph, "KICK", PRI_NORM, kick_cb, NULL);

 xchat_hook_timer() 

Prototype: xchat_hook *xchat_hook_timer(xchat_plugin *ph, int timeout, xchat_timer_cb *callb, void *userdata);

Description: Registers a function to be called every "timeout" milliseconds.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
timeout: Timeout in milliseconds (1000 is 1 second).
callb: Callback function. This will be called every "timeout" milliseconds.
userdata: Pointer passed to the callback function.
Returns: Pointer to the hook. Can be passed to xchat_unhook.

Example:
static xchat_hook *myhook;

static int stop_cb(char *word[], char *word_eol[], void *userdata)
{
	if(myhook != NULL)
	{
		xchat_unhook(ph, myhook);
		myhook = NULL;
		xchat_print(ph, "Timeout removed!\n");
	}

	return EAT_ALL;
}

static int timeout_cb(void *userdata)
{
	xchat_print(ph, "Annoying message every 5 seconds! Type /STOP to stop it.\n");
	return 1;	/* return 1 to keep the timeout going */
}

myhook = xchat_hook_timer(ph, 5000, timeout_cb, NULL);
xchat_hook_command(ph, "STOP", PRI_NORM, stop_cb, NULL, NULL);

 xchat_unhook() 

Prototype: void *xchat_unhook(xchat_plugin *ph, xchat_hook *hook);

Description: Unhooks any hook registered with xchat_hook_print/server/timer/command.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
hook: Pointer to the hook, as returned by xchat_hook_*.
Returns: The userdata you originally gave to xchat_hook_*.

 xchat_command() 

Prototype: void xchat_command(xchat_plugin *ph, char *command);

Description: Executes a command as if it were typed in xchat's input box.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
command: Command to execute, without the forward slash "/".

 xchat_commandf() 

Prototype: void xchat_commandf(xchat_plugin *ph, char *format, ...);

Description: Executes a command as if it were typed in xchat's input box and provides string formating like printf.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
format: The format string.

 xchat_print() 

Prototype: void xchat_print(xchat_plugin *ph, char *text);

Description: Prints some text to the current tab/window.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
text: Text to print. May contain mIRC color codes.

 xchat_printf() 

Prototype: void xchat_printf(xchat_plugin *ph, char *format, ...);

Description: Prints some text to the current tab/window and provides formating like printf.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
format: The format string.

 xchat_find_context() 

Prototype: xchat_context *xchat_find_context(xchat_plugin *ph, char *servname, char *channel);

Description: Finds a context based on a channel and servername. If servname is NULL, it finds any channel (or query) by the given name. If channel is NULL, it finds the front-most tab/window of the given servname.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
servname: Servername or NULL.
channel: Channelname or NULL.
Returns: Context pointer (for use with xchat_set_context) or NULL.


 xchat_get_context() 

Prototype: xchat_context *xchat_get_context(xchat_plugin *ph);

Description: Returns the current context for your plugin. You can use this later with xchat_set_context.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
Returns: Context pointer (for use with xchat_set_context).


 xchat_get_info() 

Prototype: const char *xchat_get_info(xchat_plugin *ph, char *id);

Description: Returns information based on your current context.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
id: ID of the information you want. Currently supported IDs are (case sensitive):
awayaway reason or NULL if you are not away.
channelcurrent channel name.
hostreal hostname of the server you connected to.
nickyour current nick name.
servercurrent server name (what the server claims to be). NULL if you are not connected.
topiccurrent channel topic.
versionxchat version number.
xchatdirxchat config directory, e.g.: /home/user/.xchat.
Returns: A string of the requested information, or NULL. This string must not be freed and must be copied if needed after the call to xchat_get_info.


 xchat_set_context() 

Prototype: int xchat_set_context(xchat_plugin *ph, xchat_context *ctx);

Description: Changes your current context to the one given.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
ctx: Context to change to (obtained with xchat_get_context or xchat_find_context).
Returns: 1 for success, 0 for failure.


 xchat_nickcmp() 

Prototype: int xchat_nickcmp(xchat_plugin *ph, char *s1, char *s2);

Description: Performs a RFC1459 compliant string compare. Use this to compare channels and nicknames. The function works the same way as strcasecmp.

Arguments:
ph: Plugin handle (as given to xchat_plugin_init).
s1: String to compare.
s2: String to compare s1 to.
Quote from RFC1459:
Because of IRC's scandanavian origin, the characters {}| are considered to be the lower case equivalents of the characters []\, respectively. This is a critical issue when determining the equivalence of two nicknames.
Returns: An integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.