#relay #protocols #messages #command #weechat #interfacing #wee-chat

bin+lib weechat-relay-rs

A library for interfacing with the WeeChat Relay protocol

1 unstable release

0.1.0 Nov 1, 2022

#365 in Compression

39 downloads per month

MIT/Apache

125KB
3K SLoC

A Rust implementation of the WeeChat Relay protocol

weechat-relay-rs is a pure-Rust library for interfacing with a WeeChat Relay, including sending commands and receiving messages. This repo also hosts weechat-relay-cli, a minimal terminal application for testing, debugging, and scripting with weechat-relay-rs. All code is safe Rust, and the library only depends on the standard library and nom.

What is the WeeChat Relay protocol?

WeeChat, the extensible chat client, can operate as a "relay" for other clients to connect to as "remote interfaces". This allows users to pick their own interface for their desktop, browser, mobile device, etc.. To use this functionality, the client must use WeeChat's custom Relay protocol.

Why does the WeeChat Relay protocol need a library?

Because the protocol is entirely bespoke, and does not use any sort of generalized representation (e.g., XML, JSON, protobufs, etc.), clients cannot rely on popular, well-established libraries for parsing or representing WeeChat relay commands and messages. The protocol was not written to be cleanly abstracted, and in a lot of ways simply exposes the relay's underlying C representations. This means that even simple interactions can be cumbersome to get working, with subtle edge cases cropping up in unexpected ways. A library that is (hopefully) correct should make it a lot easier to get up and running.

Why a Rust library?

The protocol makes heavy use of complicated structures with many types and various constraints. Aside from the obvious advantages of memory safety, the compile-time type checks make Rust a language well-suited to consuming the relay protocol, preventing bugs that might otherwise occur in rare edge-cases at run-time.

What is the status of the library?

The library is "complete" in the sense that it should be able to handle all commands and messages currently possible in the latest WeeChat (other than compressed messages), but is very much incomplete in that the API is not yet stable. It is currently published as something good enough to get fully working code, but there are still rough edges that were left in to get it working quickly. Feel free to use it, but if you do so, understand that future updates will likely break your code.

How does the CLI tool work?

The CLI tool is not meant to be used as a full WeeChat client, but instead, to play around with the library and WeeChat. You can also use it to do some basic shell scripting that interfaces with a WeeChat relay. It takes as input, either in a prompt or from a supplied file or pipe, WeeChat relay commands in essentially their raw form (parsed locally for basic correctness before sending), and prints any response messages in a mostly-human-readable layout, with the message ID followed by one message object per line.

The the CLI doesn't support compression, but otherwise, it can do most things a WeeChat client can. Aside from the normal WeeChat relay commands, there are a few commands to interact with the application rather than the relay, currently signified by starting with an underscore (_):

  • _get [n]: Get n messages from the relay. Most commands expect 0 or 1 messages in response, and the CLI will wait as appropriate for that many messages, but if you want to use the sync command, messages will start coming in asynchronously, so you'll have to use _get to see them all. You'll probably want to set the --timeout option if you want to use this, else you'll be waiting a while to do anything if there were fewer messages available than you requested.
  • _sleep [n]: Sleep n seconds before sending the next command. Can be useful when running commands from a file.
  • _quit: Quit the client. If running commands from a file, the application will also quit normally once it gets to the end of the file.

Run the executable with -h to see all the invocation options.

An example session: cargo run --features cli -- --host "127.0.0.1:9001" --init "my weechat password"

> (before) info version
(before)
inf: ("version": "3.6")
> input core.weechat /upgrade
> (after) info version
(after)
inf: ("version": "3.7.1")
> _quit

With the --script option or a pipe, you can use hooks from your package manager to automatically upgrade WeeChat when the package upgrades, set up a cron job to automatically update channel topics, or anything else you can think of with a shell, all run from your WeeChat instance (no need for separate bot accounts to manage).

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.8–1.4MB
~23K SLoC