]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
* Minor fix to some long lines perl comlained about
[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     if ($also or $also_or) {
56         my $author  = &getFactInfo($from, 'created_by');
57         $author =~ /^(.*)!/;
58         my $created_by = $1;
59
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");
63             return 1;
64
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.");
68             return 1;
69         }
70     }
71
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'.");
78
79         my @list;
80         my $count = 0;
81         $lhs =~ s/^/cmd: /;
82         while ($lhs =~ s/\$(\S+)/(.*?)/) {
83             push(@list, "\$$1");
84             $count++;
85             last if ($count >= 10);
86         }
87
88         if ($count >= 10) {
89             &msg($who, "error: could not SAR properly.");
90             &DEBUG("error: lhs => '$lhs' rhs => '$rhs'.");
91             return;
92         }
93
94         my $z = join(',',@list);
95         $rhs =~ s/^/($z): /;
96
97         &status("Update: new lhs => '$lhs' rhs => '$rhs'.");
98     }
99
100     # the fun begins.
101     my $exists = &getFactoid($lhs);
102
103     if (!$exists) {
104         # nice 'are' hack (or work-around).
105         if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
106             &status("Update: 'are' hack detected.");
107             $mhs = 'is';
108             $rhs = "<REPLY> are ". $rhs;
109         }
110
111         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
112         $count{'Update'}++;
113
114         &performAddressedReply('okay');
115
116         &sqlInsert('factoids', {
117                 created_by      => $nuh,
118                 created_time    => time(),      # modified time.
119                 factoid_key     => $lhs,
120                 factoid_value   => $rhs,
121         } );
122
123         if (!defined $rhs or $rhs eq '') {
124             &ERROR("Update: rhs1 == NULL.");
125         }
126
127         return 1;
128     }
129
130     # factoid exists.
131     if ($exists eq $rhs) {
132         # this catches the following situation: (right or wrong?)
133         #    "test is test"
134         #    "test is also test"
135         &performAddressedReply("i already had it that way");
136         return 1;
137     }
138
139     if ($also) {                        # 'is also'.
140         my $redircount = 5;
141         my $origlhs = $lhs;
142         while ($exists =~ /^<REPLY> ?see (.*)/i) {
143             $redircount--;
144             unless ($redircount) {
145                 &msg($who, "$origlhs has too many levels of redirection.");
146                 return 1;
147             }
148
149             $lhs = $1;
150             $exists = &getFactoid($lhs);
151             unless( $exists ) {
152                 &msg($who, "$1 is a dangling redirection.");
153                 return 1;
154             }
155         }
156         if ($exists =~ /^<REPLY> ?see (.*)/i) {
157             &TODO("Update.pl: append to linked factoid.");
158         }
159
160         if ($also_or) {                 # 'is also ||'.
161             $rhs = $exists.' || '.$rhs;
162         } else {
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");
168                 # $rhs =~ s/^\s+//;
169                 # $rhs = $exists." ".$rhs;      # keep comma.
170             }
171
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");
176                 # $rhs =~ s/^\s+//;
177                 # use ucfirst();?
178                 # $rhs = $exists."  ".$rhs;     # keep comma.
179             }
180
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;
188                 }
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);
194                 $rhs = $exists.$rhs;
195             } else {
196                 $rhs = $exists.', or '.$rhs;
197             }
198         }
199
200         # max length check again.
201         if (length $rhs > $param{'maxDataSize'}) {
202             if (length $rhs > length $exists) {
203                 &performAddressedReply("that's too long");
204                 return 1;
205             } else {
206                 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
207             }
208         }
209
210         &performAddressedReply('okay');
211
212         $count{'Update'}++;
213         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
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     } else {                            # not 'also'
224
225         if (!$correction_plausible) {   # "no, blah is ..."
226             if ($addressed) {
227                 &performStrictReply("...but \002$lhs\002 is already something else...");
228                 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
229             }
230             return 1;
231         }
232
233         my $author = &getFactInfo($lhs, 'created_by') || '';
234
235         if (IsFlag('m') ne 'm' && IsFlag('o') ne 'o' &&
236             $author !~ /^\Q$who\E\!/i
237         ) {
238             &msg($who, "you can't change that factoid.");
239             return 1;
240         }
241
242         &performAddressedReply('okay');
243
244         $count{'Update'}++;
245         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
246
247         &sqlSet('factoids', {'factoid_key' => $lhs}, {
248                 modified_by     => $nuh,
249                 modified_time   => time(),
250                 factoid_value   => $rhs,
251         } );
252
253         if (!defined $rhs or $rhs eq '') {
254             &ERROR("Update: rhs1 == NULL.");
255         }
256     }
257
258     return 1;
259 }
260
261 1;
262
263 # vim:ts=4:sw=4:expandtab:tw=80