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