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