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