Perl is the first scripting language compatible with Gaim, and has been a valuable aid to developers wishing to write scripts to modify the behavior of their favorite instant messenger. A perl script acts like a normal plugin, and appears in the list of plugins in Gaim's preferences pane. Until now, they have been very basic, and consisted of only a few functions. However, with the latest changes to Gaim's perl API, a much larger part of Gaim is now open. In time, even such things as Gtk+ preference panes for perl scripts will be possible.
First off, we're going to assume here that you are familiar with perl. Perl scripts in Gaim are just normal perl scripts, which happen to use Gaim's loadable perl module.
Now, you're going to want to place your script in $HOME/.gaim/plugins/. That's the place where all plugins and scripts go, regardless of language.
The first thing you're going to write in your script is the necessary code to actually register and initialize your script, which looks something like this:
use Gaim; %PLUGIN_INFO = ( perl_api_version => 2, name => "Your Plugin's Name", version => "0.1", summary => "Brief summary of your plugin.", description => "Detailed description of what your plugin does.", author => "Your Name <email\@address>", url => "http://yoursite.com/", load => "plugin_load", unload => "plugin_unload" ); sub plugin_init { return %PLUGIN_INFO; } sub plugin_load { my $plugin = shift; } sub plugin_unload { my $plugin = shift; }
The first thing you see is a hash called %PLUGIN_INFO
. It contains the basic information on your plugin. In the future, additional fields may be allowed, such as ones to setup a Gtk+ preferences pane.
The plugin_init
function is required in all perl scripts. Its job is to return the %PLUGIN_INFO
hash, which Gaim will use to register and initialize your plugin.
The plugin_load
function is called when the user loads your plugin from the preferences, or on startup. It is passed one variable, which is a handle to the plugin. This must be used when registering signal handlers or timers.
The plugin_unload
function is called when the user is unloading your plugin. Its job is to clean up anything that must be dealt with before unloading, such as removing temporary files or saving configuration information. It does not have to unregister signal handlers or timers, as Gaim will do that for you.
sub timeout_cb { my $data = shift; Gaim::debug_info("my perl plugin", "Timeout callback called! data says: $data\n"); } sub plugin_load { my $plugin = shift; # Start a timeout for 5 seconds. Gaim::timeout_add($plugin, 5, \&timeout_cb, "Hello!"); }
Here's another way of calling a timeout:
sub plugin_load { my $plugin = shift; # Start a timeout for 5 seconds. Gaim::timeout_add($plugin, 5, sub { my $data = shift; Gaim::debug_info("my perl plugin", "Timeout callback called! data says: $data\n"); }, "Hello!"); }
A timeout automatically unregisters when it reaches 0 (which is also when the callback is called). If you want a timeout to call a function every specified number of seconds, just re-register the timeout at the end of the callback.
The data parameter is optional. If you don't have data to pass to the callback, simply omit the parameter.
A signal is registered by connecting a signal name owned by an instance handle to a callback on the plugin handle. This is best illustrated with an example.
sub signed_on_cb { my ($gc, $data) = @_; my $account = $gc->get_account(); Gaim::debug_info("my perl plugin", "Account " . $account->get_username() . " signed on.\n"); } sub plugin_load { my $plugin = shift; my $data = ""; Gaim::signal_connect(Gaim::Connections::handle, "signed-on", $plugin, \&signed_on_cb, $data); }
Like timeouts, the callback can be an embedded subroutine, and also like timeouts, the data parameter can be omitted.
Perl API Reference