aboutsummaryrefslogtreecommitdiff
path: root/saxrobot
blob: da51f8f532e179f20f2148e764440503af77a1db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env perl

use strict;
use warnings;
use POE;
use POE::Kernel;
use POE::Component::IRC;
use POE::Component::IRC::Plugin::NickServID;
use sb_config;

my %config = sb_config::parse_config('bot.conf');

# New PoCo-IRC object
my $irc = POE::Component::IRC->spawn(
	nick => $config{nick},
	ircname => $config{ircname},
	server  => $config{server},
	username => $config{username},
) or die "Failed to create new PoCo-IRC: $!";

# Plugins
$irc->plugin_add('NickServID', POE::Component::IRC::Plugin::NickServID->new(
	Password => $config{password}
));

POE::Session->create(
	package_states => [
		main => [ qw(_default _start irc_001 irc_kick irc_public irc_msg) ],
	],
	heap => { irc => $irc },
);

our $hold_op = {};

$poe_kernel->run();

sub _start {
	my $heap = $_[HEAP];
	my $irc = $heap->{irc};
	$irc->yield(register => 'all');
	$irc->yield(connect => { });
	return;
}

sub irc_001 {
	my $sender = $_[SENDER];
	my $irc = $sender->get_heap();

	print "Connected to server ", $irc->server_name(), "\n";

	$irc->yield( join => $_ ) for @{$config{channels}};
	return;
}

sub irc_kick {
	my ($kicker, $channel, $kickee, $reason) = @_[ARG0 .. ARG3];
	if ($kickee eq $config{nick}) {
		print "I was kicked by $kicker ($reason). Rejoining now.\n";
		$irc->yield(join => $channel);
	}
	return;
}

sub irc_public {
	my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
	my $nick = ( split /!/, $who )[0];
	my $channel = $where->[0];

	print("$channel $who: $what\n");

	# reject ignored nicks first
	return if (grep {$_ eq $nick} @{$config{ignore}});

	my $me = $irc->nick_name;

	my $saxcount = () = $what =~ /sa+x/gi;

	if ($saxcount > 0) {
		$irc->yield(privmsg => $channel => "🎷 " x $saxcount);
	}
	return;
}

sub irc_msg {
	my ($who, $to, $what, $ided) = @_[ARG0, ARG1, ARG2, ARG3];
	my $nick = (split /!/, $who)[0];
	if ($ided != 1) {
		$irc->yield(privmsg => $nick => "You must identify with services");
		return;
	}
	if (!grep {$_ eq $who} @{$config{admins}}) {
		$irc->yield(privmsg => $nick => "I am bot, go away");
		return;
	}
	if ($what =~ /^say\s/) {
		my ($channel, $message) = $what =~ /^say\s+(\S+)\s(.*)$/;
		if ($channel and $message) {
			$irc->yield(privmsg => $channel => $message);
		} else {
			$irc->yield(privmsg => $nick => "Syntax: say <channel> <msg>");
		}
	}
	return;
}

sub _default {
	my ($event, $args) = @_[ARG0 .. $#_];
	my @output = ( "$event: " );

	for my $arg (@$args) {
		if ( ref $arg eq 'ARRAY' ) {
			push( @output, '[' . join(', ', @$arg ) . ']' );
		}
		else {
			push ( @output, "'$arg'" );
		}
	}
	print join ' ', @output, "\n";
	return;
}