-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbobot.pl
executable file
·182 lines (156 loc) · 4.72 KB
/
bobot.pl
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/perl
#===============================================================================
#
# FILE: bobot.pl
# USAGE: ./bobot.pl
#
# AUTHOR: bobpp < [email protected] >
# VERSION: 1.0
# CREATED: 08/30/2009 04:19:53 PM
#===============================================================================
use strict;
use warnings;
use utf8;
use File::Spec;
use YAML;
use Net::IRC;
use DBI;
use Time::Local;
my $config_file_path = shift;
my $config = YAML::LoadFile(File::Spec->catfile($config_file_path));
my $i = new Net::IRC;
my $conn = $i->newconn(
Nick => $config->{nick},
Server => $config->{server},
Port => $config->{port},
Ircname => $config->{nick},
Username => $config->{nick},
Password => $config->{password},
);
my $dbh = DBI->connect("dbi:SQLite:dbname=bobot.sqlite3", "", "", {AutoCommit => 1});
# for karma
my $karma_exists_sth = $dbh->prepare(<<'SQL');
SELECT id FROM karma WHERE channel = ? AND name = ?
SQL
my $karma_initialize_sth = $dbh->prepare(<<'SQL');
INSERT INTO karma (channel, name) VALUES (?, ?)
SQL
my $karma_plus_sth = $dbh->prepare(<<'SQL');
UPDATE karma SET total = total+1, plus = plus+1 WHERE channel = ? AND name = ?
SQL
my $karma_minus_sth = $dbh->prepare(<<'SQL');
UPDATE karma SET total = total-1, minus = minus+1 WHERE channel = ? AND name = ?
SQL
my $karma_check_sth = $dbh->prepare(<<'SQL');
SELECT total, plus, minus FROM karma WHERE channel = ? AND name = ?
SQL
# for msgs
my $msg_check_sth = $dbh->prepare(<<'SQL');
SELECT from_name, body FROM msg WHERE channel = ? AND to_name = ?
SQL
my $set_msg_sth = $dbh->prepare(<<'SQL');
INSERT INTO msg (channel, from_name, to_name, body) VALUES (?, ?, ?, ?)
SQL
my $del_msg_sth = $dbh->prepare(<<'SQL');
DELETE FROM msg WHERE channel = ? AND to_name = ?
SQL
# 時報
my $timer_check_sth = $dbh->prepare(<<'SQL');
SELECT id, body FROM timer WHERE channel = ? AND hour = ? AND minute = ? AND last_sent < ?
SQL
my $timer_set_sth = $dbh->prepare(<<'SQL');
INSERT INTO timer (channel, hour, minute, body) VALUES (?, ?, ?, ?)
SQL
my $timer_sent_sth = $dbh->prepare(<<'SQL');
UPDATE timer SET last_sent = ? WHERE id = ?
SQL
# help msg
my $help_msg = <<EOH;
(\\w+)(++|--) : 投票
ex: bobpp++, hogefuga--
vote (\\w+) : 投票結果を見る
ex: vote bobpp
msg (\\w+) <msg> : (\\w+)さんに伝言する (次回 join 時に発言します)
ex: msg bobpp 明日のみにいこうよ
timer (\\d{2}:\\d{2}) <msg> : (\\d{2}:\\d{2})に <msg> を時報としてセットする
ex: timer 12:00 ご飯の時間ですよ
EOH
sub show_karma {
my ($irc, $c, $n) = @_;
$karma_check_sth->execute($c, $n);
my ($total, $plus, $minus) = $karma_check_sth->fetchrow_array;
$total ||= 0;
$plus ||= 0;
$minus ||= 0;
$irc->notice($c, "$n => $total (++:$plus, --:$minus)");
}
# 発言に関する
$conn->add_handler('public', sub {
my ($self, $e) = @_;
my $channel = $e->{to}[0];
my $msg = $e->{args}[0];
# karma
# [!-~] == symbol + \w
if ($msg =~ /([!-~]+)(\+\+|\-\-)/) {
$karma_exists_sth->execute($channel, $1);
unless (my ($id) = $karma_exists_sth->fetchrow_array) {
$karma_initialize_sth->execute($channel, $1);
}
my $karma_change_sth = ($2 eq '++') ? $karma_plus_sth : $karma_minus_sth;
$karma_change_sth->execute($channel, $1);
show_karma($self, $channel, $1);
}
# karma check
if ($msg =~ /^vote ([!-~]+)$/) {
show_karma($self, $channel, $1);
}
# msg
if ($msg =~ /^msg ([!-~]+) (.*)$/) {
$set_msg_sth->execute($channel, $e->{nick}, $1, $2);
$self->notice($channel, "Message set OK");
}
# 時報
if ($msg =~ /^timer (\d{2}):(\d{2}) (.*)$/) {
$timer_set_sth->execute($channel, $1, $2, $3);
$self->notice($channel, "Timer set OK");
}
# help, misc...
if ($msg =~ /^bobot: (\w+)$/) {
if ($1 eq 'help') {
$self->notice($channel, $_) for split /\n/, $help_msg;
}
}
});
# Join に関する
$conn->add_handler('join', sub {
my ($self, $e) = @_;
my $channel = $e->{to}[0];
my $nick = $e->{nick};
# msg
$msg_check_sth->execute($channel, $nick);
my @messages;
while (my ($f, $m) = $msg_check_sth->fetchrow_array) {
push @messages, "From:$f / $m";
}
if (scalar @messages) {
$self->privmsg($channel, "Welcome $nick メッセージがあるよ");
$self->notice($channel, $_) for @messages;
}
$del_msg_sth->execute($channel, $nick);
});
# 30sec ごと
sub timer_tick {
my ($self, $e) = @_;
# 時報
for my $channel (@{$config->{channels}}) {
$timer_check_sth->execute($channel, (localtime)[2,1], time - 120);
while (my ($id, $msg) = $timer_check_sth->fetchrow_array) {
$self->notice($channel, "Timer: $msg");
$timer_sent_sth->execute(time, $id);
}
}
$self->schedule(30, \&timer_tick);
}
$conn->schedule(0, \&timer_tick);
$conn->join($_) for (@{$config->{channels}});
$i->start;