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