<?xml version="1.0" encoding="iso-8859-1"?>

<!--

WeeChat documentation (english version)

Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>

This manual is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This manual is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

-->

<chapter id="chapPlugins">
  <title>Plugins</title>
  
  <para>
    This chapter describes WeeChat plugins interface (API) and
    the default scripts plugins (Perl, Python, Ruby, Lua), provided with
    WeeChat.
  </para>
  
  <section id="secPluginsInWeeChat">
    <title>Plugins in WeeChat</title>
    
    <para>
      A plugin is a C program which can call WeeChat functions defined in
      an interface.
    </para>
    
    <para>
      This C program does not need WeeChat sources to compile and can be
      dynamically loaded into WeeChat with command
      <command>/plugin</command>.
    </para>
    
    <para>
      The plugin has to be a dynamic library, for dynamic loading by
      operating system.
      Under GNU/Linux, the file has ".so" extension, ".dll" under
      Windows.
    </para>
    
  </section>
  
  <section id="secWriteAPlugin">
    <title>Write a plugin</title>
    
    <para>
      The plugin has to include "weechat-plugin.h" file (available in
      WeeChat source code).
      This file defines structures and types used to communicate with
      WeeChat.
    </para>
    
    <para>
      The plugin must have some variables and functions (mandatory,
      without them the plugin can't load):
      <informaltable colsep="0" frame="none">
        <tgroup cols="2">
          <thead>
            <row>
              <entry>Variable</entry>
              <entry>Description</entry>
            </row>
          </thead>
          <tbody>
            <row>
              <entry><literal>char plugin_name[]</literal></entry>
              <entry>plugin name</entry>
            </row>
            <row>
              <entry><literal>char plugin_version[]</literal></entry>
              <entry>plugin version</entry>
            </row>
            <row>
              <entry><literal>char plugin_description[]</literal></entry>
              <entry>short description of plugin</entry>
            </row>
          </tbody>
        </tgroup>
      </informaltable>
      
      <informaltable colsep="0" frame="none">
        <tgroup cols="2">
          <thead>
            <row>
              <entry>Function</entry>
              <entry>Description</entry>
            </row>
          </thead>
          <tbody>
            <row>
              <entry><literal>int weechat_plugin_init (t_weechat_plugin *plugin)</literal></entry>
              <entry>
                function called when plugin is loaded, must return
                PLUGIN_RC_OK if successful, PLUGIN_RC_KO if error
                (if error, plugin will NOT be loaded)
              </entry>
            </row>
            <row>
              <entry><literal>void weechat_plugin_end (t_weechat_plugin *plugin)</literal></entry>
              <entry>function called when plugin is unloaded</entry>
            </row>
          </tbody>
        </tgroup>
      </informaltable>
    </para>
    
    &plugin_api.en.xml;
      
    <section id="secCompilePlugin">
      <title>Compile plugin</title>
      
      <para>
        Compile does not need WeeChat sources, only file
        "<literal>weechat-plugin.h</literal>".
      </para>
      
      <para>
        To compile a plugin which has one file "toto.c" (under GNU/Linux):
<screen>
<prompt>$ </prompt><userinput>gcc -fPIC -Wall -c toto.c</userinput>
<prompt>$ </prompt><userinput>gcc -shared -fPIC -o libtoto.so toto.o</userinput>
</screen>
      </para>
      
    </section>
    
    <section id="secLoadPlugin">
      <title>Load plugin into WeeChat</title>
      
      <para>
        Copy "libtoto.so" file into system plugins directory (for example
        "<literal>/usr/local/lib/weechat/plugins</literal>") or into
        user's plugins directory (for example
        "<literal>/home/xxxxx/.weechat/plugins</literal>").
      </para>
      
      <para>
        Under WeeChat:
        <screen><userinput>/plugin load toto</userinput></screen>
      </para>
      
    </section>
    
    <section id="secPluginExample">
      <title>Plugin example</title>
      
      <para>
        Full example of plugin, which adds a /double command, which displays
        two times arguments on current channel (ok that's not very useful,
        but that's just an example!):
<screen>
#include &lt;stdlib.h&gt;

#include "weechat-plugin.h"

char plugin_name[]        = "Double";
char plugin_version[]     = "0.1";
char plugin_description[] = "Test plugin for WeeChat";

/* "/double" command manager */

int double_cmd (t_weechat_plugin *plugin, int argc, char **argv,
                char *handler_args, void *handler_pointer)
{
    if (argv[2] &amp;&amp; (argv[2][0] != '/'))
    {
        plugin->exec_command (plugin, NULL, NULL, argv[2]);
        plugin->exec_command (plugin, NULL, NULL, argv[2]);
    }
    return PLUGIN_RC_OK;
}

int weechat_plugin_init (t_weechat_plugin *plugin)
{
    plugin->cmd_handler_add (plugin, "double",
                             "Display two times a message",
                             "msg",
                             "msg: message to display two times",
                             NULL,
                             &amp;double_cmd,
                             NULL, NULL);
    return PLUGIN_RC_OK;
}

void weechat_plugin_end (t_weechat_plugin *plugin)
{
    /* nothing done here */
}
</screen>
      </para>
      
    </section>
    
  </section>
  
  &plugin_charset.en.xml;
  
  &plugin_scripts.en.xml;
  
</chapter>