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

Latest version of this document can be found on this page .

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 supported script languages:

  • Python

  • Perl

  • Ruby

  • Lua

  • Tcl

  • Guile (Scheme)

  • JavaScript

  • PHP

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

2. Scripts in WeeChat

2.1. WeeChat architecture

WeeChat is single-threaded, and this applies to scripts as well.

The code of a script is executed:

  • when the script is loaded: typically a call to the register function

  • when a hook callback is called by WeeChat (see the chapter Hooks).

When the code of a script is executed, WeeChat waits for the end of execution before going on. Therefore the script must NOT do blocking operations like network calls without using a dedicated API function like hook_process.

A script must NEVER fork or create threads without using a dedicated API function, this can crash WeeChat.
If something must be run in background, the function hook_process can be used. See example in the chapter Run a background process and the documentation on the function hook_process in the WeeChat plugin API reference .

2.2. Languages specificities

Python

Module

WeeChat defines a weechat module which must be imported with import weechat.
A Python stub for WeeChat API is available in the repository: weechat.pyi .

Functions

Functions are called with weechat.xxx(arg1, arg2, ...).

Functions print* are called prnt* in python (because print was a reserved keyword in Python 2).

Strings received in callbacks

In Python 3 and with WeeChat ≥ 2.7, the strings received in callbacks have type str if the string has valid UTF-8 data (which is the most common case), or bytes if the string is not UTF-8 valid. So the callback should take care about this type if some invalid UTF-8 content can be received.

Some invalid UTF-8 data may be received in these cases, so the callback can receive a string of type str or bytes (this list is not exhaustive):

API function Arguments Examples Description

hook_modifier

irc_in_yyy

irc_in_privmsg
irc_in_notice

A message received in IRC plugin, before it is decoded to UTF-8 (used internally).

It is recommended to use modifier irc_in2_yyy instead, the string received is always UTF-8 valid.
See function hook_modifier in the WeeChat plugin API reference .

hook_signal

xxx,irc_out_yyy
xxx,irc_outtags_yyy

*,irc_out_privmsg
*,irc_out_notice
*,irc_outtags_privmsg
*,irc_outtags_notice

A message sent by IRC plugin, after it is encoded to the encode charset defined by the user (if different from the default UTF-8).

It is recommended to use signal xxx,irc_out1_yyy instead, the string received is always UTF-8 valid.
See function hook_signal in the WeeChat plugin API reference .

hook_process
hook_process_hashtable

-

-

Output of the command, sent to the callback, can contain invalid UTF-8 data.

Perl

Functions

Functions are called with weechat::xxx(arg1, arg2, ...);.

Ruby

Initialization

You have to define weechat_init and call register inside.

Functions

Functions are called with Weechat.xxx(arg1, arg2, ...).

Due to a limitation of Ruby (15 arguments max by function), the function Weechat.config_new_option receives the callbacks in an array of 6 strings (3 callbacks + 3 data strings), so a call to this function looks like:

Weechat.config_new_option(config, section, "name", "string", "description of option", "", 0, 0,
                          "value", "value", 0, ["check_cb", "", "change_cb", "", "delete_cb", ""])

And the function Weechat.bar_new receives the colors in an array of 4 strings (color_fg, color_delim, color_bg, color_bg_inactive), so a call to this function looks like:

Weechat.bar_new("name", "off", "0", "window", "", "left", "vertical", "vertical", "0", "0",
                ["default", "default", "default", "default"], "0", "items")

Lua

Functions

Functions are called with weechat.xxx(arg1, arg2, ...).

Tcl

Functions

Functions are called with weechat::xxx arg1 arg2 ....

Null values

Since Tcl only has string types, there’s no null type to pass as an argument when a function accepts null values or to get as an argument in a callback function. To overcome this the WeeChat API defines the constant $::weechat::WEECHAT_NULL which acts as a null value. This constant is defined as \uFFFF\uFFFF\uFFFFWEECHAT_NULL\uFFFF\uFFFF\uFFFF, so it’s very unlikely to appear unintentionally.

