2 # Update.pl: Add or modify factoids in the db.
12 my($lhs, $mhs, $rhs) = @_;
15 s/^i (heard|think) //i;
16 s/^some(one|1|body) said //i;
21 return if (&IsLocked($lhs) == 1);
24 if (&IsParam("profanityCheck") and &hasProfanity($rhs)) {
25 &performReply("please, watch your language.");
30 if (&IsFlag("t") ne "t" && &IsFlag("o") ne "o") {
31 &msg($who, "permission denied.");
32 &status("alert: $who wanted to teach me.");
37 if ($mhs !~ /^(is|are)$/i) {
38 &ERROR("UNKNOWN verb: $mhs.");
42 # check if the arguments are too long to be stored in our table.
44 $toolong++ if (length $lhs > $param{'maxKeySize'});
45 $toolong++ if (length $rhs > $param{'maxDataSize'});
47 &performAddressedReply("that's too long");
52 my $also = ($rhs =~ s/^-?also //i);
53 my $also_or = ($also and $rhs =~ s/\s+(or|\|\|)\s+//);
55 # factoid arguments handler.
56 # must start with a non-variable
57 if (&IsChanConf("factoidArguments") > 0 and $lhs =~ /^[^\$]+.*\$/) {
58 &status("Update: Factoid Arguments found.");
59 &status("Update: orig lhs => '$lhs'.");
60 &status("Update: orig rhs => '$rhs'.");
65 while ($lhs =~ s/\$(\S+)/(.*?)/) {
68 last if ($count >= 10);
72 &msg($who, "error: could not SAR properly.");
73 &DEBUG("error: lhs => '$lhs' rhs => '$rhs'.");
77 my $z = join(',',@list);
80 &status("Update: new lhs => '$lhs' rhs => '$rhs'.");
84 my $exists = &getFactoid($lhs);
87 # nice 'are' hack (or work-around).
88 if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
89 &status("Update: 'are' hack detected.");
91 $rhs = "<REPLY> are ". $rhs;
94 &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
97 &performAddressedReply("okay");
99 &sqlReplace("factoids", {
101 created_time => time(), # modified time.
103 factoid_value => $rhs,
106 if (!defined $rhs or $rhs eq "") {
107 &ERROR("Update: rhs1 == NULL.");
114 if ($exists eq $rhs) {
115 # this catches the following situation: (right or wrong?)
117 # "test is also test"
118 &performAddressedReply("i already had it that way");
122 if ($also) { # 'is also'.
123 if ($exists =~ /^<REPLY> see /i) {
124 &TODO("Update.pl: append to linked factoid.");
127 if ($also_or) { # 'is also ||'.
128 $rhs = $exists.' || '.$rhs;
130 # if ($exists =~ s/\,\s*$/, /) {
131 if ($exists =~ /\,\s*$/) {
132 &DEBUG("current has trailing comma, just append as is");
133 &DEBUG("Up: exists => $exists");
134 &DEBUG("Up: rhs => $rhs");
136 # $rhs = $exists." ".$rhs; # keep comma.
139 if ($exists =~ /\.\s*$/) {
140 &DEBUG("current has trailing period, just append as is with 2 WS");
141 &DEBUG("Up: exists => $exists");
142 &DEBUG("Up: rhs => $rhs");
145 # $rhs = $exists." ".$rhs; # keep comma.
148 if ($rhs =~ /^[A-Z]/) {
149 if ($rhs =~ /\w+\s*$/) {
150 &status("auto insert period to factoid.");
151 $rhs = $exists.". ".$rhs;
152 } else { # '?' or '.' assumed at end.
153 &status("orig factoid already had trailing symbol; not adding period.");
154 $rhs = $exists." ".$rhs;
156 } elsif ($exists =~ /[\,\.\-]\s*$/) {
157 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
158 $rhs = $exists." ".$rhs;
159 } elsif ($rhs =~ /^\./) {
160 &VERB("U: new text has ^.; appending directly",2);
163 $rhs = $exists.', or '.$rhs;
167 # max length check again.
168 if (length $rhs > $param{'maxDataSize'}) {
169 if (length $rhs > length $exists) {
170 &performAddressedReply("that's too long");
173 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
177 &performAddressedReply("okay");
180 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
181 &sqlReplace("factoids", {
184 modified_time => time(),
185 factoid_value => $rhs,
188 if (!defined $rhs or $rhs eq "") {
189 &ERROR("Update: rhs1 == NULL.");
191 } else { # not "also"
193 if (!$correction_plausible) { # "no, blah is ..."
195 &performStrictReply("...but \002$lhs\002 is already something else...");
196 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
201 my $author = &getFactInfo($lhs, "created_by") || "";
203 if (IsFlag("m") ne "m" && IsFlag("o") ne "o" &&
204 $author !~ /^\Q$who\E\!/i
206 &msg($who, "you can't change that factoid.");
210 &performAddressedReply("okay");
213 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
215 &sqlReplace("factoids", {
218 modified_time => time(),
219 factoid_value => $rhs,
222 if (!defined $rhs or $rhs eq "") {
223 &ERROR("Update: rhs1 == NULL.");