74 |
use Getopt::Long; |
use Getopt::Long; |
75 |
use DateTime; |
use DateTime; |
76 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
77 |
|
use Net::Twitter; |
78 |
|
|
79 |
my $import_dircproxy; |
my $import_dircproxy; |
80 |
my $log_path; |
my $log_path; |
91 |
|
|
92 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
93 |
|
|
94 |
eval { |
my $sql_schema = { |
95 |
$dbh->do(qq{ select count(*) from log }); |
log => ' |
|
}; |
|
|
|
|
|
if ($@) { |
|
|
warn "creating database table in $DSN\n"; |
|
|
$dbh->do(<<'_SQL_SCHEMA_'); |
|
|
|
|
96 |
create table log ( |
create table log ( |
97 |
id serial, |
id serial, |
98 |
time timestamp default now(), |
time timestamp default now(), |
106 |
create index log_time on log(time); |
create index log_time on log(time); |
107 |
create index log_channel on log(channel); |
create index log_channel on log(channel); |
108 |
create index log_nick on log(nick); |
create index log_nick on log(nick); |
109 |
|
', |
110 |
|
meta => ' |
111 |
|
create table meta ( |
112 |
|
nick text not null, |
113 |
|
channel text not null, |
114 |
|
name text not null, |
115 |
|
value text, |
116 |
|
changed timestamp default now(), |
117 |
|
primary key(nick,channel,name) |
118 |
|
); |
119 |
|
', |
120 |
|
}; |
121 |
|
|
122 |
|
foreach my $table ( keys %$sql_schema ) { |
123 |
|
|
124 |
|
eval { |
125 |
|
$dbh->do(qq{ select count(*) from $table }); |
126 |
|
}; |
127 |
|
|
128 |
_SQL_SCHEMA_ |
if ($@) { |
129 |
|
warn "creating database table $table in $DSN\n"; |
130 |
|
$dbh->do( $sql_schema->{ $table } ); |
131 |
|
} |
132 |
} |
} |
133 |
|
|
134 |
|
|
135 |
|
=head2 meta |
136 |
|
|
137 |
|
Set or get some meta data into database |
138 |
|
|
139 |
|
meta('nick','channel','var_name', $var_value ); |
140 |
|
|
141 |
|
$var_value = meta('nick','channel','var_name'); |
142 |
|
( $var_value, $changed ) = meta('nick','channel','var_name'); |
143 |
|
|
144 |
|
=cut |
145 |
|
|
146 |
|
sub meta { |
147 |
|
my ($nick,$channel,$name,$value) = @_; |
148 |
|
|
149 |
|
# normalize channel name |
150 |
|
$channel =~ s/^#//; |
151 |
|
|
152 |
|
if (defined($value)) { |
153 |
|
|
154 |
|
my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? }); |
155 |
|
|
156 |
|
eval { $sth->execute( $value, $nick, $channel, $name ) }; |
157 |
|
|
158 |
|
# error or no result |
159 |
|
if ( $@ || ! $sth->rows ) { |
160 |
|
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) }); |
161 |
|
$sth->execute( $value, $nick, $channel, $name ); |
162 |
|
_log "created $nick/$channel/$name = $value"; |
163 |
|
} else { |
164 |
|
_log "updated $nick/$channel/$name = $value "; |
165 |
|
} |
166 |
|
|
167 |
|
return $value; |
168 |
|
|
169 |
|
} else { |
170 |
|
|
171 |
|
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? }); |
172 |
|
$sth->execute( $nick, $channel, $name ); |
173 |
|
my ($v,$c) = $sth->fetchrow_array; |
174 |
|
_log "fetched $nick/$channel/$name = $v [$c]"; |
175 |
|
return ($v,$c) if wantarray; |
176 |
|
return $v; |
177 |
|
|
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
184 |
insert into log |
insert into log |
185 |
(channel, me, nick, message, time) |
(channel, me, nick, message, time) |
186 |
values (?,?,?,?,?) |
values (?,?,?,?,?) |
187 |
}); |
}); |
188 |
|
|
189 |
|
|
190 |
my $tags; |
my $tags; |
191 |
my $tag_regex = '\b([\w-_]+)//'; |
my $tag_regex = '\b([\w-_]+)//'; |
192 |
|
|
471 |
message => $a->{msg}); |
message => $a->{msg}); |
472 |
} |
} |
473 |
|
|
474 |
|
|
475 |
if ($import_dircproxy) { |
if ($import_dircproxy) { |
476 |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
477 |
warn "importing $import_dircproxy...\n"; |
warn "importing $import_dircproxy...\n"; |
536 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
537 |
|
|
538 |
save_message( channel => $channel, me => 0, nick => $nick, msg => $msg); |
save_message( channel => $channel, me => 0, nick => $nick, msg => $msg); |
539 |
|
meta( $nick, $channel, 'last-msg', $msg ); |
540 |
}, |
}, |
541 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
542 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
545 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
546 |
|
|
547 |
save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); |
save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); |
548 |
|
|
549 |
|
if ( my $twitter = meta( $nick, $channel, 'twitter' ) ) { |
550 |
|
my ($login,$passwd) = split(/\s+/,$twitter,2); |
551 |
|
_log("sending twitter for $nick/$login on $channel "); |
552 |
|
my $bot = Net::Twitter->new( username=>$login, password=>$passwd ); |
553 |
|
$bot->update("<${channel}> $msg"); |
554 |
|
} |
555 |
|
|
556 |
}, |
}, |
557 |
irc_ping => sub { |
irc_ping => sub { |
558 |
warn "pong ", $_[ARG0], $/; |
warn "pong ", $_[ARG0], $/; |
559 |
$ping->{$_[ARG0]++}; |
$ping->{ $_[ARG0] }++; |
560 |
}, |
}, |
561 |
irc_invite => sub { |
irc_invite => sub { |
562 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
563 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
564 |
my $channel = $_[ARG1]; |
my $channel = $_[ARG1]; |
|
|
|
565 |
|
|
566 |
warn "invited to $channel by $nick"; |
warn "invited to $channel by $nick"; |
567 |
|
|
573 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
574 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
575 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
576 |
|
my $channel = $_[ARG1]->[0]; |
577 |
from_to($msg, 'UTF-8', $ENCODING); |
from_to($msg, 'UTF-8', $ENCODING); |
578 |
|
|
579 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
614 |
$res .= join(" | ", @users); |
$res .= join(" | ", @users); |
615 |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
616 |
|
|
617 |
foreach my $res (get_from_log( limit => ($1 || 100) )) { |
my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10; |
618 |
|
|
619 |
|
foreach my $res (get_from_log( limit => $limit )) { |
620 |
_log "last: $res"; |
_log "last: $res"; |
621 |
from_to($res, $ENCODING, 'UTF-8'); |
from_to($res, $ENCODING, 'UTF-8'); |
622 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
673 |
|
|
674 |
} elsif ($msg =~ m/^ping/) { |
} elsif ($msg =~ m/^ping/) { |
675 |
$res = "ping = " . dump( $ping ); |
$res = "ping = " . dump( $ping ); |
676 |
|
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
677 |
|
if ( ! defined( $1 ) ) { |
678 |
|
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
679 |
|
$sth->execute( $nick, $channel ); |
680 |
|
$res = "config for $nick on $channel"; |
681 |
|
while ( my ($n,$v) = $sth->fetchrow_array ) { |
682 |
|
$res .= " | $n = $v"; |
683 |
|
} |
684 |
|
} elsif ( ! $2 ) { |
685 |
|
my $val = meta( $nick, $channel, $1 ); |
686 |
|
$res = "current $1 = " . ( $val ? $val : 'undefined' ); |
687 |
|
} else { |
688 |
|
my $validate = { |
689 |
|
'last-size' => qr/^\d+/, |
690 |
|
'twitter' => qr/^\w+\s+\w+/, |
691 |
|
}; |
692 |
|
|
693 |
|
my ( $op, $val ) = ( $1, $2 ); |
694 |
|
|
695 |
|
if ( my $regex = $validate->{$op} ) { |
696 |
|
if ( $val =~ $regex ) { |
697 |
|
meta( $nick, $channel, $op, $val ); |
698 |
|
$res = "saved $op = $val"; |
699 |
|
} else { |
700 |
|
$res = "config option $op = $val doesn't validate against $regex"; |
701 |
|
} |
702 |
|
} else { |
703 |
|
$res = "config option $op doesn't exist"; |
704 |
|
} |
705 |
|
} |
706 |
} |
} |
707 |
|
|
708 |
if ($res) { |
if ($res) { |