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'.
125 while ($exists =~ /^<REPLY> ?see (.*)/i) {
127 unless ($redircount) {
128 &msg($who, "$origlhs has too many levels of redirection.");
133 $exists = &getFactoid($lhs);
135 &msg($who, "$1 is a dangling redirection.");
139 if ($exists =~ /^<REPLY> ?see (.*)/i) {
140 &TODO("Update.pl: append to linked factoid.");
143 if ($also_or) { # 'is also ||'.
144 $rhs = $exists.' || '.$rhs;
146 # if ($exists =~ s/\,\s*$/, /) {
147 if ($exists =~ /\,\s*$/) {
148 &DEBUG("current has trailing comma, just append as is");
149 &DEBUG("Up: exists => $exists");
150 &DEBUG("Up: rhs => $rhs");
152 # $rhs = $exists." ".$rhs; # keep comma.
155 if ($exists =~ /\.\s*$/) {
156 &DEBUG("current has trailing period, just append as is with 2 WS");
157 &DEBUG("Up: exists => $exists");
158 &DEBUG("Up: rhs => $rhs");
161 # $rhs = $exists." ".$rhs; # keep comma.
164 if ($rhs =~ /^[A-Z]/) {
165 if ($rhs =~ /\w+\s*$/) {
166 &status("auto insert period to factoid.");
167 $rhs = $exists.". ".$rhs;
168 } else { # '?' or '.' assumed at end.
169 &status("orig factoid already had trailing symbol; not adding period.");
170 $rhs = $exists." ".$rhs;
172 } elsif ($exists =~ /[\,\.\-]\s*$/) {
173 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
174 $rhs = $exists." ".$rhs;
175 } elsif ($rhs =~ /^\./) {
176 &VERB("U: new text has ^.; appending directly",2);
179 $rhs = $exists.', or '.$rhs;
183 # max length check again.
184 if (length $rhs > $param{'maxDataSize'}) {
185 if (length $rhs > length $exists) {
186 &performAddressedReply("that's too long");
189 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
193 &performAddressedReply("okay");
196 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
197 &sqlReplace("factoids", {
200 modified_time => time(),
201 factoid_value => $rhs,
204 if (!defined $rhs or $rhs eq "") {
205 &ERROR("Update: rhs1 == NULL.");
207 } else { # not "also"
209 if (!$correction_plausible) { # "no, blah is ..."
211 &performStrictReply("...but \002$lhs\002 is already something else...");
212 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
217 my $author = &getFactInfo($lhs, "created_by") || "";
219 if (IsFlag("m") ne "m" && IsFlag("o") ne "o" &&
220 $author !~ /^\Q$who\E\!/i
222 &msg($who, "you can't change that factoid.");
226 &performAddressedReply("okay");
229 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
231 &sqlReplace("factoids", {
234 modified_time => time(),
235 factoid_value => $rhs,
238 if (!defined $rhs or $rhs eq "") {
239 &ERROR("Update: rhs1 == NULL.");