]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
- it would help if I had checked before committing to cvs.
[infobot.git] / src / Process.pl
1 ###
2 ### Process.pl: Kevin Lenzo 1997-1999
3 ###
4
5 #
6 # process the incoming message
7 #
8
9 #use strict;
10
11 sub process {
12     $learnok    = 0;    # Able to learn?
13     $talkok     = 0;    # Able to yap?
14     $force_public_reply = 0;
15     $literal    = 0;
16
17     return 'X'                  if $who eq $ident;      # self-message.
18     return 'addressedother set' if ($addressedother);
19
20     $talkok     = ($param{'addressing'} =~ /^OPTIONAL$/i or $addressed);
21     $learnok    = ($param{'learn'}      =~ /^HUNGRY$/i   or $addressed);
22
23     &shmFlush();                # hack.
24
25     # hack to support channel +o as "+o" in bot user file.
26     # requires +O in user file.
27     # is $who arg lowercase?
28     if (exists $channels{$chan}{o}{ $orig{who} } && &IsFlag("O") eq "O") {
29         &status("Gave $who/$chan +o (+O)\'ness");
30         $users{$userHandle}{FLAGS} .= "o";
31     }
32
33     # check if we have our head intact.
34     if ($lobotomized) {
35         if ($addressed and IsFlag("o") eq "o") {
36             my $delta_time      = time() - ($cache{lobotomy}{$who} || 0);
37             &msg($who, "give me an unlobotomy.") if ($delta_time > 60*60);
38             $cache{lobotomy}{$who} = time();
39         }
40         return 'LOBOTOMY';
41     }
42
43     # talkMethod.
44     if ($param{'talkMethod'} =~ /^PRIVATE$/i) {
45         if ($msgType =~ /public/ and $addressed) {
46             &msg($who, "sorry. i'm in 'PRIVATE' talkMethod mode ".
47                   "while you sent a message to me ${msgType}ly.");
48
49             return 'TALKMETHOD';
50         }
51     }
52
53     # join, must be done before outsider checking.
54     if ($message =~ /^join(\s+(.*))?\s*$/i) {
55         return 'join: not addr' unless ($addressed);
56
57         $2 =~ /^($mask{chan})(,(\S+))?/;
58         my($thischan, $key) = (lc $1, $3);
59         my $chankey     = lc $thischan;
60         $chankey        .= " $key"      if (defined $key);
61
62         if ($thischan eq "") {
63             &help("join");
64             return;
65         }
66
67         if (&IsFlag("o") ne "o") {
68             if (!exists $chanconf{$thischan}) {
69                 &msg($who, "I am not allowed to join $thischan.");
70                 return;
71             }
72
73             if (&validChan($thischan)) {
74                 &msg($who,"warn: I'm already on $thischan, joining  anyway...");
75 #               return;
76             }
77         }
78         $cache{join}{$thischan} = $who; # used for on_join self.
79
80         &joinchan($chankey);
81         &status("JOIN $chankey <$who>");
82         &msg($who, "joining $chankey");
83         &joinNextChan();        # hack.
84
85         return;
86     }
87
88     # 'identify'
89     if ($msgType =~ /private/ and $message =~ s/^identify//i) {
90         $message =~ s/^\s+|\s+$//g;
91         my @array = split / /, $message;
92
93         if ($who =~ /^_default$/i) {
94             &pSReply("you are too eleet.");
95             return;
96         }
97
98         if (!scalar @array or scalar @array > 2) {
99             &help("identify");
100             return;
101         }
102
103         my $do_nick = $array[1] || $who;
104
105         if (!exists $users{$do_nick}) {
106             &pSReply("nick $do_nick is not in user list.");
107             return;
108         }
109
110         my $crypt = $users{$do_nick}{PASS};
111         if (!defined $crypt) {
112             &pSReply("user $do_nick has no passwd set.");
113             return;
114         }
115
116         if (!&ckpasswd($array[0], $crypt)) {
117             &pSReply("invalid passwd for $do_nick.");
118             return;
119         }
120
121         my $mask = "*!$user@".&makeHostMask($host);
122         ### TODO: prevent adding multiple dupe masks?
123         ### TODO: make &addHostMask() CMD?
124         &pSReply("Added $mask for $do_nick...");
125         $users{$do_nick}{HOSTS}{$mask} = 1;
126
127         return;
128     }
129
130     # 'pass'
131     if ($msgType =~ /private/ and $message =~ s/^pass//i) {
132         $message =~ s/^\s+|\s+$//g;
133         my @array = split ' ', $message;
134
135         if ($who =~ /^_default$/i) {
136             &pSReply("you are too eleet.");
137             return;
138         }
139
140         if (scalar @array != 1) {
141             &help("pass");
142             return;
143         }
144
145         # todo: use &getUser()?
146         my $first       = 1;
147         foreach (keys %users) {
148             if ($users{$_}{FLAGS} =~ /n/) {
149                 $first = 0;
150                 last;
151             }
152         }
153
154         if (!exists $users{$who} and !$first) {
155             &pSReply("nick $who is not in user list.");
156             return;
157         }
158
159         if ($first) {
160             &pSReply("First time user... adding you as Master.");
161             $users{$who}{FLAGS} = "mrsteon";
162         }
163
164         my $crypt = $users{$who}{PASS};
165         if (defined $crypt) {
166             &pSReply("user $who already has pass set.");
167             return;
168         }
169
170         if (!defined $host) {
171             &WARN("pass: host == NULL.");
172             return;
173         }
174
175         if (!scalar keys %{ $users{$who}{HOSTS} }) {
176             my $mask = "*!$user@".&makeHostMask($host);
177             &pSReply("Added hostmask '\002$mask\002' to $who");
178             $users{$who}{HOSTS}{$mask}  = 1;
179         }
180
181         $crypt                  = &mkcrypt($array[0]);
182         $users{$who}{PASS}      = $crypt;
183         &pSReply("new pass for $who, crypt $crypt.");
184
185         return;
186     }
187
188     # allowOutsiders.
189     if (&IsParam("disallowOutsiders") and $msgType =~ /private/i) {
190         my $found = 0;
191
192         foreach (keys %channels) {
193             next unless (&IsNickInChan($who,$_));
194
195             $found++;
196             last;
197         }
198
199         if (!$found and scalar(keys %channels)) {
200             &status("OUTSIDER <$who> $message");
201             return 'OUTSIDER';
202         }
203     }
204
205     # override msgType.
206     if ($msgType =~ /public/ and $message =~ s/^\+//) {
207         &status("Process: '+' flag detected; changing reply to public");
208         $msgType = 'public';
209         $who     = $chan;       # major hack to fix &msg().
210         $force_public_reply++;
211         # notice is still NOTICE but to whole channel => good.
212     }
213
214     # User Processing, for all users.
215     if ($addressed) {
216         my $retval;
217         return 'returned from pCH'   if &parseCmdHook("main",$message);
218
219         $retval = &userCommands();
220         return unless (defined $retval);
221         return if ($retval eq $noreply);
222     }
223
224     ###
225     # once useless messages have been parsed out, we match them.
226     ###
227
228     # confused? is this for infobot communications?
229     foreach (keys %{ $lang{'confused'} }) {
230         my $y = $_;
231
232         next unless ($message =~ /^\Q$y\E\s*/);
233         return 'CONFUSO';
234     }
235
236     # hello. [took me a while to fix this. -xk]
237     if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
238         return '' unless ($talkok);
239
240         # 'mynick: hi' or 'hi mynick' or 'hi'.
241         &status("somebody said hello");
242
243         # 50% chance of replying to a random greeting when not addressed
244         if (!defined $5 and $addressed == 0 and rand() < 0.5) {
245             &status("not returning unaddressed greeting");
246             return;
247         }
248
249         # customized random message.
250         my $tmp = (rand() < 0.5) ? ", $who" : "";
251         &pSReply( &getRandom(keys %{ $lang{'hello'} }) . $tmp );
252         return;
253     }
254
255     # greetings.
256     if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
257         my $reply = &getRandom(keys %{ $lang{'howareyou'} });
258
259         &performReply($reply);
260         
261         return;
262     }
263
264     # praise.
265     if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
266         $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
267     {
268         return 'praise: no addr' unless ($addressed);
269
270         &status("random praise detected");
271
272         my $tmp = (rand() < 0.5) ? "thanks $who " : "";
273         &pSReply($tmp.":)");
274
275         return;
276     }
277
278     # thanks.
279     if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
280         return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
281
282         &performReply( &getRandom(keys %{ $lang{'welcome'} }) );
283         return;
284     }
285
286     ###
287     ### bot commands...
288     ###
289
290     # karma. set...
291     if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
292         return '' unless (&hasParam("karma"));
293
294         my($term,$inc) = (lc $1,$2);
295
296         if ($msgType !~ /public/i) {
297             &msg($who, "karma must be done in public!");
298             return;
299         }
300
301         if (lc $term eq lc $who) {
302             &msg($who, "please don't karma yourself");
303             return;
304         }
305
306         my $karma = &dbGet("stats", "counter", "nick=".&dbQuote($term).
307                         " AND type='karma'") || 0;
308         if ($inc eq '++') {
309             $karma++;
310         } else {
311             $karma--;
312         }
313
314         &dbSet("stats", 
315                 { nick => $term, type => "karma" },
316                 { counter => $karma }
317         );
318
319         return;
320     }
321
322     # here's where the external routines get called.
323     # if they return anything but null, that's the "answer".
324     if ($addressed) {
325         if ( &parseCmdHook("extra",$message) ) {
326             return 'DID SOMETHING IN PCH.';
327         }
328
329         my $er = &Modules();
330         if (!defined $er) {
331             return 'SOMETHING 1';
332         }
333
334         if (0 and $addrchar) {
335             &msg($who, "I don't trust people to use the core commands while addressing me in a short-cut way.");
336             return;
337         }
338     }
339
340     if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|sqlite|pgsql|dbm)/i) {
341         &FactoidStuff();
342     } elsif ($param{'DBType'} =~ /^none$/i) {
343         return "NO FACTOIDS.";
344     } else {
345         &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
346         &shutdown();
347         exit 0;
348     }
349 }
350
351 1;