]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
closed 17031 -- Fix up appending to factoids
[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 $noreply if (&IsLocked($lhs) == 1);
20
21     # profanity.
22     if (&IsParam("profanityCheck") and &hasProfanity($rhs)) {
23         &msg($who, "please, watch your language.");
24         return $noreply;
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 $noreply;
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 $noreply;
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 $noreply;
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 $noreply;
67         }
68
69         if ($also) {                    # 'is also'.
70             if ($also_or) {                     # 'is also ||'.
71                 $rhs = $exists.' || '.$rhs;
72             } else {
73                 if ($rhs =~ /^[A-Z]/) {
74                     if ($rhs =~ /\w+\s*$/) {
75                         &status("auto insert period to factoid.");
76                         $rhs = $exists.".  ".$rhs;
77                     } else {    # '?' or '.' assumed at end.
78                         &status("orig factoid already had trailing symbol; not adding period.");
79                         $rhs = $exists."  ".$rhs;
80                     }
81                 } elsif ($exists =~ /[\,\.\-]\s*$/) {
82                     &VERB("U: current has trailing symbols; inserting whitespace + new.",2);
83                     $rhs = $exists." ".$rhs;
84                 } elsif ($rhs =~ /^\./) {
85                     &VERB("U: new text has ^.; appending directly",2);
86                     $rhs = $exists.$rhs;
87                 } else {
88                     $rhs = $exists.', or '.$rhs;
89                 }
90             }
91
92             # max length check again.
93             if (length($rhs) > $param{'maxDataSize'}) {
94                 &performAddressedReply("that's too long");
95                 return $noreply;
96             }
97
98             &performAddressedReply("okay");
99
100             $count{'Update'}++;
101             &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
102             &AddModified($lhs,$nuh);
103             &setFactInfo($lhs, "factoid_value", $rhs);
104         } else {                                # not "also"
105             if ($correction_plausible) {        # "no, blah is ..."
106                 my $author = &getFactInfo($lhs, "created_by");
107
108                 &DEBUG("Update: check: '$author' == '$who' ?");
109
110                 if (IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
111                     &msg($who, "you can't change that factoid.");
112                     return $noreply;
113                 }
114
115                 &performAddressedReply("okay");
116
117                 $count{'Update'}++;
118                 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
119
120                 &delFactoid($lhs);
121                 &setFactInfo($lhs,"created_by", $nuh);
122                 &setFactInfo($lhs,"created_time", time());
123                 &setFactInfo($lhs,"factoid_value", $rhs);
124             } else {                     # "blah is ..."
125                 if ($addressed) {
126                     &performStrictReply("...but \002$lhs\002 is already something else...");
127                     &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
128                 }
129                 return $noreply;
130             }
131         }
132     } else {                    # not exists.
133
134         # nice 'are' hack (or work-around).
135         if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
136             &DEBUG("Update: 'are' hack detected.");
137             $mhs = "is";
138             $rhs = "<REPLY> are ". $rhs;
139         }
140
141         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
142         $count{'Update'}++;
143
144         &performAddressedReply("okay");
145
146         &setFactInfo($lhs,"created_by", $nuh);
147         &setFactInfo($lhs,"created_time", time());
148         &setFactInfo($lhs,"factoid_value", $rhs);
149     }
150
151     return "$lhs $mhs $rhs";
152 }
153
154 1;