]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
- Remaining files that were changed due to removal of $noreply or
[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     $lhs =~ s/^i (heard|think) //i;
15     $lhs =~ s/^some(one|1|body) said //i;
16     $lhs =~ s/\s+/ /g;
17
18     # locked.
19     return if (&IsLocked($lhs) == 1);
20
21     # profanity.
22     if (&IsParam("profanityCheck") and &hasProfanity($rhs)) {
23         &msg($who, "please, watch your language.");
24         return;
25     }
26
27     # teaching.
28     if (&IsFlag("t") ne "t") {
29         &msg($who, "permission denied.");
30         &status("alert: $who wanted to teach me.");
31         return;
32     }
33
34     # invalid verb.
35     if ($mhs !~ /^(is|are)$/i) {
36         &ERROR("UNKNOWN verb: $mhs.");
37         return;
38     }
39
40     # check if the arguments are too long to be stored in our table.
41     if (length($lhs) > $param{'maxKeySize'} or 
42         length($rhs) > $param{'maxDataSize'})
43     {
44         &performAddressedReply("that's too long");
45         return;
46     }
47
48     #
49     # lets do it...
50     #
51
52     my $also    = ($rhs =~ s/^also //i);
53     my $also_or = ($also and $rhs =~ s/\s+(or|\|\|)\s+//);
54
55     if (&IsParam("freshmeatForFactoid")) {
56         if (&dbGet("freshmeat", "name", $lhs, "name")) {
57             &msg($who, "permission denied. (freshmeat)");
58             &status("alert: $who wanted to teach me something that freshmeat already has info on.");
59             return;
60         }
61     }
62
63     if (my $exists = &getFactoid($lhs)) {       # factoid exists.
64         if ($exists eq $rhs) {
65             &performAddressedReply("i already had it that way");
66             return;
67         }
68
69         if ($also) {                    # 'is also'.
70             if ($also_or) {                     # 'is also ||'.
71                 $rhs = $exists.' || '.$rhs;
72             } else {
73 #               if ($exists =~ s/\,\s*$/,  /) {
74                 if ($exists =~ /\,\s*$/) {
75                     &DEBUG("current has trailing comma, just append as is");
76                     # $rhs =~ s/^\s+//;
77                     # $rhs = $exists." ".$rhs;  # keep comma.
78                 }
79
80                 if ($exists =~ /\.\s*$/) {
81                     &DEBUG("current has trailing period, just append as is with 2 WS");
82                     # $rhs =~ s/^\s+//;
83                     # use ucfirst();?
84                     # $rhs = $exists."  ".$rhs; # keep comma.
85                 }
86
87                 if ($rhs =~ /^[A-Z]/) {
88                     if ($rhs =~ /\w+\s*$/) {
89                         &status("auto insert period to factoid.");
90                         $rhs = $exists.".  ".$rhs;
91                     } else {    # '?' or '.' assumed at end.
92                         &status("orig factoid already had trailing symbol; not adding period.");
93                         $rhs = $exists."  ".$rhs;
94                     }
95                 } elsif ($exists =~ /[\,\.\-]\s*$/) {
96                     &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
97                     $rhs = $exists." ".$rhs;
98                 } elsif ($rhs =~ /^\./) {
99                     &VERB("U: new text has ^.; appending directly",2);
100                     $rhs = $exists.$rhs;
101                 } else {
102                     $rhs = $exists.', or '.$rhs;
103                 }
104             }
105
106             # max length check again.
107             if (length($rhs) > $param{'maxDataSize'}) {
108                 if (length($rhs) > length($exists)) {
109                     &performAddressedReply("that's too long");
110                     return;
111                 } else {
112                     &status("Update: new length is still longer than maxDataSize but less than before, we'll let it go.");
113                 }
114             }
115
116             &performAddressedReply("okay");
117
118             $count{'Update'}++;
119             &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
120             &AddModified($lhs,$nuh);
121             &setFactInfo($lhs, "factoid_value", $rhs);
122
123             if (!defined $rhs or $rhs eq "") {
124                 &ERROR("Update: rhs1 == NULL.");
125             }
126         } else {                                # not "also"
127             if ($correction_plausible) {        # "no, blah is ..."
128                 my $author = &getFactInfo($lhs, "created_by") || "";
129
130                 if (IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
131                     &msg($who, "you can't change that factoid.");
132                     return;
133                 }
134
135                 &performAddressedReply("okay");
136
137                 $count{'Update'}++;
138                 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
139
140                 &delFactoid($lhs);
141                 &setFactInfo($lhs,"created_by", $nuh);
142                 &setFactInfo($lhs,"created_time", time());
143                 &setFactInfo($lhs,"factoid_value", $rhs);
144
145                 if (!defined $rhs or $rhs eq "") {
146                     &ERROR("Update: rhs1 == NULL.");
147                 }
148
149             } else {                     # "blah is ..."
150                 if ($addressed) {
151                     &performStrictReply("...but \002$lhs\002 is already something else...");
152                     &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
153                 }
154                 return;
155             }
156         }
157     } else {                    # not exists.
158
159         # nice 'are' hack (or work-around).
160         if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
161             &DEBUG("Update: 'are' hack detected.");
162             $mhs = "is";
163             $rhs = "<REPLY> are ". $rhs;
164         }
165
166         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
167         $count{'Update'}++;
168
169         &performAddressedReply("okay");
170
171         &setFactInfo($lhs,"created_by", $nuh);
172         &setFactInfo($lhs,"created_time", time());
173         &setFactInfo($lhs,"factoid_value", $rhs);
174
175         if (!defined $rhs or $rhs eq "") {
176             &ERROR("Update: rhs1 == NULL.");
177         }
178     }
179
180     return "$lhs $mhs $rhs";
181 }
182
183 1;