]> ruin.nu Git - NDIRC.git/blob - ndawn.pl
Basic conversion to POE::Component::IRC
[NDIRC.git] / ndawn.pl
1 #!/usr/bin/perl -w
2
3 #**************************************************************************
4 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
5 #                                                                         *
6 #   This program is free software; you can redistribute it and/or modify  *
7 #   it under the terms of the GNU General Public License as published by  *
8 #   the Free Software Foundation; either version 2 of the License, or     *
9 #   (at your option) any later version.                                   *
10 #                                                                         *
11 #   This program is distributed in the hope that it will be useful,       *
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14 #   GNU General Public License for more details.                          *
15 #                                                                         *
16 #   You should have received a copy of the GNU General Public License     *
17 #   along with this program; if not, write to the                         *
18 #   Free Software Foundation, Inc.,                                       *
19 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
20 #**************************************************************************
21
22 use strict;
23 use warnings;
24 use feature ':5.10';
25 use POE qw(Component::IRC::Qnet::State);
26
27 use POE::Component::IRC::Plugin::Logger;
28 use POE::Component::IRC::Plugin::BotTraffic;
29 use POE::Component::IRC::Plugin::Connector;
30 use POE::Component::IRC::Plugin::DCC;
31
32 use ND::DB;
33 use ND::Include;
34 use NDIRC::Dispatcher;
35 use NDIRC::Context;
36 use NDIRC::Command;
37 use NDIRC::Misc;
38 use NDIRC::CommonStates;
39
40 my $nickname = 'ndbot';
41 my $ircname = 'ND test bot';
42 my $server = 'irc.netgamers.org';
43
44 # We create a new PoCo-IRC object
45 my $irc = POE::Component::IRC::Qnet::State->spawn(
46         nick => $nickname,
47         ircname => $ircname,
48         server => $server,
49         servers => [$server, 'underworld.no.eu.netgamers.org', ]
50 ) or die "Oh noooo! $!";
51
52 $irc->service_bots(QBOT => 'P@cservice.netgamers.org');
53
54 $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
55 $irc->plugin_add( 'DCC', POE::Component::IRC::Plugin::DCC->new() );
56 $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
57         Path    => 'irclogs',
58         DCC     => 0,
59         Private => 1,
60         Public  => 1,
61         Sort_by_date => 1,
62         Strip_color => 1,
63         Strip_formatting => 1,
64 ));
65
66 my $DBH = DB();
67 my $TICK = $DBH->selectrow_array('SELECT tick()');
68
69 $ND::scanchan = '#testarmer';
70 $ND::defchan = '#testarlite';
71 $ND::memchan = '#testarmer';
72
73 POE::Session->create(
74         package_states => [
75                 'NDIRC::CommonStates' => [ qw(_default _start irc_001 sig_DIE signal_handler irc_disconnected irc_invite) ],
76                 main => [ qw(irc_public irc_msg refresh sig_usr2 irc_join) ],
77         ],
78         heap => { irc => $irc},
79 );
80
81 $poe_kernel->run();
82
83
84 sub sig_usr2 {
85         my $heap = $_[HEAP];
86
87         my $disp = new NDIRC::Dispatcher;
88
89         $disp->load(qw/Basic SMS Channel Def Intel Members PA Quotes Usermgm/);
90
91         open CHANNELS, 'channels';
92         while (<CHANNELS>){
93                 print;
94                 my ($chan, @types) = split /\s+/;
95                 say "$chan - @types";
96                 $disp->add_channel($chan,\@types);
97         }
98         close CHANNELS;
99
100         $heap->{disp} = $disp;
101 }
102
103
104 sub irc_public {
105         my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
106         my ($nick,$username,$address) = ( split /[!@]/, $who );
107         my $channel = $where->[0];
108
109         my $irc = $sender->get_heap();
110
111         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
112
113         if ($msg =~ /^(\S+): (.+)$/ && $heap->{disp}->has_command('anon',$channel)){
114                 my $_ = $1;
115                 my $text = $2;
116                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
117                         $msg = ".anon $_ $text";
118                 }
119
120         }
121         if (parseCommand($msg,$irc,$nick,$address,$channel,$heap->{disp},DB())){
122                 #Command parsed and run successfully
123         }
124 }
125
126
127 sub irc_msg {
128         my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
129         my ($nick,$username,$address) = ( split /[!@]/, $who );
130         my $irc = $sender->get_heap();
131
132         if (parseCommand($msg,$irc,$nick,$address,'pm',$heap->{disp},DB())){
133                 #Command parsed and run successfully
134         }
135 }
136
137 sub irc_join {
138         my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
139         my ($nick,$username,$address) = ( split /[!@]/, $who );
140         my $irc = $sender->get_heap();
141
142         if($heap->{disp}->has_command('voice',$channel)){
143                 my $dbh = DB();
144                 my $flags = $dbh->prepare_cached(q{
145 SELECT DISTINCT flag
146 FROM users u
147         JOIN groupmembers g USING (uid)
148         JOIN channel_group_flags gf USING (gid)
149 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
150                 });
151                 $flags->execute($address, $channel);
152                 my $mode = '';
153                 my @who;
154                 while (my ($flag) = $flags->fetchrow()){
155                         $mode .= $flag;
156                         push @who, $nick;
157                 }
158                 say "$mode - @who";
159                 $irc->yield(mode => $channel, $mode, @who) if $mode;
160         }
161 }
162
163 sub refresh {
164         my ($kernel,$heap) = @_[KERNEL,HEAP];
165         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
166         $kernel->delay( refresh => 60 );
167         return;
168 }