WeeChat IRC Client - Formatting Bridge Bot Output
18 Nov 2018 · Comments: · Tags: IRC, WeeChat, BridgeBotSummary
In order for a community (such as the participants of an open source project) to collaborate online in real-time, a chat platform is required. Some communities have adopted multiple platforms such as IRC, Slack and Discord. Consequently a means of bridging these platforms together is required so that the users of each can converse with each other.
One method of doing so is to use a bridge bot. The bot joins the community’s channel on each respective platform. Whenever a message is received, the bot relays it to all platforms other than the one it originated from.
The way in which messages from the bot are presented is dependent on the capabilities/limitations of the platform and the creativity of the bot’s admin. On IRC such messages typically contain the name of the original sender (along with other metadata) within the message text.
The following excerpt illustrates an IRC channel in which a bot named Kilroy
is relaying messages from the users of other platforms:
I’ll return to this excerpt in more detail later.
The purpose of this post is to demonstrate a technique whereby the WeeChat IRC client’s scripting API can be used (with Python) to format such messages to appear as though they originate from native IRC users, thus making it easier to follow conversations.
There are other solutions available that’ll more or less achieve the same thing and may be better suited to your needs. I won’t cover these in depth but have listed a few at the end of the post under Alternative Solutions.
format_bridge_bot_output.py
The WeeChat IRC client provides a scripting API with support for several languages including Python, Perl and Ruby. I had wanted an excuse to dabble with Python for quite a while, so that’s what I opted to use.
The script can be found here: format_bridge_bot_output.py. It intercepts messages before WeeChat displays them and if found to have been sent by a bridge bot proceeds to alter the message so that when displayed it has the appearance of being sent by a native IRC user.
Installation
Since this script has not been published to the WeeChat Script Repository it must be installed manually:
- Download the script to Weechat’s python directory:
wget -O ~/.weechat/python/format_bridge_bot_output.py https://raw.githubusercontent.com/thecliguy/WeeChat-Scripts/master/format_bridge_bot_output/format_bridge_bot_output.py
- Create a link to the autoload directory so that the script is loaded when WeeChat starts:
cd ~/.weechat/python/autoload
ln -s ../format_bridge_bot_output.py
Configuration
Customisable non-volatile settings pertaining to a script are referred to as
script options
in WeeChat terminology.
This script groups related options together, the purpose of which is to accommodate users who participate in multiple IRC channels where bridge bots are used. Each channel can have its own group of options.
The available options are:
<optgroup>.bot_nicks
The bridge bot nick(s). Multiple nicks can be specified using a comma separated list.<optgroup>.channel
The channel name.<optgroup>.nick_display_max_length
The maximum number of characters to be displayed for nicks of non-IRC users. Those greater than the specified value are truncated with an ellipsis appended. At the time of writing, Slack permits a nick of up to 80 characters, this would dominate too much screen real estate within WeeChat, hence why it is important to impose a limit.<optgroup>.regex
A regular expression used to extract the following from messages relayed by the bridge bot:- Action: For when a user sends an action message (typically done using
/me
, EG/me sighs
). - Network: The external networks bridged to IRC by the bot, EG Slack or Discord.
- Nick: The nick of the sender (not the nick of the bot).
- Text: The message text.
- Action: For when a user sends an action message (typically done using
<optgroup>.server
The internal server name used by WeeChat. To obtain a list of server names, use the WeeChat command/server list
.
Please refer to the script for details regarding how to add and remove script options.
Example Usage
Returning to the excerpt at the beginning of the post, we already know that the
bot’s name is Kilroy
and its message text is formatted as (network) <nick> Message
.
To expand on that, let’s suppose that the channel is called #foobar
, the server
is GroovyIRC
and the maximum permitted nick length is 20. The options for such
a channel would be as follows:
MyGroup.bot_nicks = Kilroy
MyGroup.channel = #foobar
MyGroup.nick_display_max_length = 20
MyGroup.regex = (?P<action>(?:^[\x01]ACTION |^))\((?P<network>(?:slack|discord))\) <(?P<nick>.+?)> (?P<text>.*)
MyGroup.server = GroovyIRC
This is the conversation as it would appear without any modification:
This is the same conversation as it would appear using format_bridge_bot_output.py
,
the messages would be intercepted and amended to appear as though Barry, Charles
and Nigel are native IRC users:
Alternative Solutions
These are a few alternative ways to format messages from bridge bots using the WeeChat IRC client:
- parse_relay_msg.pl - A Perl script in the official WeeChat scripts repository.
- weechat_bot2human.py - My script (
format_bridge_bot_output.py
) started life asweechat_bot2human.py
(version 0.1.1) from the scripts repository of the TUNA (Tsinghua University TUNA Association) organization on Github. It has since been heavily modified and thus now bears little resemblance to the original script. - WeeChat Triggers - Run the command
/help trigger
for more information.
NB: I cannot personally vouch for parse_relay_msg.pl
or WeeChat triggers as I
have not tried them myself.
Comments