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