]> ruin.nu Git - NDIRC.git/blob - CommonStates.pm
Basic conversion to POE::Component::IRC
[NDIRC.git] / CommonStates.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19 package NDIRC::CommonStates;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use POE::Session;
26
27 # We registered for all events, this will produce some debug info.
28 sub _default {
29         my ($event, $args) = @_[ARG0 .. $#_];
30         my @output = ( "$event: " );
31
32         for my $arg (@$args) {
33                 if ( ref $arg eq 'ARRAY' ) {
34                         push( @output, '[' . join(', ', @$arg ) . ']' );
35                 }
36                 else {
37                         push ( @output, "'$arg'" );
38                 }
39         }
40         print join ' ', @output, "\n";
41         return 0;
42 }
43
44 sub _start {
45         my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION];
46
47         # retrieve our component's object from the heap where we stashed it
48         my $irc = $heap->{irc};
49         $kernel->sig( DIE => 'sig_DIE' );
50         $kernel->sig( USR1 => 'sig_usr1' );
51         $kernel->sig( USR2 => 'sig_usr2' );
52         $kernel->sig( INT => 'signal_handler' );
53
54         $heap->{connector} = POE::Component::IRC::Plugin::Connector->new();
55         $irc->plugin_add( 'Connector' => $heap->{connector} );
56
57         $kernel->signal($session => 'USR2');
58
59         $irc->yield( register => 'all' );
60         $irc->yield( connect => { } );
61
62         $kernel->delay( refresh => 60 );
63         return;
64 }
65
66 sub sig_DIE {
67         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
68         say "DIED!!!!!!!!!!!!!!";
69         # $sig is 'DIE'
70         # $ex is the exception hash
71         warn "$$: error in event: $ex->{error_str}";
72         $kernel->sig_handled();
73
74         # Send the signal to session that sent the original event.
75         #if( $ex->{source_session} ne $_[SESSION] ) {
76         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
77         #}
78 }
79
80 sub signal_handler {
81         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
82         print "First session caught SIG$signal_name\n";
83
84         $heap->{irc}->yield(privmsg => '#testarlite', "SIGNAL $signal_name!");
85
86         given($signal_name){
87                 when ('INT') {
88                         $heap->{irc}->yield(quit => 'Bye!');
89                         $kernel->sig_handled();
90                 }
91         }
92         #$kernel->sig_handled();
93 }
94
95 sub irc_disconnected {
96         my ($sender,$heap) = @_[SENDER,HEAP];
97
98         exit;
99 }
100
101 sub irc_001 {
102         my ($sender,$heap) = @_[SENDER,HEAP];
103
104         # Since this is an irc_* event, we can get the component's object by
105         # accessing the heap of the sender. Then we register and connect to the
106         # specified server.
107         my $irc = $sender->get_heap();
108
109         print "Connected to ", $irc->server_name(), "\n";
110
111         # we join our channels
112         $irc->yield( join => $_ ) for grep /^#/, keys %{$heap->{disp}->channels};
113         return;
114 }
115
116 sub irc_invite {
117         my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
118         my $irc = $sender->get_heap();
119
120         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$heap->{disp}->channels}
121 }
122
123 1;