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'
62 and &IsFlag('M') ne 'M'
63 and &IsFlag('o') ne 'o' )
65 &performReply("You do not have permission to modify factoids");
68 # If they have +M but they didnt create the factoid
70 elsif ( &IsFlag('M') eq 'M'
71 and $who !~ /^\Q$created_by\E$/i
72 and &IsFlag('m') ne 'm'
73 and &IsFlag('o') ne 'o' )
75 &performReply("factoid '$lhs' is not yours to modify.");
80 # factoid arguments handler.
81 # must start with a non-variable
82 if ( &IsChanConf('factoidArguments') > 0 and $lhs =~ /^[^\$]+.*\$/ ) {
83 &status("Update: Factoid Arguments found.");
84 &status("Update: orig lhs => '$lhs'.");
85 &status("Update: orig rhs => '$rhs'.");
90 while ( $lhs =~ s/\$(\S+)/(.*?)/ ) {
91 push( @list, "\$$1" );
93 last if ( $count >= 10 );
97 &msg( $who, "error: could not SAR properly." );
98 &DEBUG("error: lhs => '$lhs' rhs => '$rhs'.");
102 my $z = join( ',', @list );
105 &status("Update: new lhs => '$lhs' rhs => '$rhs'.");
109 my $exists = &getFactoid($lhs);
113 # nice 'are' hack (or work-around).
114 if ( $mhs =~ /^are$/i and $rhs !~ /<\S+>/ ) {
115 &status("Update: 'are' hack detected.");
117 $rhs = "<REPLY> are " . $rhs;
120 &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
123 &performAddressedReply('okay');
129 created_time => time(), # modified time.
131 factoid_value => $rhs,
135 if ( !defined $rhs or $rhs eq '' ) {
136 &ERROR("Update: rhs1 == NULL.");
143 if ( $exists eq $rhs ) {
145 # this catches the following situation: (right or wrong?)
147 # "test is also test"
148 &performAddressedReply("i already had it that way");
152 if ($also) { # 'is also'.
155 while ( $exists =~ /^<REPLY> ?see (.*)/i ) {
157 unless ($redircount) {
158 &msg( $who, "$origlhs has too many levels of redirection." );
163 $exists = &getFactoid($lhs);
165 &msg( $who, "$1 is a dangling redirection." );
169 if ( $exists =~ /^<REPLY> ?see (.*)/i ) {
170 &TODO("Update.pl: append to linked factoid.");
173 if ($also_or) { # 'is also ||'.
174 $rhs = $exists . ' || ' . $rhs;
178 # if ($exists =~ s/\,\s*$/, /) {
179 if ( $exists =~ /\,\s*$/ ) {
180 &DEBUG("current has trailing comma, just append as is");
181 &DEBUG("Up: exists => $exists");
182 &DEBUG("Up: rhs => $rhs");
185 # $rhs = $exists." ".$rhs; # keep comma.
188 if ( $exists =~ /\.\s*$/ ) {
190 "current has trailing period, just append as is with 2 WS");
191 &DEBUG("Up: exists => $exists");
192 &DEBUG("Up: rhs => $rhs");
196 # $rhs = $exists." ".$rhs; # keep comma.
199 if ( $rhs =~ /^[A-Z]/ ) {
200 if ( $rhs =~ /\w+\s*$/ ) {
201 &status("auto insert period to factoid.");
202 $rhs = $exists . ". " . $rhs;
204 else { # '?' or '.' assumed at end.
206 "orig factoid already had trailing symbol; not adding period."
208 $rhs = $exists . " " . $rhs;
211 elsif ( $exists =~ /[\,\.\-]\s*$/ ) {
213 "U: current has trailing symbols; inserting whitespace + new.",
216 $rhs = $exists . " " . $rhs;
218 elsif ( $rhs =~ /^\./ ) {
219 &VERB( "U: new text has ^.; appending directly", 2 );
220 $rhs = $exists . $rhs;
223 $rhs = $exists . ', or ' . $rhs;
227 # max length check again.
228 if ( length $rhs > $param{'maxDataSize'} ) {
229 if ( length $rhs > length $exists ) {
230 &performAddressedReply("that's too long");
235 "Update: new length is still longer than maxDataSize but less than before, we'll let it go."
240 &performAddressedReply('okay');
243 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
246 { 'factoid_key' => $lhs },
249 modified_time => time(),
250 factoid_value => $rhs,
254 if ( !defined $rhs or $rhs eq '' ) {
255 &ERROR("Update: rhs1 == NULL.");
260 if ( !$correction_plausible ) { # "no, blah is ..."
263 "...but \002$lhs\002 is already something else...");
264 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
269 my $author = &getFactInfo( $lhs, 'created_by' ) || '';
271 if ( IsFlag('m') ne 'm'
272 && IsFlag('o') ne 'o'
273 && $author !~ /^\Q$who\E\!/i )
275 &msg( $who, "you can't change that factoid." );
279 &performAddressedReply('okay');
282 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
286 { 'factoid_key' => $lhs },
289 modified_time => time(),
290 factoid_value => $rhs,
294 if ( !defined $rhs or $rhs eq '' ) {
295 &ERROR("Update: rhs1 == NULL.");
304 # vim:ts=4:sw=4:expandtab:tw=80