]> ruin.nu Git - NDIRC.git/blob - Bot.pm
Use discord channel id
[NDIRC.git] / Bot.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 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::Bot;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26
27 use POE::Component::IRC::Common qw/irc_to_utf8/;
28 use POE::Session;
29 use POE::Component::IRC::Plugin::Logger;
30 use POE::Component::IRC::Plugin::BotTraffic;
31 use POE::Component::IRC::Plugin::Connector;
32 use POE::Component::IRC::Plugin::AutoJoin;
33 use POE::Component::IRC::Plugin::NickReclaim;
34
35 use Mojo::Discord;
36 use Mojo::IOLoop;
37
38 use NDIRC::Dispatcher;
39 use NDIRC::IrcContext;
40
41 use IO::File;
42
43 has disp => (
44         is => 'rw',
45         isa => 'Object',
46         lazy_build => 1
47 );
48
49 has discord => (
50         is => 'rw',
51         isa => 'Object'
52 );
53
54 has discord_name => (
55         is => 'rw',
56         isa => 'Str'
57 );
58
59 has discord_id => (
60         is => 'rw',
61         isa => 'Str'
62 );
63
64 has discord_channels => (
65         is => 'rw',
66         isa => 'HashRef',
67         default => sub { {} }
68 );
69
70 # We registered for all events, this will produce some debug info.
71 sub _default {
72         my ($event, $args) = @_[ARG0 .. $#_];
73         my @output = ( "$event: " );
74
75         for my $arg (@$args) {
76                 if ( ref $arg eq 'ARRAY' ) {
77                         push( @output, '[' . join(', ', @$arg ) . ']' );
78                 }
79                 else {
80                         push ( @output, "'$arg'" );
81                 }
82         }
83         print join ' ', @output, "\n";
84         return 0;
85 }
86
87 sub _start {
88         my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
89
90         # retrieve our component's object from the heap where we stashed it
91         my $irc = $heap->{irc};
92         $kernel->sig( DIE => 'sig_DIE' );
93         $kernel->sig( USR1 => 'sig_usr1' );
94         $kernel->sig( USR2 => 'sig_usr2' );
95         $kernel->sig( INT => 'signal_handler' );
96
97         $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
98         $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
99         $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
100         $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
101                 Path    => 'irclogs',
102                 DCC     => 0,
103                 Private => 1,
104                 Public  => 1,
105                 Sort_by_date => 1,
106                 Strip_color => 1,
107                 Strip_formatting => 1,
108                 Notices => 1,
109         ));
110
111         $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
112                 servers => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
113                         ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
114         );
115         $irc->plugin_add( 'Connector' => $heap->{connector} );
116
117         $irc->yield( register => 'all' );
118         $irc->yield( connect => { server => 'irc.netgamers.org' } );
119
120         $kernel->delay( refresh => 60 );
121
122         if (my $f =  new IO::File 'discord'){
123                 my $user = <$f>;
124                 chomp $user;
125                 my $token = <$f>;
126                 chomp $token;
127
128                 $self->discord(Mojo::Discord->new(
129                                 'token'     => $token,
130                                 'name'      => $user,
131                                 'url'       => 'https://nd.ruin.nu',
132                                 'version'   => '1.0',
133                                 'callbacks' => {
134                                         'READY'          => sub { $self->discord_ready(@_) },
135                                         'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
136                                         'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
137                                         'CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) },
138                                 },
139                                 'reconnect' => 1,
140                                 'verbose'   => 1,
141                         ));
142                 $self->discord->init();
143         }
144         return;
145 }
146
147 sub auth {
148         my $heap = $_[HEAP];
149
150         if (my $f =  new IO::File 'auth'){
151                 my $user = <$f>;
152                 chomp $user;
153                 my $pass = <$f>;
154                 chomp $pass;
155                 $heap->{irc}->yield(qbot_auth => $user => $pass);
156         }
157 }
158
159 sub sig_usr1 {
160         my ($kernel,$heap) = @_[KERNEL,HEAP];
161
162         $kernel->yield( 'refresh' );
163 }
164
165 sub clear_constraint {
166         my $tc = shift;
167
168         while (1) {
169                 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
170                         for my $t (@{$tc->{type_constraints}}){
171                                 clear_constraint($t);
172                         }
173
174                 }
175                 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
176                         clear_constraint($tc->{type_parameter});
177                 }
178                 last if ref $tc eq 'HASH';
179                 last if ref $tc eq '';
180                 if (defined $tc->{_type_constraint}){
181                         $tc = $tc->{_type_constraint};
182                 }elsif(defined $tc->{__type_constraint}){
183                         $tc = $tc->{__type_constraint};
184                 }else{
185                         last;
186                 }
187         }
188 }
189
190 sub clear_metains {
191         my $ins = shift;
192
193         for my $a (@{$ins->{attributes}}){
194                 for my $m (@{$a->{associated_methods}}){
195                         $m->{body} = undef;
196                 }
197                 clear_constraint($a->{isa});
198         }
199 }
200
201 sub clear_cycles {
202         my $c = shift;
203
204         for my $m (values %{$c->meta->{methods}}){
205                 clear_constraint($m->{type_constraint});
206
207                 my $ps = $m->{parsed_signature};
208                 for my $p (@{$ps->{_positional_params}->{params}}){
209                         clear_metains($p->{__MOP__}->{_meta_instance});
210                 }
211
212                 $m->{body} = undef;
213         }
214         clear_metains($c->meta->{_meta_instance});
215 }
216
217
218 sub sig_usr2 {
219         my $self = shift @_;
220
221         for my $c (values %{$self->disp->commands}){
222                 clear_cycles($c);
223         }
224
225         $self->disp($self->_build_disp);
226 }
227
228 sub _build_disp {
229         my ($self) = @_;
230         my $disp = new NDIRC::Dispatcher;
231
232         if (my $commands = new IO::File 'commands'){
233                 my @commands = split /\W+/, do{local $/; <$commands>};
234                 say "Loading commands from: @commands";
235                 $disp->load(@commands);
236         }
237
238         my $channels = new IO::File 'channels' or die $!;;
239         while (<$channels>){
240                 my ($chan, @types) = split /\s+/;
241                 say "$chan - @types";
242                 if ($chan =~ /^(.*):(.*)$/){
243                         $chan = $1;
244                         $disp->set_target($2,$chan);
245                 }
246                 $disp->add_channel($chan,\@types);
247         }
248
249         return $disp;
250 }
251
252 sub sig_DIE {
253         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
254         say "DIED!!!!!!!!!!!!!!";
255         # $sig is 'DIE'
256         # $ex is the exception hash
257         warn "$$: error in event: $ex->{error_str}";
258         $kernel->sig_handled();
259
260         # Send the signal to session that sent the original event.
261         #if( $ex->{source_session} ne $_[SESSION] ) {
262         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
263         #}
264 }
265
266 sub signal_handler {
267         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
268         print "First session caught SIG$signal_name\n";
269
270         given($signal_name){
271                 when ('INT') {
272                         exit unless $heap->{irc}->connected;
273                         $heap->{INT} = 1;
274                         $heap->{irc}->yield(quit => 'Bye!');
275                         $kernel->sig_handled();
276                 }
277         }
278         #$kernel->sig_handled();
279 }
280
281 sub irc_disconnected {
282         my ($sender,$heap) = @_[SENDER,HEAP];
283
284         exit if $heap->{INT};
285 }
286
287 sub irc_001 {
288         my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
289
290         # Since this is an irc_* event, we can get the component's object by
291         # accessing the heap of the sender. Then we register and connect to the
292         # specified server.
293         my $irc = $sender->get_heap();
294
295         print "Connected to ", $irc->server_name(), "\n";
296
297         $kernel->yield( 'auth' );
298         $irc->yield( mode => $irc->nick_name, '+ix');
299
300         # we join our channels
301         $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
302         return;
303 }
304
305 sub irc_invite {
306         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
307         my $irc = $sender->get_heap();
308
309         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
310 }
311
312 sub irc_public {
313         $_[ARG2] = irc_to_utf8 $_[ARG2];
314 }
315
316 sub irc_msg {
317         $_[ARG2] = irc_to_utf8 $_[ARG2];
318 }
319
320 sub  refresh {
321 }
322
323 sub irc_join {
324 }
325
326 sub discord_ready {
327         my $self = shift;
328         my $hash = shift;
329         $self->discord_id($hash->{user}{id});
330         $self->discord_name($hash->{user}{username});
331
332         say localtime(time) . " - Connected to Discord. $self->{discord_id}";
333 }
334
335 sub discord_message_create {
336 }
337
338 sub discord_guild_create {
339         my $self = shift;
340         my $hash = shift;
341
342         for my $chan (@{$hash->{channels}}) {
343                 say localtime(time) . " - $chan->{id} - $chan->{name}";
344                 $self->discord_channels->{$chan->{id}} = $chan;
345         }
346 }
347
348 sub discord_channel_create {
349         my $self = shift;
350         my $chan = shift;
351
352         for my $key (keys %{$chan}) {
353                 say localtime(time) . " - $key - $chan->{$key}";
354         }
355         $self->discord_channels->{$chan->{id}} = $chan;
356 }
357
358 sub parseCommand {
359         my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
360
361         return if $channel !~ /^#/ && $msg =~ /^~/;
362         $msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;
363
364         my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
365
366         if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
367                 if (!$command || $command =~ m{^https?://}){
368                         ($p,$command,$args) = ('.','addscan',$msg);
369                 }elsif($command ne 'addscan'){
370                         $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
371                 }
372         }
373
374         return 0 unless $self->disp->has_command($command,$channel);
375
376         my $reply_string;
377         given ($p){
378                 when ('!'){
379                         $reply_string = "privmsg $nick";
380                 }
381                 when ('~'){
382                         $reply_string = "privmsg $channel";
383                 }
384                 default {
385                         $reply_string = "notice $nick";
386                 }
387         }
388
389         $address =~ s/.*@(.*)/$1/;
390         my $c = NDIRC::IrcContext->new({
391                         host => $address,
392                         nick => $nick,
393                         channel => $channel,
394                         disp => $self->disp,
395                         model => $model,
396                         server => $server,
397                         reply_string => $reply_string,
398                 });
399
400         return $self->disp->run_command($c,$command,$args);
401 }
402
403 1;