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