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