+sub factoidArgs {
+ my($str) = @_;
+ my $result;
+
+ # to make it eleeter, split each arg and use "blah OR blah or BLAH"
+ # which will make it less than linear => quicker!
+ # TODO: cache this, update cache when altered. !!! !!! !!!
+# my $t = &timeget();
+ my ($first) = split(/\s+/, $str);
+
+ # ignore split to commands [dumb commands vs. factoids] (editing commands?)
+ return undef if $str =~ /\s+\=\~\s+s[\#\/\:]/;
+ my @list = &searchTable('factoids', 'factoid_key', 'factoid_key', "^cmd: $first ");
+# my $delta_time = &timedelta($t);
+# &DEBUG("factArgs: delta_time = $delta_time s");
+# &DEBUG("factArgs: list => ".scalar(@list) );
+
+ # from a design perspective, it's better to have the regex in
+ # the factoid key to reduce repetitive processing.
+
+ # it does not matter if it's not alphabetically sorted.
+ foreach (sort { length($b) <=> length($a) } @list) {
+ next if (/#DEL#/); # deleted.
+
+ s/^cmd: //i;
+# &DEBUG("factarg: '$str' =~ /^$_\$/");
+ my $arg = $_;
+
+ # eval (evil!) code. cleaned up courtesy of lear.
+ my @vals;
+ eval {
+ @vals = ($str =~ /^$arg$/i);
+ };
+
+ if ($@) {
+ &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
+ next;
+ }
+
+ next unless (@vals);
+
+ if (defined $result) {
+ &WARN("factargs: '$_' matches aswell.");
+ next;
+ }
+
+# &DEBUG("vals => @vals");
+
+ &status("Question: factoid Arguments for '$str'");
+ # TODO: use getReply() - need to modify it :(
+ my $i = 0;
+ my $q = "cmd: $_";
+ my $r = &getFactoid($q);
+ if (!defined $r) {
+ &DEBUG("question: !result... should this happen?");
+ return;
+ }
+
+ # update stats. old mysql/sqlite don't do +1
+ my ($count) = &sqlSelect('factoids', 'requested_count', { factoid_key => $q });
+ $count++;
+ &sqlSet('factoids', {'factoid_key' => $q}, {
+ requested_by => $nuh,
+ requested_time => time(),
+ requested_count => $count
+ } );
+
+ # end of update stats.
+
+ $result = $r;
+
+ $result =~ s/^\((.*?)\): //;
+ my $vars = $1;
+
+ # start nasty hack to get partial &getReply() functionality.
+ $result = &SARit($result);
+
+ foreach ( split(',', $vars) ) {
+ my $val = $vals[$i];
+# &DEBUG("val => $val");
+
+ if (!defined $val) {
+ &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
+ next;
+ }
+
+ my $done = 0;
+ my $old = $result;
+ while (1) {
+# &DEBUG("Q: result => $result (1before)");
+ $result = &substVars($result,1);
+# &DEBUG("Q: result => $result (1after)");
+
+ last if ($old eq $result);
+ $old = $result;
+ $done++;
+ }
+
+ # hack.
+ $vals[$i] =~ s/^me$/$who/gi;
+
+# if (!$done) {
+ &status("factArgs: SARing '$_' to '$vals[$i]'.");
+ $result =~ s/\Q$_\E/$vals[$i]/g;
+# }
+ $i++;
+ }
+
+ # rest of nasty hack to get partial &getReply() functionality.
+ $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
+ $result =~ s/^\s*<reply>\s*//i;
+
+# well... lets go through all of them. not advisable if we have like
+# 1000 commands, heh.
+# return $result;
+ $cmdstats{'Factoid Commands'}++;
+ }
+
+ return $result;
+}
+