Skip to content

Instantly share code, notes, and snippets.

@matematikaadit
Last active September 20, 2019 10:49
Show Gist options
  • Save matematikaadit/ef20718400184c6e4de0240d0c2c7128 to your computer and use it in GitHub Desktop.
Save matematikaadit/ef20718400184c6e4de0240d0c2c7128 to your computer and use it in GitHub Desktop.
Weechat Plugin API Summary

Entry/Exit Point of The Plugin

The place where you your code will be executed when it's loaded and unloaded, respectively

int weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]);

int weechat_plugin_end (struct t_weechat_plugin *plugin);

Arguments

  • t_weechat_plugin is the struct that provides the plugin api.
  • argc and argv is the argument passed to the plugin when it's loaded, manually via /plugin load command or when weechat started for autoloaded plugin. See the weechat_plugin_init for more explanation.

Return Value

  • WEECHAT_RC_OK if successful (plugin will be loaded)
  • WEECHAT_RC_ERROR if error (plugin will NOT be loaded)

The weechat_plugin_init is basically the main function of your plugin. That's where the real stuff happen. The weechat_plugin_end is for when you need to free stuff that you allocated previously.

How It Would Look From The Rust Side

#[no_mangle]
pub extern "C" weechat_plugin_init(plugin: *mut t_weechat_plugin,
                                   argc: libc::c_int,
                                   argv: *mut *mut libc::c_char) -> libc::c_int {
    // Your rust code here
    // ...
}

#[no_mangle]
pub extern "C" weechat_plugin_end(plugin: *mut t_weechat_plugin) -> libc::c_int {
    // Your rust code here
    // ...
}

Some API

Note: When an API takes a callback, it also takes an argument of callback_pointer and callback_data.

  • callback_pointer: passed to the callback, won't be freed.
  • callback_data: passed to the callback, managed by weechat. will be freed when the hook is unhooked.

Opening New Buffer

The name must be unique for the plugin.

struct t_gui_buffer *weechat_buffer_new (const char *name,
                                         int (*input_callback)(const void *pointer,
                                                               void *data,
                                                               struct t_gui_buffer *buffer,
                                                               const char *input_data),
                                         const void *input_callback_pointer,
                                         void *input_callback_data,
                                         int (*close_callback)(const void *pointer,
                                                               void *data,
                                                               struct t_gui_buffer *buffer),
                                         const void *close_callback_pointer,
                                         void *close_callback_data);

#define weechat_buffer_new(__name, __input_callback,                    \
                           __input_callback_pointer,                    \
                           __input_callback_data,                       \
                           __close_callback,                            \
                           __close_callback_pointer,                    \
                           __close_callback_data)                       \
    (weechat_plugin->buffer_new)(weechat_plugin, __name,                \
                                 __input_callback,                      \
                                 __input_callback_pointer,              \
                                 __input_callback_data,                 \
                                 __close_callback,                      \
                                 __close_callback_pointer,              \
                                 __close_callback_data)

NOTE: *_callback_data is pointer given to the callback when it's called by WeeChat; If not NULL, it must have been allocated with malloc and it's automatically freed when the buffer is clsed.

Hook Print

Hook a message printed. It is called when a line has been added in a buffer with formatted content.

struct t_hook *weechat_hook_print (struct t_gui_buffer *buffer,
                                   const char *tags,
                                   const char *message,
                                   int strip_colors,
                                   int (*callback)(const void *pointer,
                                                   void *data,
                                                   struct t_gui_buffer *buffer,
                                                   time_t date,
                                                   int tags_count,
                                                   const char **tags,
                                                   int displayed,
                                                   int highlight,
                                                   const char *prefix,
                                                   const char *message),
                                   const void *callback_pointer,
                                   void *callback_data);

#define weechat_hook_print(__buffer, __tags, __msg, __strip__colors,    \
                           __callback, __pointer, __data)               \
    (weechat_plugin->hook_print)(weechat_plugin, __buffer, __tags,      \
                                 __msg, __strip__colors, __callback,    \
                                 __pointer, __data)
  • If the buffer is NULL, capture message from any buffer. Otherwise, capture message for that buffer.
  • tags and message for capturing the message containing them. See hook_print for more explanation.

Get Buffer Property

Return string value of buffer property (there are other type too such as integer and pointer).

const char *weechat_buffer_get_string (struct t_gui_buffer *buffer,
                                       const char *property);

#define weechat_buffer_get_string(__buffer, __property)                 \
    (weechat_plugin->buffer_get_string)(__buffer, __property)

See also: String property "plugin" (for "irc" plugin) and "type" (for "channel" type). And also "localvar_channel" for localvar channel name. More on buffer_get_string.

Printing

void weechat_printf_date_tags (struct t_gui_buffer *buffer, time_t date,
                               const char *tags, const char *message, ...);

#define weechat_printf_date_tags(__buffer, __date, __tags, __message,   \
                                 __argz...)                             \
    (weechat_plugin->printf_date_tags)(__buffer, __date, __tags,        \
                                       __message, ##__argz)

void weechat_log_printf (const char *message, ...);

#define weechat_log_printf(__message, __argz...)                        \
    (weechat_plugin->log_printf)(__message, ##__argz)

Buffer Set Property

void weechat_buffer_set (struct t_gui_buffer *buffer, const char *property,
                         const char *value);

#define weechat_buffer_set(__buffer, __property, __value)               \
    (weechat_plugin->buffer_set)(__buffer, __property, __value)

See also: "hotlist" property and WEECHAT_HOTLIST_HIGHLIGHT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment