=head1 X-Chat 2 Perl Interface =head2 Introduction This is the new Perl interface for X-Chat 2. However, due to changes in xchat's plugin code you will need xchat 2.0.8 or above to load this. Scripts written using the old interface will continue to work. If there are any problems, questions, comments or suggestions please email them to the address on the bottom of this page. =head2 Constants =head3 Priorities =over 3 =item * C =item * C =item * C =item * C =item * C =back =head3 Return values =over 3 =item * C - pass the event along =item * C - don't let xchat see this event =item * C - don't let other scripts and plugins see this event but xchat will still see it =item * C - don't let anything else see this event =back =head4 Timer and fd hooks =over 3 =item * C - keep the timer going or hook watching the handle =item * C - remove the timer or hook watching the handle =back =head3 hook_fd flags =over 3 =item * C - invoke the callback when the handle is ready for reading =item * C - invoke the callback when the handle is ready for writing =item * C - invoke the callback if an exception occurs =item * C - indicate that the handle being hooked is not a socket =back =head2 Functions =head3 C =over 3 =item * C<$name> - The name of this script =item * C<$version> - This script's version =item * C<$description> - A description for this script =item * C<$callback> - This is a function that will be called when the is script unloaded. This can be either a reference to a function or an anonymous sub reference. =back This is the first thing to call in every script. =head3 C =head3 C =head3 C =head3 C =head3 C These functions can be to intercept various events. hook_server can be used to intercept any incoming message from the IRC server. hook_command can be used to intercept any command, if the command doesn't currently exist then a new one is created. hook_print can be used to intercept any of the events listed in Setttings-EAdvanced-EText Events hook_timer can be used to create a new timer =over 3 =item * C<$message> - server message to hook such as PRIVMSG =item * C<$command> - command to intercept, without the leading / =item * C<$event> - one of the events listed in Settings-EAdvanced-EText Events =item * C<$timeout> - timeout in milliseconds =item * C<$handle> - the I/O handle you want to monitor with hook_fd. This must be something that has a fileno. See perldoc -f fileno or L =item * C<$callback> - callback function, this is called whenever the hooked event is trigged, the following are the conditions that will trigger the different hooks. This can be either a reference to a function or an anonymous sub reference. =item * \%options - a hash reference containing addional options for the hooks =back Valid keys for \%options: =begin html
data Additional data that is to be associated with the
hook. For timer hooks this value can be provided either as
Xchat::hook_timer( $timeout, $cb,{data=>$data})
or Xchat::hook_timer( $timeout, $cb, $data ).
However, this means that hook_timer cannot be provided
with a hash reference containing data as a key.
example:
my $options = { data => [@arrayOfStuff] };
Xchat::hook_timer( $timeout, $cb, $options );

In this example, the timer's data will be
[@arrayOfStuff] and not { data => [@arrayOfStuff] }

This key is valid for all of the hook functions.

Default is undef.
priority Sets the priority for the hook.
It can be set to one of the Xchat::PRI_* constants.

This key only applies to server, command and print hooks.

Default is Xchat::PRI_NORM.
help_text Text displayed for /help $command.

This key only applies to command hooks.

Default is "".
flags Specify the flags for a fd hook.

See hook fd flags section for valid values.

On Windows if the handle is a pipe you specify
Xchat::FD_NOTSOCKET in addition to any other flags you might be using.

This key only applies to fd hooks.
Default is Xchat::FD_READ
=end html =head4 When callbacks are invoked Each of the hooks will be triggered at different times depending on the type of hook. =begin html
Hook Type When the callback will be invoked
server hooks a $message message is received from the server
command hooks the $command command is executed, either by the user or from a script
print hooks X-Chat is about to print the message for the $event event
timer hooks called every $timeout milliseconds (1000 millisecond is 1 second)
the callback will be executed in the same context where the hook_timer was called, if the context no longer exists then it will execute in a random context
fd hooks depends on the flags that were passed to hook_fd
See hook_fd flags section.
=end html The value return from these hook functions can be passed to C to remove the hook. =head4 Callback Arguments All callback functions will receive their arguments in C<@_> like every other Perl subroutine. =begin html

