]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
take a few more things literally
[infobot.git] / src / Factoids / Update.pl
1 #
2 # Update.pl: Add or modify factoids in the db.
3 #    Author: Kevin Lenzo
4 #            dms
5 #   Version: 19991209
6 #   Created: 1997
7 #
8
9 # use strict;   # TODO
10
11 sub update {
12     my($lhs, $mhs, $rhs) = @_;
13
14     for ($lhs) {
15         s/^i (heard|think) //i;
16         s/^some(one|1|body) said //i;
17         s/\s+/ /g;
18     }
19
20     # locked.
21     return if (&IsLocked($lhs) == 1);
22
23     # profanity.
24     if (&IsParam('profanityCheck') and &hasProfanity($rhs)) {
25         &performReply("please, watch your language.");
26         return 1;
27     }
28
29     # teaching.
30     if (&IsFlag('t') ne 't' && &IsFlag('o') ne 'o') {
31         &msg($who, "permission denied.");
32         &status("alert: $who wanted to teach me.");
33         return 1;
34     }
35
36     # invalid verb.
37     if ($mhs !~ /^(is|are)$/i) {
38         &ERROR("UNKNOWN verb: $mhs.");
39         return;
40     }
41
42     # check if the arguments are too long to be stored in our table.
43     my $toolong = 0;
44     $toolong++  if (length $lhs > $param{'maxKeySize'});
45     $toolong++  if (length $rhs > $param{'maxDataSize'});
46     if ($toolong) {
47         &performAddressedReply("that's too long");
48         return 1;
49     }
50
51     # also checking.
52     my $also    = ($rhs =~ s/^-?also //i);
53     my $also_or = ($also and $rhs =~ s/\s+(or|\|\|)\s+//);
54
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'.");
61
62         my @list;
63         my $count = 0;
64         $lhs =~ s/^/cmd: /;
65         while ($lhs =~ s/\$(\S+)/(.*?)/) {
66             push(@list, "\$$1");
67             $count++;
68             last if ($count >= 10);
69         }
70
71         if ($count >= 10) {
72             &msg($who, "error: could not SAR properly.");
73             &DEBUG("error: lhs => '$lhs' rhs => '$rhs'.");
74             return;
75         }
76
77         my $z = join(',',@list);
78         $rhs =~ s/^/($z): /;
79
80         &status("Update: new lhs => '$lhs' rhs => '$rhs'.");
81     }
82
83     # the fun begins.
84     my $exists = &getFactoid($lhs);
85
86     if (!$exists) {
87         # nice 'are' hack (or work-around).
88         if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
89             &status("Update: 'are' hack detected.");
90             $mhs = 'is';
91             $rhs = "<REPLY> are ". $rhs;
92         }
93
94         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
95         $count{'Update'}++;
96
97         &performAddressedReply('okay');
98
99         &sqlInsert('factoids', {
100                 created_by      => $nuh,
101                 created_time    => time(),      # modified time.
102                 factoid_key     => $lhs,
103                 factoid_value   => $rhs,
104         } );
105
106         if (!defined $rhs or $rhs eq '') {
107             &ERROR("Update: rhs1 == NULL.");
108         }
109
110         return 1;
111     }
112
113     # factoid exists.
114     if ($exists eq $rhs) {
115         # this catches the following situation: (right or wrong?)
116         #    "test is test"
117         #    "test is also test"
118         &performAddressedReply("i already had it that way");
119         return 1;
120     }
121
122     if ($also) {                        # 'is also'.
123         if ($exists =~ /^<REPLY> see /i) {
124             &TODO("Update.pl: append to linked factoid.");
125         }
126
127         if ($also_or) {                 # 'is also ||'.
128             $rhs = $exists.' || '.$rhs;
129         } else {
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");
135                 # $rhs =~ s/^\s+//;
136                 # $rhs = $exists." ".$rhs;      # keep comma.
137             }
138
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");
143                 # $rhs =~ s/^\s+//;
144                 # use ucfirst();?
145                 # $rhs = $exists."  ".$rhs;     # keep comma.
146             }
147
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;
155                 }
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);
161                 $rhs = $exists.$rhs;
162             } else {
163                 $rhs = $exists.', or '.$rhs;
164             }
165         }
166
167         # max length check again.
168         if (length $rhs > $param{'maxDataSize'}) {
169             if (length $rhs > length $exists) {
170                 &performAddressedReply("that's too long");
171                 return 1;
172             } else {
173                 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
174             }
175         }
176
177         &performAddressedReply('okay');
178
179         $count{'Update'}++;
180         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
181         &sqlSet('factoids', {'factoid_key' => $lhs}, {
182                 modified_by     => $nuh,
183                 modified_time   => time(),
184                 factoid_value   => $rhs,
185         } );
186
187         if (!defined $rhs or $rhs eq '') {
188             &ERROR("Update: rhs1 == NULL.");
189         }
190     } else {                            # not 'also'
191
192         if (!$correction_plausible) {   # "no, blah is ..."
193             if ($addressed) {
194                 &performStrictReply("...but \002$lhs\002 is already something else...");
195                 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
196             }
197             return 1;
198         }
199
200         my $author = &getFactInfo($lhs, 'created_by') || '';
201
202         if (IsFlag('m') ne 'm' && IsFlag('o') ne 'o' &&
203             $author !~ /^\Q$who\E\!/i
204         ) {
205             &msg($who, "you can't change that factoid.");
206             return 1;
207         }
208
209         &performAddressedReply('okay');
210
211         $count{'Update'}++;
212         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
213
214         &sqlSet('factoids', {'factoid_key' => $lhs}, {
215                 modified_by     => $nuh,
216                 modified_time   => time(),
217                 factoid_value   => $rhs,
218         } );
219
220         if (!defined $rhs or $rhs eq '') {
221             &ERROR("Update: rhs1 == NULL.");
222         }
223     }
224
225     return 1;
226 }
227
228 1;