| 1 |
elmex |
1.1 |
package Net::XMPP2::Event; |
| 2 |
|
|
|
| 3 |
|
|
=head1 NAME |
| 4 |
|
|
|
| 5 |
|
|
Net::XMPP2::Event - A event handler class |
| 6 |
|
|
|
| 7 |
|
|
=head1 SYNOPSIS |
| 8 |
|
|
|
| 9 |
|
|
package foo; |
| 10 |
|
|
use Net::XMPP2::Event; |
| 11 |
|
|
|
| 12 |
|
|
our @ISA = qw/Net::XMPP2::Event/; |
| 13 |
|
|
|
| 14 |
|
|
package main; |
| 15 |
|
|
my $o = foo->new; |
| 16 |
|
|
$o->reg_cb (foo => sub { ...; 1 }); |
| 17 |
|
|
$o->event (foo => 1, 2, 3); |
| 18 |
|
|
|
| 19 |
|
|
=head1 DESCRIPTION |
| 20 |
|
|
|
| 21 |
|
|
This module is just a small helper module for the connection |
| 22 |
|
|
and client classes. |
| 23 |
|
|
|
| 24 |
|
|
You may only derive from this package. |
| 25 |
|
|
|
| 26 |
|
|
=head2 reg_cb ($eventname1, $cb1, [$eventname2, $cb2, ...]) |
| 27 |
|
|
|
| 28 |
|
|
This method registers a callback C<$cb1> for the event with the |
| 29 |
|
|
name C<$eventname1>. You can also pass multiple of these eventname => callback |
| 30 |
|
|
pairs. |
| 31 |
|
|
|
| 32 |
|
|
To see a documentation of emitted events please take a look at the EVENTS section |
| 33 |
|
|
below. |
| 34 |
|
|
|
| 35 |
|
|
=cut |
| 36 |
|
|
|
| 37 |
|
|
sub reg_cb { |
| 38 |
|
|
my ($self, %regs) = @_; |
| 39 |
|
|
|
| 40 |
|
|
for my $cmd (keys %regs) { |
| 41 |
|
|
push @{$self->{events}->{lc $cmd}}, $regs{$cmd} |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
|
1; |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
=head2 event ($eventname, @args) |
| 48 |
|
|
|
| 49 |
|
|
Emits the event C<$eventname> and passes the arguments C<@args>. |
| 50 |
|
|
|
| 51 |
|
|
=cut |
| 52 |
|
|
|
| 53 |
|
|
sub event { |
| 54 |
|
|
my ($self, $ev, @arg) = @_; |
| 55 |
|
|
|
| 56 |
|
|
my $nxt = []; |
| 57 |
|
|
|
| 58 |
|
|
my $handled; |
| 59 |
|
|
for (@{$self->{events}->{lc $ev}}) { |
| 60 |
|
|
$_->($self, @arg) and push @$nxt, $_; |
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
|
|
$self->{events}->{lc $ev} = $nxt; |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
=head1 AUTHOR |
| 67 |
|
|
|
| 68 |
|
|
Robin Redeker, C<< <elmex at ta-sa.org> >> |
| 69 |
|
|
|
| 70 |
|
|
=head1 COPYRIGHT & LICENSE |
| 71 |
|
|
|
| 72 |
|
|
Copyright 2007 Robin Redeker, all rights reserved. |
| 73 |
|
|
|
| 74 |
|
|
This program is free software; you can redistribute it and/or modify it |
| 75 |
|
|
under the same terms as Perl itself. |
| 76 |
|
|
|
| 77 |
|
|
=cut |
| 78 |
|
|
|
| 79 |
|
|
1; |