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 $who !~ /^\Q$created_by\E$/i &IsFlag('m') ne 'm' &IsFlag('o') ne 'o') {
67 &performReply("factoid '$lhs' is not yours to modify.");
72 # factoid arguments handler.
73 # must start with a non-variable
74 if (&IsChanConf('factoidArguments') > 0 and $lhs =~ /^[^\$]+.*\$/) {
75 &status("Update: Factoid Arguments found.");
76 &status("Update: orig lhs => '$lhs'.");
77 &status("Update: orig rhs => '$rhs'.");
82 while ($lhs =~ s/\$(\S+)/(.*?)/) {
85 last if ($count >= 10);
89 &msg($who, "error: could not SAR properly.");
90 &DEBUG("error: lhs => '$lhs' rhs => '$rhs'.");
94 my $z = join(',',@list);
97 &status("Update: new lhs => '$lhs' rhs => '$rhs'.");
101 my $exists = &getFactoid($lhs);
104 # nice 'are' hack (or work-around).
105 if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
106 &status("Update: 'are' hack detected.");
108 $rhs = "<REPLY> are ". $rhs;
111 &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
114 &performAddressedReply('okay');
116 &sqlInsert('factoids', {
118 created_time => time(), # modified time.
120 factoid_value => $rhs,
123 if (!defined $rhs or $rhs eq '') {
124 &ERROR("Update: rhs1 == NULL.");
131 if ($exists eq $rhs) {
132 # this catches the following situation: (right or wrong?)
134 # "test is also test"
135 &performAddressedReply("i already had it that way");
139 if ($also) { # 'is also'.
142 while ($exists =~ /^<REPLY> ?see (.*)/i) {
144 unless ($redircount) {
145 &msg($who, "$origlhs has too many levels of redirection.");
150 $exists = &getFactoid($lhs);
152 &msg($who, "$1 is a dangling redirection.");
156 if ($exists =~ /^<REPLY> ?see (.*)/i) {
157 &TODO("Update.pl: append to linked factoid.");
160 if ($also_or) { # 'is also ||'.
161 $rhs = $exists.' || '.$rhs;
163 # if ($exists =~ s/\,\s*$/, /) {
164 if ($exists =~ /\,\s*$/) {
165 &DEBUG("current has trailing comma, just append as is");
166 &DEBUG("Up: exists => $exists");
167 &DEBUG("Up: rhs => $rhs");
169 # $rhs = $exists." ".$rhs; # keep comma.
172 if ($exists =~ /\.\s*$/) {
173 &DEBUG("current has trailing period, just append as is with 2 WS");
174 &DEBUG("Up: exists => $exists");
175 &DEBUG("Up: rhs => $rhs");
178 # $rhs = $exists." ".$rhs; # keep comma.
181 if ($rhs =~ /^[A-Z]/) {
182 if ($rhs =~ /\w+\s*$/) {
183 &status("auto insert period to factoid.");
184 $rhs = $exists.". ".$rhs;
185 } else { # '?' or '.' assumed at end.
186 &status("orig factoid already had trailing symbol; not adding period.");
187 $rhs = $exists." ".$rhs;
189 } elsif ($exists =~ /[\,\.\-]\s*$/) {
190 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
191 $rhs = $exists." ".$rhs;
192 } elsif ($rhs =~ /^\./) {
193 &VERB("U: new text has ^.; appending directly",2);
196 $rhs = $exists.', or '.$rhs;
200 # max length check again.
201 if (length $rhs > $param{'maxDataSize'}) {
202 if (length $rhs > length $exists) {
203 &performAddressedReply("that's too long");
206 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
210 &performAddressedReply('okay');
213 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
214 &sqlSet('factoids', {'factoid_key' => $lhs}, {
216 modified_time => time(),
217 factoid_value => $rhs,
220 if (!defined $rhs or $rhs eq '') {
221 &ERROR("Update: rhs1 == NULL.");
223 } else { # not 'also'
225 if (!$correction_plausible) { # "no, blah is ..."
227 &performStrictReply("...but \002$lhs\002 is already something else...");
228 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
233 my $author = &getFactInfo($lhs, 'created_by') || '';
235 if (IsFlag('m') ne 'm' && IsFlag('o') ne 'o' &&
236 $author !~ /^\Q$who\E\!/i
238 &msg($who, "you can't change that factoid.");
242 &performAddressedReply('okay');
245 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
247 &sqlSet('factoids', {'factoid_key' => $lhs}, {
249 modified_time => time(),
250 factoid_value => $rhs,
253 if (!defined $rhs or $rhs eq '') {
254 &ERROR("Update: rhs1 == NULL.");
263 # vim:ts=4:sw=4:expandtab:tw=80