]> git.vpit.fr Git - perl/scripts/xchat.git/blob - Xchat/XPI/Events.pm
Fix callback argument passing in Xchat::XPI::Events
[perl/scripts/xchat.git] / Xchat / XPI / Events.pm
1 package Xchat::XPI::Events;
2
3 use strict;
4 use warnings;
5
6 use Xchat qw<:all>;
7
8 use lib get_info 'xchatdir';
9 use Xchat::XPI::Utils qw<called_from_script>;
10
11 use constant DEFAULT_DELAY => 1000;
12
13 our $VERSION = '0.04';
14
15 sub _delay_cb {
16  my ($cb, @args) = @{$_[0]};
17
18  $cb->(@args);
19
20  return REMOVE;
21 }
22
23 sub delay {
24  my ($delay, @args) = @_;
25
26  if (defined $delay) {
27   $delay = int $delay;
28  } else {
29   $delay = DEFAULT_DELAY;
30  }
31
32  called_from_script {
33   hook_timer $delay, \&_delay_cb, { data => \@args }
34  }
35 }
36
37 sub _filter_cb {
38  my @args      = @{$_[0]};
39  my ($cb, $to) = @{$_[1]};
40
41  if ($cb->(@args)) {
42   emit_print $to, @args;
43   return EAT_ALL;
44  } else {
45   return EAT_NONE;
46  }
47 }
48
49 sub filter {
50  my ($from, $cb, $to) = @_;
51
52  called_from_script {
53   hook_print $from, \&_filter_cb, { data => [ $cb, $to ] }
54  }
55 }
56
57 use base qw<Exporter>;
58
59 our @EXPORT         = ();
60 our %EXPORT_TAGS    = (
61  'funcs'  => [ qw<delay filter>  ],
62  'consts' => [ qw<DEFAULT_DELAY> ],
63 );
64 our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
65 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
66
67 1;