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