You can pass this constant when a function accepts null as an argument and you will get it as the value of an argument in a callback function if the argument value is null. To see which functions accept null values and passes null values to callbacks, look at the Python prototypes in the WeeChat plugin API reference .

Guile (Scheme)

Functions

Functions are called with (weechat:xxx arg1 arg2 ...).

The following functions take one list of arguments (instead of many arguments for other functions), because number of arguments exceed number of allowed arguments in Guile:

  • config_new_section

  • config_new_option

  • bar_new

JavaScript

Functions

Functions are called with weechat.xxx(arg1, arg2, ...);.

PHP

Functions

Functions are called with weechat_xxx(arg1, arg2, ...);.

2.3. Register function

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

Prototype (Python):

def register(name: str, author: str, version: str, license: str, description: str, shutdown_function: str, charset: str) -> int: ...

Arguments:

  • name: string, internal name of script

  • author: string, author name

  • version: string, script version

  • license: string, script license

  • description: string, short description of script

  • shutdown_function: string, name of function called when script is unloaded (can be empty string)

  • charset: string, script charset (if your script is UTF-8, you can use blank value here, because UTF-8 is default charset)

Example of script, for each language:

  • Python:

import weechat

weechat.register("test_python", "FlashCode", "1.0", "GPL3", "Test script", "", "")
weechat.prnt("", "Hello, from python script!")
  • Perl:

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

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
  • Lua:

weechat.register("test_lua", "FlashCode", "1.0", "GPL3", "Test script", "", "")
weechat.print("", "Hello, from lua script!")
  • Tcl:

weechat::register "test_tcl" "FlashCode" "1.0" "GPL3" "Test script" "" ""
weechat::print "" "Hello, from tcl script!"
  • Guile (Scheme):

(weechat:register "test_scheme" "FlashCode" "1.0" "GPL3" "Test script" "" "")
(weechat:print "" "Hello, from scheme script!")
  • JavaScript:

weechat.register("test_js", "FlashCode", "1.0", "GPL3", "Test script", "", "");
weechat.print("", "Hello, from javascript script!");
  • PHP:

weechat_register('test_php', 'FlashCode', '1.0', 'GPL3', 'Test script', '', '');
weechat_print('', 'Hello, from PHP script!');

2.4. Load script

It is recommended to use the "script" plugin to load scripts, for example:

/script load script.py
/script load script.pl
/script load script.rb
/script load script.lua
/script load script.tcl
/script load script.scm
/script load script.js
/script load script.php

Each language has also its own command:

/python load script.py
/perl load script.pl
/ruby load script.rb
/lua load script.lua
/tcl load script.tcl
/guile load script.scm
/javascript load script.js
/php load script.php

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

For example with Python:

$ cd ~/.local/share/weechat/python/autoload
$ ln -s ../script.py
When installing a script with command /script install the link in autoload directory is automatically created.

3. Differences with C 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 python with command /python.

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

               ┌──────────────────────┐        ╔══════════════════╗
               │     python plugin    │        ║  WeeChat "core"  ║
               ├────────────┬─────────┤        ╟─────────┐        ║
test.py ─────► │ script API │  C API  │ ─────► ║  C API  │        ║
               └────────────┴─────────┘        ╚═════════╧════════╝

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

╔══════════════════╗        ┌──────────────────────┐
║  WeeChat "core"  ║        │     python plugin    │
║        ┌─────────╢        ├─────────┬────────────┤
║        │  C API  ║ ─────► │  C API  │ script API │ ─────► test.py
╚════════╧═════════╝        └─────────┴────────────┘

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!")
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.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 "callback_pointer" and "callback_data" arguments, which are pointers. In script API, there is only "callback_data" (or "data"), and it is a string instead of a pointer.

Example of callback, for each language:

  • Python:

def timer_cb(data, remaining_calls):
    weechat.prnt("", "timer! data=%s" % data)
    return weechat.WEECHAT_RC_OK

weechat.hook_timer(1000, 0, 1, "timer_cb", "test")
  • Perl:

sub timer_cb {
    my ($data, $remaining_calls) = @_;
    weechat::print("", "timer! data=$data");
    return weechat::WEECHAT_RC_OK;
}

