]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
- moved scripts/setup_sql.pl to src/db_mysql as &createTables()
[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", $lhs, "name")) {
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     if (&IsChanConf("factoidArguments") and $lhs =~ /\$/) {
66         &status("Update: Factoid Arguments found.");
67         &status("Update: orig lhs => '$lhs'.");
68         &status("Update: orig rhs => '$rhs'.");
69         $lhs =~ s/^/CMD: /;
70         my @list;
71         while ($lhs =~ s/\$(\S+)/(.*?)/) {
72             push(@list, "\$$1");
73         }
74         my $z = join(',',@list);
75         $rhs =~ s/^/($z): /;
76
77         &status("Update: new  lhs => '$lhs'.");
78         &status("Update: new  rhs => '$rhs'.");
79     }
80
81     # the fun begins.
82     my $exists = &getFactoid($lhs);
83
84     if (!$exists) {
85         # nice 'are' hack (or work-around).
86         if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
87             &status("Update: 'are' hack detected.");
88             $mhs = "is";
89             $rhs = "<REPLY> are ". $rhs;
90         }
91
92         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
93         $count{'Update'}++;
94
95         &performAddressedReply("okay");
96
97         &setFactInfo($lhs,"created_by", $nuh);
98         &setFactInfo($lhs,"created_time", time());
99         &setFactInfo($lhs,"factoid_value", $rhs);
100
101         if (!defined $rhs or $rhs eq "") {
102             &ERROR("Update: rhs1 == NULL.");
103         }
104
105         return 1;
106     }
107
108     # factoid exists.
109     if ($exists eq $rhs) {
110         &performAddressedReply("i already had it that way");
111         return 1;
112     }
113
114     if ($also) {                        # 'is also'.
115         if ($exists =~ /^<REPLY> see /i) {
116             &DEBUG("todo: append to linked factoid.");
117         }
118
119         if ($also_or) {                 # 'is also ||'.
120             $rhs = $exists.' || '.$rhs;
121         } else {
122 #           if ($exists =~ s/\,\s*$/,  /) {
123             if ($exists =~ /\,\s*$/) {
124                 &DEBUG("current has trailing comma, just append as is");
125                 &DEBUG("Up: exists => $exists");
126                 &DEBUG("Up: rhs    => $rhs");
127                 # $rhs =~ s/^\s+//;
128                 # $rhs = $exists." ".$rhs;      # keep comma.
129             }
130
131             if ($exists =~ /\.\s*$/) {
132                 &DEBUG("current has trailing period, just append as is with 2 WS");
133                 &DEBUG("Up: exists => $exists");
134                 &DEBUG("Up: rhs    => $rhs");
135                 # $rhs =~ s/^\s+//;
136                 # use ucfirst();?
137                 # $rhs = $exists."  ".$rhs;     # keep comma.
138             }
139
140             if ($rhs =~ /^[A-Z]/) {
141                 if ($rhs =~ /\w+\s*$/) {
142                     &status("auto insert period to factoid.");
143                     $rhs = $exists.".  ".$rhs;
144                 } else {        # '?' or '.' assumed at end.
145                     &status("orig factoid already had trailing symbol; not adding period.");
146                     $rhs = $exists."  ".$rhs;
147                 }
148             } elsif ($exists =~ /[\,\.\-]\s*$/) {
149                 &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
150                 $rhs = $exists." ".$rhs;
151             } elsif ($rhs =~ /^\./) {
152                 &VERB("U: new text has ^.; appending directly",2);
153                 $rhs = $exists.$rhs;
154             } else {
155                 $rhs = $exists.', or '.$rhs;
156             }
157         }
158
159         # max length check again.
160         if (length $rhs > $param{'maxDataSize'}) {
161             if (length $rhs > length $exists) {
162                 &performAddressedReply("that's too long");
163                 return 1;
164             } else {
165                 &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
166             }
167         }
168
169         &performAddressedReply("okay");
170
171         $count{'Update'}++;
172         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
173         &AddModified($lhs,$nuh);
174         &setFactInfo($lhs, "factoid_value", $rhs);
175
176         if (!defined $rhs or $rhs eq "") {
177             &ERROR("Update: rhs1 == NULL.");
178         }
179     } else {                            # not "also"
180
181         if (!$correction_plausible) {   # "no, blah is ..."
182             if ($addressed) {
183                 &performStrictReply("...but \002$lhs\002 is already something else...");
184                 &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
185             }
186             return;
187         }
188
189         my $author = &getFactInfo($lhs, "created_by") || "";
190
191         if (IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
192             &msg($who, "you can't change that factoid.");
193             return 1;
194         }
195
196         &performAddressedReply("okay");
197
198         $count{'Update'}++;
199         &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
200
201         &delFactoid($lhs);
202         &setFactInfo($lhs,"created_by", $nuh);
203         &setFactInfo($lhs,"created_time", time());
204         &setFactInfo($lhs,"factoid_value", $rhs);
205
206         if (!defined $rhs or $rhs eq "") {
207             &ERROR("Update: rhs1 == NULL.");
208         }
209     }
210
211     return 1;
212 }
213
214 1;