Trac is being migrated to new services! Issues can be found in our new YouTrack instance and WIKI pages can be found on our website.

Changes between Version 6 and Version 7 of CHowTo/CommandAPIHowTo


Ignore:
Timestamp:
Nov 27, 2009, 12:32:27 PM (14 years ago)
Author:
hamiltont
Comment:

added more code comments, unregistered commands at unload, cleaned up testing section

Legend:

Unmodified
Added
Removed
Modified
  • CHowTo/CommandAPIHowTo

    v6 v7  
    7878#define PLUGIN_AUTHOR "Hamilton Turner <hamiltont@gmail.com>"
    7979
    80 /* Initialized in the plugin_load() method, this allows us to keep a handle to
    81  * ourself */
     80/**
     81 * Initialized in the plugin_load() method, this allows us to keep a handle to
     82 * ourself. Such a handle is needed to register for commands, so that libpurple
     83 * has a handle to the plugin that registered for the command
     84 */
    8285static PurplePlugin *command_example = NULL;
    8386
    84 
    85 /* The callback function for our /log command. This function simply prints
     87/**
     88 * Used to hold a handle to the commands we register. Holding this handle
     89 * allows us to unregister the commands at a later time.
     90 */
     91static PurpleCmdId log_command_id, notify_command_id;
     92
     93/**
     94 * The callback function for our /log command. This function simply prints
    8695 * whatever was entered as the argument to the debug command into the debug
    87  * log. */
     96 * log.
     97 *
     98 * @param conv The conversation that the command occurred in
     99 * @param cmd The exact command that was entered
     100 * @param args The args that were passed with the command
     101 * @param error ?Any errors that occurred?
     102 * @param data Any special user-defined data that was assigned during
     103 *             cmd_register
     104 */
    88105static PurpleCmdRet
    89106log_cb(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data)
     
    96113}
    97114
    98 /* The callback function for our /notify command. This function simply pops up
    99  * a notification with the word entered as the argument to the command */
     115/**
     116 * The callback function for our /notify command. This function simply pops up
     117 * a notification with the word entered as the argument to the command
     118 *
     119 * @param conv The conversation that the command occurred in
     120 * @param cmd The exact command that was entered
     121 * @param args The args that were passed with the command
     122 * @param error ?Any errors that occurred?
     123 * @param data Any special user-defined data that was assigned during
     124 *             cmd_register
     125 */
    100126static PurpleCmdRet
    101127notify_cb(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data)
     
    103129
    104130  purple_notify_info(command_example,
    105                      "Notify Notification Title",
    106                      "Notify Notification Primary Text",
    107                      args[0]);   /* Secondary text is the word entered */
     131                     "Notify Notification Title",
     132                     "Notify Notification Primary Text",
     133                     args[0]);   /* Secondary text is the word entered */
    108134
    109135  return PURPLE_CMD_RET_OK;
     
    111137}
    112138
    113 
    114 
    115 
    116 /* As we've covered before, libpurple calls this function, if present, when it
    117  * loads the plugin.  Here we're using it to show off the capabilities of the
    118  * debug API and just blindly returning TRUE to tell libpurple it's safe to
    119  * continue loading. */
     139/**
     140 * Because we added a pointer to this function in the load section of our
     141 * PurplePluginInfo, this function is called when the plugin loads. 
     142 * Here we're using it to show off the capabilities of the
     143 * command API by registering a few commands. We return TRUE to tell libpurple
     144 * it's safe to continue loading.
     145 */
    120146static gboolean
    121147plugin_load(PurplePlugin *plugin)
    122148{
    123   /* Declare all vars up front. Avoids warnings on some compilers */
     149  // Declare all vars up front. Avoids warnings on some compilers
    124150  gchar *log_help = NULL;
    125151  gchar *notify_help = NULL;
    126152 
    127   /* Save a handle to ourself for later */
     153  // Save a handle to ourself for later
    128154  command_example = plugin;
    129155
    130   /* Help message for log command, in the correct format */
     156  // Help message for log command, in the correct format
    131157  log_help = "log &lt;log message here&gt;:  Prints a message to the debug log.";
    132158
    133   /* Register a command to allow a user to enter /log some message and have
    134    * that message stored to the log. This command runs with default priority,
    135    * can only be used in a standard chat message, not while in group chat
    136    * mode */
    137   purple_cmd_register
     159  // Register a command to allow a user to enter /log some message and have
     160  // that message stored to the log. This command runs with default priority,
     161  // can only be used in a standard chat message, not while in group chat
     162  // mode
     163  log_command_id = purple_cmd_register
    138164    ("log",                         /* command name */
    139      "s",                           /* command arguments */
    140      PURPLE_CMD_P_DEFAULT,          /* command priority */ 
    141      PURPLE_CMD_FLAG_IM,            /* flags saying where
    142                                        command can be used */
    143      
    144      NULL,                          /* We do not need to pass
    145                                        plugin ID */
    146      
    147      log_cb,                        /* Name of the callback
    148                                        function */
    149      log_help,
    150 
    151      NULL                           /* We do not need any special
    152                                        user defined data here */
     165     "s",                           /* command argument format */
     166     PURPLE_CMD_P_DEFAULT,          /* command priority flags */ 
     167     PURPLE_CMD_FLAG_IM,            /* command usage flags */
     168     PLUGIN_ID,                     /* Plugin ID */
     169     log_cb,                        /* Name of the callback function */
     170     log_help,                      /* Help message */
     171     NULL                           /* Any special user-defined data */
    153172     );
    154        
    155 
    156   /* Help message for notify command, in the correct format */
     173       
     174
     175  // Help message for notify command, in the correct format
    157176  notify_help = "notify &lt;notify word here&gt;:  Pops up a notification with the word you enter.";
    158177
    159 
    160   /* Register a command to allow a user to enter /notify some word and have
    161    * that word notified back to them. This command runs with high priority,
    162    * can only be used both in group and standard chat messages */
    163   purple_cmd_register
     178  // Register a command to allow a user to enter /notify some word and have
     179  // that word notified back to them. This command runs with high priority,
     180  // and can be used in both group and standard chat messages
     181  notify_command_id = purple_cmd_register
    164182    ("notify",                      /* command name */
    165      "w",                           /* command arguments */
    166      PURPLE_CMD_P_HIGH,             /* command priority */ 
     183     "w",                           /* command argument format */
     184     PURPLE_CMD_P_HIGH,             /* command priority flags */ 
    167185     PURPLE_CMD_FLAG_IM |
    168        PURPLE_CMD_FLAG_CHAT,        /* flags saying where
    169                                        command can be used */
    170      
    171      NULL,                          /* We do not need to pass
    172                                        plugin ID */
    173      
    174      notify_cb,                     /* Name of the callback
    175                                        function */
    176      notify_help,
    177 
    178      NULL                           /* We do not need any special
    179                                        user defined data here */
     186       PURPLE_CMD_FLAG_CHAT,        /* command usage flags */
     187     PLUGIN_ID,                     /* Plugin ID */
     188     notify_cb,                     /* Callback function */
     189     notify_help,                   /* Help message */
     190     NULL                           /* Any special user-defined data */
    180191     );
    181192 
     
    185196}
    186197
     198/**
     199 * Because we added a pointer to this function in the unload section of our
     200 * PurplePluginInfo, this function is called when the plugin unloads. 
     201 * Here we're using it to show off the capabilities of the
     202 * command API by unregistering a few commands. We return TRUE to tell libpurple
     203 * it's safe to continue unloading.
     204 */
     205static gboolean
     206plugin_unload(PurplePlugin *plugin)
     207{
     208  purple_cmd_unregister(log_command_id);
     209  purple_cmd_unregister(notify_command_id);
     210
     211  /* Just return true to tell libpurple to finish unloading. */
     212  return TRUE;
     213}
     214
     215/**
     216 * Struct used to let Pidgin understand our plugin
     217 */
    187218static PurplePluginInfo info = {
    188         PURPLE_PLUGIN_MAGIC,        /* magic number */
    189         PURPLE_MAJOR_VERSION,       /* purple major */
    190         PURPLE_MINOR_VERSION,       /* purple minor */
    191         PURPLE_PLUGIN_STANDARD,     /* plugin type */
    192         NULL,                       /* UI requirement */
    193         0,                          /* flags */
    194         NULL,                       /* dependencies */
    195         PURPLE_PRIORITY_DEFAULT,    /* priority */
    196 
    197         PLUGIN_ID,                  /* id */
    198         "Command API Example",      /* name */
    199         "1.0",                      /* version */
    200         "Command API Example",      /* summary */
    201         "Command API Example",      /* description */
    202         PLUGIN_AUTHOR,              /* author */
    203         "http://pidgin.im",         /* homepage */
    204 
    205         plugin_load,                /* load */
    206         NULL,                       /* unload */
    207         NULL,                       /* destroy */
    208 
    209         NULL,                       /* ui info */
    210         NULL,                       /* extra info */
    211         NULL,                       /* prefs info */
    212         NULL,                       /* actions */
    213         NULL,                       /* reserved */
    214         NULL,                       /* reserved */
    215         NULL,                       /* reserved */
    216         NULL                        /* reserved */
     219        PURPLE_PLUGIN_MAGIC,        /* magic number */
     220        PURPLE_MAJOR_VERSION,       /* purple major */
     221        PURPLE_MINOR_VERSION,       /* purple minor */
     222        PURPLE_PLUGIN_STANDARD,     /* plugin type */
     223        NULL,                       /* UI requirement */
     224        0,                          /* flags */
     225        NULL,                       /* dependencies */
     226        PURPLE_PRIORITY_DEFAULT,    /* priority */
     227
     228        PLUGIN_ID,                  /* id */
     229        "Command API Example",      /* name */
     230        "1.0",                      /* version */
     231        "Command API Example",      /* summary */
     232        "Command API Example",      /* description */
     233        PLUGIN_AUTHOR,              /* author */
     234        "http://pidgin.im",         /* homepage */
     235
     236        plugin_load,                /* load */
     237        plugin_unload,              /* unload */
     238        NULL,                       /* destroy */
     239
     240        NULL,                       /* ui info */
     241        NULL,                       /* extra info */
     242        NULL,                       /* prefs info */
     243        NULL,                       /* actions */
     244        NULL,                       /* reserved */
     245        NULL,                       /* reserved */
     246        NULL,                       /* reserved */
     247        NULL                        /* reserved */
    217248};
    218249
     
    223254
    224255PURPLE_INIT_PLUGIN(commandexample, init_plugin, info)
    225 
    226256}}}
    227257
     
    230260As before, run make command_example.so (make -f Makefile.mingw command_example.dll for you Windows types) to build the plugin. Copy the resulting command_example.so to ~/.purple/plugins, or copy the resulting command_example.dll to %APPDATA%\.purple\plugins.
    231261
    232 To test (we will assume you are using Pidgin), go to Tools->Plugins and enable the Command API Example plugin. Also, go to Help->Debug Window to show the Debug Window. Open an IM chat session, and type ''/log my log message here''. Your message should show up in the Debug Window. Then type ''/notify Hello!''. You should receive a notification box saying Hello! Also, be sure to try ''/notify Hello World!'' and not that the command fails, because you input multiple arguments (2) when it was only expecting a single value.
     262To test (we will assume you are using Pidgin), go to Tools->Plugins and enable the Command API Example plugin. Also, go to Help->Debug Window to show the Debug Window. Open an IM chat session, and type
     263{{{
     264/log my log message here
     265}}}
     266Your message should show up in the Debug Window. Then type
     267{{{
     268/notify Hello!
     269}}}
     270You should receive a notification box saying '''Hello!''' Also, be sure to try
     271{{{
     272/notify Hello World!
     273}}}
     274Note that the command fails, because you input multiple arguments (2) when it was only expecting a single value. It prints ''Syntax Error:  You typed the wrong number of arguments to that command.''
     275
     276Lastly, if you disable the Command API Example plugin, and then type a command, you will send an IM. Try it!
     277{{{
     278/log why is this not working now
     279}}}
     280
     281 
    233282
    234283== Beyond the Command API ==
    235 The command API provides a basic command framework for plugins, which can be an extremely valuable resource to plugin authors.  The other areas we didn't cover here include much of the true power of the command API, but we will leave those as an area for your exploration at least for now.  This document is done, but there are still others in the [wiki:CHowTo C Plugin How-To] for you to explore!
    236 
    237 == Updates Needed to this page ==
    238   * Need to clean up the commands (purple_cmd_unregister) at unload.
     284The command API provides a basic command framework for plugins, which can be an extremely valuable resource to plugin authors.  The other areas we didn't cover here include much of the true power of the command API, but we will leave those as an area for your exploration.  This document is done, but there are still others in the [wiki:CHowTo C Plugin How-To] for you to explore!
All information, including names and email addresses, entered onto this website or sent to mailing lists affiliated with this website will be public. Do not post confidential information, especially passwords!