weechat::hook_timer(1000, 0, 1, "timer_cb", "test");
  • Ruby:

def timer_cb(data, remaining_calls)
  Weechat.print("", "timer! data=#{data}");
  return Weechat::WEECHAT_RC_OK
end

Weechat.hook_timer(1000, 0, 1, "timer_cb", "test");
  • Lua:

function timer_cb(data, remaining_calls)
    weechat.print("", "timer! data="..data)
    return weechat.WEECHAT_RC_OK
end

weechat.hook_timer(1000, 0, 1, "timer_cb", "test")
  • Tcl:

proc timer_cb { data remaining_calls } {
    weechat::print {} "timer! data=$data"
    return $::weechat::WEECHAT_RC_OK
}

weechat::hook_timer 1000 0 1 timer_cb test
  • Guile (Scheme):

(define (timer_cb data remaining_calls)
  (weechat:print "" (string-append "timer! data=" data))
  weechat:WEECHAT_RC_OK
)

(weechat:hook_timer 1000 0 1 "timer_cb" "test")
  • JavaScript:

function timer_cb(data, remaining_calls) {
    weechat.print("", "timer! data=" + data);
    return weechat.WEECHAT_RC_OK;
}

weechat.hook_timer(1000, 0, 1, "timer_cb", "test");
  • PHP:

$timer_cb = function ($data, $remaining_calls) {
    weechat_print('', 'timer! data=' . $data);
    return WEECHAT_RC_OK;
};

weechat_hook_timer(1000, 0, 1, $timer_cb, 'test');

4. Script API

For more information about functions in API, please read the WeeChat plugin API reference .

4.1. Functions

