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