2 ### Reply.pl: Kevin Lenzo (c) 1997
6 # x is y === $lhs $mhs $rhs
10 # rhs - factoid message.
14 use POSIX qw(strftime);
16 use vars qw($msgType $uh $lastWho $ident);
17 use vars qw(%lang %lastWho);
21 my ( $lhs, $mhs, $rhs );
22 my ( $reply, $count, $fauthor, $result, $factoid, $search, @searches );
23 $orig{message} = $message;
25 if ( !defined $message or $message =~ /^\s*$/ ) {
26 &WARN("getR: message == NULL.");
30 $message =~ tr/A-Z/a-z/;
33 split( /\s+/, &getChanConfDefault( 'factoidSearch', '_default', $chan ) );
34 &::DEBUG( "factoidSearch: $chan is: " . join( ':', @searches ) );
36 # requesting the _default one, ignore factoidSearch
37 if ( $message =~ /^_default\s+/ ) {
38 @searches = ('_default');
39 $message =~ s/^_default\s+//;
42 # check for factoids with each prefix
43 foreach $search (@searches) {
44 if ( $search eq '$chan' ) {
45 $factoid = "$chan $message";
47 elsif ( $search eq '_default' ) {
51 $factoid = "$search $message";
53 ( $count, $fauthor, $result ) = &sqlSelect(
55 "requested_count,created_by,factoid_value",
56 { factoid_key => $factoid }
60 $result = encode_utf8($result) if is_utf8($result);
67 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
73 # if there was a head...
74 my (@poss) = split '\|\|', $result;
76 $poss[$#poss] =~ s/\s$//;
79 $result = &getRandom(@poss);
83 $result = &SARit($result);
86 if ( $result ne '' ) {
87 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
88 ### FLOOD REPETION AND PROTECTION. -20000124
91 ### FIXME: old mysql/sqlite doesn't support
92 ### "requested_count=requested_count+1".
96 { 'factoid_key' => $factoid },
99 requested_time => time(),
100 requested_count => $count
104 # TODO: rename $real to something else!
107 # my $author = &getFactInfo($lhs,'created_by') || '';
108 # $real++ if ($author =~ /^\Q$who\E\!/);
109 # $real++ if (&IsFlag('n'));
110 $real = 0 if ( $msgType =~ /public/ );
112 ### fix up the reply.
113 # only remove '<reply>'
114 if ( !$real and $reply =~ s/^\s*<reply>\s*//i ) {
117 if ( $reply =~ s/^are /$lhs are /i ) {
118 &VERB( "Reply.pl: el-cheapo 'are' fix executed.", 2 );
122 elsif ( !$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i ) {
124 # only remove '<action>' and make it an action.
126 else { # not a short reply
129 if ( exists $bots{$nuh} and $rhs !~ /^\s*$/ ) {
130 return "$lhs $mhs $rhs";
133 ### bot->person reply.
134 # result is random if separated by '||'.
135 # rhs is full factoid with '||'.
136 if ( $mhs eq 'is' ) {
137 $reply = &getRandom( keys %{ $lang{'factoid'} } );
138 $reply =~ s/##KEY/$lhs/;
139 $reply =~ s/##VALUE/$result/;
142 $reply = "$lhs $mhs $result";
145 if ( $reply =~ s/^\Q$who\E is/you are/i ) {
150 if ( $reply =~ /^you are / or $reply =~ / you are / ) {
151 return if ($addressed);
157 # remove excessive beginning and end whitespaces.
158 $reply =~ s/^\s+|\s+$//g;
160 if ( $reply =~ /^\s+$/ ) {
161 &DEBUG("Reply: Null factoid ($message)");
165 return $reply unless ( $reply =~ /\$/ );
171 # don't evaluate if it has factoid arguments.
172 # if ($message =~ /^cmd:/i) {
173 # &status("Reply: not doing substVars (eval dollar vars)");
175 $reply = &substVars( $reply, 1 );
184 my ( $l, $r ) = ( 0, 0 ); # l = left, r = right.
185 my ( $s, $t ) = ( 0, 0 ); # s = start, t = marker.
190 foreach ( split //, $string ) {
207 my $substr = substr( $old, $s, $i - $s + 1 );
208 push( @rand, substr( $old, $t + 1, $i - $t - 1 ) );
210 my $rand = $rand[ rand @rand ];
212 # &status("SARing '$substr' to '$rand'.");
213 $string =~ s/\Q$substr\E/$rand/;
218 if ( $_ eq "|" and $l + $r == 0 and $l == 1 ) {
219 push( @rand, substr( $old, $t + 1, $i - $t - 1 ) );
226 if ( $old eq $string ) {
227 &WARN("smart_replace: no subst made. (string => $string)");
238 while ( $txt =~ /\((.*?)\)\?/ ) {
240 if ( rand() > 0.5 ) { # fix.
241 &status("Factoid transform: keeping '$str'.");
242 $txt =~ s/\(\Q$str\E\)\?/$str/;
245 &status("Factoid transform: removing '$str'.");
246 $txt =~ s/\(\Q$str\E\)\?\s?//;
249 last if ( $done >= 10 ); # just in case.
253 # EG: (0-32768) => 6325
254 ### TODO: (1-10,20-30,40) => 24
255 while ( $txt =~ /\((\d+)-(\d+)\)/ ) {
256 my ( $lower, $upper ) = ( $1, $2 );
257 my $new = int( rand $upper - $lower ) + $lower;
259 &status("SARing '$&' to '$new' (2).");
262 last if ( $done >= 10 ); # just in case.
266 # EG: (blah1|blah2|blah3|) => blah1
267 while ( $txt =~ /.*\((.*\|.*?)\).*/ ) {
268 $txt = &smart_replace($txt);
271 last if ( $done >= 10 ); # just in case.
273 &status("Reply.pl: $done SARs done.") if ($done);
277 while ( $txt =~ /<URL>(.*)<\/URL>/ ) {
278 &status("we have to norm this <URL></URL> stuff, SARing");
280 if ( $foobar =~ m/(http:\/\/[^?]+)\?(.*)/ ) {
281 my ( $pig1, $pig2 ) = ( $1, $2 );
282 &status("SARing using URLencode");
283 $pig2 =~ s/([^\w])/sprintf("%%%02x",ord($1))/gie;
284 $foobar = $pig1 . "?" . $pig2;
286 $txt =~ s/<URL>(.*)<\/URL>/$foobar/;
292 my ( $reply, $flag ) = @_;
294 # $date, $time, $day.
295 # TODO: support localtime.
296 my $date = strftime( "%Y.%m.%d", gmtime() );
297 $reply =~ s/\$date/$date/gi;
298 my $time = strftime( "%k:%M:%S", gmtime() );
299 $reply =~ s/\$time/$time/gi;
300 my $day = strftime( "%A", gmtime() );
301 $reply =~ s/\$day/$day/gi;
303 # support $ident when I have multiple nicks
304 my $mynick = $conn->nick() if $conn;
308 $reply =~ s/\$nick/$who/g;
309 $reply =~ s/\$who/$who/g; # backward compat.
312 if ( $reply =~ /\$(user(name)?|host)/ ) {
313 my ( $username, $hostname ) = split /\@/, $uh;
314 $reply =~ s/\$user(name)?/$username/g;
315 $reply =~ s/\$host(name)?/$hostname/g;
317 $reply =~ s/\$chan(nel)?/$talkchannel/g;
318 if ( $msgType =~ /public/ ) {
319 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
322 $reply =~ s/\$lastspeaker/$lastWho/g;
325 if ( $reply =~ /\$rand/ ) {
329 if ( $reply =~ /\$randnick/ ) {
330 my @nicks = keys %{ $channels{$chan}{''} };
331 my $randnick = $nicks[ int( $rand * $#nicks ) ];
332 $reply =~ s/\$randnick/$randnick/g;
336 if ( $reply =~ /\$rand(\d+)(\.(\d+))?/ ) {
341 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
342 $rand = sprintf( "%.*f", $dot, $rand * $max );
344 &DEBUG("swapping $orig to $rand");
345 $reply =~ s/\Q$orig\E/$rand/eg;
348 $reply =~ s/\$rand/$rand/g;
352 $reply =~ s/\$ident/$mynick/g;
354 if ( $reply =~ /\$startTime/ ) {
355 my $time = scalar( gmtime $^T );
356 $reply =~ s/\$startTime/$time/;
359 if ( $reply =~ /\$uptime/ ) {
360 my $uptime = &Time2String( time() - $^T );
361 $reply =~ s/\$uptime/$uptime/;
364 if ( $reply =~ /\$factoids/ ) {
365 my $factoids = &countKeys('factoids');
366 $reply =~ s/\$factoids/$factoids/;
369 if ( $reply =~ /\$Fupdate/ ) {
371 "\002$count{'Update'}\002 "
372 . &fixPlural( 'modification', $count{'Update'} );
373 $reply =~ s/\$Fupdate/$x/;
376 if ( $reply =~ /\$Fquestion/ ) {
378 "\002$count{'Question'}\002 "
379 . &fixPlural( 'question', $count{'Question'} );
380 $reply =~ s/\$Fquestion/$x/;
383 if ( $reply =~ /\$Fdunno/ ) {
385 "\002$count{'Dunno'}\002 " . &fixPlural( 'dunno', $count{'Dunno'} );
386 $reply =~ s/\$Fdunno/$x/;
389 $reply =~ s/\$memusage/$memusage/;
396 # vim:ts=4:sw=4:expandtab:tw=80