summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2005-07-28 01:09:55 +0000
committererdgeist <>2005-07-28 01:09:55 +0000
commit8b603cb46145f151cb76df807d21cddfbbb5a9b0 (patch)
treec0f0549cc30c14f31e56a331630b592771297813
parent47b4fb71337b08935dd2418dfd7438939059232c (diff)
Much perl magic added, topten0r works
-rw-r--r--bot.pl279
-rw-r--r--tables.sql1
2 files changed, 113 insertions, 167 deletions
diff --git a/bot.pl b/bot.pl
index d426602..ea7a831 100644
--- a/bot.pl
+++ b/bot.pl
@@ -7,8 +7,8 @@ use DBI;
7use POE; 7use POE;
8use POE::Component::IRC; 8use POE::Component::IRC;
9 9
10my $current_nick = 'francoise'; 10my $current_nick = 'francoise_';
11my $channel = '#kiffer.de'; 11my $channel = '#test';
12 12
13POE::Component::IRC->new("francoise"); 13POE::Component::IRC->new("francoise");
14POE::Session->new ( _start => \&irc_start, 14POE::Session->new ( _start => \&irc_start,
@@ -23,14 +23,9 @@ POE::Session->new ( _start => \&irc_start,
23 irc_public => \&irc_pub_msg, 23 irc_public => \&irc_pub_msg,
24 irc_msg => \&irc_priv_msg, 24 irc_msg => \&irc_priv_msg,
25 irc_ctcp_action => \&irc_action, 25 irc_ctcp_action => \&irc_action,
26# _default => \&_default, 26# _default => \&irc_default,
27); 27);
28 28
29sub _default {
30 if ( $_[ARG0] =~ /^irc_(.*)$/ ) {
31 print "IRC $1 received\n";
32 }
33}
34 29
35my %commands = ( 'help' => \&francoise_help, 30my %commands = ( 'help' => \&francoise_help,
36 'stat' => \&francoise_stat, 31 'stat' => \&francoise_stat,
@@ -75,27 +70,25 @@ sub irc_start {
75 ); 70 );
76} 71}
77 72
78sub irc_connect { 73# minifunctions, console output only
79 my $kernel = $_[KERNEL]; 74sub irc_connect { $_[KERNEL]->post(francoise=>join=>$channel); }
80 $kernel->post(francoise=>join=>$channel); 75sub irc_motd { my $msg = $_[ARG1]; print "MOTD: $msg\n"; }
81} 76sub irc_part { my $channel = $_[ARG1]; my $nick = (split /!/, $_[ARG0])[0]; print "#-> $nick has parted $channel\n"; }
82 77sub irc_quit { my $nick = $_[ARG0]; my $reason = $_[ARG1]; print "#-> $nick has quit ($reason)\n"; }
83sub irc_motd { 78sub irc_default { print "IRC $1 received\n" if ( $_[ARG0] =~ /^irc_(.*)$/ ); }
84 my $msg = $_[ARG1];
85 print "MOTD: $msg\n";
86}
87 79
80#names list on join, check all users
88sub irc_names { 81sub irc_names {
89 my $kernel = $_[KERNEL]; 82 my $kernel = $_[KERNEL];
90 my ( $channel, $names ) = (split /:/, $_[ARG1]); 83 my ( $channel, $names ) = (split /:/, $_[ARG1]);
91 84
92 $channel =~ s/[@|=] (.*?) /$1/; 85 for my $user ( split ' ', $names ) {
93 print "#-> Users on $channel [ $names ]\n";
94
95 for my $user (split / /, $names) {
96 $user =~ s/^[@%+]//; 86 $user =~ s/^[@%+]//;
97 $kernel->post( 'francoise', 'whois', $user); 87 $kernel->post( 'francoise', 'whois', $user);
98 } 88 }
89
90 $channel =~ s/[@|=] (.*?) /$1/;
91 print "#-> Users on $channel [ $names ]\n";
99} 92}
100 93
101#nick change 94#nick change
@@ -106,144 +99,112 @@ sub irc_nick {
106 my $oldnick = francoise_getbasenick( $oldnick_ ); 99 my $oldnick = francoise_getbasenick( $oldnick_ );
107 my $newnick = francoise_getbasenick( $newnick_ ); 100 my $newnick = francoise_getbasenick( $newnick_ );
108 101
109 if( $newnick_ ne $current_nick && $newnick ne $oldnick ) { 102 francoise_verifyuser( $newnick_, '', 0, $kernel )
110 francoise_verifyuser( $newnick_, '', 0, $kernel ); 103 if $newnick_ ne $current_nick && $newnick ne $oldnick;
111 }
112 104
113 print "#-> $oldnick_ is now known as $newnick_\n"; 105 print "#-> $oldnick_ is now known as $newnick_\n";
114} 106}
115 107
116#user parted
117sub irc_part {
118 my $channel = $_[ARG1];
119 if ( $_[ARG0] =~ /(.+)!~(.+)@(.+)/ ) {
120 my $nick = $1; my $user = $2; my $host = $3;
121 print "#-> $nick has parted $channel\n";
122 }
123}
124
125#user joined 108#user joined
126sub irc_join { 109sub irc_join {
127 my ( $kernel, $channel ) = @_[KERNEL, ARG1]; 110 my ( $kernel, $channel ) = @_[KERNEL, ARG1];
128 111
129 if ( $_[ARG0] =~ /(.+)!~(.+)@(.+)/ ) { 112 $_[ARG0] =~ /(.+)!~(.+)@(.+)/ or return;
130 my $nick = $1; my $user = $2; my $host = $3; 113 my ( $nick, $user, $host ) = ($1,$2,$3);
131 114
132 if( $nick eq $current_nick ) { 115 $kernel->post( 'francoise', 'privmsg', $channel, 'Hier bin ich!'), return
133 $kernel->post( 'francoise', 'privmsg', $channel, 'Hier bin ich!'); 116 if $nick eq $current_nick;
134 } else {
135 if( $host eq 'jamaica.kiffer.de' ) {
136 if( $jamaica{$user} && time() - $jamaicat{$user} < $jamaicatimeout ) {
137 $kernel->post( 'francoise', 'privmsg', $channel, "Ich glaub ja, $jamaica{$user} und $nick kennen sich." )
138 }
139 $jamaica{$user} = $nick; $jamaicat{$user} = time();
140 }
141 117
142 francoise_verifyuser( $nick, $host, 1 ); 118 francoise_verifyuser( $nick, $host, 1 );
143 } 119 return unless $host eq 'jamaica.kiffer.de';
144 120
145 print "#-> $nick has joined $channel\n"; 121 $kernel->post( 'francoise', 'privmsg', $channel, "Ich glaub ja, $jamaica{$user} und $nick kennen sich.")
146 } 122 if $jamaica{$user} && $nick ne $jamaica{$user} && time() - $jamaicat{$user} < $jamaicatimeout;
147} 123 $jamaica{$user} = $nick; $jamaicat{$user} = time();
148 124
149#user quit 125 print "#-> $nick has joined $channel\n";
150sub irc_quit {
151 my $nick = $_[ARG0];
152 my $reason = $_[ARG1];
153 print "#-> $nick has quit ($reason)\n";
154} 126}
155 127
128
156sub irc_pub_msg{ 129sub irc_pub_msg{
157 my ( $kernel, $msg ) = @_[KERNEL, ARG2]; 130 my ( $kernel, $msg ) = @_[KERNEL, ARG2];
158 my $channel = $_[ARG1]->[0]; 131 my $channel = $_[ARG1]->[0];
159 my $nick_ = (split /!/, $_[ARG0])[0]; 132 my $nick_ = (split /!/, $_[ARG0])[0];
160 my @words = (split / /, $msg); 133 my @words = (split / /, $msg);
161 my $numwords = $#words; 134 my $w0rds = 0;
162 135
136 #tidy nick and tidy msg from dest nick
163 my $nick = francoise_verifyuser( $nick_, "", 0, $kernel ); 137 my $nick = francoise_verifyuser( $nick_, "", 0, $kernel );
138 $msg =~ s/^\S+: +//;
164 139
165 if( $msg =~ /^!(\S+) *(.*)$/ ) { 140 #execute commands
166 &{$commands{ $1 } || \&francoise_donothing } ( $kernel, $channel, $2 ); 141 &{$commands{ $1 } || \&francoise_donothing } ( $kernel, $channel, $2 )
167 } 142 if $msg =~ /^!(\S+) *(.*)$/;
143
144 #update dictionary
145 my $sth_lookup = $dbh->prepare( "SELECT COUNT(*) FROM words WHERE word = ?");
146 my $sth_insert = $dbh->prepare( "INSERT INTO words ( word, is_0r, is_action ) VALUES ( ?, ?, ? )");
168 147
169 for my $word ( @words ) { 148 for my $word ( @words ) {
170 my $is0r = 'false'; my $isact = 'false'; 149 my $is0r = 'false'; my $isact = 'false';
171 my $sth = $dbh->prepare( "SELECT COUNT(*) FROM words WHERE word = ?"); 150 $sth_lookup->execute( $word );
172 $sth->execute( $word ); 151 $is0r = 'true', $w0rds++ if $word =~ /0r/;
173 next if (($sth->fetchrow_array)) > 0; 152 $isact = 'true' if $word =~ /^\*.*\*$/;
174 $is0r = 'true' if $word =~ /0r/; 153 $sth_insert->execute( $word, $is0r, $isact )
175 $isact = 'true' if $word =~ /^\*.*\*$/; 154 unless ($sth_lookup->fetchrow_array)[0];
176 $sth = $dbh->prepare("INSERT INTO words ( word, is_0r, is_action ) VALUES ( ?, ?, ? )");
177 $sth->execute( $word, $is0r, $isact );
178 } 155 }
179 156
157 #reply to whereis requests
180 if ( $msg =~ /^wo ist (\S+?)\??$/i ) { 158 if ( $msg =~ /^wo ist (\S+?)\??$/i ) {
181 my $wois_ = $1; 159 my $wois_ = $1;
182 my $wois = francoise_getbasenick( $1 ); 160 my $wois = francoise_getbasenick( $1 );
183 my $sth = $dbh->prepare( "SELECT awaymsg FROM users WHERE nick = ? AND isaway = 'true'"); 161 my ($awaymsg) = $dbh->selectrow_array( "SELECT awaymsg FROM users WHERE nick = ? AND isaway = 'true'", undef, $wois);
184 $sth->execute( $wois ); 162
185 if ( my @awaymsg = $sth->fetchrow_array ) { 163 $kernel->post( 'francoise', 'privmsg', $channel, $awaymsg ? "$wois_ ist $awaymsg" : "Ich weiss nicht, wo $wois_ ist." );
186 $kernel->post( 'francoise', 'privmsg', $channel, "$wois_ ist $awaymsg[0]" ); 164
187 } else { 165 } elsif( $msg =~ /^(.+?)\s+($hv)\s+(.*)$/i ) {
188 $kernel->post( 'francoise', 'privmsg', $channel, "Ich weiss nicht, wo $wois_ ist." ); 166 #learn some new knowledge
189 } 167 my ( $trigger, $hilfsverb, $reply ) = ($1,$2,$3);
190 } elsif( $msg =~ /^(?:\S+: )?(.+?)\s+($hv)\s+(.*)$/i ) { 168
191 my $trigger = $1; 169 $dbh->do( "INSERT INTO knowledge ( trigger, hilfsverb, reply ) VALUES ( ?, ?, ?) ", undef, $trigger, $hilfsverb, $reply )
192 my $hilfsverb = $2; 170 unless ($dbh->selectrow_array(
193 my $reply = $3; 171 "SELECT COUNT(*) FROM knowledge WHERE trigger = ? AND hilfsverb = ? AND reply = ?", undef, $trigger, $hilfsverb, $reply
194 172 ))[0];
195 my $sth = $dbh->prepare( "SELECT COUNT(*) FROM knowledge WHERE trigger = ? AND hilfsverb = ? AND reply = ?;" ); 173
196 $sth->execute( $trigger, $hilfsverb, $reply );
197 my ($cnt) = $sth->fetchrow_array;
198 print "$cnt \n";
199
200 if( $cnt == 0 ) {
201 $sth = $dbh->prepare( "INSERT INTO knowledge ( trigger, hilfsverb, reply ) VALUES ( ?, ?, ?) " );
202 $sth->execute( $trigger, $hilfsverb, $reply );
203 }
204 } else { 174 } else {
205 my $sth = $dbh->prepare( "SELECT trigger, hilfsverb, reply FROM knowledge WHERE trigger = ? ORDER BY RANDOM() LIMIT 1"); 175 #search in our knowledge
206 $sth->execute( $msg ); 176 my $thr = join ' ', $dbh->selectrow_array(
207 if( my ($trigger, $hilfsverb, $reply) = $sth->fetchrow_array ) { 177 "SELECT trigger, hilfsverb, reply FROM knowledge WHERE trigger = ? ORDER BY RANDOM() LIMIT 1", undef, $msg );
208 if( $reply =~ /^<reply>(.*)$/ ) { 178 $thr =~ s/^.*?(?:<reply> )(.*)$/$1/;
209 $kernel->post( 'francoise', 'privmsg', $channel, "$1"); 179 $kernel->post( 'francoise', 'privmsg', $channel, $thr ) if $thr;
210 } else {
211 $kernel->post( 'francoise', 'privmsg', $channel, "$trigger $hilfsverb $reply");
212 }
213 }
214 } 180 }
215 181
216 if( $nick ne $current_nick ) { 182 #credit word and line count to user
217 my $sth = $dbh->prepare( "UPDATE users SET lines = lines + 1, words = words + $numwords + 1, isaway = false WHERE nick = ?" ); 183 $dbh->do( "UPDATE users SET lines = lines + 1, words = words + ?, w0rds = w0rds + ?, isaway = false WHERE nick = ?",
218 $sth->execute( $nick ); 184 undef, $#words + 1, $w0rds, $nick ) if $nick ne $current_nick;
219 }
220 185
221 print "$channel: <$nick> $msg\n"; 186 print "$channel: <$nick> $msg\n";
222} 187}
223 188
224sub irc_action{ 189sub irc_action{
225 my ( $who, $msg ) = @_[ ARG0, ARG2 ]; 190 my ( $who, $msg ) = @_[ ARG0, ARG2 ];
226 my $nick_ = ( split /!/, $who )[0]; 191 my $nick = francoise_getbasenick( ( split /!/, $who )[0] );
227
228 my $nick = francoise_getbasenick( $nick_ );
229 192
230 if( $msg =~ /^ist (.+)$/ ) { 193 #note whereis information
231 my $awaymsg = $1; 194 $dbh->do( "UPDATE users SET isaway = true, awaymsg = ? WHERE nick = ?", undef, $1, $nick )
232 my $sth = $dbh->prepare( "UPDATE users SET isaway = true, awaymsg = ? WHERE nick = ?"); 195 if( $msg =~ /^ist (.+)$/ );
233 $sth->execute( $awaymsg, $nick );
234 }
235} 196}
236 197
237sub irc_priv_msg{ 198sub irc_priv_msg{
238 my ( $kernel, $msg ) = @_[KERNEL, ARG2]; 199 my ( $kernel, $msg ) = @_[KERNEL, ARG2];
239 my $nick = (split /!/, $_[ARG0])[0]; 200 my $nick = (split /!/, $_[ARG0])[0];
240 201
241 if( $msg =~ /^!(\S+) *(.*)$/ ) { 202 &{$commands{ $1 } || \&francoise_donothing } ( $kernel, $nick, $2 )
242 &{$commands{ $1 } || \&francoise_donothing } ( $kernel, $nick, $2 ); 203 if( $msg =~ /^!(\S+) *(.*)$/ );
243 }
244 204
245 if( $msg =~ /^!say (.*)$/ ) { 205 #todo: hide that better, the lady is no puppet on a string
246 $kernel->post( 'francoise', 'privmsg', $channel, $1); } 206 $kernel->post( 'francoise', 'privmsg', $channel, $1)
207 if( $msg =~ /^!say (.*)$/ );
247 208
248 print "PRIV: [$nick] $msg\n"; 209 print "PRIV: [$nick] $msg\n";
249} 210}
@@ -251,38 +212,28 @@ sub irc_priv_msg{
251sub irc_whois{ 212sub irc_whois{
252 my ( $nick, $host ) = (split / /, $_[ARG1])[0,2]; 213 my ( $nick, $host ) = (split / /, $_[ARG1])[0,2];
253 214
254 if( $nick ne $current_nick ) { 215 francoise_verifyuser( $nick, $host, 0 )
255 francoise_verifyuser( $nick, $host, 0 ); 216 if( $nick ne $current_nick );
256 }
257} 217}
258 218
259sub francoise_getbasenick{ 219sub francoise_getbasenick{
260 my $nick = $_[0]; 220 my $nick = $_[0]; $nick =~ s/^(.+?)[_|^-].*/$1/;
261 $nick =~ s/^(.+)[_|^-].*/$1/; 221 return ($dbh->selectrow_array( "SELECT nick FROM aliases WHERE alias = ?", undef, $nick ))[0] || $nick;
262
263 my $sth = $dbh->prepare( "SELECT nick FROM aliases WHERE alias = ?" );
264 $sth->execute($nick);
265 if( my @row = $sth->fetchrow_array ) {
266 $nick = $row[0];
267 }
268
269 return $nick;
270} 222}
271 223
272sub francoise_verifyuser { 224sub francoise_verifyuser {
273 my ( $nick_, $host, $updtime, $kernel ) = @_; 225 my ( $nick_, $host, $updtime, $kernel ) = @_;
274 my $nick = francoise_getbasenick( $nick_ ); 226 my $nick = francoise_getbasenick( $nick_ );
275 227
276 if( $host ) { 228 $kernel->post( 'francoise', 'whois', $nick_), return $nick
277 my ($cnt) = $dbh->selectrow_array( "SELECT COUNT(*) FROM users WHERE nick='$nick'" ); 229 unless $host;
278 if ( $cnt == 0 ) { 230
279 $dbh->do( "INSERT INTO users(id, nick, words, lines, lastlogin, lasthost) VALUES ( '', '$nick', 0, 0, now(), '$host' )"); 231 my ($cnt) = $dbh->selectrow_array( "SELECT COUNT(*) FROM users WHERE nick=?", undef, $nick );
280 } else { 232
281 $dbh->do( "UPDATE users SET lasthost = '$host'" . ( $updtime ? ", lastlogin = now()" : "" ) . " WHERE nick = '$nick'" ); 233 $dbh->do( "INSERT INTO users(id, nick, words, lines, lastlogin, lasthost) VALUES ( '', ?, 0, 0, now(), ? )", undef, $nick, $host)
282 } 234 unless $cnt;
283 } else { 235 $dbh->do( "UPDATE users SET lasthost = ?" . ( $updtime ? ", lastlogin = now()" : "" ) . " WHERE nick = ?", undef, $host, $nick )
284 $kernel->post( 'francoise', 'whois', $nick_); 236 if $cnt;
285 }
286 237
287 return $nick; 238 return $nick;
288} 239}
@@ -304,7 +255,7 @@ sub francoise_stat {
304 my $age = time() - $starttime; 255 my $age = time() - $starttime;
305 my $secs = $age % 60; 256 my $secs = $age % 60;
306 my $mins = ( $age / 60 ) % 3600; 257 my $mins = ( $age / 60 ) % 3600;
307 my $hours = ( $age / 3660 ) % 86400; 258 my $hours = ( $age / 3600 ) % 86400;
308 my $days = $age / 86400; 259 my $days = $age / 86400;
309 260
310 my $agestring; 261 my $agestring;
@@ -327,55 +278,49 @@ sub francoise_stat {
327sub francoise_topten{ 278sub francoise_topten{
328 my ($kernel, $dest ) = @_; 279 my ($kernel, $dest ) = @_;
329 280
330 my $sth = $dbh->prepare( "SELECT words, nick FROM users ORDER BY words DESC LIMIT 10" ); 281 my $sth = $dbh->prepare( "SELECT words, nick FROM users WHERE words > 0 ORDER BY words DESC LIMIT 10" );
331 $sth->execute(); 282 $sth->execute();
332 while ( my @row = $sth->fetchrow_array ) { 283 while ( my ($words, $nick) = $sth->fetchrow_array ) {
333 $kernel->post( 'francoise', 'privmsg', $dest, "$row[0] $row[1]" ); 284 $kernel->post( 'francoise', 'privmsg', $dest, "$words $nick" );
334 } 285 }
335} 286}
336 287
337sub francoise_alias{ 288sub francoise_topten0r {
338 my ($kernel, $dest, $msg ) = @_; 289 my ($kernel, $dest ) = @_;
339 290
340 my ($nick, $alias) = (split / /, $msg ); 291 my $sth = $dbh->prepare( "SELECT w0rds, nick FROM users WHERE w0rds > 0 ORDER BY w0rds DESC LIMIT 10" );
292 $sth->execute();
341 293
342 my $sth = $dbh->prepare( "SELECT nick FROM aliases WHERE alias = ?" ); 294 while ( my ($w0rds, $nick ) = $sth->fetchrow_array ) {
343 $sth->execute( $nick ); 295 $kernel->post( 'francoise', 'privmsg', $dest, "$w0rds $nick" );
344 if( my ($tmp) = $sth->fetchrow_array ) {
345 $alias = $nick; $nick = $tmp;
346 } 296 }
297}
347 298
348 $sth = $dbh->prepare( "SELECT COUNT(*) FROM aliases WHERE nick = ? AND alias = ?" ); 299sub francoise_alias{
349 $sth->execute( $nick, $alias); 300 my ($kernel, $dest, $msg ) = @_;
350 return if( ($sth->fetchrow_array)[0] > 0 ); 301 my ($nick, $alias) = (split / /, $msg );
351
352 $sth = $dbh->prepare( "SELECT words, lines FROM users WHERE nick = ?");
353 $sth->execute($nick); my @userrow = $sth->fetchrow_array;
354 $sth->execute($alias); my @aliasrow = $sth->fetchrow_array;
355 302
356 print "@userrow @aliasrow \n"; 303 my ($tmp) = $dbh->selectrow_array( "SELECT nick FROM aliases WHERE alias = ?", undef, $nick );
304 $alias = $nick, $nick = $tmp if $tmp;
357 305
358 if( @userrow && @aliasrow ) { 306 ($tmp) = $dbh->selectrow_array( "SELECT COUNT(*) FROM aliases WHERE nick = ? AND alias = ?", undef, $nick, $alias );
359 $sth = $dbh->prepare( "UPDATE users SET isaway = false, words = ?, lines = ? WHERE nick = ?" ); 307 return if $tmp;
360 $sth->execute( $userrow[0]+$aliasrow[0], $userrow[1]+$aliasrow[1], $nick );
361 308
362 $sth = $dbh->prepare( "INSERT INTO aliases ( nick, alias ) VALUES ( ?, ? )" ); 309 my ( $usw, $usl, $us0 ) = $dbh->selectrow_array( "SELECT words, lines, w0rds FROM users WHERE nick = ?", undef, $nick );
363 $sth->execute($nick, $alias); 310 my ( $alw, $all, $al0 ) = $dbh->selectrow_array( "SELECT words, lines, w0rds FROM users WHERE nick = ?", undef, $alias);
364 311
365 $sth = $dbh->prepare( "DELETE FROM users WHERE nick = ?" ); 312 if( $alw || $all ) {
366 $sth->execute( $alias ); 313 $dbh->do( "UPDATE users SET isaway = false, words = ?, lines = ?, w0rds = ? WHERE nick = ?",
314 undef, $usw+$alw, $usl+$all, $us0 + $al0, $nick );
315 $dbh->do( "INSERT INTO aliases ( nick, alias ) VALUES ( ?, ? )", undef, $nick, $alias );
316 $dbh->do( "DELETE FROM users WHERE nick = ?", undef, $alias );
367 } 317 }
368} 318}
369 319
370sub francoise_topten0r {
371}
372
373sub francoise_forget { 320sub francoise_forget {
374
375} 321}
376 322
377sub francoise_donothing { 323sub francoise_donothing {
378
379} 324}
380 325
381#start everything 326#start everything
diff --git a/tables.sql b/tables.sql
index 27bdc4f..ed1e842 100644
--- a/tables.sql
+++ b/tables.sql
@@ -13,6 +13,7 @@ create table users (
13 13
14 words integer, 14 words integer,
15 lines integer, 15 lines integer,
16 w0rds integer,
16 17
17 isaway bool default false, 18 isaway bool default false,
18 awaymsg text, 19 awaymsg text,