This manual documents WeeChat chat client, it is part of WeeChat.

Latest version of this document can be found on this page: http://www.weechat.org/doc

1. Introduction

WeeChat (Wee Enhanced Environment for Chat) is a free chat client, fast and light, designed for many operating systems.

This manual documents way to write scripts for WeeChat, using one of five supported script languages: perl, python, ruby, lua or tcl.

Note
Almost all examples in this doc are written in Python, but API is the same for other languages.

2. Scripts in WeeChat

2.1. Languages specificities

Some things are specific to languages:

2.2. Register

All WeeChat scripts must "register" themselves to WeeChat, and this must be first WeeChat function called in script.

Prototype:

weechat.register(name, author, version, license, description, shutdown_function, charset)

Arguments:

2.3. Script example

Example of script, for each language:

weechat::register("test_perl", "FlashCode", "1.0", "GPL3", "Test script", "", "");
weechat::print("", "Hello, from perl script!");
import weechat

weechat.register("test_python", "FlashCode", "1.0", "GPL3", "Test script", "", "")
weechat.prnt("", "Hello, from python script!")
def weechat_init
  Weechat.register("test_ruby", "FlashCode", "1.0", "GPL3", "Test script", "", "")
  Weechat.print("", "Hello, from ruby script!")
  return Weechat::WEECHAT_RC_OK
end
weechat.register("test_lua", "FlashCode", "1.0", "GPL3", "Test script", "", "")
weechat.print("", "Hello, from lua script!")
weechat::register "test_tcl" "FlashCode" "1.0" "GPL3" "Test script" "" ""
weechat::print "" "Hello, from tcl script!"

2.4. Load script

You have to use command, depending on language:

/perl load perl/script.pl
/python load python/script.py
/ruby load ruby/script.rb
/lua load lua/script.lua
/tcl load tcl/script.tcl

You can make link in directory language/autoload to autoload script when WeeChat is starting.

For example with perl:

$ cd ~/.weechat/perl/autoload
$ ln -s ../script.pl

3. Script API

Script API is almost the same as C plugin API. You can look at WeeChat Plugin API Reference for detail about each function in API: prototype, arguments, return values, examples.

It’s important to make difference between a plugin and a script: a plugin is a binary file compiled and loaded with command /plugin, whereas a script is a text file loaded with a plugin like perl with command /perl.

When your script test.py calls a WeeChat API function, path is like that:

       (script API)                           (C API)
            \/                                   \/
test.py  ------->  python plugin (python.so)  ------->  WeeChat core

When WeeChat calls a callback in your script test.py, it’s reverse of previous path:

              (C API)                            (script API)
                 \/                                   \/
WeeChat core  ------->  python plugin (python.so)  ------->  test.py

3.1. Functions

List of functions in script API:

3.2. Constants

List of constants in script API:

3.3. Differences with C API

3.3.1. Pointers

As you probably know, there is not really "pointers" in scripts. So when API functions return pointer, it is converted to string for script.

For example, if function return pointer 0x1234ab56, script will get string "0x1234ab56".

And when an API function expects a pointer in arguments, script must give that string value. C plugin will convert it to real pointer before calling C API function.

Empty string or "0x0" are allowed, they means NULL in C. For example, to print data on core buffer (WeeChat main buffer), you can do:

weechat.prnt("", "hi!")
Warning
In many functions, for speed reasons, WeeChat does not check if your pointer is correct or not. It’s your job to check you’re giving a valid pointer, otherwise you may see a nice crash report ;)

3.3.2. Callbacks

Almost all WeeChat callbacks must return WEECHAT_RC_OK or WEECHAT_RC_ERROR (exception is modifier callback, which returns a string).

C callbacks are using a "data" argument, which is a pointer. In script API, this "data" is a string with a any value (it’s not a pointer).

For example:

weechat.hook_timer(1000, 0, 1, "my_timer_cb", "my data")

def my_timer_cb(data, remaining_calls):
    # this will display: "my data"
    weechat.prnt("", data)
    return weechat.WEECHAT_RC_OK