2011/02/25

irssi Fun

For those who don't know, irssi is an irc client which uses perl5 scripts.  perl for these types of things is your friend.  It makes parsing through text a breeze.  Plus it's fairy C like in nature.  For some good learning references for perl5 see:
http://learn.perl.org/books/beginning-perl/

This is what I did to get started. I read through about the first 8 chapters or so and played with ideas code they listed in there.  There's also some stuff writing irssi scripts.
http://juerd.nl/site.plp/irssiscripttut

Although I mostly just read the scripst on irssi's site:
http://scripts.irssi.org/

Now on to some of the basics of writing a irssi script.  First you need to include some stuff for irssi.  The first part of my bot's script looks like this.

  use strict;
  use vars qw($VERSION %IRSSI);
  use Irssi;   # qw(command_bind signal_add);
  use File::Copy;
  use Encode;
 
  $VERSION = '0.35';
  %IRSSI = (
          authors         =>      'larks',
          contant         =>      'this blog',
          name            =>      'LarksBot',
          description     =>      'LarksBots script',
          license         =>      'I dunno'
  );
You need to have the "use vars qw($VERSION %IRSSI)" and the "use Irssi" in there.  Basically making sure the script can use irssi functions and has the right variables.  Then you have to naturally setting the $VERSION and %IRSSI variables.

Next is tying in the functions to the irssi signals.  There's a list of irssi signals here:
http://irssi.org/documentation/signals

For my bot I used the "message public" signal.  So to use a function/subroutine "event_msg" I used
Irssi::signal_add("message public", "event_mesg");

"Irssi::signal_add" is naturally a function that "use Irssi" provides.  It adds a signal to a function/etc in a script.  If you look at the link above, for the signals, you'll see:

"message public", SERVER_REC, char *msg, char *nick, char *address, char *target

where,
    "message public" is the signal name

    "SERVER_REC" is the server info
    "char *msg" is the message that is received
    "char *nick" is the nick that sent the mesage
    "char *address" is the address of the nick that sent it
    "char * target" is the channel it was sent to.

Everything after the signal name is an input to your function.  To have meaning variables etc something like this can be used.

sub event_mesg {
         my($server, $msg, $nick, $address, $target) = @_;
         #meaningful code here
}
This will extract the data from the input array, @_, and save it to the variables which are much easier to work with that trying to keep up with $_[2] etc.

So now that you have your script includes etc and the signal set up, what do you do with it?  Well let's start with a simple hello world script.


  1 use strict;
  2 use Irssi;
  3 use vars qw($VERSION %IRSSI);
  4
  5 $VERSION = "1.0";
  6 %IRSSI = (
  7     authors => 'larks',
  8     contact => 'larks blog',
  9     name => 'hello world',
 10     description => 'a simple hello world script',
 11     license => 'GPL v2',
 12     url => 'http://larkstongues42.blogspot.com/'
 13 );
 14
 15 sub event_msg {
 16         my($server, $msg, $nick, $address, $target) = @_;
 17         $_ = $msg;
 18         if (/^\.hello\s*$/i) {
 19         $server->command('msg '.$target.' Hello world!');
 20         }
 21         return 0;
 22 }
 23 Irssi::signal_add('message public', 'event_msg');
which will produce

23:25:56 <~`-`> .hello
23:25:56 <~larks> Hello world!
It's often convenient to use $_ so that when checking the messages for something you don't have to use "$msg =~ /somethingofind/" rather than "/somethingtofind". The
$server->command('msg '.$target.' Hello world!');
command is really the
Irssi::Server::command($server,'msg '.$target.' Hello world!');
command. Taken from perl.txt from the irssi documentation:

If there's a "Xxxx::" text before the command, it means that it belongs to
that package. Like "Server::command" means that you should either call it as
  Irssi::Server::command($server, $cmd);
or more easily:
  $server->command($cmd);
 The docs that come with irssi have a wealth of information.  They're in "/usr/share/doc/irssi/" if you use Debian.  There's a ton of useful Irssi commands listed in the documentation which I highly suggest you read over.  With a decent knowledge of perl, reading over some docs and experimenting, you can really get pretty far.  I honestly hadn't done much programming before starting this other that microcontroller stuff which is completely different and I've managed to do some pretty interesting stuff.  Most of it is isn't that hard, it's just specifying what you want, planing out your code, and then writing it.  Another useful trick it so write the code in a plain old perl script and get it working before trying to code it into an irssi script.  This splits up the process into a debugging stage for the actual algorithms(if you can call them that) and interfacing with irssi.  Sometimes this can't be done, but it's pretty useful when you can. 

Well this concludes my very simple bare bones irssi scripting how to.

2011/02/23

Hello World

This is my blog for random technical stuff.  By that I mean linux, coding, EE/CompE stuff and of the likes.  I'll most likely do a entry on irssi bot scripting before too long.  See you around.