Server and command callbacks

$_[0] - array reference containing the IRC message or command and arguments broken into words
example:
/command arg1 arg2 arg3
$_[0][0] - command
$_[0][1] - arg1
$_[0][2] - arg2
$_[0][3] - arg3

$_[1] - array reference containing the Nth word to the last word
example:
/command arg1 arg2 arg3
$_[1][0] - command arg1 arg2 arg3
$_[1][1] - arg1 arg2 arg3
$_[1][2] - arg2 arg3
$_[1][3] - arg3

$_[2] - the data that was passed to the hook function

Print callbacks

$_[0] - array reference containing the values for the text event see Settings->Advanced->Text Events
$_[1] - the data that was passed to the hook function

Timer callbacks

$_[0] - the data that was passed to the hook function

fd callbacks

$_[0] - the handle that was passed to hook_fd
$_[1] - flags indicating why the callback was called
$_[2] - the data that was passed to the hook function

=end html =head4 Callback return values All server, command and print callbacks should return one of the C constants. Timer callbacks can return Xchat::REMOVE to remove the timer or Xchat::KEEP to keep it going =head4 Miscellaneous Hook Related Information For server hooks, if C<$message> is "RAW LINE" then C<$cb> will be called for every IRC message than X-Chat receives. For command hooks if C<$command> is "" then C<$cb> will be called for messages entered by the user that is not a command. For print hooks besides those events listed in Settings-EAdvanced-EText Events, these additional events can be used. =begin html
Event Description
"Open Context" a new context is created
"Close Context" a context has been close
"Focus Tab" when a tab is brought to the front
"Focus Window" when a top level window is focused or the main tab window is focused by the window manager
"DCC Chat Text" when text from a DCC Chat arrives. $_[0] will have these values

