]> ruin.nu Git - NDIRC.git/blob - CommonStates.pm
7da1c512788281d2d9954689e4a9a59824576e10
[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 use NDIRC::Dispatcher;
27
28 # We registered for all events, this will produce some debug info.
29 sub _default {
30         my ($event, $args) = @_[ARG0 .. $#_];
31         my @output = ( "$event: " );
32
33         for my $arg (@$args) {
34                 if ( ref $arg eq 'ARRAY' ) {
35                         push( @output, '[' . join(', ', @$arg ) . ']' );
36                 }
37                 else {
38                         push ( @output, "'$arg'" );
39                 }
40         }
41         print join ' ', @output, "\n";
42         return 0;
43 }
44
45 sub _start {
46         my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION];
47
48         # retrieve our component's object from the heap where we stashed it
49         my $irc = $heap->{irc};
50         $kernel->sig( DIE => 'sig_DIE' );
51         $kernel->sig( USR1 => 'sig_usr1' );
52         $kernel->sig( USR2 => 'sig_usr2' );
53         $kernel->sig( INT => 'signal_handler' );
54
55         $heap->{connector} = POE::Component::IRC::Plugin::Connector->new();
56         $irc->plugin_add( 'Connector' => $heap->{connector} );
57
58         $kernel->signal($session => 'USR2');
59
60         $irc->yield( register => 'all' );
61         $irc->yield( connect => { } );
62
63         $kernel->delay( refresh => 60 );
64         return;
65 }
66
67 sub sig_usr2 {
68         my $heap = $_[HEAP];
69
70         open COMMANDS, 'commands';
71         my @commands = split /\W+/, do { local $/; <COMMANDS> };
72         close COMMANDS;
73
74         my $disp = new NDIRC::Dispatcher;
75         $disp->load(@commands);
76
77         open CHANNELS, 'channels';
78         while (<CHANNELS>){
79                 print;
80                 my ($chan, @types) = split /\s+/;
81                 say "$chan - @types";
82                 $disp->add_channel($chan,\@types);
83         }
84         close CHANNELS;
85
86         $heap->{disp} = $disp;
87 }
88
89 sub sig_DIE {
90         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
91         say "DIED!!!!!!!!!!!!!!";
92         # $sig is 'DIE'
93         # $ex is the exception hash
94         warn "$$: error in event: $ex->{error_str}";
95         $kernel->sig_handled();
96
97         # Send the signal to session that sent the original event.
98         #if( $ex->{source_session} ne $_[SESSION] ) {
99         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
100         #}
101 }
102
103 sub signal_handler {
104         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
105         print "First session caught SIG$signal_name\n";
106
107         $heap->{irc}->yield(privmsg => '#testarlite', "SIGNAL $signal_name!");
108
109         given($signal_name){
110                 when ('INT') {
111                         $heap->{irc}->yield(quit => 'Bye!');
112                         $kernel->sig_handled();
113                 }
114         }
115         #$kernel->sig_handled();
116 }
117
118 sub irc_disconnected {
119         my ($sender,$heap) = @_[SENDER,HEAP];
120
121         exit;
122 }
123
124 sub irc_001 {
125         my ($sender,$heap) = @_[SENDER,HEAP];
126
127         # Since this is an irc_* event, we can get the component's object by
128         # accessing the heap of the sender. Then we register and connect to the
129         # specified server.
130         my $irc = $sender->get_heap();
131
132         print "Connected to ", $irc->server_name(), "\n";
133
134         # we join our channels
135         $irc->yield( join => $_ ) for grep /^#/, keys %{$heap->{disp}->channels};
136         return;
137 }
138
139 sub irc_invite {
140         my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
141         my $irc = $sender->get_heap();
142
143         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$heap->{disp}->channels}
144 }
145
146 1;