List of functions in script API:

  • register

  • plugin_get_name

  • charset_set

  • iconv_to_internal

  • iconv_from_internal

  • gettext

  • ngettext

  • strlen_screen

  • string_match

  • string_match_list

  • string_has_highlight

  • string_has_highlight_regex

  • string_mask_to_regex

  • string_format_size

  • string_parse_size

  • string_color_code_size

  • string_remove_color

  • string_is_command_char

  • string_input_for_buffer

  • string_eval_expression

  • string_eval_path_home

  • mkdir_home

  • mkdir

  • mkdir_parents

  • list_new

  • list_add

  • list_search

  • list_search_pos

  • list_casesearch

  • list_casesearch_pos

  • list_get

  • list_set

  • list_next

  • list_prev

  • list_string

  • list_size

  • list_remove

  • list_remove_all

  • list_free

  • config_new

  • config_set_version

  • config_new_section

  • config_search_section

  • config_new_option

  • config_search_option

  • config_string_to_boolean

  • config_option_reset

  • config_option_set

  • config_option_set_null

  • config_option_unset

  • config_option_rename

  • config_option_get_string

  • config_option_get_pointer

  • config_option_is_null

  • config_option_default_is_null

  • config_boolean

  • config_boolean_default

  • config_boolean_inherited

  • config_integer

  • config_integer_default

  • config_integer_inherited

  • config_string

  • config_string_default

  • config_string_inherited

  • config_color

  • config_color_default

  • config_color_inherited

  • config_enum

  • config_enum_default

  • config_enum_inherited

  • config_write_option

  • config_write_line

  • config_write

  • config_read

  • config_reload

  • config_option_free

  • config_section_free_options

  • config_section_free

  • config_free

  • config_get

  • config_get_plugin

  • config_is_set_plugin

  • config_set_plugin

  • config_set_desc_plugin

  • config_unset_plugin

  • key_bind

  • key_unbind

  • prefix

  • color

  • prnt

  • prnt_date_tags

  • prnt_datetime_tags

  • prnt_y

  • prnt_y_date_tags

  • prnt_y_datetime_tags

  • log_print

  • hook_command

  • hook_completion

  • hook_completion_get_string

  • hook_completion_list_add

  • hook_command_run

  • hook_timer

  • hook_fd

  • hook_process

  • hook_process_hashtable

  • hook_url

  • hook_connect

  • hook_line

  • hook_print

  • hook_signal

  • hook_signal_send

  • hook_hsignal

  • hook_hsignal_send

  • hook_config

  • hook_modifier

  • hook_modifier_exec

  • hook_info

  • hook_info_hashtable

  • hook_infolist

  • hook_focus

  • hook_set

  • unhook

  • unhook_all

  • buffer_new

  • buffer_new_props

  • buffer_search

  • buffer_search_main

  • current_buffer

  • buffer_clear

  • buffer_close

  • buffer_merge

  • buffer_unmerge

  • buffer_get_integer

  • buffer_get_string

  • buffer_get_pointer

  • buffer_set

  • buffer_string_replace_local_var

  • buffer_match_list

  • current_window

  • window_search_with_buffer

  • window_get_integer

  • window_get_string

  • window_get_pointer

  • window_set_title

  • nicklist_add_group

  • nicklist_search_group

  • nicklist_add_nick

  • nicklist_search_nick

  • nicklist_remove_group

  • nicklist_remove_nick

  • nicklist_remove_all

  • nicklist_group_get_integer

  • nicklist_group_get_string

  • nicklist_group_get_pointer

  • nicklist_group_set

  • nicklist_nick_get_integer

  • nicklist_nick_get_string

  • nicklist_nick_get_pointer

  • nicklist_nick_set

  • bar_item_search

  • bar_item_new

  • bar_item_update

  • bar_item_remove

  • bar_search

  • bar_new

  • bar_set

  • bar_update

  • bar_remove

  • command

  • command_options

  • completion_new

  • completion_search

  • completion_get_string

  • completion_list_add

  • completion_free

  • info_get

  • info_get_hashtable

  • infolist_new

  • infolist_new_item

  • infolist_new_var_integer

  • infolist_new_var_string

  • infolist_new_var_pointer

  • infolist_new_var_time

  • infolist_search_var

  • infolist_get

  • infolist_next

  • infolist_prev

  • infolist_reset_item_cursor

  • infolist_fields

  • infolist_integer

  • infolist_string

  • infolist_pointer

  • infolist_time

  • infolist_free

  • hdata_get

  • hdata_get_var_offset

  • hdata_get_var_type_string

  • hdata_get_var_array_size

  • hdata_get_var_array_size_string

  • hdata_get_var_hdata

  • hdata_get_list

  • hdata_check_pointer

  • hdata_move

  • hdata_search

  • hdata_char

  • hdata_integer

  • hdata_long

  • hdata_longlong

  • hdata_string

  • hdata_pointer

  • hdata_time

  • hdata_hashtable

  • hdata_compare

  • hdata_update

  • hdata_get_string

  • upgrade_new

  • upgrade_write_object

  • upgrade_read

  • upgrade_close

4.2. Constants

List of constants in script API:

Constant Type Value

WEECHAT_RC_OK

integer

0

WEECHAT_RC_OK_EAT

integer

1

WEECHAT_RC_ERROR

integer

-1

WEECHAT_CONFIG_READ_OK

integer

0

WEECHAT_CONFIG_READ_MEMORY_ERROR

integer

-1

WEECHAT_CONFIG_READ_FILE_NOT_FOUND

integer

-2

WEECHAT_CONFIG_WRITE_OK

integer

0

WEECHAT_CONFIG_WRITE_ERROR

integer

-1

WEECHAT_CONFIG_WRITE_MEMORY_ERROR

integer

-2

WEECHAT_CONFIG_OPTION_SET_OK_CHANGED

integer

2

WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE

integer

1

WEECHAT_CONFIG_OPTION_SET_ERROR

integer

0

WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND

integer

-1

WEECHAT_CONFIG_OPTION_UNSET_OK_NO_RESET

integer

0

WEECHAT_CONFIG_OPTION_UNSET_OK_RESET

integer

1

WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED

integer

2

WEECHAT_CONFIG_OPTION_UNSET_ERROR

integer

-1

WEECHAT_LIST_POS_SORT

string

sort

WEECHAT_LIST_POS_BEGINNING

string

beginning

WEECHAT_LIST_POS_END

