Humans love shiny. And there’s nothing more shiny to a programmer than a brand new version of a tool used every day. Here at Mystic we use IRC as our primary mode of communication and project collaboration, and several of us have chosen Weechat. Recently we wrote about receiving irc notifications from Weechat over ssh, and the plugin written was for the current stable version of Weechat 0.2.6. The development version of Weechat is a complete rewrite, and breaks plugin compatibility. If we rewrote growl-net-notify.pl for this new version, the choice was to manage two separate source files, or find some way to make one work for both. I chose the latter method because programmers as I’ve always said, are smart lazy people, we’ll do whatever we can to automate something.
Identifying Weechat version
First step in combining the version code is identifying which version we’re running under. Since I don’t want to cause any errors in either version when the plugin is loaded, I decided to use Perl’s exists method which can be used to check existence of a subroutine:
if(exists &weechat::info_get)
With this in hand, we can perform the setup commands for 0.2.6 and 0.2.7 independent of each other, and preset the $weechat_version variable so we can key off of it in other portions of the code.
Changes in 0.2.7
Notifications to the user are prevalent throughout our code, we have several easy commands so the user can easily setup their network settings and parameters that are important. So we use weechat::print a lot, and it has changed in 0.2.7.
The previous version gave you a single parameter for weechat::print, and output to whatever your active buffer was. The new version forces you to specify a buffer, and allows you to pass in an empty string to denote the main buffer.
weechat::print(message) becomes weechat::print(buffer, message)
The developers have standardized on different naming conventions for 0.2.7, and a lot of action oriented items are hook’s. So instead of weechat::add_command_handler, you would write weechat::hook_command. There were some subtle parameter changes as well, and since 0.2.7 is not final, this still might change.
weechat::register has also seen some minor changes, including parameters for author, and license type.
Purely cosmetic changes it seems, weechat::get_plugin_config becomes weechat::config_get_plugin, and weechat::set_plugin_config becomes weechat::config_set_plugin.
The status constants have also changed, so weechat::PLUGIN_RC_OK becomes weechat::WEECHAT_RC_OK, and weechat::PLUGIN_RC_KO becomes weechat::WEECHAT_RC_ERROR.
Message Handling
The majority of the work done in upgrading for 0.2.7 came at the hands of the public message highlights, and the private message highlights. In the previous version, we used Parse::IRC to pull out the pieces of the IRC message that were important to us to show in our notification. In 0.2.7, they’ve simplified things for us greatly.
weechat::hook_print allows us to watch each message, check if our nick has been highlighted, and process accordingly. We’re passed in a nice array which gives us if we’re highlighted, who said it, and the message.
weechat::hook_signal allows us to watch private messages, and gives us a string separated by a tab with the nick that is private messaging, and the message. We might have liked it if we got a nice fancy array passed in just like hook_print gives us, but this works out just fine.
Because we were writing this for both 0.2.6 and 0.2.7, I wanted to get rid of the dependency on Parse::IRC and put a little bit of Perl regex magic to work. Together armed with RegexTester.com, I was able to pull out the data needed in these legacy methods and achieve detachment from needing Parse::IRC.
Now I’m sure there are some Perl-style tricks that I’m not doing here, and the code definitely fails because it is readable :). And of course, after talking with FlashCode on #weechat, he suggested we continue development only on 0.2.7, so the version in source control has two versions now. Feel free to look at older commits to see the combined version, it was definitely fun to figure out how to write it.
If you’d just like to use this plugin for your Growl notification over SSH needs:
If you’re also interested in reviewing the source: