comparison doc/PERL-HOWTO.dox @ 15069:3d6f2568457c

[gaim-migrate @ 17853] Various spelling, grammar, clarity and correctness fixes for the Perl HOWTO. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Thu, 30 Nov 2006 05:56:29 +0000
parents e53cceebebb3
children b449dc6b8a20
comparison
equal deleted inserted replaced
15068:c8228e154e24 15069:3d6f2568457c
1 /** @page perl-howto Perl Scripting HOWTO 1 /** @page perl-howto Perl Scripting HOWTO
2 2
3 @section Introduction 3 @section Introduction
4 Gaim Perl Plugins are setup very similarly to their C counterparts. Most of the API calls are implemented and are divided into pacakges. There are some significant differences between the Perl and C API. Much like the C API, the best place to seek guidances is the source located in the plugins/perl/common directory. The tutorial that follows will be example based and attempt to touch on the salient features of the embedded perl interpreter. It is also important to note that some of the C API is missing in Gaim's perl API. 4 Gaim Perl Plugins are set up very similarly to their C counterparts. Most of the API calls are implemented and are divided into packages. There are some significant differences between the Perl and C API. Much like the C API, the best place to seek guidance is the source, which is located in the *.xs files in the [libgaim|gtk]/plugins/perl/common directories. The tutorial that follows will be example based and attempt to touch on some of the notable features of the embedded perl interpreter. It is also important to note that some of the C API is missing in Gaim's perl API.
5 5
6 It is possible to get Gtk2-Perl to work with Gaim's perl API, however you must not load the module with @c use but with @c require. If you are uninterested in using Gtk with your perl plugins than this still has bearing on you if you would like to use certain perl modules that are dynamically loaded. By always using @c require instead of @c use the problem is avoided. 6 It is possible to get Gtk2-Perl to work with Gaim's Perl API, however you must not load the module with @c use, but rather with @c require. Keep this in mind if you would like to use other perl modules that are dynamically loaded. You can avoid the problem by always using @c require instead of @c use.
7 7
8 @section first-script Writing your first script 8 @section first-script Writing your first script
9 9
10 Let us start with a simple example of a Gaim perl plugin. The following code sample is a complete plugin that can be copied and used as is. 10 Let us start with a simple example of a Gaim perl plugin. The following code sample is a complete plugin that can be copied and used as-is.
11 11
12 @code 12 @code
13 use Gaim; 13 use Gaim;
14 14
15 %PLUGIN_INFO = ( 15 %PLUGIN_INFO = (
38 my $plugin = shift; 38 my $plugin = shift;
39 Gaim::Debug::info("testplugin", "plugin_unload() - Test Plugin Unloaded.\n"); 39 Gaim::Debug::info("testplugin", "plugin_unload() - Test Plugin Unloaded.\n");
40 } 40 }
41 @endcode 41 @endcode
42 42
43 It is necessary to load the libgaim perl package with the line @code use Gaim; @endcode which will make all the libgaim perl API available to the script. The @c \%PLUGIN_INFO has contains all the information that will be displayed in the Plugin frame of the Preferences dialog. In addition to information needed to describe the plugin to the user, information about how the plugin is to be handled is present. The keys @c load and @c unload specify and action to take when the plugin is loaded and when it is unloaded from the Preferences dialog respectively. There are other key values that may be present in the @c \%PLUGIN_INFO hash that will be covered in the following sections. 43 It is necessary to load the libgaim perl package with the @code use Gaim; @endcode line; this makes all the libgaim perl API available to the script. The @c \%PLUGIN_INFO hash contains all the information that will be displayed in the Plugin frame of the Preferences dialog, it also contains information about how the plugin is to be handled. The @c load and @c unload keys specify subroutines to be called when the plugin is loaded and unloaded. There are other key values that may be present in the @c \%PLUGIN_INFO hash; some of those will be covered in the following sections.
44 44
45 The Perl subroutine @c plugin_init is executed when the plugin is probed by the plugin subsystem. What this means is as soon as Gaim is started, this subroutine is run once, regardless of whether the plugin is loaded or not. The other two subroutines present are those defined by the @c \%PLUGIN_INFO hash and take the plugin handle as an argument. When the plugin is loaded and subsequently unloaded it will print a message to the debug window using the @c Gaim::Debug::info() Gaim perl API call. 45 The Perl subroutine @c plugin_init is executed when the plugin is probed by the plugin subsystem. What this means is that as soon as gaim is started, this subroutine is run once, regardless of whether or not the plugin is actually loaded. The other two subroutines present are the @c load and @c unload routines specified in the @c \%PLUGIN_INFO hash and will receive the plugin handle as an argument. When the plugin is loaded and subsequently unloaded, it will print a message to the debug window using the @c Gaim::Debug::info() Gaim perl API call.
46 46
47 The last step is to save the script with a .pl file extention in your ~/.gaim/plugins directory. After restarting gaim the plugin "Perl Test Plugin" should now appear under "Tools -> Plugins". To view the messages make sure you run Gaim from the console with the '-d' flag or open the Debug Window from inside Gaim under "Help". When you enable the checkbox next the plugin you should see a message appear in the Debug Window (or console) and when you disable the checkbox you should see another message appear. You have now created the framework that will allow you to create almost any kind of Gaim plugin you can imagine. 47 In order to use this simple plugin, save the script text in a file with a @c .pl extension in your ~/.gaim/plugins directory. After restarting gaim, you should see the plugin ("Perl Test Plugin") listed in the plugin list under "Tools -> Plugins". To view the debug output, make sure you run Gaim from the console with the '-d' flag or open the Debug Window which is available in the "Help" menu. When you check the checkbox next the plugin, you should see a message appear in the Debug Window (or console); similarly, when you uncheck the checkbox you should see another message appear. You have now created a framework that will allow you to create almost any kind of Gaim plugin you can imagine.
48 48
49 @section account-api Account and Account Option Functions 49 @section account-api Account and Account Option Functions
50 50
51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages 51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages;
52 and both are nearly identical to their C counterparts @c gaim_account_ and @c 52 both are nearly identical to their C counterparts, @c gaim_account_ and @c
53 gaim_accounts_. The Account Option API is in the package @c 53 gaim_accounts_. The Account Option API is in the @c Gaim::Account::Option
54 Gaim::Account::Option and is identical to its C implementation @c 54 package and is identical to the C implementation, @c gaim_account_option.
55 gaim_account_option. 55
56 56 The Account* APIs allow scripts to create, remove, and edit accounts. An
57 The Account* APIs allow for scripts to create, remove, and edit accounts of the 57 account will have the Perl type of "GaimAccount". (Note: Gaim types have no real
58 type GaimAccount. (Note: Gaim types have no real meaning in perl scripts other 58 meaning in perl scripts, other than that the types passed to perl subroutines
59 than the types of the arguments of the perl subroutines need to be of the 59 need to be correct.) This section will not go into detail about the @c
60 expected type.) This section will not go into detail about the @c 60 Gaim::Account::Option package since its use is mainly in building protocol
61 Gaim::Account::Option package for its use in building protocol plugins which 61 plugins, which are outside the scope of this document. However, the API for the
62 are outside the scope of this document. However, most of the API calls for the 62 @c Gaim::Account::Option package should function as expected, should you need to
63 @c Gaim::Account::Option package should function as expected so if there is a 63 use it.
64 need to access any of the Account Options the function calls necessary are 64
65 avaialbe in the Gaim perl API. 65 To reduce redundant code, the following code examples will use the simple plugin
66 66 from the previous section as a template. To highlight some of the more useful
67 To reduce redundant code the following code examples are going to use the 67 features of the Account API, we will be replacing the @c plugin_load perl
68 template shown in the previous section. To highlight some of the more useful 68 subroutine. For testing purposes, we will display output on the command line by
69 features of the Account API we will be replacing the @c plugin_load perl
70 subroutine. For testing purposes we will display output on the command line by
71 using perl @c print commands. 69 using perl @c print commands.
72 70
73 @code 71 @code
74 sub plugin_load { 72 sub plugin_load {
75 $plugin = shift; 73 $plugin = shift;
117 print "Okay.\n"; 115 print "Okay.\n";
118 } else { 116 } else {
119 print "Failed!\n"; 117 print "Failed!\n";
120 } 118 }
121 119
122 # It follows that to connect a user you mest set the account status to 120 # It follows that to connect a user you must set the account status to
123 # "available" similarly we can disconnect a user by setting the account 121 # "available" similarly we can disconnect a user by setting the account
124 # status to "offline" 122 # status to "offline"
125 123
126 print "Testing: Gaim::Accounts::connect()...pending...\n"; 124 print "Testing: Gaim::Accounts::connect()...pending...\n";
127 125
130 $account->connect(); 128 $account->connect();
131 129
132 } 130 }
133 @endcode 131 @endcode
134 132
135 For the most part the code listed above is explained by the comments, however 133 The above code is mostly explained in the comments, however there are a few
136 there are a few other points to make. The variables above are all specialized 134 notable points to make. The variables above are all specialized Perl types that
137 Perl types that contain pointers to the actual Gaim types. They can be 135 contain pointers to the actual Gaim types. They can be reassigned at will, just
138 reasigned at will just like any other variable in Perl. The only way to edit 136 like any other variable in Perl. The only way to edit the internal values of a
139 the values of a Gaim type from within perl are through accessor methods such as 137 Gaim type from within perl is to use the accessor methods, e.g.
140 @c Gaim::Account::get_username(). For arguments that you would make @c NULL in 138 @c Gaim::Account::get_username(). If you would like to assign a C @c NULL value,
141 C should be set to @c undef in Perl. 139 simply use @c undef.
142 140
143 @section buddylist-api Buddylist, Group and Chat API 141 @section buddylist-api Buddylist, Group and Chat API
144 142
145 The BuddyList, Group and Chat APIs are very similar and whatever is shown for the @c Gaim::BuddyList API should carry over to @c Gaim::BuddyList::Chat and @c Gaim::BuddyList::Group. Note that there is a @c Gaim::Find package that was created to keep the naming consistent with how these functions are named in the C API. 143 The BuddyList, Group and Chat APIs are very similar and whatever is shown for
144 the @c Gaim::BuddyList API should carry over to @c Gaim::BuddyList::Chat and
145 @c Gaim::BuddyList::Group. Note that a @c Gaim::Find package was created to
146 keep the naming consistent with the C API.
146 147
147 @code 148 @code
148 sub plugin_load { 149 sub plugin_load {
149 my $plugin = shift; 150 my $plugin = shift;
150 151
185 # To add the buddy we need to have the buddy, contact, group and node for insertion. 186 # To add the buddy we need to have the buddy, contact, group and node for insertion.
186 # For this example we can let contact be undef and set the insertion node as the group 187 # For this example we can let contact be undef and set the insertion node as the group
187 Gaim::Debug::info("testplugin", "Testing: Gaim::BuddyList::add_buddy...\n"); 188 Gaim::Debug::info("testplugin", "Testing: Gaim::BuddyList::add_buddy...\n");
188 Gaim::BuddyList::add_buddy($buddy, undef, $group, $group); 189 Gaim::BuddyList::add_buddy($buddy, undef, $group, $group);
189 190
190 # The example that follows gives an indiction of how an API call that returns a list is handled. 191 # The example that follows gives an indication of how an API call that returns a list is handled.
191 # In this case the buddies of the account found earlier are retrieved and put in an array '@buddy_array' 192 # In this case the buddies of the account found earlier are retrieved and put in an array '@buddy_array'
192 # Further down an accessor method is used, 'get_name()' -- see source for details on the full set of methods 193 # Further down an accessor method is used, 'get_name()' -- see source for details on the full set of methods
193 Gaim::Debug::info("testplugin", "Testing: Gaim::Find::buddies...\n"); 194 Gaim::Debug::info("testplugin", "Testing: Gaim::Find::buddies...\n");
194 @buddy_array = Gaim::Find::buddies($account, undef); 195 @buddy_array = Gaim::Find::buddies($account, undef);
195 if (@buddy_array) { 196 if (@buddy_array) {
199 } 200 }
200 } 201 }
201 } 202 }
202 @endcode 203 @endcode
203 204
204 The BuddyList API allows for plugins to edit buddies in the list, find the buddies on a given account, set alias, and manipulate the structure as needed. It is also contains the methods for accessing @c Gaim::BuddyList::Group and @c Gaim::BuddyList::Chat types. 205 The BuddyList API allows plugins to edit buddies in the list, find the buddies
206 for a given account, assign aliases, and further manipulate the structure
207 as needed. It is also contains methods for accessing @c Gaim::BuddyList::Group
208 and @c Gaim::BuddyList::Chat types.
205 209
206 @section conn-api Connection API 210 @section conn-api Connection API
207 211
208 The @c Gaim::Connection API is one of the many packages that will not be covered in depth in this tutorial. They are more useful to protocol plugin developers. However, the entire @c gaim_connection_ API has corresponding, functioning perl subroutines. 212 The @c Gaim::Connection API is one of the many packages that will not be covered
213 in-depth in this tutorial. It is most useful to protocol plugin developers.
214 However, the entire @c gaim_connection_ API has corresponding, functioning perl subroutines.
209 215
210 @section conv-api Conversation API 216 @section conv-api Conversation API
211 217
212 The Gaim perl API for @c gaim_conversation_ and @c gaim_conv_window_ allow 218 The Gaim perl APIs for @c gaim_conversation_ and @c gaim_conv_window_ allow
213 plugins to interact with open conversations, create new conversations, and 219 plugins to interact with existing conversations, create new conversations, and
214 modify conversations at will. In the example script, a new window is created, 220 modify conversations at will. In the example script, a new window is created,
215 displayed and a new conversation instant message is created. The following 221 displayed and a new conversation instant message is created. The following
216 example again replaces the @c plugin_load subroutine. The 222 example again replaces the @c plugin_load subroutine in the simple plugin
217 @c Gaim::Conversation::Chat package handles the @c gaim_conv_chat_ portion of 223 template. The @c Gaim::Conversation::Chat package handles the
218 the API very similarly to the examples that follow. 224 @c gaim_conv_chat_ portion of the API very similarly to how the
225 Gaim::Conversation::IM package is used in the examples that follow.
219 226
220 Notice that the interaction with the conversation window is in the @c Gaim::GtkUI package as it 227 Notice that the interaction with the conversation window is in the @c Gaim::GtkUI package as it
221 is UI-specific code interacting with gtkgaim and not libgaim. 228 is UI-specific code interacting with gtkgaim and not libgaim.
222 To use any of the Gaim::GtkUI functionality, you will need to add the following to the top of your script: @code use Gaim::GtkUI; @endcode 229 To use any of the Gaim::GtkUI functionality, you will need to add the following to the top of your script: @code use Gaim::GtkUI; @endcode
223 230
288 } 295 }
289 @endcode 296 @endcode
290 297
291 @section plugin-pref-api Plugin Preference and Gtk Preference API 298 @section plugin-pref-api Plugin Preference and Gtk Preference API
292 299
293 The plugin preference API allows the plugin to display options in a preference pane that the user can change to manipulate the behaviour of the particular perl plugin. The method used for creating the pane in C does not allow a direct mapping into perl. Therefore perl plugin writers must be aware that there will be significant differences in how they create plugin preference panes. 300 The plugin preference API allows plugins to display options for manipulating the
294 301 plugin's behavior in a popup window. The method used for creating the pane in
295 To first create a standard plugin preference tab we need to add some key/value pairs to the @c \%PLUGIN_INFO hash. The first line contains the perl subroutine that must be provided and will return a @c Gaim::Pref::Frame. 302 native C plugins does not allow a direct mapping into perl. Therefore, perl
303 plugin writers must be aware that there will be significant differences in how
304 they create plugin preference panes.
305
306 To first create a standard plugin preference tab, we need specify the
307 @c prefs_info key/value pair in the @c \%PLUGIN_INFO hash. This specifies the
308 name of the perl subroutine that will build and return a @c Gaim::Pref::Frame.
296 309
297 @code 310 @code
298 %PLUGIN_INFO = { 311 %PLUGIN_INFO = {
299 ..., 312 ...,
300 prefs_info => "prefs_info_cb" 313 prefs_info => "prefs_info_cb"
301 }; 314 };
302 @endcode 315 @endcode
303 316
304 The perl subroutine @c prefs_info_cb will be called to create the tab for the perl plugin in the Preferences dialog. An example of this function will explain the details of creating a preference frame. However, it is necessary to first create the preferences from @c plugin_load as follows. 317 The perl subroutine @c prefs_info_cb will be called to create the preferences
318 popup for the perl plugin. The following example will demonstrate creating a
319 preference frame. It is necessary to first initialize the preferences to be used
320 in the @c plugin_load subroutine, as follows.
305 321
306 @code 322 @code
307 sub plugin_load { 323 sub plugin_load {
308 my $plugin = shift; 324 my $plugin = shift;
309 325
315 Gaim::Prefs::add_int("/plugins/core/perl_test/choice_int", 1); 331 Gaim::Prefs::add_int("/plugins/core/perl_test/choice_int", 1);
316 Gaim::Prefs::add_string("/plugins/core/perl_test/text", "Foobar"); 332 Gaim::Prefs::add_string("/plugins/core/perl_test/text", "Foobar");
317 } 333 }
318 @endcode 334 @endcode
319 335
320 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO . 336 Now we can provide an UI for manipulating these preferences in our @prefs_info
337 function.
321 338
322 @code 339 @code
323 sub prefs_info_cb { 340 sub prefs_info_cb {
324 # The first step is to initialize the Gaim::Pref::Frame that will be returned 341 # The first step is to initialize the Gaim::Pref::Frame that will be returned
325 $frame = Gaim::PluginPref::Frame->new(); 342 $frame = Gaim::PluginPref::Frame->new();
331 348
332 $ppref = Gaim::PluginPref->new_with_name_and_label( 349 $ppref = Gaim::PluginPref->new_with_name_and_label(
333 "/plugins/core/perl_test/bool", "Boolean Preference"); 350 "/plugins/core/perl_test/bool", "Boolean Preference");
334 $frame->add($ppref); 351 $frame->add($ppref);
335 352
336 # Create a set of choices. To do so we must set the type to 1 which is 353 # Create a set of choices. To do so, we must set the type to 1 which is
337 # the numerical equivalent of the GaimPrefType for choice. 354 # the numerical equivalent of the GaimPrefType for choice.
338 $ppref = Gaim::PluginPref->new_with_name_and_label( 355 $ppref = Gaim::PluginPref->new_with_name_and_label(
339 "/plugins/core/perl_test/choice_str", "Choice Preference"); 356 "/plugins/core/perl_test/choice_str", "Choice Preference");
340 $ppref->set_type(1); 357 $ppref->set_type(1);
341 $ppref->add_choice("ch0", "ch0"); 358 $ppref->add_choice("ch0", "ch0");
409 @endcode 426 @endcode
410 --> 427 -->
411 428
412 @section request-api Request Dialog Box API 429 @section request-api Request Dialog Box API
413 430
414 The @c Gaim::Request package allows for plugins to have interactive dialog boxes without the need for creating them in Gtk creating a seperation between the user interfaace and the plugin. The portion of the Request API available to perl scripts is listed below followed by an example illustrating their use. 431 The @c Gaim::Request package allows plugins to have interactive dialog boxes
432 without the need to directly interact with the UI (e.g. GTK+). This allows core
433 (libgaim) plugins to create a UI that can be displayed on whichever UI frontend
434 is being used. The portion of the Request API available to perl scripts is
435 listed below, followed by an example illustrating its use.
415 436
416 These arguments are the same for each of the three request types: 437 These arguments are the same for each of the three request types:
417 @arg @em handle - The plugin handle. 438 @arg @em handle - The plugin handle.
418 @arg @em title - String title for the dialog. 439 @arg @em title - String title for the dialog.
419 @arg @em primary - The first sub-heading. 440 @arg @em primary - The first sub-heading.
424 @arg @em cancel_cb - The string name of the perl subroutine to call when the Cancel button is clicked. 445 @arg @em cancel_cb - The string name of the perl subroutine to call when the Cancel button is clicked.
425 @arg @em default_value - Default text string to display in the input box. 446 @arg @em default_value - Default text string to display in the input box.
426 @arg @em multiline - Boolean where true indicates multiple line input boxes are allowed. 447 @arg @em multiline - Boolean where true indicates multiple line input boxes are allowed.
427 @arg @em masked - Boolean indicating if the user can edit the text. 448 @arg @em masked - Boolean indicating if the user can edit the text.
428 @arg @em hint - See source for more information - can be left blank. 449 @arg @em hint - See source for more information - can be left blank.
429 @arg @em filename - String defualt file name value. 450 @arg @em filename - String default file name value.
430 @arg @em savedialog - Boolean where true indicates use as a save file dialog and false indicates an open file dialog. 451 @arg @em savedialog - Boolean where true indicates use as a save file dialog and false indicates an open file dialog.
431 452
432 @code 453 @code
433 # Create a simple text input box 454 # Create a simple text input box
434 Gaim::Request::input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, ok_cb, cancel_text, cancel_cb); 455 Gaim::Request::input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, ok_cb, cancel_text, cancel_cb);
438 459
439 # Create a unique input dialog as shown in the following example 460 # Create a unique input dialog as shown in the following example
440 Gaim::Request::fields(handle, title, primary, secondary, fields, ok_text, ok_cb, cancel_text, cancel_cb); 461 Gaim::Request::fields(handle, title, primary, secondary, fields, ok_text, ok_cb, cancel_text, cancel_cb);
441 @endcode 462 @endcode
442 463
443 What follows is an example of a @c Gaim::Request::fields() dialog box with two callbacks for an OK button and a Cancel Button. 464 The following is an example of a @c Gaim::Request::fields() dialog box with a
465 callback for the OK button and another for the Cancel Button.
444 466
445 @code 467 @code
446 sub ok_cb_test{ 468 sub ok_cb_test{
447 # The $fields is passed to the callback function when the button is clicked. 469 # The $fields is passed to the callback function when the button is clicked.
448 # To get the values they must be extracted from $fields by name. 470 # To access a specific field, it must be extracted from $fields by name.
449 $fields = shift; 471 $fields = shift;
450 $account = Gaim::Request::Fields::get_account($fields, "acct_test"); 472 $account = Gaim::Request::Fields::get_account($fields, "acct_test");
451 $int = Gaim::Request::Fields::get_integer($fields, "int_test"); 473 $int = Gaim::Request::Fields::get_integer($fields, "int_test");
452 $choice = Gaim::Request::Fields::get_choice($fields, "ch_test"); 474 $choice = Gaim::Request::Fields::get_choice($fields, "ch_test");
453 } 475 }
454 476
455 sub cancel_cb_test{ 477 sub cancel_cb_test{
456 # Cancel does nothing but is symmetric to the ok_cb_test 478 # Cancel does nothing but is equivalent to the ok_cb_test
457 } 479 }
458 480
459 sub plugin_load { 481 sub plugin_load {
460 my $plugin = shift; 482 my $plugin = shift;
461 483
462 # Create a group to pool together mutltiple fields. 484 # Create a group to pool together multiple fields.
463 $group = Gaim::Request::Field::Group::new("Group Name"); 485 $group = Gaim::Request::Field::Group::new("Group Name");
464 486
465 # Each fields is created with Gaim::Request::*_new(), is made viewable with Gaim::Request::field_*_set_show_all() 487 # Each field is created with Gaim::Request::*_new(), is made viewable with Gaim::Request::field_*_set_show_all()
466 # and is then added to the group with Gaim::Request::field_group_add_field() 488 # and is then added to the group with Gaim::Request::field_group_add_field()
467 489
468 # Add an account drop down list showing all active accounts 490 # Add an account combobox containing all active accounts
469 $field = Gaim::Request::Field::account_new("acct_test", "Account Text", undef); 491 $field = Gaim::Request::Field::account_new("acct_test", "Account Text", undef);
470 Gaim::Request::Field::account_set_show_all($field, 0); 492 Gaim::Request::Field::account_set_show_all($field, 0);
471 Gaim::Request::Field::Group::add_field($group, $field); 493 Gaim::Request::Field::Group::add_field($group, $field);
472 494
473 # Add an integer input box 495 # Add an integer input box
498 } 520 }
499 @endcode 521 @endcode
500 522
501 @section timeout-cb Misc: Plugin Actions, Timeouts and Callbacks 523 @section timeout-cb Misc: Plugin Actions, Timeouts and Callbacks
502 524
503 This section of the manual covers some of the more important features that can be added to Gaim perl Plugins. Plugin actions are callback functions that are accessible to the user from the Gaim main window under "Tools -> [Plugin Name]". Timeouts allow a plugin to exectue a perl subroutine after a given period of time. Note that timeouts only occur once, so if the timeout must occur periodically just add a new timeout at the end of the timeout callback function. Callbacks are functions that are called when an event occurs such as a buddy signing-on or a message being received. These three tools will be discussed in the following three examples. 525 This section of the manual covers some of the important features that can be
504 526 added to Gaim perl Plugins. Plugin actions are callback functions that are
505 Plugin actions require the @c \%PLUGIN_INFO hash to have a key/value pair added and a callback perl subroutine defined to list the available actions, as well as one callback subroutine for each action. Also, a @c \%plugin_actions hash needs to be defined relating the action names returned by the callback to the appropriate callback subroutines for each action. 527 accessible to the user from the Buddy List window under "Tools -> [Plugin Name]".
528 Timeouts allow a plugin to execute a perl subroutine after a given period of time.
529 Note that timeouts only occur once, so if the timeout must occur periodically,
530 the timeout needs to be re-added at the end of the timeout callback function.
531 Callbacks are functions that are called when an event occurs (such as a buddy
532 signing-on, or a message being received). The following three examples will
533 demonstrate the usage of these features.
534
535 Plugin actions require the @c \%PLUGIN_INFO hash to have a key/value pair added,
536 specifying a perl subroutine which will list the available actions; each action
537 listed must have a definition, including a reference to a callback subroutine,
538 added to the @c \%plugin_actions hash.
506 539
507 @code 540 @code
508 %PLUGIN_INFO = { 541 %PLUGIN_INFO = {
509 ..., 542 ...,
510 plugin_action_sub => "plugin_actions_cb", 543 plugin_action_sub => "plugin_actions_cb",
528 sub action2_cb { 561 sub action2_cb {
529 Gaim::Debug::info("Test Plugin", "Action 2 activated\n"); 562 Gaim::Debug::info("Test Plugin", "Action 2 activated\n");
530 } 563 }
531 @endcode 564 @endcode
532 565
533 Timeouts allow a perl subroutine to be exectued after a specified time. They only occur once, so as stated earlier the timeout must be reregistered after every time it is called. 566 Timeouts allow a perl subroutine to be executed after a specified time. They only occur once, so, as stated earlier, the timeout must be re-registered after every time it is called.
534 567
535 @code 568 @code
536 sub timeout_cb { 569 sub timeout_cb {
537 my $plugin = shift; 570 my $plugin = shift;
538 Gaim::Debug::info("testplugin", "Timeout occurred.\n"); 571 Gaim::Debug::info("testplugin", "Timeout occurred.\n");
547 # Schedule a timeout for ten seconds from now 580 # Schedule a timeout for ten seconds from now
548 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); 581 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin);
549 } 582 }
550 @endcode 583 @endcode
551 584
552 Callbacks are handled by creating a perl subroutine to serve as the callback and then attaching the callback to a signal. To use callbacks it is necessary to first obtain the plugin handle with the @c Gaim::Plugin::get_handle() subroutine to pass as an argument for the callback. 585 Callbacks are handled by creating a perl subroutine to serve as the callback and
586 then attaching the callback to a signal.
553 587
554 @code 588 @code
555 sub signal_cb { 589 sub signal_cb {
556 # The signal data and the user data come in as arguments 590 # The signal data and the user data come in as arguments
557 my ($account, $data) = @_; 591 my ($account, $data) = @_;