html/timer.pl


   1 # Fixes for multiple servers and window items by dg
   2 #
   3 # 2003-08-27 coekie:
   4 # - use item names and server tags, fixes irssi crash if window item or server is destroyed
   5 #
   6 # 2003-08-19
   7 #  - changed timer stop code a bit.
   8 #    should fix the random timer o.O never happened to me before.
   9 #
  10 # 2002-12-21 darix:
  11 #  - nearly complete rewrite ;) the old version wasnt "use strict;" capable =)
  12 #  - still some warnings with "use warnings;"
  13 #  - use of command_runsub now :)
  14 #
  15 
  16 use strict;
  17 use Data::Dumper;
  18 use warnings;
  19 use vars  qw ($VERSION %IRSSI);
  20 use Irssi 20020325 qw (command_bind command_runsub command timeout_add timeout_remove signal_add_first);
  21 
  22 $VERSION = '0.5';
  23 %IRSSI = (
  24     authors     => 'Kimmo Lehto, Marcus Rueckert',
  25     contact     => 'kimmo@a-men.org, darix@irssi.org' ,
  26     name        => 'Timer',
  27     description => 'Provides /timer command for mIRC/BitchX type timer functionality.',
  28     license     => 'Public Domain',
  29     changed     => '2003-08-27'
  30 );
  31 
  32 our %timers;
  33 # my %timer = { repeat => \d+, command => '' , windowitem => NULL , server=> NULL, timer = NULL};
  34 
  35 sub timer_command {
  36     my ( $name ) = @_;
  37     if ( exists ( $timers{$name} ) ) {
  38         if ( $timers{$name}->{'repeat'} != -1 ) {
  39             if ( $timers{$name}->{'repeat'}-- == 0) {
  40                 cmd_timerstop( $name );
  41                 return;
  42             }
  43         }
  44 
  45         my ($server, $item);
  46         if ($timers{$name}->{'server'}) {
  47             $server = Irssi::server_find_tag( $timers{$name}->{'server'} );
  48         }
  49         if ( $server ) {
  50 	    if ( $timers{$name}->{'windowitem'}) {
  51                 $item = $server->window_find_item( $timers{$name}->{'windowitem'} );
  52             }
  53             ($item ? $item : $server)->command( $timers{$name}->{'command'} );
  54         } else {
  55             command( $timers{$name}->{'command'} );
  56         }
  57     }
  58 }
  59 
  60 sub cmd_timerstop {
  61     my ( $name ) = @_;
  62 
  63     if ( exists ( $timers{$name} ) ) {
  64         timeout_remove($timers{$name}->{'timer'});
  65         $timers{$name} = ();
  66         delete ( $timers{$name} );
  67         print( CRAP "Timer \"$name\" stopped." );
  68     }
  69     else {
  70         print( CRAP "\cBTimer:\cB No such timer \"$name\"." );
  71     }
  72 }
  73 
  74 sub cmd_timer_help {
  75     print ( <<EOF
  76 
  77 TIMER LIST
  78 TIMER ADD  <name> <internal in seconds> [<repeat>] <command>
  79 TIMER STOP <name>
  80 
  81 repeat value of 0 means unlimited too
  82 
  83 EOF
  84     );
  85 }
  86 
  87 command_bind 'timer add' => sub {
  88     my ( $data, $server, $item ) = @_;
  89     my ( $name, $interval, $times, $command );
  90 
  91     if ( $data =~ /^\s*(\w+)\s+(\d+)\s+(-?\d+)\s+(.*)$/ ) {
  92         ( $name, $interval, $times, $command ) = ( $1, $2, $3, $4 );
  93         $times = -1 if ( $times == 0 );
  94     }
  95     elsif ( $data =~ /^\s*(\w+)\s+(\d+)\s+(.*)$/ )
  96     {
  97         ( $name, $interval, $times, $command ) = ( $1, $2, -1, $3 );
  98     }
  99     else {
 100         print( CRAP "\cBTimer:\cB parameters not understood. commandline was: timer add $data");
 101         return;
 102     };
 103 
 104     if ( $times < -1 ) {
 105         print( CRAP "\cBTimer:\cB repeat should be greater or equal to -1" );
 106         return;
 107     };
 108 
 109     if ( $command eq "" ) {
 110         print( CRAP "\cBTimer:\cB command is empty commandline was: timer add $data" );
 111         return;
 112     };
 113 
 114     if ( exists ( $timers{$name} ) ) {
 115         print( CRAP "\cBTimer:\cB Timer \"$name\" already active." );
 116     }
 117     else {
 118         #$timers{$name} = {};
 119         $timers{$name}->{'repeat'}     = $times;
 120         $timers{$name}->{'interval'}   = $interval;
 121         $timers{$name}->{'command'}    = $command;
 122 	if ($item) {
 123             $timers{$name}->{'windowitem'} = $item->{'name'};
 124 	}
 125 	if ($server) {
 126             $timers{$name}->{'server'}     = $server->{'tag'};
 127 	}
 128 
 129         if ( $times == -1 ) {
 130             $times = 'until stopped.';
 131         }
 132         else {
 133             $times .= " times.";
 134         }
 135 
 136         print( CRAP "Starting timer \"$name\" repeating \"$command\" every $interval seconds $times" );
 137 
 138         $timers{$name}->{'timer'} = timeout_add( $interval * 1000, \&timer_command, $name );
 139     }
 140 };
 141 
 142 command_bind 'timer list' => sub {
 143     print( CRAP "Active timers:" );
 144     foreach my $name ( keys %timers ) {
 145         if ( $timers{$name}->{repeat} == -1 ) {
 146             print( CRAP "$name = $timers{$name}->{'command'} (until stopped)");
 147         }
 148         else {
 149             print( CRAP "$name = $timers{$name}->{'command'} ($timers{$name}->{'repeat'} repeats left)" );
 150         }
 151     }
 152     print( CRAP "End of /timer list" );
 153 };
 154 
 155 command_bind 'timer stop' => sub {
 156     my ( $data, $server, $item ) = @_;
 157     cmd_timerstop ($data);
 158 };
 159 
 160 command_bind 'timer help' => sub { cmd_timer_help() };
 161 
 162 command_bind 'timer' => sub {
 163     my ( $data, $server, $item ) = @_;
 164     $data =~ s/\s+$//g;
 165     command_runsub ( 'timer', $data, $server, $item ) ;
 166 };
 167 
 168 
 169 signal_add_first 'default command timer' => sub {
 170 #
 171 # gets triggered if called with unknown subcommand
 172 #
 173     cmd_timer_help()
 174 }
 175