-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogbot.pl
executable file
·175 lines (152 loc) · 4.51 KB
/
logbot.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
#!/usr/bin/env perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::IRC::Client;
use AnyEvent::IRC::Util qw(split_prefix);
use DBI;
use Getopt::Long;
use HTTP::Tiny;
use constant {
DEFAULT_MESSAGE_NUM => 10
};
$| = 1;
my %opt = (
channel => '#burato',
nick => "logger1",
port => 6667,
server => 'irc.freenode.net',
dbname => 'irc_log.db',
'enable-private' => 0,
'no-greeting' => 0,
);
### Example:
# ./logbot.pl --channel=#llamaz --nick=mike
GetOptions(\%opt,
'channel=s', 'nick=s', 'port=s', 'server=s', 'dbname=s',
'enable-private', 'no-greeting'
);
my $message = shift || "I started logging you all at " . localtime;
my $ua = HTTP::Tiny->new;
init_db();
my $c = AnyEvent->condvar;
my $con = AnyEvent::IRC::Client->new;
$con->reg_cb(
join => sub {
my( $con, $nick, $channel, $is_myself ) = @_;
if (!$opt{'no-greeting'} && $is_myself && $channel eq $opt{channel}) {
$con->send_chan( $channel, PRIVMSG => ($channel, $message) );
}
},
publicmsg => sub {
my( $con, $nick, $ircmsg ) = @_;
my $msg = $ircmsg->{params}[1];
if ($msg =~ /^showlog\s*(\d*)$/) {
my $count = $1 || DEFAULT_MESSAGE_NUM;
my $res = showlog( $count );
$con->send_chan(
$opt{channel},
PRIVMSG => ($opt{channel}, "Last $count messages: $res")
);
}
elsif ($msg =~ /^lastmsg$/) {
my $res = getlast(1);
$con->send_chan(
$opt{channel},
PRIVMSG => ($opt{channel}, "Last message: $res")
);
}
else {
my $db = connect_db();
my $sql = 'insert into messages (nick, message) values (?, ?)';
my $sth = $db->prepare( $sql ) or die $db->errstr;
my( $nick ) = split_prefix( $ircmsg->{prefix} );
$sth->execute( $nick, $msg );
}
},
privatemsg => sub {
return unless $opt{'enable-private'};
my( $con, $nick, $ircmsg ) = @_;
my( $peernick ) = split_prefix( $ircmsg->{prefix} );
my $msg = $ircmsg->{params}[1];
if ($msg =~ /^showlog\s*(\d*)$/) {
my $count = $1 || DEFAULT_MESSAGE_NUM;
my $res = showlog( $count );
$con->send_srv(
PRIVMSG => ($peernick, "Last $count messages: $res")
);
}
elsif ($msg =~ /^lastmsg$/) {
my $res = getlast(1);
$con->send_srv(
PRIVMSG => ($peernick, "Last message: $res")
);
}
},
kick => sub {
my( $con, $kicked, $channel, $is_myself, $msg, $kicker ) = @_;
if ($kicked eq $opt{nick}) {
$con->send_srv( JOIN => $channel );
$con->send_chan(
$channel,
PRIVMSG => ($channel, "Go kick yourself, $kicker!!")
);
warn $msg if $is_myself;
}
},
quit => sub {
my( $con, $nick, $msg ) = @_;
my $db = connect_db();
my $sql = 'insert into messages (nick, message) values (?, ?)';
my $sth = $db->prepare( $sql ) or die $db->errstr;
$sth->execute( $nick, $msg );
},
);
$con->connect ( $opt{server}, $opt{port}, { nick => $opt{nick} } );
$con->send_srv( JOIN => $opt{channel} );
$c->wait;
$con->disconnect;
sub getlast {
my $count = shift;
my $db = connect_db();
my $sql = << "SQL";
select id, nick, message, datetime(time, 'localtime') as time
from messages order by id desc limit $count
SQL
my $sth = $db->prepare( $sql ) or die $db->errstr;
$sth->execute or die $sth->errstr;
my $msgs = $sth->fetchall_hashref('id');
my $log;
$log .= join '',
'[', $msgs->{$_}{time}, '] ',
$msgs->{$_}{nick}, ": ",
$msgs->{$_}{message}, "\n" for sort {$a <=> $b} keys %{$msgs};
return $log;
}
sub showlog {
my $count = shift;
my $log = getlast($count);
my $res = $ua
->post_form('http://sprunge.us', {sprunge => $log})
->{content};
$res =~ s/\s*$/?irc/;
return $res;
}
sub connect_db {
my $dbfile = $opt{dbname};
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile")
or die $DBI::errstr;
return $dbh;
}
sub init_db {
my $db = connect_db();
my $schema = do {local $/ = <DATA>};
$db->do($schema) or die $db->errstr;
}
__DATA__
create table if not exists messages (
id integer primary key autoincrement,
nick string not null,
message string not null,
time timestamp default current_timestamp
);