summaryrefslogtreecommitdiffstats
path: root/scripts/randaway.pl
blob: 91c8d381c55085295d9614d3b8255acd834b16e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/perl -w
# $Id: randaway.pl,v 1.12 2003/01/10 10:47:04 lkarsten Exp lkarsten $
# Irssi script for random away-messages.
#
# adds /raway, /awayadd, /awayreasons and /awayreread.
#
# Based on simular script written by c0ffee.
# original version made public in march 2002.
#
# changelog: 
# sep/02 - kuba wszolek (hipis@linux.balta.pl) reported problems with multiple
# servers in v1.8 .. proposed fix is imported.
# jan/03 - Wouter Coekaerts (wouter@coekaerts.be) provided fix using 
# get_irssi_dir() instead of $ENV[]. imported.
#

use strict;
use Irssi 20011116;
use Irssi::Irc;
use vars qw($VERSION %IRSSI);

$VERSION = '1.14';
%IRSSI = (
    authors	=> "Lasse Karstensen",
    contact	=> "lkarsten\@stud.ntnu.no",
    name 	=> "randaway.pl",
    description => "Random away-messages",
    license	=> "Public Domain",
    url		=> "http://www.stud.ntnu.no/~lkarsten/irssi/",
);

# file to read random reasons from. It should contain one
# reason at each line, empty lines and lines starting with # is 
# skipped.
my $reasonfile = Irssi::get_irssi_dir() . "/awayreasons";

my @awayreasons;

sub readreasons {
        undef @awayreasons;
        if (-f $reasonfile) { 
                Irssi::print("=> Trying to read awayreasons from $reasonfile");
		open F, "<", $reasonfile;

		# this actually makes the while() work like a while and not
		# like a read() .. ie, stopping at each \n.
		local $/ = "\n";
                while (<F>) {
		    my $reason = $_;

		    # remove any naughty linefeeds.
		    chomp($reason);
		    
		    # skips reason if it's an empty line or line starts with #
		    if ($reason =~ /^$/ ) { next; }
		    if ($reason =~ /^#/ ) { next; }
		    
		    Irssi::print("\"$reason\"");
		    
		    # adds to array.
		    push(@awayreasons, $reason); 
                }
                close F; 
                Irssi::print("=> Read " . scalar(@awayreasons) . " reasons.");
        } else {
	    # some default away-reasons.
	    Irssi::print("Unable to find $reasonfile, no reasons loaded.");
	    push(@awayreasons, "i\'m pretty lame!");
	    push(@awayreasons, "i think i forgot something!"); 
	};  
}

sub cmd_away {
    # only do our magic if we're not away already. 
    
    if (Irssi::active_server()->{usermode_away} == 0) {
        my ($reason) = @_; 
	# using supplied reason if .. eh, supplied, else find a random one if not.
	if (!$reason) { $reason = $awayreasons[rand @awayreasons]; }
	Irssi::print("awayreason used: $reason");
        my $server = Irssi::servers();	
        $server->command('AWAY '.$reason);	
    } else {
	Irssi::print("you're already away");
    }
} 

sub add_reason {
    my ($reason) = @_;
    if (!$reason) {
        Irssi::print("Refusing to add empty reason.");
    } else {
	chomp($reason);
	# adding to current environment.
	push(@awayreasons, $reason);
	# and also saving it for later.  
	open(F, ">>", $reasonfile);
	print F $reason,"\n";
	close F;
	Irssi::print("Added: $reason");
    }
}

sub reasons {
    Irssi::print("Listing current awayreasons");
    foreach my $var (@awayreasons) {
        Irssi::print("=> \"$var\""); 
    }
}

# -- main program --

readreasons();
Irssi::command_bind('raway', 'cmd_away');
Irssi::command_bind('awayreread', 'readreasons');
Irssi::command_bind('awayadd', 'add_reason');
Irssi::command_bind('awayreasons', 'reasons');

# -- end of script --