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 if ($also or $also_or) {
56 my $author = &getFactInfo($from, 'created_by');
60 # Can they even modify factoids?
61 if (&IsFlag('m') ne 'm' and &IsFlag('M') ne 'M' and &IsFlag('o') ne 'o') {
62 &performReply("You do not have permission to modify factoids");
65 # If they have +M but they didnt create the factoid
66 } elsif (&IsFlag('M') eq 'M' and
67 $who !~ /^\Q$created_by\E$/i and
68 &IsFlag('m') ne 'm' and
69 &IsFlag('o') ne 'o') {
70 &performReply("factoid '$lhs' is not yours to modify.");
75 # factoid arguments handler.
76 # must start with a non-variable
77 if (&IsChanConf('factoidArguments') > 0 and $lhs =~ /^[^\$]+.*\$/) {
78 &status("Update: Factoid Arguments found.");
79 &status("Update: orig lhs => '$lhs'.");
80 &status("Update: orig rhs => '$rhs'.");
85 while ($lhs =~ s/\$(\S+)/(.*?)/) {
88 last if ($count >= 10);
92 &msg($who, "error: could not SAR properly.");
93 &DEBUG("error: lhs => '$lhs' rhs => '$rhs'.");
97 my $z = join(',',@list);
100 &status("Update: new lhs => '$lhs' rhs => '$rhs'.");
104 my $exists = &getFactoid($lhs);
107 # nice 'are' hack (or work-around).
108 if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
109 &status("Update: 'are' hack detected.");
111 $rhs = "<REPLY> are ". $rhs;
114 &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
117 &performAddressedReply('okay');
119 &sqlInsert('factoids', {
121 created_time => time(), # modified time.
123 factoid_value => $rhs,
126 if (!defined $rhs or $rhs eq '') {
127 &ERROR("Update: rhs1 == NULL.");
134 if ($exists eq $rhs) {
135 # this catches the following situation: (right or wrong?)
137 # "test is also test"
138 &performAddressedReply("i already had it that way");
142 if ($also) { # 'is also'.
145 while ($exists =~ /^<REPLY> ?see (.*)/i) {
147 unless ($redircount) {
148 &msg($who, "$origlhs has too many levels of redirection.");
153 $exists = &getFactoid($lhs);
155 &msg($who, "$1 is a dangling redirection.");
159 if ($exists =~ /^<REPLY> ?see (.*)/i) {
160 &TODO("Update.pl: append to linked factoid.");
163 if ($also_or) { # 'is also ||'.
164 $rhs = $exists.' || '.$rhs;
166 # if ($exists =~ s/\,\s*$/, /) {
167 if ($exists =~ /\,\s*$/) {
168 &DEBUG("current has trailing comma, just append as is");
169 &DEBUG("Up: exists => $exists");
170 &DEBUG("Up: rhs => $rhs");
172 # $rhs = $exists." ".$rhs; # keep comma.
175 if ($exists =~ /\.\s*$/) {
176 &DEBUG("current has trailing period, just append as is with 2 WS");
177 &DEBUG("Up: exists => $exists");
178 &DEBUG("Up: rhs => $rhs");
181 # $rhs = $exists." ".$rhs; # keep comma.
184 if ($rhs =~ /^[A-Z]/) {
185 if ($rhs =~ /\w+\s*$/) {
186 &status("auto insert period to factoid.");
187 $rhs = $exists.". ".$rhs;
188 } else { # '?' or '.' assumed at end.
189 &status("orig factoid already had trailing symbol; not adding period.");
190 $rhs = $exists." ".$rhs;
192 } elsif ($exists =~ /[\,\.\-]\s*$/) {
193 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
194 $rhs = $exists." ".$rhs;
195 } elsif ($rhs =~ /^\./) {
196 &VERB("U: new text has ^.; appending directly",2);
199 $rhs = $exists.', or '.$rhs;
203 # max length check again.
204 if (length $rhs > $param{'maxDataSize'}) {
205 if (length $rhs > length $exists) {
206 &performAddressedReply("that's too long");
209 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
213 &performAddressedReply('okay');
216 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
217 &sqlSet('factoids', {'factoid_key' => $lhs}, {
219 modified_time => time(),
220 factoid_value => $rhs,
223 if (!defined $rhs or $rhs eq '') {
224 &ERROR("Update: rhs1 == NULL.");
226 } else { # not 'also'
228 if (!$correction_plausible) { # "no, blah is ..."
230 &performStrictReply("...but \002$lhs\002 is already something else...");
231 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
236 my $author = &getFactInfo($lhs, 'created_by') || '';
238 if (IsFlag('m') ne 'm' && IsFlag('o') ne 'o' &&
239 $author !~ /^\Q$who\E\!/i
241 &msg($who, "you can't change that factoid.");
245 &performAddressedReply('okay');
248 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
250 &sqlSet('factoids', {'factoid_key' => $lhs}, {
252 modified_time => time(),
253 factoid_value => $rhs,
256 if (!defined $rhs or $rhs eq '') {
257 &ERROR("Update: rhs1 == NULL.");
266 # vim:ts=4:sw=4:expandtab:tw=80