$_[0][0] - Address
$_[0][1] - Port
$_[0][2] - Nick
$_[0][3] - Message
"Key Press" used for intercepting key presses
$_[0][0] - key value
$_[0][1] - state bitfield, 1 - shift, 4 - control, 8 - alt
$_[0][2] - string version of the key which might be empty for unprintable keys
$_[0][3] - length of the string in $_[0][2]
=end html =head3 C =over 3 =item * C<$hook> - the hook that was previously returned by one of the C functions =back This function is used to removed a hook previously added with one of the C functions It returns the data that was passed to the C function when the hook was added =head3 C =over 3 =item * C<$text> - the text to print =item * C<\@lines> - array reference containing lines of text to be printed all the elements will be joined together before printing =item * C<$channel> - channel or tab with the given name where C<$text> will be printed =item * C<$server> - specifies that the text will be printed in a channel or tab that is associated with C<$server> =back The first argument can either be a string or an array reference of strings. Either or both of C<$channel> and C<$server> can be undef. If called as C, it will always return true. If called with either the channel or the channel and the server specified then it will return true if a context is found and false otherwise. The text will not be printed if the context is not found. The meaning of setting C<$channel> or C<$server> to undef is the same as L. =head3 C =over 3 =item * C<$format> - a format string, see "perldoc -f L" for further detail =item * LIST - list of values for the format fields =back =head3 C =over 3 =item * C<$command> - the command to execute, without the leading / =item * C<\@commands> - array reference containing a list of commands to execute =item * C<$channel> - channel or tab with the given name where C<$command> will be executed =item * C<$server> - specifies that the command will be executed in a channel or tab that is associated with C<$server> =back The first argument can either be a string or an array reference of strings. Either or both of C<$channel> and C<$server> can be undef. If called as C, it will always return true. If called with either the channel or the channel and the server specified then it will return true if a context is found and false otherwise. The command will not be executed if the context is not found. The meaning of setting C<$channel> or C<$server> to undef is the same as find_context. =head3 C =over 3 =item * C<$format> - a format string, see "perldoc -f L" for further detail =item * LIST - list of values for the format fields =back =head3 C =over 3 =item * C<$channel> - name of a channel =item * C<$server> - name of a server =back Either or both of C<$channel> and $server can be undef. Calling C is the same as calling C and C is the same as C. If C<$server> is undef, find any channel named $channel. If C<$channel> is undef, find the front most window or tab named C<$server>.If both $channel and C<$server> are undef, find the currently focused tab or window. Return the context found for one of the above situations or undef if such a context cannot be found. =head3 C =over 3 =back Returns the current context. =head3 C =over 3 =item * C<$context> - context value as returned from L,L or one of the fields in the list of hashrefs returned by list_get =item * C<$channel> - name of a channel you want to switch context to =item * C<$server> - name of a server you want to switch context to =back See L for more details on C<$channel> and C<$server>. Returns true on success, false on failure =head3 C =over 3 =item * C<$id> - one of the following case sensitive values =back =begin html
ID Return value Associated Command(s)
away away reason or undef if you are not away AWAY, BACK
channel current channel name SETTAB
charset character-set used in the current context CHARSET
event_text <Event Name> text event format string for <Event name>
Example:
my $channel_msg_format = Xchat::get_info( "event_text Channel Message" );
host real hostname of the current server
id connection id
inputbox contents of the inputbox SETTEXT
libdirfs the system wide directory where xchat will look for plugins. this string is in the same encoding as the local file system
modes the current channels modes or undef if not known MODE
network current network name or undef, this value is taken from the Network List
nick current nick NICK
nickserv nickserv password for this network or undef, this value is taken from the Network List
server current server name
(what the server claims to be) undef if not connected
state_cursor current inputbox cursor position in characters SETCURSOR
topic current channel topic TOPIC
version xchat version number
win_status status of the xchat window, possible values are "active", "hidden" and "normal" GUI
win_ptr native window pointer, GtkWindow * on Unix, HWND on Win32.
On Unix if you have the Glib module installed you can use my $window = Glib::Object->new_from_pointer( Xchat::get_info( "win_ptr" ) ); to get a Gtk2::Window object.
Additionally when you have detached tabs, each of the windows will return a different win_ptr for the different Gtk2::Window objects.
See char_count.pl for a longer example of a script that uses this to show how many characters you currently have in your input box.
gtkwin_ptr similar to win_ptr except it will always be a GtkWindow *
configdir HexChat config directory encoded in UTF-8
examples:
/home/user/.config/hexchat
C:\Users\user\Appdata\Roaming\HexChat

This function is used to retrieve certain information about the current context. If there is an associated command then that command can be used to change the value for a particular ID.

=end html =head3 C =over 3 =item * C<$name> - name of a X-Chat setting (available through the /set command) =back This function provides a way to retrieve X-Chat's setting information. Returns C if there is no setting called called C<$name>. =head3 C =over 3 =item * C<$event> - name from the Event column in Settings-EAdvanced-EText Events =item * LIST - this depends on the Description column on the bottom of Settings-EAdvanced-EText Events =back This functions is used to generate one of the events listed under Settings-EAdvanced-EText Events Note: when using this function you MUST return Xchat::EAT_ALL otherwise you will end up with duplicate events. One is the original and the second is the one you emit. Returns true on success, false on failure =head3 C =over 3 =item * C<$target> - a single nick to set the mode on =item * C<\@targets> - an array reference of the nicks to set the mode on =item * C<$sign> - the mode sign, either '+' or '-' =item * C<$mode> - the mode character such as 'o' and 'v', this can only be one character long =item * C<$modes_per_line> - an optional argument maximum number of modes to send per at once, pass 0 use the current server's maximum (default) =back Send multiple mode changes for the current channel. It may send multiple MODE lines if the request doesn't fit on one. Example: =begin html
use strict; use warning; use Xchat qw(:all); hook_command( "MODES", sub { my (undef, $who, $sign, $mode) = @{$_[0]}; my @targets = split /,/, $who; if( @targets > 1 ) { send_modes( \@targets, $sign, $mode, 1 ); } else { send_modes( $who, $sign, $mode ); } return EAT_XCHAT; });
=end html =head3 C =over 3 =item * C<$nick1, $nick2> - the two nicks or channel names that are to be compared =back The comparsion is based on the current server. Either a RFC1459 compliant string compare or plain ascii will be using depending on the server. The comparison is case insensitive. Returns a number less than, equal to or greater than zero if C<$nick1> is found respectively, to be less than, to match, or be greater than C<$nick2>. =head3 C =over 3 =item * C<$name> - name of the list, one of the following: "channels", "dcc", "ignore", "notify", "users" =back This function will return a list of hash references. The hash references will have different keys depend on the list. An empty list is returned if there is no such list. =begin html

