]> git.vpit.fr Git - perl/scripts/xchat.git/blobdiff - Xchat/XPI/Events.pm
Make sure local_context() runs the code only when the context was found
[perl/scripts/xchat.git] / Xchat / XPI / Events.pm
index b26f784ff6e1161b076786e7d7c0a10f16f1f7b5..3b3963b32f502359a748ecbd0d26972bdc103b48 100644 (file)
@@ -3,62 +3,65 @@ package Xchat::XPI::Events;
 use strict;
 use warnings;
 
-use Xchat qw/:all/;
+use Xchat qw<:all>;
 
 use lib get_info 'xchatdir';
-use Xchat::XPI;
+use Xchat::XPI::Utils qw<called_from_script>;
 
-use constant {
- DEFAULT_DELAY => 1000
-};
+use constant DEFAULT_DELAY => 1000;
 
-our $VERSION = '0.03';
+our $VERSION = '0.04';
 
-my $ph;
+sub _delay_cb {
+ my ($cb, @args) = @{$_[0]};
+
+ $cb->(@args);
+
+ return REMOVE;
+}
 
 sub delay {
- my $d = shift;
- $d = (defined $d) ? int $d : DEFAULT_DELAY;
- return hook_timer $d, sub {
-  my ($cb, $data) = @{$_[0]};
-  &$cb($data);
-  return REMOVE;
- }, { data => \@_ };
+ my ($delay, @args) = @_;
+
+ if (defined $delay) {
+  $delay = int $delay;
+ } else {
+  $delay = DEFAULT_DELAY;
+ }
+
+ called_from_script {
+  hook_timer $delay, \&_delay_cb, { data => \@args }
+ }
 }
 
-my %reorder;
+sub _filter_cb {
+ my @args      = @{$_[0]};
+ my ($cb, $to) = @{$_[1]};
+
+ if ($cb->(@args)) {
+  emit_print $to, @args;
+  return EAT_ALL;
+ } else {
+  return EAT_NONE;
+ }
+}
 
 sub filter {
- return unless @_ == 3;
- not defined and return for @_;
- my ($cb, $to) = @_[1, 2];
- return hook_print $_[0], sub {
-  my @args = @{$_[0]};
-  if ($cb->(@args)) {
-   $reorder{$to}->(@args) if exists $reorder{$to};
-   emit_print $to, @args;
-   return EAT_ALL;
-  } else {
-   return EAT_NONE;
-  }
+ my ($from, $cb, $to) = @_;
+
+ called_from_script {
+  hook_print $from, \&_filter_cb, { data => [ $cb, $to ] }
  }
 }
 
-use base qw/Exporter/;
+use base qw<Exporter>;
 
 our @EXPORT         = ();
 our %EXPORT_TAGS    = (
- 'funcs'  => [ qw/delay filter/ ],
- 'consts' => [ qw/DEFAULT_DELAY/ ]
+ 'funcs'  => [ qw<delay filter>  ],
+ 'consts' => [ qw<DEFAULT_DELAY> ],
 );
 our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
 
-$ph = new Xchat::XPI name   => 'Extended Xchat Perl Interface :: Events',
-                     tag    => 'XPI::Events',
-                     desc   => 'More events handlers',
-                     author => 'Vincent Pit (VPIT)', 
-                     email  => 'perl@profvince.com',
-                     url    => 'http://www.profvince.com';
-
 1;