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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# Fixes for multiple servers and window items by dg
#
# 2003-08-27 coekie:
# - use item names and server tags, fixes irssi crash if window item or server is destroyed
#
# 2003-08-19
# - changed timer stop code a bit.
# should fix the random timer o.O never happened to me before.
#
# 2002-12-21 darix:
# - nearly complete rewrite ;) the old version wasnt "use strict;" capable =)
# - still some warnings with "use warnings;"
# - use of command_runsub now :)
#
use strict;
use Data::Dumper;
use warnings;
use vars qw ($VERSION %IRSSI);
use Irssi 20020325 qw (command_bind command_runsub command timeout_add timeout_remove signal_add_first);
$VERSION = '0.8';
%IRSSI = (
authors => 'Kimmo Lehto, Marcus Rueckert',
contact => 'kimmo@a-men.org, darix@irssi.org' ,
name => 'Timer',
description => 'Provides /timer command for mIRC/BitchX type timer functionality.',
license => 'Public Domain',
changed => '2015-02-07'
);
Irssi::settings_add_bool('timer', 'timer_stop_msgs', 1);
our %timers;
# my %timer = { repeat => \d+, command => '' , windowitem => NULL , server=> NULL, timer = NULL};
sub timer_command {
my ( $name ) = @_;
if ( exists ( $timers{$name} ) ) {
my ($server, $item);
if ($timers{$name}->{'server'}) {
$server = Irssi::server_find_tag( $timers{$name}->{'server'} );
}
if ( $server ) {
if ( $timers{$name}->{'windowitem'}) {
$item = $server->window_find_item( $timers{$name}->{'windowitem'} );
}
($item ? $item : $server)->command( $timers{$name}->{'command'} );
} else {
command( $timers{$name}->{'command'} );
}
if ( $timers{$name}->{'repeat'} != -1 ) {
if ( --$timers{$name}->{'repeat'} == 0) {
cmd_timerstop( $name );
}
}
}
}
sub cmd_timerstop {
my ( $name ) = @_;
my $verbose = Irssi::settings_get_bool('timer_stop_msgs');
if ( exists ( $timers{$name} ) ) {
timeout_remove($timers{$name}->{'timer'});
$timers{$name} = ();
delete ( $timers{$name} );
print( CRAP "Timer \"$name\" stopped." ) if $verbose;
}
else {
print( CRAP "\cBTimer:\cB No such timer \"$name\"." ) if $verbose;
}
}
sub cmd_timer_help {
print ( <<EOF
TIMER LIST
TIMER ADD <name> <interval in seconds> [<repeat>] <command>
TIMER STOP <name>
repeat value of 0 means unlimited too
EOF
);
}
command_bind 'timer add' => sub {
my ( $data, $server, $item ) = @_;
my ( $name, $interval, $times, $command );
if ( $data =~ /^\s*(\S+)\s+(\d+(?:\.\d+)?)\s+(-?\d+)\s+(.*)$/ ) {
( $name, $interval, $times, $command ) = ( $1, $2, $3, $4 );
$times = -1 if ( $times == 0 );
}
elsif ( $data =~ /^\s*(\S+)\s+(\d+(?:\.\d+)?)\s+(.*)$/ )
{
( $name, $interval, $times, $command ) = ( $1, $2, -1, $3 );
}
else {
print( CRAP "\cBTimer:\cB parameters not understood. commandline was: timer add $data");
return;
};
if ( $times < -1 ) {
print( CRAP "\cBTimer:\cB repeat should be greater or equal to -1" );
return;
};
if ( $command eq "" ) {
print( CRAP "\cBTimer:\cB command is empty commandline was: timer add $data" );
return;
};
if ( exists ( $timers{$name} ) ) {
print( CRAP "\cBTimer:\cB Timer \"$name\" already active." );
}
else {
#$timers{$name} = {};
$timers{$name}->{'repeat'} = $times;
$timers{$name}->{'interval'} = $interval;
$timers{$name}->{'command'} = $command;
if ($item) {
$timers{$name}->{'windowitem'} = $item->{'name'};
}
if ($server) {
$timers{$name}->{'server'} = $server->{'tag'};
}
if ( $times == -1 ) {
$times = 'until stopped.';
}
else {
$times .= " times.";
}
print( CRAP "Starting timer \"$name\" repeating \"$command\" every $interval seconds $times" );
$timers{$name}->{'timer'} = timeout_add( $interval * 1000, \&timer_command, $name );
}
};
command_bind 'timer list' => sub {
print( CRAP "Active timers:" );
foreach my $name ( keys %timers ) {
if ( $timers{$name}->{repeat} == -1 ) {
print( CRAP "$name = $timers{$name}->{'command'} (until stopped)");
}
else {
print( CRAP "$name = $timers{$name}->{'command'} ($timers{$name}->{'repeat'} repeats left)" );
}
}
print( CRAP "End of /timer list" );
};
command_bind 'timer stop' => sub {
my ( $data, $server, $item ) = @_;
cmd_timerstop ($data);
};
command_bind 'timer help' => sub { cmd_timer_help() };
command_bind 'timer' => sub {
my ( $data, $server, $item ) = @_;
$data =~ s/\s+$//g;
command_runsub ( 'timer', $data, $server, $item ) ;
};
signal_add_first 'default command timer' => sub {
#
# gets triggered if called with unknown subcommand
#
cmd_timer_help()
}
|