"channels" - list of channels, querys and their server

Key Description
channel tab name
chantypes channel types supported by the server, typically "#&"
context can be used with set_context
flags Server Bits:
0 - Connected
1 - Connecting
2 - Away
3 - EndOfMotd(Login complete)
4 - Has WHOX
5 - Has IDMSG (FreeNode)

The following correspond to the /chanopt command

6 - Hide Join/Part Message (text_hidejoinpart)
7 - unused (was for color paste)
8 - Beep on message (alert_beep)
9 - Blink Tray (alert_tray)
10 - Blink Task Bar (alert_taskbar)

Example of checking if the current context has Hide Join/Part messages set:

if( Xchat::context_info->{flags} & (1 << 6) ) { Xchat::print( "Hide Join/Part messages is enabled" ); }
id Unique server ID
lag lag in milliseconds
maxmodes Maximum modes per line
network network name to which this channel belongs
nickprefixes Nickname prefixes e.g. "+@"
nickmodes Nickname mode chars e.g. "vo"
queue number of bytes in the send queue
server server name to which this channel belongs
type the type of this context
1 - server
2 - channel
3 - dialog
4 - notices
5 - server notices
users Number of users in this channel

"dcc" - list of DCC file transfers

Key Value
address32 address of the remote user(ipv4 address)
cps bytes per second(speed)
destfile destination full pathname
file file name
nick nick of the person this DCC connection is connected to
port TCP port number
pos bytes sent/received
poshigh bytes sent/received, high order 32 bits
resume point at which this file was resumed
(zero if it was not resumed)
resumehigh point at which this file was resumed, high order 32 bits
size file size in bytes low order 32 bits
sizehigh file size in bytes, high order 32 bits (when the files is > 4GB)
status DCC Status:
0 - queued
1 - active
2 - failed
3 - done
4 - connecting
5 - aborted
type DCC Type:
0 - send
1 - receive
2 - chatrecv
3 - chatsend

"ignore" - current ignore list

Key Value
mask ignore mask. e.g: *!*@*.aol.com
flags Bit field of flags.
0 - private
1 - notice
2 - channel
3 - ctcp
4 - invite
5 - unignore
6 - nosave
7 - dcc

"notify" - list of people on notify

Key Value
networks comma separated list of networks where you will be notfified about this user's online/offline status or undef if you will be notificed on every network you are connected to
nick nickname
flags 0 = is online
on time when user came online
off time when user went offline
seen time when user was last verified still online

the values indexed by on, off and seen can be passed to localtime and gmtime, see perldoc -f localtime and perldoc -f gmtime for more detail

"users" - list of users in the current channel

Key Value
away away status(boolean)
lasttalk last time a user was seen talking, this is the an epoch time(number of seconds since a certain date, that date depends on the OS)
nick nick name
host host name in the form: user@host or undef if not known
prefix prefix character, .e.g: @ or +
realname Real name or undef
selected selected status in the user list, only works when retrieving the user list of the focused tab. You can use the /USELECT command to select the nicks

"networks" - list of networks and the associated settings from network list

