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