1#!/usr/local/bin/perl -w 2# $Id: randaway.pl,v 1.12 2003/01/10 10:47:04 lkarsten Exp lkarsten $ 3# Irssi script for random away-messages. 4# 5# adds /raway, /awayadd, /awayreasons and /awayreread. 6# 7# Based on simular script written by c0ffee. 8# original version made public in march 2002. 9# 10# changelog: 11# sep/02 - kuba wszolek (hipis@linux.balta.pl) reported problems with multiple 12# servers in v1.8 .. proposed fix is imported. 13# jan/03 - Wouter Coekaerts (wouter@coekaerts.be) provided fix using 14# get_irssi_dir() instead of $ENV[]. imported. 15# 16 17use strict; 18use Irssi 20011116; 19use Irssi::Irc; 20use vars qw($VERSION %IRSSI); 21 22$VERSION = '1.14'; 23%IRSSI = ( 24 authors => "Lasse Karstensen", 25 contact => "lkarsten\@stud.ntnu.no", 26 name => "randaway.pl", 27 description => "Random away-messages", 28 license => "Public Domain", 29 url => "http://www.stud.ntnu.no/~lkarsten/irssi/", 30); 31 32# file to read random reasons from. It should contain one 33# reason at each line, empty lines and lines starting with # is 34# skipped. 35my $reasonfile = Irssi::get_irssi_dir() . "/awayreasons"; 36 37my @awayreasons; 38 39sub readreasons { 40 undef @awayreasons; 41 if (-f $reasonfile) { 42 Irssi::print("=> Trying to read awayreasons from $reasonfile"); 43 open F, "<", $reasonfile; 44 45 # this actually makes the while() work like a while and not 46 # like a read() .. ie, stopping at each \n. 47 local $/ = "\n"; 48 while (<F>) { 49 my $reason = $_; 50 51 # remove any naughty linefeeds. 52 chomp($reason); 53 54 # skips reason if it's an empty line or line starts with # 55 if ($reason =~ /^$/ ) { next; } 56 if ($reason =~ /^#/ ) { next; } 57 58 Irssi::print("\"$reason\""); 59 60 # adds to array. 61 push(@awayreasons, $reason); 62 } 63 close F; 64 Irssi::print("=> Read " . scalar(@awayreasons) . " reasons."); 65 } else { 66 # some default away-reasons. 67 Irssi::print("Unable to find $reasonfile, no reasons loaded."); 68 push(@awayreasons, "i\'m pretty lame!"); 69 push(@awayreasons, "i think i forgot something!"); 70 }; 71} 72 73sub cmd_away { 74 # only do our magic if we're not away already. 75 76 if (Irssi::active_server()->{usermode_away} == 0) { 77 my ($reason) = @_; 78 # using supplied reason if .. eh, supplied, else find a random one if not. 79 if (!$reason) { $reason = $awayreasons[rand @awayreasons]; } 80 Irssi::print("awayreason used: $reason"); 81 my $server = Irssi::servers(); 82 $server->command('AWAY '.$reason); 83 } else { 84 Irssi::print("you're already away"); 85 } 86} 87 88sub add_reason { 89 my ($reason) = @_; 90 if (!$reason) { 91 Irssi::print("Refusing to add empty reason."); 92 } else { 93 chomp($reason); 94 # adding to current environment. 95 push(@awayreasons, $reason); 96 # and also saving it for later. 97 open(F, ">>", $reasonfile); 98 print F $reason,"\n"; 99 close F; 100 Irssi::print("Added: $reason"); 101 } 102} 103 104sub reasons { 105 Irssi::print("Listing current awayreasons"); 106 foreach my $var (@awayreasons) { 107 Irssi::print("=> \"$var\""); 108 } 109} 110 111# -- main program -- 112 113readreasons(); 114Irssi::command_bind('raway', 'cmd_away'); 115Irssi::command_bind('awayreread', 'readreasons'); 116Irssi::command_bind('awayadd', 'add_reason'); 117Irssi::command_bind('awayreasons', 'reasons'); 118 119# -- end of script -- 120