string

end

WEECHAT_HOTLIST_LOW

string

0

WEECHAT_HOTLIST_MESSAGE

string

1

WEECHAT_HOTLIST_PRIVATE

string

2

WEECHAT_HOTLIST_HIGHLIGHT

string

3

WEECHAT_HOOK_PROCESS_RUNNING

integer

-1

WEECHAT_HOOK_PROCESS_ERROR

integer

-2

WEECHAT_HOOK_CONNECT_OK

integer

0

WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND

integer

1

WEECHAT_HOOK_CONNECT_IP_ADDRESS_NOT_FOUND

integer

2

WEECHAT_HOOK_CONNECT_CONNECTION_REFUSED

integer

3

WEECHAT_HOOK_CONNECT_PROXY_ERROR

integer

4

WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR

integer

5

WEECHAT_HOOK_CONNECT_GNUTLS_INIT_ERROR

integer

6

WEECHAT_HOOK_CONNECT_GNUTLS_HANDSHAKE_ERROR

integer

7

WEECHAT_HOOK_CONNECT_MEMORY_ERROR

integer

8

WEECHAT_HOOK_CONNECT_TIMEOUT

integer

9

WEECHAT_HOOK_CONNECT_SOCKET_ERROR

integer

10

WEECHAT_HOOK_SIGNAL_STRING

string

string

WEECHAT_HOOK_SIGNAL_INT

string

int

WEECHAT_HOOK_SIGNAL_POINTER

string

pointer

5. Common tasks

This chapter shows some common tasks, with examples. Only partial things in API are used here, for full reference, see the WeeChat plugin API reference .

5.1. Buffers

Display messages

An empty string is often used to work with WeeChat core buffer. For other buffers, you must give pointer (as string, see pointers).

Examples:

# display "hello" on core buffer
weechat.prnt("", "hello")

# display "hello" on core buffer, but do not write it to log file
# (version ≥ 0.3.3 only)
weechat.prnt_date_tags("", 0, "no_log", "hello")

# display prefix "==>" and message "hello" on current buffer
# (prefix and message must be separated by tab)
weechat.prnt(weechat.current_buffer(), "==>\thello")

# display error message on core buffer (with error prefix)
weechat.prnt("", "%swrong arguments" % weechat.prefix("error"))

# display message with color on core buffer
weechat.prnt("", "text %syellow on blue" % weechat.color("yellow,blue"))

# search buffer and display message
# (full name of buffer is plugin.name, for example: "irc.libera.#weechat")
buffer = weechat.buffer_search("irc", "libera.#weechat")
weechat.prnt(buffer, "message on #weechat channel")

# other solution to find an IRC buffer (better)
# (note that server and channel are separated by a comma)
buffer = weechat.info_get("irc_buffer", "libera,#weechat")
weechat.prnt(buffer, "message on #weechat channel")
Print function is called prnt in Python and print in other languages.

Send text to buffer

You can send text or command to a buffer. This is exactly like if you type text on command line and press [Enter].

Examples:

# execute command "/help" on current buffer (result is on core buffer)
weechat.command("", "/help")

# send "hello" to #weechat IRC channel (users on channel will see message)
buffer = weechat.info_get("irc_buffer", "libera,#weechat")
weechat.command(buffer, "hello")

Create new buffer

You can create a new buffer in your script, then use it for displaying messages.

Two callbacks can be called (they are optional): one for input data (when you type some text and press [Enter] on buffer), the other is called when buffer is closed (for example by /buffer close).

Example:

# callback for data received in input
def buffer_input_cb(data, buffer, input_data):
    # ...
    return weechat.WEECHAT_RC_OK

# callback called when buffer is closed
def buffer_close_cb(data, buffer):
    # ...
    return weechat.WEECHAT_RC_OK

# create buffer
buffer = weechat.buffer_new("mybuffer", "buffer_input_cb", "", "buffer_close_cb", "")

# set title
weechat.buffer_set(buffer, "title", "This is title for my buffer.")

# disable logging, by setting local variable "no_log" to "1"
weechat.buffer_set(buffer, "localvar_set_no_log", "1")

