1#####################
2#
3# irssi autoreminder script.
4# Copyright (C) Terry Lewis
5# Terry Lewis <mrkennie@kryogenic.co.uk>
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20#
21#####################
22#
23# Auto reminder script for irssi
24# This is really a first attempt at an irssi script,
25# really more of a hack I suppose, to auto remind
26# someone at certain intervals.
27# It will not remind at every interval defined, so its
28# kinda less annoying, but hopefully effective.
29#
30# To start:
31#     /start <nick> <"reminder message"> [interval]
32#     (<> = required, [] = optional)
33# reminder Message must use "" parenthasis.
34#
35# to stop reminding use /stop
36#
37# I know the code is not fantastic but I will appreciate
38# any patches for improvements, just mail them to me if
39# you do improve it :)
40#
41# I use a rather nice script called cron.pl by Piotr
42# Krukowiecki which I found at http://www.irssi.org/scripts/
43# so I can start and stop the script at certain times.
44# I hope someone finds this useful, Enjoy =)
45#
46#####################
47
48use strict;
49use vars qw($VERSION %IRSSI);
50
51use Irssi;
52$VERSION = '0.01';
53%IRSSI = (
54    authors     => 'Terry Lewis',
55    contact     => 'terry@kryogenic.co.uk',
56    name        => 'Auto Reminder',
57    description => 'This script ' .
58                   'Reminds people ' .
59                   'to do stuff! :)',
60    license     => 'GPLv2',
61);
62
63my($timeout_tag, $timeout, $state, @opts, $date, @time, @hour, $start_hour, $end_hour);
64
65
66#default state 0 meaning we are not started yet
67$state = 0;
68
69
70# /start <nick> <"message"> [interval]
71sub cmd_start {
72    if($state != 1){
73        my($data,$server,$channel) = @_;
74        @opts = split(/\s\B\"(.*)\b\"/, $data);
75
76	if($opts[0] ne ''){
77	    if($opts[1] ne ''){
78	        if($opts[0] =~ /\s/g){
79	            Irssi::print("Invalid username");
80            	}elsif($opts[1] eq ''){
81	            Irssi::print("You must type a message to send");
82                }else{
83
84	            $state = 1;
85
86	            if($opts[2] =~ /[0-9]/g){
87		        $opts[2] =~ s/\s//g;
88		        $timeout = $opts[2];
89                        timeout_init($timeout);
90	            }else{
91		        Irssi::print("Invalid interval value, using defaults (15mins)") unless $opts[2] eq '';
92		        $timeout = "900000";
93		        timeout_init($timeout);
94		    }
95		    Irssi::print "Bugging $opts[0] with message \"$opts[1]\" every \"$timeout ms\"";
96	        }
97            }else{
98	         Irssi::print ("Usage: /start nick \"bug_msg\" [interval] (interval is optional)");
99	    }
100	}else{
101	    Irssi::print ("Usage: /start nick \"bug_msg\" [interval] (interval is optional)");
102	}
103
104    }else{
105        Irssi::print "Already started";
106    }
107}
108
109# /stop
110sub cmd_stop {
111    if($state == 1){
112        $state = 0;
113	Irssi::print "No longer bugging $opts[0]";
114	Irssi::timeout_remove($timeout_tag);
115	$timeout_tag = undef;
116    }else{
117      Irssi::print "Not started";
118    }
119}
120
121sub timeout_init {
122    if($state == 1){
123
124        Irssi::timeout_remove($timeout_tag);
125        $timeout_tag = undef;
126        $timeout_tag = Irssi::timeout_add($timeout, "remind_them", "");
127    }
128}
129
130sub remind_them {
131    if($state == 1){
132        my (@servers) = Irssi::servers();
133
134	# make it random, so we dont remind at every defined interval
135        my $time = rand()*3;
136
137        if($time < 1){
138            $servers[0]->command("MSG $opts[0] Hi, this is an automated reminder, $opts[1]");
139	}
140        timeout_init($timeout);
141    }
142}
143
144
145Irssi::command_bind('start', \&cmd_start);
146Irssi::command_bind('stop', \&cmd_stop);
147
148