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 }
66 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
72 # if there was a head...
73 my (@poss) = split '\|\|', $result;
75 $poss[$#poss] =~ s/\s$//;
78 $result = &getRandom(@poss);
82 $result = &SARit($result);
85 if ( $result ne '' ) {
86 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
87 ### FLOOD REPETION AND PROTECTION. -20000124
90 ### FIXME: old mysql/sqlite doesn't support
91 ### "requested_count=requested_count+1".
95 { 'factoid_key' => $factoid },
98 requested_time => time(),
99 requested_count => $count
103 # TODO: rename $real to something else!
106 # my $author = &getFactInfo($lhs,'created_by') || '';
107 # $real++ if ($author =~ /^\Q$who\E\!/);
108 # $real++ if (&IsFlag('n'));
109 $real = 0 if ( $msgType =~ /public/ );
111 ### fix up the reply.
112 # only remove '<reply>'
113 if ( !$real and $reply =~ s/^\s*<reply>\s*//i ) {
116 if ( $reply =~ s/^are /$lhs are /i ) {
117 &VERB( "Reply.pl: el-cheapo 'are' fix executed.", 2 );
121 elsif ( !$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i ) {
123 # only remove '<action>' and make it an action.
125 else { # not a short reply
128 if ( exists $bots{$nuh} and $rhs !~ /^\s*$/ ) {
129 return "$lhs $mhs $rhs";
132 ### bot->person reply.
133 # result is random if separated by '||'.
134 # rhs is full factoid with '||'.
135 if ( $mhs eq 'is' ) {
136 $reply = &getRandom( keys %{ $lang{'factoid'} } );
137 $reply =~ s/##KEY/$lhs/;
138 $reply =~ s/##VALUE/$result/;
141 $reply = "$lhs $mhs $result";
144 if ( $reply =~ s/^\Q$who\E is/you are/i ) {
149 if ( $reply =~ /^you are / or $reply =~ / you are / ) {
150 return if ($addressed);
156 # remove excessive beginning and end whitespaces.
157 $reply =~ s/^\s+|\s+$//g;
159 if ( $reply =~ /^\s+$/ ) {
160 &DEBUG("Reply: Null factoid ($message)");
164 return $reply unless ( $reply =~ /\$/ );
170 # don't evaluate if it has factoid arguments.
171 # if ($message =~ /^cmd:/i) {
172 # &status("Reply: not doing substVars (eval dollar vars)");
174 $reply = &substVars( $reply, 1 );
183 my ( $l, $r ) = ( 0, 0 ); # l = left, r = right.
184 my ( $s, $t ) = ( 0, 0 ); # s = start, t = marker.
189 foreach ( split //, $string ) {
206 my $substr = substr( $old, $s, $i - $s + 1 );
207 push( @rand, substr( $old, $t + 1, $i - $t - 1 ) );
209 my $rand = $rand[ rand @rand ];
211 # &status("SARing '$substr' to '$rand'.");
212 $string =~ s/\Q$substr\E/$rand/;
217 if ( $_ eq "|" and $l + $r == 0 and $l == 1 ) {
218 push( @rand, substr( $old, $t + 1, $i - $t - 1 ) );
225 if ( $old eq $string ) {
226 &WARN("smart_replace: no subst made. (string => $string)");
237 while ( $txt =~ /\((.*?)\)\?/ ) {
239 if ( rand() > 0.5 ) { # fix.
240 &status("Factoid transform: keeping '$str'.");
241 $txt =~ s/\(\Q$str\E\)\?/$str/;
244 &status("Factoid transform: removing '$str'.");
245 $txt =~ s/\(\Q$str\E\)\?\s?//;
248 last if ( $done >= 10 ); # just in case.
252 # EG: (0-32768) => 6325
253 ### TODO: (1-10,20-30,40) => 24
254 while ( $txt =~ /\((\d+)-(\d+)\)/ ) {
255 my ( $lower, $upper ) = ( $1, $2 );
256 my $new = int( rand $upper - $lower ) + $lower;
258 &status("SARing '$&' to '$new' (2).");
261 last if ( $done >= 10 ); # just in case.
265 # EG: (blah1|blah2|blah3|) => blah1
266 while ( $txt =~ /.*\((.*\|.*?)\).*/ ) {
267 $txt = &smart_replace($txt);
270 last if ( $done >= 10 ); # just in case.
272 &status("Reply.pl: $done SARs done.") if ($done);
276 while ( $txt =~ /<URL>(.*)<\/URL>/ ) {
277 &status("we have to norm this <URL></URL> stuff, SARing");
279 if ( $foobar =~ m/(http:\/\/[^?]+)\?(.*)/ ) {
280 my ( $pig1, $pig2 ) = ( $1, $2 );
281 &status("SARing using URLencode");
282 $pig2 =~ s/([^\w])/sprintf("%%%02x",ord($1))/gie;
283 $foobar = $pig1 . "?" . $pig2;
285 $txt =~ s/<URL>(.*)<\/URL>/$foobar/;
291 my ( $reply, $flag ) = @_;
293 # $date, $time, $day.
294 # TODO: support localtime.
295 my $date = strftime( "%Y.%m.%d", gmtime() );
296 $reply =~ s/\$date/$date/gi;
297 my $time = strftime( "%k:%M:%S", gmtime() );
298 $reply =~ s/\$time/$time/gi;
299 my $day = strftime( "%A", gmtime() );
300 $reply =~ s/\$day/$day/gi;
302 # support $ident when I have multiple nicks
303 my $mynick = $conn->nick() if $conn;
307 $reply =~ s/\$nick/$who/g;
308 $reply =~ s/\$who/$who/g; # backward compat.
311 if ( $reply =~ /\$(user(name)?|host)/ ) {
312 my ( $username, $hostname ) = split /\@/, $uh;
313 $reply =~ s/\$user(name)?/$username/g;
314 $reply =~ s/\$host(name)?/$hostname/g;
316 $reply =~ s/\$chan(nel)?/$talkchannel/g;
317 if ( $msgType =~ /public/ ) {
318 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
321 $reply =~ s/\$lastspeaker/$lastWho/g;
324 if ( $reply =~ /\$rand/ ) {
328 if ( $reply =~ /\$randnick/ ) {
329 my @nicks = keys %{ $channels{$chan}{''} };
330 my $randnick = $nicks[ int( $rand * $#nicks ) ];
331 $reply =~ s/\$randnick/$randnick/g;
335 if ( $reply =~ /\$rand(\d+)(\.(\d+))?/ ) {
340 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
341 $rand = sprintf( "%.*f", $dot, $rand * $max );
343 &DEBUG("swapping $orig to $rand");
344 $reply =~ s/\Q$orig\E/$rand/eg;
347 $reply =~ s/\$rand/$rand/g;
351 $reply =~ s/\$ident/$mynick/g;
353 if ( $reply =~ /\$startTime/ ) {
354 my $time = scalar( gmtime $^T );
355 $reply =~ s/\$startTime/$time/;
358 if ( $reply =~ /\$uptime/ ) {
359 my $uptime = &Time2String( time() - $^T );
360 $reply =~ s/\$uptime/$uptime/;
363 if ( $reply =~ /\$factoids/ ) {
364 my $factoids = &countKeys('factoids');
365 $reply =~ s/\$factoids/$factoids/;
368 if ( $reply =~ /\$Fupdate/ ) {
370 "\002$count{'Update'}\002 "
371 . &fixPlural( 'modification', $count{'Update'} );
372 $reply =~ s/\$Fupdate/$x/;
375 if ( $reply =~ /\$Fquestion/ ) {
377 "\002$count{'Question'}\002 "
378 . &fixPlural( 'question', $count{'Question'} );
379 $reply =~ s/\$Fquestion/$x/;
382 if ( $reply =~ /\$Fdunno/ ) {
384 "\002$count{'Dunno'}\002 " . &fixPlural( 'dunno', $count{'Dunno'} );
385 $reply =~ s/\$Fdunno/$x/;
388 $reply =~ s/\$memusage/$memusage/;
395 # vim:ts=4:sw=4:expandtab:tw=80