Buffer properties

You can read buffer properties, as string, integer or pointer.

Examples:

buffer = weechat.current_buffer()

number = weechat.buffer_get_integer(buffer, "number")
name = weechat.buffer_get_string(buffer, "name")
short_name = weechat.buffer_get_string(buffer, "short_name")

It is possible to add, read or delete local variables in buffer:

# add local variable
weechat.buffer_set(buffer, "localvar_set_myvar", "my_value")

# read local variable
myvar = weechat.buffer_get_string(buffer, "localvar_myvar")

# delete local variable
weechat.buffer_set(buffer, "localvar_del_myvar", "")

To see local variables of a buffer, do this command in WeeChat:

/buffer listvar

5.2. Hooks

Add new command

Add a custom command with hook_command. You can use a custom completion template to complete arguments of your command.

Example:

def my_command_cb(data, buffer, args):
    # ...
    return weechat.WEECHAT_RC_OK

hook = weechat.hook_command("myfilter", "description of myfilter",
    "[list] | [enable|disable|toggle [name]] | [add name plugin.buffer tags regex] | [del name|-all]",
    "description of arguments...",
    "list"
    " || enable %(filters_names)"
    " || disable %(filters_names)"
    " || toggle %(filters_names)"
    " || add %(filters_names) %(buffers_plugins_names)|*"
    " || del %(filters_names)|-all",
    "my_command_cb", "")

And then in WeeChat:

/help myfilter

/myfilter arguments...

Add a timer

Add a timer with hook_timer.

Example:

def timer_cb(data, remaining_calls):
    # ...
    return weechat.WEECHAT_RC_OK

# timer called each minute when second is 00
weechat.hook_timer(60 * 1000, 60, 0, "timer_cb", "")

Run a background process

You can run a background process with hook_process. Your callback will be called when data is ready. It may be called many times.

For the last call to your callback, return_code is set to 0 or positive value, it is the return code of command.

Example:

def my_process_cb(data, command, return_code, out, err):
    if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
        weechat.prnt("", "Error with command '%s'" % command)
        return weechat.WEECHAT_RC_OK
    if return_code >= 0:
        weechat.prnt("", "return_code = %d" % return_code)
    if out:
        weechat.prnt("", "stdout: %s" % out)
    if err:
        weechat.prnt("", "stderr: %s" % err)
    return weechat.WEECHAT_RC_OK

weechat.hook_process("/bin/ls -l /etc", 10 * 1000, "my_process_cb", "")

You can also call directly a script function that does something blocking, instead of an external command:

def get_status(data):
    # do something blocking...
    # ...
    return "this is the result"

def my_process_cb(data, command, return_code, out, err):
    if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
        weechat.prnt("", "Error with command '%s'" % command)
        return weechat.WEECHAT_RC_OK
    if return_code >= 0:
        weechat.prnt("", "return_code = %d" % return_code)
    if out:
        weechat.prnt("", "stdout: %s" % out)
    if err:
        weechat.prnt("", "stderr: %s" % err)
    return weechat.WEECHAT_RC_OK

hook = weechat.hook_process("func:get_status", 5000, "my_process_cb", "")

URL transfer

New in version 0.3.7.

To download URL (or post to URL), you have to use function hook_process, or hook_process_hashtable if you need to set options for URL transfer.

Example of URL transfer without option: the HTML page will be received as "out" in callback (standard output of process):

# Display latest stable version of WeeChat.
weechat_latest_version = ""

def weechat_process_cb(data, command, return_code, out, err):
    global weechat_latest_version
    if out:
        weechat_latest_version += out
    if return_code >= 0:
        weechat.prnt("", "Latest WeeChat version: %s" % weechat_latest_version)
    return weechat.WEECHAT_RC_OK

weechat.hook_process("url:https://weechat.org/dev/info/stable/",
                     30 * 1000, "weechat_process_cb", "")
All infos available about WeeChat are on this page .

Example of URL transfer with an option: download latest WeeChat development package in file /tmp/weechat-devel.tar.gz:

