How to Make IRC Scripts

From Redbrick Wiki
Jump to navigation Jump to search

This article is an educational one, and not meant to be a copy-and-paste job so you fuckers can spam #lobby and the likes with even more bots and scripts. The following will give a run-through on how to run a fortune-based perl script to give random quotes in irssi.

fortune and strfile

fortune gives random quotes. Point it to a file and it will give a line of information from that file. strfile is used to create fortune files. A plain fortune file looks like this:

  Quote 1
  %
  Quote 2
  %
  Yore ma blows goats
  %
  joe5ie is the shit

Save your quotes into a file, and run strfile <filename>. That will create a .dat file. You will need both later on.

The Code

Here is the code for a fortune file IRC reader thing. You will need to edit the things in bold.

  use Irssi;
  use Irssi::Irc;
  use vars qw($VERSION %IRSSI);
  
  $mytimeout;
  $d_tmout = 0;
  $msecs = 300000;
  $limit = 1;
  
  sub cmd_hl {
   my ($server, $data, $nick, $mask, $target) =@_;
  
   if ($data=~/^!trigger/)
   {
       if($d_tmout > $limit)
       {
           print NOTICES "timeout enabled $d_tmout";
           return;
       }
       else
       {
           print NOTICES "timeout ok $d_tmout";
       }
      if ($server || $server->{connected})
       {
           $frtn = "fortune -n 200 -s ";
           if ($data=~/^!trigger/)
           {
               $frtn .= " ~user/path";
           }
  
           $cookie = `$frtn`;
  
           $cookie =~ s/\s*\n\s*/ /g;
           $cookie =~ s/\t/ /g;
           if ($cookie)
           {
               $server->command("/msg ".$target." ".$cookie);
           } else
           {
               Irssi::print ("No cookie.");
               $return = 1;
           }
       } else
       {
           Irssi::print ("Not connected to server");
           $return = 1;
       }
  
       $d_tmout += 1;
  
       if ($d_tmout == 1)
       {
           Irssi::print("adding timeout");
           $mytimeout = Irssi::timeout_add_once($msecs, sub {$d_tmout = 0; Irssi::timeout_remove($mytimeout);print NOTICES $
       }
  
   }
   return $return;
  }
  Irssi::signal_add_last('message public', 'cmd_hl');
  Irssi::print("quotes  script by nchip loaded.");


At the top, the $msecs bit refers to the period of time (in milliseconds) in which the script can be triggered. $limit is the number of times that the script can be triggered within the period $msecs. !trigger can be set to whatever you want the trigger to be. ~user/path/filename is the path to the original text file that you wrote, before running strfile. However, the .dat must be in the same directory as this file in order for it to work.

Running the script

The script needs to be placed in .irssi/scripts/ (in your home directory) and saved in .pl format. Start up irssi and the type /script load scriptname.pl (replacing scriptname with whatever the .pl file is called).

Get someoen to type your trigger (including the "!" at the start) and enjoy the ensuing hilarity and/or complaints.

--Lithium 22:06, 9 Mar 2008 (UTC)