]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
- modules: woops, used wrong var. should work now.
[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             # don't test for $channel{_default} elsewhere !!!
194             next if (/^\s*$/ || /^_?default$/);
195             next unless (&IsNickInChan($who,$_));
196
197             $found++;
198             last;
199         }
200
201         if (!$found and scalar(keys %channels)) {
202             &status("OUTSIDER <$who> $message");
203             return 'OUTSIDER';
204         }
205     }
206
207     # override msgType.
208     if ($msgType =~ /public/ and $message =~ s/^\+//) {
209         &status("Process: '+' flag detected; changing reply to public");
210         $msgType = 'public';
211         $who     = $chan;       # major hack to fix &msg().
212         $force_public_reply++;
213         # notice is still NOTICE but to whole channel => good.
214     }
215
216     # User Processing, for all users.
217     if ($addressed) {
218         my $retval;
219         return 'returned from pCH'   if &parseCmdHook("main",$message);
220
221         $retval = &userCommands();
222         return unless (defined $retval);
223         return if ($retval eq $noreply);
224     }
225
226     ###
227     # once useless messages have been parsed out, we match them.
228     ###
229
230     # confused? is this for infobot communications?
231     foreach (keys %{ $lang{'confused'} }) {
232         my $y = $_;
233
234         next unless ($message =~ /^\Q$y\E\s*/);
235         return 'CONFUSO';
236     }
237
238     # hello. [took me a while to fix this. -xk]
239     if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
240         return '' unless ($talkok);
241
242         # 'mynick: hi' or 'hi mynick' or 'hi'.
243         &status("somebody said hello");
244
245         # 50% chance of replying to a random greeting when not addressed
246         if (!defined $5 and $addressed == 0 and rand() < 0.5) {
247             &status("not returning unaddressed greeting");
248             return;
249         }
250
251         # customized random message.
252         my $tmp = (rand() < 0.5) ? ", $who" : "";
253         &pSReply( &getRandom(keys %{ $lang{'hello'} }) . $tmp );
254         return;
255     }
256
257     # greetings.
258     if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
259         my $reply = &getRandom(keys %{ $lang{'howareyou'} });
260
261         &performReply($reply);
262         
263         return;
264     }
265
266     # praise.
267     if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
268         $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
269     {
270         return 'praise: no addr' unless ($addressed);
271
272         &status("random praise detected");
273
274         my $tmp = (rand() < 0.5) ? "thanks $who " : "";
275         &pSReply($tmp.":)");
276
277         return;
278     }
279
280     # thanks.
281     if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
282         return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
283
284         &performReply( &getRandom(keys %{ $lang{'welcome'} }) );
285         return;
286     }
287
288     ###
289     ### bot commands...
290     ###
291
292     # karma. set...
293     if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
294         return '' unless (&hasParam("karma"));
295
296         my($term,$inc) = (lc $1,$2);
297
298         if ($msgType !~ /public/i) {
299             &msg($who, "karma must be done in public!");
300             return;
301         }
302
303         if (lc $term eq lc $who) {
304             &msg($who, "please don't karma yourself");
305             return;
306         }
307
308         my $karma = &dbGet("stats", "counter", "nick=".&dbQuote($term).
309                         " AND type='karma'") || 0;
310         if ($inc eq '++') {
311             $karma++;
312         } else {
313             $karma--;
314         }
315
316         &dbSet("stats", 
317                 { nick => $term, type => "karma" },
318                 { counter => $karma }
319         );
320
321         return;
322     }
323
324     # here's where the external routines get called.
325     # if they return anything but null, that's the "answer".
326     if ($addressed) {
327         if ( &parseCmdHook("extra",$message) ) {
328             return 'DID SOMETHING IN PCH.';
329         }
330
331         my $er = &Modules();
332         if (!defined $er) {
333             return 'SOMETHING 1';
334         }
335
336         if (0 and $addrchar) {
337             &msg($who, "I don't trust people to use the core commands while addressing me in a short-cut way.");
338             return;
339         }
340     }
341
342     if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|sqlite|pgsql|dbm)/i) {
343         &FactoidStuff();
344     } elsif ($param{'DBType'} =~ /^none$/i) {
345         return "NO FACTOIDS.";
346     } else {
347         &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
348         &shutdown();
349         exit 0;
350     }
351 }
352
353 1;