def my_process_cb(data, command, return_code, out, err):
    if return_code >= 0:
        weechat.prnt("", "End of transfer (return code = %d)" % return_code)
    return weechat.WEECHAT_RC_OK

weechat.hook_process_hashtable("url:https://weechat.org/files/src/weechat-devel.tar.gz",
                               {"file_out": "/tmp/weechat-devel.tar.gz"},
                               30 * 1000, "my_process_cb", "")

For more information about URL transfer and available options, see functions hook_process and hook_process_hashtable in WeeChat plugin API reference .

5.3. Config / options

Set options for script

Function config_is_set_plugin is used to check if an option is set or not, and config_set_plugin to set option.

Example:

script_options = {
    "option1": "value1",
    "option2": "value2",
    "option3": "value3",
}
for option, default_value in script_options.items():
    if not weechat.config_is_set_plugin(option):
        weechat.config_set_plugin(option, default_value)

Detect changes

You must use hook_config to be notified if user changes some script options.

Example:

SCRIPT_NAME = "myscript"

# ...

def config_cb(data, option, value):
    """Callback called when a script option is changed."""
    # for example, read all script options to script variables...
    # ...
    return weechat.WEECHAT_RC_OK

# ...

weechat.hook_config("plugins.var.python." + SCRIPT_NAME + ".*", "config_cb", "")
# for other languages, change "python" with your language (perl/ruby/lua/tcl/guile/javascript)

Read WeeChat options

Function config_get returns pointer to option. Then, depending on option type, you must call config_string, config_boolean, config_integer or config_color.

# string
weechat.prnt("", "value of option weechat.look.item_time_format is: %s"
                 % (weechat.config_string(weechat.config_get("weechat.look.item_time_format"))))

# boolean
weechat.prnt("", "value of option weechat.look.day_change is: %d"
                 % (weechat.config_boolean(weechat.config_get("weechat.look.day_change"))))

# integer
weechat.prnt("", "value of option weechat.look.scroll_page_percent is: %d"
                 % (weechat.config_integer(weechat.config_get("weechat.look.scroll_page_percent"))))

# color
weechat.prnt("", "value of option weechat.color.chat_delimiters is: %s"
                 % (weechat.config_color(weechat.config_get("weechat.color.chat_delimiters"))))

5.4. IRC

Catch messages

IRC plugin sends four signals for a message received (xxx is IRC internal server name, yyy is IRC command name like JOIN, QUIT, PRIVMSG, 301, ..):

xxx,irc_in_yyy

signal sent before processing message, only if message is not ignored

xxx,irc_in2_yyy

signal sent after processing message, only if message is not ignored

xxx,irc_raw_in_yyy

signal sent before processing message, even if message is ignored

xxx,irc_raw_in2_yyy

signal sent after processing message, even if message is ignored

def join_cb(data, signal, signal_data):
    # signal is for example: "libera,irc_in2_join"
    # signal_data is IRC message, for example: ":nick!user@host JOIN :#channel"
    server = signal.split(",")[0]
    msg = weechat.info_get_hashtable("irc_message_parse", {"message": signal_data})
    buffer = weechat.info_get("irc_buffer", "%s,%s" % (server, msg["channel"]))
    if buffer:
        weechat.prnt(buffer, "%s (%s) has joined this channel!" % (msg["nick"], msg["host"]))
    return weechat.WEECHAT_RC_OK

# it is useful here to use "*" as server, to catch JOIN messages on all IRC
# servers
weechat.hook_signal("*,irc_in2_join", "join_cb", "")

Modify messages

IRC plugin sends two "modifiers" for a message received ("xxx" is IRC command), so that you can modify it:

irc_in_xxx

modifier sent before charset decoding: use with caution, the string may contain invalid UTF-8 data; use only for raw operations on a message

irc_in2_xxx

modifier sent after charset decoding, so the string received is always UTF-8 valid (recommended)

def modifier_cb(data, modifier, modifier_data, string):
    # add server name to all messages received
    # (OK that's not very useful, but that's just an example!)
    return "%s %s" % (string, modifier_data)

