]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Update.pl
Initial revision
[infobot.git] / src / Factoids / Update.pl
1 #
2 # Update.pl: Add or modify factoids in the db.
3 #    Author: Kevin Lenzo
4 #            xk <xk@leguin.openprojects.net>
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     # nice 'are' hack (or work-around).
35     if ($mhs =~ /^are$/i and $rhs !~ /<\S+>/) {
36         $mhs = "is";
37         $rhs = "<REPLY> are ". $rhs;
38     }
39
40     # invalid verb.
41     if ($mhs !~ /^is$/i) {
42         &ERROR("UNKNOWN verb: $mhs.");
43         return;
44     }
45
46     # check if the arguments are too long to be stored in our table.
47     if (length($lhs) > $param{'maxKeySize'} or 
48         length($rhs) > $param{'maxDataSize'})
49     {
50         &performAddressedReply("that's too long");
51         return 'NOREPLY';
52     }
53
54     #
55     # lets do it...
56     #
57
58     my $also    = ($rhs =~ s/^also //i);
59     my $also_or = ($also and $rhs =~ s/\s+(or|\|\|)\s+//);
60
61     if (&IsParam("freshmeatForFactoid")) {
62         if (&dbGet("freshmeat", "name", $lhs, "name")) {
63             &msg($who, "permission denied. (freshmeat)");
64             &status("alert: $who wanted to teach me something that freshmeat already has info on.");
65             return 'NOREPLY';
66         }
67     }
68
69     if (my $exists = &getFactoid($lhs)) {       # factoid exists.
70         chomp $exists;
71
72         if ($exists eq $rhs) {
73             &performAddressedReply("i already had it that way");
74             return 'NOREPLY';
75         }
76
77         if ($also) {                    # 'is also'.
78             if ($also_or) {                     # 'is also ||'.
79                 $rhs = $exists.' || '.$rhs;
80             } else {
81                 if ($rhs =~ /^[A-Z]/) {
82                     if ($rhs =~ /\w+\s*$/) {
83                         &status("auto insert period to factoid.");
84                         $rhs = $exists.".  ".$rhs;
85                     } else {    # '?' or '.' assumed at end.
86                         &status("orig factoid already had trailing symbol; not adding period.");
87                         $rhs = $exists."  ".$rhs;
88                     }
89                 } elsif ($exists =~ /\,\s*$/) {
90                     $rhs = $exists." ".$rhs;
91                 } elsif ($rhs =~ /^\./) {
92                     $rhs = $exists.$rhs;
93                 } else {
94                     $rhs = $exists.', or '.$rhs;
95                 }
96             }
97
98             # max length check again.
99             if (length($rhs) > $param{'maxDataSize'}) {
100                 &performAddressedReply("that's too long");
101                 return 'NOREPLY';
102             }
103
104             &performAddressedReply("okay");
105
106             $count{'Update'}++;
107             &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
108             &AddModified($lhs,$nuh);
109             &setFactInfo($lhs, "factoid_value", $rhs);
110         } else {                                # not "also"
111             if ($correction_plausible) {        # "no, blah is ..."
112                 my $author = &getFactInfo($lhs, "created_by");
113
114                 &DEBUG("Update: check: '$author' == '$who' ?");
115
116                 if (IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
117                     &msg($who, "you can't change that factoid.");
118                     return 'NOREPLY';
119                 }
120
121                 &performAddressedReply("okay");
122
123                 $count{'Update'}++;
124                 &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'");
125
126                 &delFactoid($lhs);
127                 &setFactInfo($lhs,"created_by", $nuh);
128                 &setFactInfo($lhs,"created_time", time());
129                 &setFactInfo($lhs,"factoid_value", $rhs);
130             } else {                     # "blah is ..."
131                 if ($addressed) {
132                     &performStrictReply("...but \002$lhs\002 is already something else...");
133                     &status("FAILED update: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
134                 }
135                 return 'NOREPLY';
136             }
137         }
138     } else {                    # not exists.
139         &status("enter: <$who> \'$lhs\' =$mhs=> \'$rhs\'");
140         $count{'Update'}++;
141
142         &performAddressedReply("okay");
143
144         &setFactInfo($lhs,"created_by", $nuh);
145         &setFactInfo($lhs,"created_time", time());
146         &setFactInfo($lhs,"factoid_value", $rhs);
147     }
148
149     return "$lhs $mhs $rhs";
150 }
151
152 1;