<?xml version="1.0" encoding="UTF-8"?>

<!--

WeeChat documentation (french 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>Extensions</title>
  
  <para>
    Ce chapitre décrit l'API extension de WeeChat et les extensions fournies
    par défaut avec WeeChat.
  </para>
  
  <section id="secPluginsInWeeChat">
    <title>Les extensions dans WeeChat</title>
    
    <para>
      Une extension ("plugin" en anglais) est un programme écrit en C
      qui peut appeler des fonctions de WeeChat définies dans une interface.
    </para>
    
    <para>
      Ce programme C n'a pas besoin des sources WeeChat pour être
      compilé et peut être chargé/déchargé dynamiquement dans
      WeeChat via la commande <command>/plugin</command>.
    </para>
    
    <para>
      L'extension doit être au format bibliothèque, chargeable
      dynamiquement par le système d'exploitation.
      Sous GNU/Linux, il s'agit d'un fichier ayant pour extension ".so",
      sous Windows ".dll".
    </para>
    
  </section>
  
  <section id="secWriteAPlugin">
    <title>Ecrire une extension</title>
    
    <para>
      L'extension doit inclure le fichier "weechat-plugin.h"
      (disponible dans les sources de WeeChat).
      Ce fichier définit les structures et types dont l'extension aura
      besoin pour communiquer avec WeeChat.
    </para>
    
    <para>
      L'extension doit comporter certaines macros obligatoires (pour définir
      des variables) et des fonctions (sans quoi l'extension ne peut être
      chargée) :
      
      <informaltable colsep="0" frame="none">
        <tgroup cols="2">
          <thead>
            <row>
              <entry>Macro</entry>
              <entry>Description</entry>
            </row>
          </thead>
          <tbody>
            <row>
              <entry><literal>WEECHAT_PLUGIN_NAME</literal></entry>
              <entry>le nom de l'extension</entry>
            </row>
            <row>
              <entry><literal>WEECHAT_PLUGIN_DESCRIPTION</literal></entry>
              <entry>une courte description de l'extension</entry>
            </row>
            <row>
              <entry><literal>WEECHAT_PLUGIN_VERSION</literal></entry>
              <entry>la version de l'extension</entry>
            </row>
            <row>
              <entry><literal>WEECHAT_PLUGIN_WEECHAT_VERSION</literal></entry>
              <entry>
                la version de WeeChat pour laquelle l'extension est destinée
                (attention: l'extension ne pourra tourner sur aucune autre
                version de WeeChat !)
              </entry>
            </row>
            <row>
              <entry><literal>WEECHAT_PLUGIN_LICENSE</literal></entry>
              <entry>la licence de l'extension</entry>
            </row>
          </tbody>
        </tgroup>
      </informaltable>
      
      <informaltable colsep="0" frame="none">
        <tgroup cols="2">
          <thead>
            <row>
              <entry>Fonction</entry>
              <entry>Description</entry>
            </row>
          </thead>
          <tbody>
            <row>
              <entry><literal>int weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])</literal></entry>
              <entry>
                fonction appelée au chargement de l'extension
                qui doit renvoyer WEECHAT_RC_OK en cas de succès,
                WEECHAT_RC_ERROR en cas d'erreur (si erreur, l'extension
                ne sera PAS chargée), argv/argv sont les paramètres pour
                l'extension (donnés sur la ligne de commande par l'utilisateur)
              </entry>
            </row>
            <row>
              <entry><literal>int weechat_plugin_end (struct t_weechat_plugin *plugin)</literal></entry>
              <entry>fonction appelée au déchargement de l'extension</entry>
            </row>
          </tbody>
        </tgroup>
      </informaltable>
    </para>
    
    <!-- &plugin_api.fr.xml; -->
    
    <section id="secCompilePlugin">
      <title>Compiler l'extension</title>
      
      <para>
        La compilation ne nécessite pas les sources WeeChat, mais seulement
        le fichier "<literal>weechat-plugin.h</literal>".
      </para>
      
      <para>
        Pour compiler une extension composée d'un fichier "toto.c" (sous
        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>Charger l'extension dans WeeChat</title>
      
      <para>
        Copier le fichier "libtoto.so" dans le répertoire système des
        extensions (par exemple
        "<literal>/usr/local/lib/weechat/plugins)</literal>" ou bien dans
        celui de l'utilisateur (par exemple
        "<literal>/home/xxxxx/.weechat/plugins</literal>").
      </para>
      
      <para>
        Sous WeeChat :
        <screen><userinput>/plugin load toto</userinput></screen>
      </para>
      
    </section>
    
    <section id="secPluginExample">
      <title>Exemple d'extension</title>
      
      <para>
        Un exemple complet d'extension, qui ajoute une commande /double
        affichant deux fois les paramètres passés sur le tampon courant
        ou exécutant deux fois une commande (d'accord ce n'est pas très utile
        mais ceci est un exemple !) :
<screen>
#include &lt;stdlib.h&gt;

#include "weechat-plugin.h"

WEECHAT_PLUGIN_NAME("double");
WEECHAT_PLUGIN_DESCRIPTION("Test plugin for WeeChat");
WEECHAT_PLUGIN_AUTHOR("FlashCode &lt;flashcode@flashtux.org&gt;");
WEECHAT_PLUGIN_VERSION("0.1");
WEECHAT_PLUGIN_WEECHAT_VERSION("0.2.7");
WEECHAT_PLUGIN_LICENSE("GPL3");

struct t_weechat_plugin *weechat_plugin = NULL;


/* "/double" command manager */

int
double_cmd (void *data, struct t_gui_buffer *buffer, int argc,
            char **argv, char **argv_eol)
{
    /* make C compiler happy */
    (void) argv;
    
    if (argc &gt; 1)
    {
        weechat_command (NULL, argv_eol[1]);
        weechat_command (NULL, argv_eol[1]);
    }
    
    return WEECHAT_RC_OK;
}

int
weechat_plugin_init (struct t_weechat_plugin *plugin,
                     int argc, char *argv[])
{
    weechat_plugin = plugin;
    
    weechat_hook_command ("double",
                          "Display two times a message",
                          "msg",
                          "msg: message to display two times",
                          NULL,
                          &amp;double_cmd, NULL);
    
    return WEECHAT_RC_OK;
}

int
weechat_plugin_end (struct t_weechat_plugin *plugin)
{
    /* nothing done here */
    (void) plugin;
    
    return WEECHAT_RC_OK;
}
</screen>
      </para>
      
    </section>
    
  </section>
  
  &plugin_irc.fr.xml;
  
  <!-- &plugin_charset.fr.xml; -->
  
  &plugin_fifo.fr.xml;
  
  <!-- &plugin_scripts.fr.xml; -->
  
  &plugin_xfer.fr.xml;
  
</chapter>