weechat.hook_modifier("irc_in2_privmsg", "modifier_cb", "")
A malformed message could crash WeeChat or cause severe problems!

Parse message

New in version 0.3.4.

You can parse an IRC message with info_hashtable called "irc_message_parse".

The result is a hashtable with following keys (the example values are built with this message: @time=2015-06-27T16:40:35.000Z :nick!user@host PRIVMSG #weechat :hello!):

Key Since WeeChat (1) Description Example

tags

0.4.0

The tags in message (can be empty).

time=2015-06-27T16:40:35.000Z

tag_xxx

3.3

Unescaped value of tag "xxx" (one key per tag).

2015-06-27T16:40:35.000Z

message_without_tags

0.4.0

The message without the tags (the same as message if there are no tags).

:nick!user@host PRIVMSG #weechat :hello!

nick

0.3.4

The origin nick.

nick

user

2.7

The origin user.

user

host

0.3.4

The origin host (includes the nick).

nick!user@host

command

0.3.4

The command (PRIVMSG, NOTICE, …​).

PRIVMSG

channel

0.3.4

The target channel.

#weechat

arguments

0.3.4

The command arguments (includes the channel).

#weechat :hello!

text

1.3

The text (for example user message).

hello!

paramN

3.4

Command parameter (from 1 to N).

#weechat

num_params

3.4

Number of command parameters.

2

pos_command

1.3

The index of command in message ("-1" if command was not found).

47

pos_arguments

1.3

The index of arguments in message ("-1" if arguments was not found).

55

pos_channel

1.3

The index of channel in message ("-1" if channel was not found).

55

pos_text

1.3

The index of text in message ("-1" if text was not found).

65

(1) The key has been introduced in this WeeChat version.
dict = weechat.info_get_hashtable(
    "irc_message_parse",
    {"message": "@time=2015-06-27T16:40:35.000Z;tag2=value\\sspace :nick!user@host PRIVMSG #weechat :hello!"})

# dict == {
#     "tags": "time=2015-06-27T16:40:35.000Z;tag2=value\\sspace",
#     "tag_time": "2015-06-27T16:40:35.000Z",
#     "tag_tag2": "value space",
#     "message_without_tags": ":nick!user@host PRIVMSG #weechat :hello!",
#     "nick": "nick",
#     "user": "user",
#     "host": "nick!user@host",
#     "command": "PRIVMSG",
#     "channel": "#weechat",
#     "arguments": "#weechat :hello!",
#     "text": "hello!",
#     "param1": "#weechat",
#     "param2": "hello!",
#     "num_params": "2",
#     "pos_command": "65",
#     "pos_arguments": "73",
#     "pos_channel": "73",
#     "pos_text": "83",
# }

5.5. Infos

WeeChat version

The best way to check version is to ask "version_number" and make integer comparison with hexadecimal version number.

Example:

version = weechat.info_get("version_number", "") or 0
if int(version) >= 0x00030200:
    weechat.prnt("", "This is WeeChat 0.3.2 or newer")
else:
    weechat.prnt("", "This is WeeChat 0.3.1 or older")
Versions ≤ 0.3.1.1 return empty string for info_get("version_number") so you must check that value returned is not empty.

To get version as string:

# this will display for example "Version 0.3.2"
weechat.prnt("", "Version %s" % weechat.info_get("version", ""))

Other infos

# WeeChat config directory, for example: "/home/user/.config/weechat"
weechat.prnt("", "WeeChat config dir: %s" % weechat.info_get("weechat_config_dir", ""))

# keyboard inactivity
weechat.prnt("", "Inactivity since %s seconds" % weechat.info_get("inactivity", ""))

5.6. Infolists

Read an infolist

You can read infolist built by WeeChat or other plugins.

Example:

# read infolist "buffer", to get list of buffers
infolist = weechat.infolist_get("buffer", "", "")
if infolist:
    while weechat.infolist_next(infolist):
        name = weechat.infolist_string(infolist, "name")
        weechat.prnt("", "buffer: %s" % name)
    weechat.infolist_free(infolist)
Don’t forget to call infolist_free to free memory used by infolist, because WeeChat will not automatically free memory.