]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
- strictify
[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     # freshmeat
56     if (&IsChanConf("freshmeatForFactoid")) {
57         # todo: "name" is invalid for fm ][
58         if ( &dbGet("freshmeat", "name", "name=".&dbQuote($lhs)) ) {
59             &msg($who, "permission denied. (freshmeat)");
60             &status("alert: $who wanted to teach me something that freshmeat already has info on.");
61             return 1;
62         }
63     }
64
65     # factoid arguments handler.
66     if (&IsChanConf("factoidArguments") and $lhs =~ /\$/) {
67         &status("Update: Factoid Arguments found.");
68         &status("Update: orig lhs => '$lhs'.");
69         &status("Update: orig rhs => '$rhs'.");
70
71         my @list;
72         my $count = 0;
73         $lhs =~ s/^/CMD: /;
74         while ($lhs =~ s/\$(\S+)/(.*?)/) {
75             push(@list, "\$$1");
76             $count++;
77             last if ($count >= 10);
78         }
79
80         if ($count >= 10) {
81             &msg($who, "error: could not SAR properly.");
82             &DEBUG("error: lhs => '$lhs'.");
83             &DEBUG("error: rhs => '$rhs'.");
84             return;
85         }
86
87         my $z = join(',',@list);
88         $rhs =~ s/^/($z): /;
89
90         &status("Update: new  lhs => '$lhs'.");
91         &status("Update: new  rhs => '$rhs'.");
92     }
93
94     # the fun begins.
95     my $exists = &getFactoid($lhs);
96
97     if (!$exists) {
98         # nice 'are' hack (or work-around).
99         if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
100             &status("Update: 'are' hack detected.");
101             $mhs = "is";
102             $rhs = "<REPLY> are ". $rhs;
103         }
104
105         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
106         $count{'Update'}++;
107
108         &performAddressedReply("okay");
109
110         &dbReplace("factoids", "factoid_key", (
111                 created_by      => $nuh,
112                 created_time    => time(),      # modified time.
113                 factoid_key     => $lhs,
114                 factoid_value   => $rhs,
115         ) );
116
117         if (!defined $rhs or $rhs eq "") {
118             &ERROR("Update: rhs1 == NULL.");
119         }
120
121         return 1;
122     }
123
124     # factoid exists.
125     if ($exists eq $rhs) {
126         # this catches the following situation: (right or wrong?)
127         #    "test is test"
128         #    "test is also test"
129         &performAddressedReply("i already had it that way");
130         return 1;
131     }
132
133     if ($also) {                        # 'is also'.
134         if ($exists =~ /^<REPLY> see /i) {
135             &DEBUG("Update.pl: todo: append to linked factoid.");
136         }
137
138         if ($also_or) {                 # 'is also ||'.
139             $rhs = $exists.' || '.$rhs;
140         } else {
141 #           if ($exists =~ s/\,\s*$/,  /) {
142             if ($exists =~ /\,\s*$/) {
143                 &DEBUG("current has trailing comma, just append as is");
144                 &DEBUG("Up: exists => $exists");
145                 &DEBUG("Up: rhs    => $rhs");
146                 # $rhs =~ s/^\s+//;
147                 # $rhs = $exists." ".$rhs;      # keep comma.
148             }
149
150             if ($exists =~ /\.\s*$/) {
151                 &DEBUG("current has trailing period, just append as is with 2 WS");
152                 &DEBUG("Up: exists => $exists");
153                 &DEBUG("Up: rhs    => $rhs");
154                 # $rhs =~ s/^\s+//;
155                 # use ucfirst();?
156                 # $rhs = $exists."  ".$rhs;     # keep comma.
157             }
158
159             if ($rhs =~ /^[A-Z]/) {
160                 if ($rhs =~ /\w+\s*$/) {
161                     &status("auto insert period to factoid.");
162                     $rhs = $exists.".  ".$rhs;
163                 } else {        # '?' or '.' assumed at end.
164                     &status("orig factoid already had trailing symbol; not adding period.");
165                     $rhs = $exists."  ".$rhs;
166                 }
167             } elsif ($exists =~ /[\,\.\-]\s*$/) {
168                 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
169                 $rhs = $exists." ".$rhs;
170             } elsif ($rhs =~ /^\./) {
171                 &VERB("U: new text has ^.; appending directly",2);
172                 $rhs = $exists.$rhs;
173             } else {
174                 $rhs = $exists.', or '.$rhs;
175             }
176         }
177
178         # max length check again.
179         if (length $rhs > $param{'maxDataSize'}) {
180             if (length $rhs > length $exists) {
181                 &performAddressedReply("that's too long");
182                 return 1;
183             } else {
184                 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
185             }
186         }
187
188         &performAddressedReply("okay");
189
190         $count{'Update'}++;
191         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
192         &AddModified($lhs,$nuh);
193         &setFactInfo($lhs, "factoid_value", $rhs);
194
195         if (!defined $rhs or $rhs eq "") {
196             &ERROR("Update: rhs1 == NULL.");
197         }
198     } else {                            # not "also"
199
200         if (!$correction_plausible) {   # "no, blah is ..."
201             if ($addressed) {
202                 &performStrictReply("...but \002$lhs\002 is already something else...");
203                 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
204             }
205             return 1;
206         }
207
208         my $author = &getFactInfo($lhs, "created_by") || "";
209
210         if (IsFlag("m") ne "m" && IsFlag("o") ne "o" &&
211             $author !~ /^\Q$who\E\!/i
212         ) {
213             &msg($who, "you can't change that factoid.");
214             return 1;
215         }
216
217         &performAddressedReply("okay");
218
219         $count{'Update'}++;
220         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
221
222         # should dbReplace be used here?
223         #&delFactoid($lhs); # breaks dbm. leave it and use modified_* - Tim Riker <Tim@Rikers.org>
224         &setFactInfo($lhs,"modified_by", $nuh);
225         &setFactInfo($lhs,"modified_time", time());
226         &setFactInfo($lhs,"factoid_value", $rhs);
227
228         if (!defined $rhs or $rhs eq "") {
229             &ERROR("Update: rhs1 == NULL.");
230         }
231     }
232
233     return 1;
234 }
235
236 1;