WeeChat DevCenter

Tag - api

Entries feed

Sunday, May 19 2024

API relay and remote connection

A brand new relay called "api" has been devleopped since 6 months : it's an HTTP REST API, which should in long term replace completey the "weechat" protocol.

The "api" relay provides the following key features:

  • easy client implementation: HTTP REST API, JSON input/output, use of ANSI color codes
  • automatic compression of responses (deflate, gzip, zstd and permessage-deflate for websocket)
  • data synchronization: real-time sync with websocket or polling with HTTP requests
  • no internal structures exposed: no use of pointers & complex structures like hdata
  • WeeChat itself can connect to another WeeChat using this protocol (also known as relay "remote")

Documentation: Relay API.

The new /remote command lets you connect to the "api" relay of another running WeeChat (it must expose the same API version).

To connect to a remote WeeChat, just add a relay on the remote (for TLS, which is recommended, replace api by tls.api):

/set relay.network.password "secr3t"
/relay add api 9000

On the client, supposing it's running on the same machine:

/remote add test http://localhost:9000 -password=secr3t
/remote connect test

Then all buffers of remote WeeChat are opened locally by relay and any text or command sent on these buffers are sent and executed on the remote WeeChat.

Input is also synchronized (from remote to local WeeChat only), so that remote buffers like /fset can be used locally, including keys like Alt+Enter to set input with a command to execute.
Only the local buffer mouse actions are not yet supported (ie you can not scroll the fset buffer with mouse).

Wednesday, January 18 2012

URL transfer in API

URL transfer has been added in API for plugins and scripts (using libcurl).

A new function has been added: "hook_process_hashtable". Behaviour is the same as "hook_process", but with an extra "options" (a hashtable). To download URL content (html page or file), the syntax is (example is python):

weechat.hook_process_hashtable('url:http://weechat.org/download/',
                               { 'file_out': '/tmp/url.txt' },
                               10000, 'my_process_cb', '')

This code will download URL in file "/tmp/url.txt" and call function "my_process_cb" when done, with return code:

  • 0: transfer ok
  • 1: invalid URL
  • 2: transfer error
  • 3: not enough memory
  • 4: error with a file

It is possible to download URL without any option, then the output is on standard output of process (received as "out" in callback, possibly in many chunks, depending on page size):

weechat.hook_process('url:http://weechat.org/download/', 10000, 'my_process_cb', '')

Saturday, May 2 2009

Data for callbacks in script API

Major changes were done in script API: data string was added to all callbacks. C plugin API is already ok (there is data pointer for callbacks).
Therefore, all scripts for development version are incompatible with this new version. All official scripts have been updated on plugins page.

This data string must be added after each callback function in arguments of functions. For example in python :

weechat.hook_command("go", "Quick jump to buffers", "", "", "", "go_cmd")
(...)
def go_cmd(buffer, args):

becomes:

weechat.hook_command("go", "Quick jump to buffers", "", "", "", "go_cmd", "")
(...)
def go_cmd(data, buffer, args):

Friday, March 6 2009

New hook "process"

New hook type "process" has been added to WeeChat. You can use it in C plugins, and all scripts languages (perl, python, ruby, lua and tcl).

It runs a command with fork, and send you result (return code, stdout and stderr) via a callback, when command has ended, or if WeeChat output buffer is full (then it will be partial result of command). There is optional timeout (in milliseconds), to kill process if it's still running after given time.

The script shell.py has been updated to use that new hook.