Key Value
autojoins An object with the following methods:
Method Description
channels() returns a list of this networks' autojoin channels in list context, a count of the number autojoin channels in scalar context
keys() returns a list of the keys to go with the channels, the order is the same as the channels, if a channel doesn't have a key, '' will be returned in it's place
pairs() a combination of channels() and keys(), returns a list of (channels, keys) pairs. This can be assigned to a hash for a mapping from channel to key.
as_hash() return the pairs as a hash reference
as_string() the original string that was used to construct this autojoin object, this can be used with the JOIN command to join all the channels in the autojoin list
as_array() return an array reference of hash references consisting of the keys "channel" and "key"
as_bool() returns true if the network has autojoins and false otherwise
connect_commands An array reference containing the connect commands for a network. An empty array if there aren't any
encoding the encoding for the network
flags a hash reference corresponding to the checkboxes in the network edit window
allow_invalid true if "Accept invalid SSL certificate" is checked
autoconnect true if "Auto connect to this network at startup" is checked
cycle true if "Connect to selected server only" is NOT checked
use_global true if "Use global user information" is checked
use_proxy true if "Bypass proxy server" is NOT checked
use_ssl true if "Use SSL for all the servers on this network" is checked
irc_nick1 Corresponds with the "Nick name" field in the network edit window
irc_nick2 Corresponds with the "Second choice" field in the network edit window
irc_real_name Corresponds with the "Real name" field in the network edit window
irc_user_name Corresponds with the "User name" field in the network edit window
network Name of the network
nickserv_password Corresponds with the "Nickserv password" field in the network edit window
selected Index into the list of servers in the "servers" key, this is used if the "cycle" flag is false
server_password Corresponds with the "Server password" field in the network edit window
servers An array reference of hash references with a "host" and "port" key. If a port is not specified then 6667 will be used.
=end html =head3 C =over 3 =item * C<$nick> - the nick to look for, if this is not given your own nick will be used as default =back This function is mainly intended to be used as a shortcut for when you need to retrieve some information about only one user in a channel. Otherwise it is better to use L. If C<$nick> is found a hash reference containing the same keys as those in the "users" list of L is returned otherwise undef is returned. Since it relies on L this function can only be used in a channel context. =head3 C =over 3 =item * C<$context> - context returned from L, L and L, this is the context that you want infomation about. If this is omitted, it will default to current context. =back This function will return the information normally retrieved with L, except this is for the context that is passed in. The information will be returned in the form of a hash. The keys of the hash are the C<$id> you would normally supply to L as well as all the keys that are valid for the items in the "channels" list from L. Use of this function is more efficient than calling get_list( "channels" ) and searching through the result. =begin html

Example:

use strict; use warnings; use Xchat qw(:all); # imports all the functions documented on this page register( "User Count", "0.1", "Print out the number of users on the current channel" ); hook_command( "UCOUNT", \&display_count ); sub display_count { prnt "There are " . context_info()->{users} . " users in this channel."; return EAT_XCHAT; }
=end html =head3 C =over 3 =item * C<$string> - string to remove codes from =back This function will remove bold, color, beep, reset, reverse and underline codes from C<$string>. It will also remove ANSI escape codes which might get used by certain terminal based clients. If it is called in void context C<$string> will be modified otherwise a modified copy of C<$string> is returned. =head2 Examples =head3 Asynchronous DNS resolution with hook_fd =begin html
use strict; use warnings; use Xchat qw(:all); use Net::DNS; hook_command( "BGDNS", sub { my $host = $_[0][1]; my $resolver = Net::DNS::Resolver->new; my $sock = $resolver->bgsend( $host ); hook_fd( $sock, sub { my $ready_sock = $_[0]; my $packet = $resolver->bgread( $ready_sock ); if( $packet->authority && (my @answers = $packet->answer ) ) { if( @answers ) { prnt "$host:"; my $padding = " " x (length( $host ) + 2); for my $answer ( @answers ) { prnt $padding . $answer->rdatastr . ' ' . $answer->type; } } } else { prnt "Unable to resolve $host"; } return REMOVE; }, { flags => FD_READ, }); return EAT_XCHAT; });
=end html =head2 Contact Information Contact Lian Wan Situ at Eatmcmnky [at] yahoo.comE for questions, comments and corrections about this page or the Perl plugin itself. You can also find me in #xchat on FreeNode under the nick Khisanth.