]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
07b8ec6e1c9bbaa413f6dcb15af7e76833c09b2f
[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 if (&IsParam("useStrict")) { use strict; }
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") {
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         if (0) {        # old
111             &setFactInfo($lhs, "factoid_value", $rhs);
112             &setFactInfo($lhs, "created_by",    $nuh);
113             &setFactInfo($lhs, "created_time",  time());
114         } else {
115             &dbReplace("factoids", "factoid_key", (
116                 created_by      => $nuh,
117                 created_time    => time(),      # modified time.
118                 factoid_key     => $lhs,
119                 factoid_value   => $rhs,
120             ) );
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         if ($exists =~ /^<REPLY> see /i) {
141             &DEBUG("Update.pl: todo: append to linked factoid.");
142         }
143
144         if ($also_or) {                 # 'is also ||'.
145             $rhs = $exists.' || '.$rhs;
146         } else {
147 #           if ($exists =~ s/\,\s*$/,  /) {
148             if ($exists =~ /\,\s*$/) {
149                 &DEBUG("current has trailing comma, just append as is");
150                 &DEBUG("Up: exists => $exists");
151                 &DEBUG("Up: rhs    => $rhs");
152                 # $rhs =~ s/^\s+//;
153                 # $rhs = $exists." ".$rhs;      # keep comma.
154             }
155
156             if ($exists =~ /\.\s*$/) {
157                 &DEBUG("current has trailing period, just append as is with 2 WS");
158                 &DEBUG("Up: exists => $exists");
159                 &DEBUG("Up: rhs    => $rhs");
160                 # $rhs =~ s/^\s+//;
161                 # use ucfirst();?
162                 # $rhs = $exists."  ".$rhs;     # keep comma.
163             }
164
165             if ($rhs =~ /^[A-Z]/) {
166                 if ($rhs =~ /\w+\s*$/) {
167                     &status("auto insert period to factoid.");
168                     $rhs = $exists.".  ".$rhs;
169                 } else {        # '?' or '.' assumed at end.
170                     &status("orig factoid already had trailing symbol; not adding period.");
171                     $rhs = $exists."  ".$rhs;
172                 }
173             } elsif ($exists =~ /[\,\.\-]\s*$/) {
174                 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
175                 $rhs = $exists." ".$rhs;
176             } elsif ($rhs =~ /^\./) {
177                 &VERB("U: new text has ^.; appending directly",2);
178                 $rhs = $exists.$rhs;
179             } else {
180                 $rhs = $exists.', or '.$rhs;
181             }
182         }
183
184         # max length check again.
185         if (length $rhs > $param{'maxDataSize'}) {
186             if (length $rhs > length $exists) {
187                 &performAddressedReply("that's too long");
188                 return 1;
189             } else {
190                 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
191             }
192         }
193
194         &performAddressedReply("okay");
195
196         $count{'Update'}++;
197         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
198         &AddModified($lhs,$nuh);
199         &setFactInfo($lhs, "factoid_value", $rhs);
200
201         if (!defined $rhs or $rhs eq "") {
202             &ERROR("Update: rhs1 == NULL.");
203         }
204     } else {                            # not "also"
205
206         if (!$correction_plausible) {   # "no, blah is ..."
207             if ($addressed) {
208                 &performStrictReply("...but \002$lhs\002 is already something else...");
209                 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
210             }
211             return 1;
212         }
213
214         my $author = &getFactInfo($lhs, "created_by") || "";
215
216         if (IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
217             &msg($who, "you can't change that factoid.");
218             return 1;
219         }
220
221         &performAddressedReply("okay");
222
223         $count{'Update'}++;
224         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
225
226         # should dbReplace be used here?
227         #&delFactoid($lhs); # breaks dbm. leave it and use modified_* - Tim Riker <Tim@Rikers.org>
228         &setFactInfo($lhs,"modified_by", $nuh);
229         &setFactInfo($lhs,"modified_time", time());
230         &setFactInfo($lhs,"factoid_value", $rhs);
231
232         if (!defined $rhs or $rhs eq "") {
233             &ERROR("Update: rhs1 == NULL.");
234         }
235     }
236
237     return 1;
238 }
239
240 1;