]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
fixed/added global factoid SAR
[infobot.git] / src / Modules / UserDCC.pl
1 #
2 #  UserDCC.pl: User Commands, DCC CHAT.
3 #      Author: dms
4 #     Version: v0.1 (20000707)
5 #     Created: 20000707 (from UserExtra.pl)
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9
10 sub userDCC {
11     # hrm...
12     $message =~ s/\s+$//;
13
14     ### for all users.
15     # quit.
16     if ($message =~ /^(exit|quit)$/i) {
17         # do ircII clients support remote close? if so, cool!
18         &status("userDCC: quit called. FIXME");
19 ###     $irc->removeconn($dcc{'CHAT'}{lc $who});
20
21         return $noreply;
22     }
23
24     # who.
25     if ($message =~ /^who$/i) {
26         my $count = scalar(keys %{$dcc{'CHAT'}});
27         &performStrictReply("Start of who ($count users).");
28         foreach (keys %{$dcc{'CHAT'}}) {
29             &performStrictReply("=> $_");
30         }
31         &performStrictReply("End of who.");
32
33         return $noreply;
34     }
35
36     ### for those users with enough flags.
37
38     # 4op.
39     if ($message =~ /^4op(\s+($mask{chan}))?$/i) {
40         return $noreply unless (&hasFlag("o"));
41
42         my $chan = $2;
43
44         if ($chan eq "") {
45             &help("4op");
46             return $noreply;
47         }
48
49         if (!$channels{$chan}{'o'}{$ident}) {
50             &msg($who, "i don't have ops on $chan to do that.");
51             return $noreply;
52         }
53
54         # on non-4mode(<4) servers, this may be exploited.
55         if ($channels{$chan}{'o'}{$who}) {
56             rawout("MODE $chan -o+o-o+o". (" $who" x 4));
57         } else {
58             rawout("MODE $chan +o-o+o-o". (" $who" x 4));
59         }
60
61         return $noreply;
62     }
63
64     # backlog.
65     if ($message =~ /^backlog(\s+(.*))?$/i) {
66         return $noreply unless (&hasFlag("o"));
67         return $noreply unless (&hasParam("backlog"));
68         my $num = $2;
69         my $max = $param{'backlog'};
70
71         if (!defined $num) {
72             &help("backlog");
73             return $noreply;
74         } elsif ($num !~ /^\d+/) {
75             &msg($who, "error: argument is not positive integer.");
76             return $noreply;
77         } elsif ($num > $max or $num < 0) {
78             &msg($who, "error: argument is out of range (max $max).");
79             return $noreply;
80         }
81
82         &msg($who, "Start of backlog...");
83         for (0..$num-1) {
84             sleep 1 if ($_ % 4 == 0 and $_ != 0);
85             $conn->privmsg($who, "[".($_+1)."]: $backlog[$max-$num+$_]");
86         }
87         &msg($who, "End of backlog.");
88
89         return $noreply;
90     }
91
92     # dump variables.
93     if ($message =~ /^dumpvars$/i) {
94         return $noreply unless (&hasFlag("o"));
95         return '' unless (&IsParam("dumpvars"));
96
97         &status("Dumping all variables...");
98         &dumpallvars();
99
100         return $noreply;
101     }
102
103     # kick.
104     if ($message =~ /^kick(\s+(\S+)(\s+(\S+))?)?/) {
105         return $noreply unless (&hasFlag("o"));
106         my ($nick,$chan) = (lc $2,lc $4);
107
108         if ($nick eq "") {
109             &help("kick");
110             return $noreply;
111         }
112
113         if (&validChan($chan) == 0) {
114             &msg($who,"error: invalid channel \002$chan\002");
115             return $noreply;
116         }
117
118         if (&IsNickInChan($nick,$chan) == 0) {
119             &msg($who,"$nick is not in $chan.");
120             return $noreply;
121         }
122
123         &kick($nick,$chan);
124
125         return $noreply;
126     }
127
128     # part.
129     if ($message =~ /^part(\s+(\S+))?$/i) {
130         return $noreply unless (&hasFlag("o"));
131         my $jchan = $2;
132
133         if ($jchan !~ /^$mask{chan}$/) {
134             &msg($who, "error, invalid chan.");
135             &help("part");
136             return $noreply;
137         }
138
139         if (!&validChan($jchan)) {
140             &msg($who, "error, I'm not on that chan.");
141             return $noreply;
142         }
143
144         &msg($jchan, "Leaving. (courtesy of $who).");
145         &part($jchan);
146         return $noreply;
147     }
148
149     # ignore.
150     if ($message =~ /^ignore(\s+(\S+))?$/i) {
151         return $noreply unless (&hasFlag("o"));
152         my $what = lc $2;
153
154         if ($what eq "") {
155             &help("ignore");
156             return $noreply;
157         }
158
159         my $expire = $param{'ignoreTempExpire'} || 60;
160         $ignoreList{$what} = time() + ($expire * 60);
161         &status("ignoring $what at $who's request");
162         &msg($who, "added $what to the ignore list");
163
164         return $noreply;
165     }
166
167     # unignore.
168     if ($message =~ /^unignore(\s+(\S+))?$/i) {
169         return $noreply unless (&hasFlag("o"));
170         my $what = $2;
171
172         if ($what eq "") {
173             &help("unignore");
174             return $noreply;
175         }
176
177         if ($ignoreList{$what}) {
178             &status("unignoring $what at $userHandle's request");
179             delete $ignoreList{$what};
180             &msg($who, "removed $what from the ignore list");
181         } else {
182             &status("unignore FAILED for $1 at $who's request");
183             &msg($who, "no entry for $1 on the ignore list");
184         }
185         return $noreply;
186     }
187
188     # clear unignore list.
189     if ($message =~ /^clear ignorelist$/i) {
190         return $noreply unless (&hasFlag("o"));
191         undef %ignoreList;
192         &status("unignoring all ($who said the word)");
193
194         return $noreply;
195     }
196
197     # lobotomy. sometimes we want the bot to be _QUIET_.
198     if ($message =~ /^(lobotomy|bequiet)$/i) {
199         return $noreply unless (&hasFlag("o"));
200
201         if ($lobotomized) {
202             &performReply("i'm already lobotomized");
203         } else {
204             &performReply("i have been lobotomized");
205             $lobotomized = 1;
206         }
207
208         return $noreply;
209     }
210
211     # unlobotomy.
212     if ($message =~ /^(unlobotomy|benoisy)$/i) {
213         return $noreply unless (&hasFlag("o"));
214         if ($lobotomized) {
215             &performReply("i have been unlobotomized, woohoo");
216             $lobotomized = 0;
217         } else {
218             &performReply("i'm not lobotomized");
219         }
220         return $noreply;
221     }
222
223     # op.
224     if ($message =~ /^op(\s+(.*))?$/i) {
225         return $noreply unless (&hasFlag("o"));
226         my ($opee) = lc $2;
227         my @chans;
228
229         if ($opee =~ / /) {
230             if ($opee =~ /^(\S+)\s+(\S+)$/) {
231                 $opee  = $1;
232                 @chans = ($2);
233                 if (!&validChan($2)) {
234                     &msg($who,"error: invalid chan ($2).");
235                     return $noreply;
236                 }
237             } else {
238                 &msg($who,"error: invalid params.");
239                 return $noreply;
240             }
241         } else {
242             @chans = keys %channels;
243         }
244
245         my $found = 0;
246         my $op = 0;
247         foreach (@chans) {
248             next unless (&IsNickInChan($opee,$_));
249             $found++;
250             if ($channels{$_}{'o'}{$opee}) {
251                 &status("op: $opee already has ops on $_");
252                 next;
253             }
254             $op++;
255
256             &status("opping $opee on $_ at ${who}'s request");
257             &performStrictReply("opping $opee on $_");
258             &op($_, $opee);
259         }
260
261         if ($found != $op) {
262             &status("op: opped on all possible channels.");
263         } else {
264             &DEBUG("found => '$found'.");
265             &DEBUG("op => '$op'.");
266         }
267
268         return $noreply;
269     }
270
271     # deop.
272     if ($message =~ /^deop(\s+(.*))?$/i) {
273         return $noreply unless (&hasFlag("o"));
274         my ($opee) = lc $2;
275         my @chans;
276
277         if ($opee =~ / /) {
278             if ($opee =~ /^(\S+)\s+(\S+)$/) {
279                 $opee  = $1;
280                 @chans = ($2);
281                 if (!&validChan($2)) {
282                     &msg($who,"error: invalid chan ($2).");
283                     return $noreply;
284                 }
285             } else {
286                 &msg($who,"error: invalid params.");
287                 return $noreply;
288             }
289         } else {
290             @chans = keys %channels;
291         }
292
293         my $found = 0;
294         my $op = 0;
295         foreach (@chans) {
296             next unless (&IsNickInChan($opee,$_));
297             $found++;
298             if (!exists $channels{$_}{'o'}{$opee}) {
299                 &status("deop: $opee already has no ops on $_");
300                 next;
301             }
302             $op++;
303
304             &status("deopping $opee on $_ at ${who}'s request");
305             &deop($_, $opee);
306         }
307
308         if ($found != $op) {
309             &status("deop: deopped on all possible channels.");
310         } else {
311             &DEBUG("deop: found => '$found'.");
312             &DEBUG("deop: op => '$op'.");
313         }
314
315         return $noreply;
316     }
317
318     # say.
319     if ($message =~ s/^say\s+(\S+)\s+(.*)//) {
320         return $noreply unless (&hasFlag("o"));
321         my ($chan,$msg) = (lc $1, $2);
322         &DEBUG("chan => '$1', msg => '$msg'.");
323
324         if (&validChan($chan)) {
325             &msg($chan, $2);
326         } else {
327             &msg($who,"i'm not on \002$1\002, sorry.");
328         }
329         return $noreply;
330     }
331
332     # die.
333     if ($message =~ /^die$/) {
334         return $noreply unless (&hasFlag("n"));
335
336         &doExit();
337
338         status("Dying by $who\'s request");
339         exit 0;
340     }
341
342     # global factoid substitution.
343     if ($message =~ m|^s([/,#])(.+?)\1(.*?)\1;?\s*$|) {
344         my ($delim,$op,$np) = ($1, $2, $3);
345         return $noreply unless (&hasFlag("n"));
346         ### TODO: support flags to do full-on global.
347
348         # incorrect format.
349         if ($np =~ /$delim/) {
350             &performReply("looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
351             return $noreply;
352         }
353
354         ### TODO: fix up $op to support mysql/pgsql/dbm(perl)
355         ### TODO: => add db/sql specific function to fix this.
356         my @list = &searchTable("factoids", "factoid_key",
357                         "factoid_value", $op);
358
359         if (!scalar @list) {
360             &performReply("Expression didn't match anything.");
361             return $noreply;
362         }
363
364         if (scalar @list > 100) {
365             &performReply("regex found more than 100 matches... not doing.");
366             return $noreply;
367         }
368
369         &status("gsubst: going to alter ".scalar(@list)." factoids.");
370         &performReply("going to alter ".scalar(@list)." factoids.");
371
372         my $error = 0;
373         foreach (@list) {
374             my $faqtoid = $_;
375
376             next if (&IsLocked($faqtoid) == 1);
377             my $result = &getFactoid($faqtoid);
378             my $was = $result;
379             &DEBUG("was($faqtoid) => '$was'.");
380
381             # global global
382             # we could support global local (once off).
383             if ($result =~ s/\Q$op/$np/gi) {
384                 if (length $result > $param{'maxDataSize'}) {
385                     &performReply("that's too long (or was long)");
386                     return $noreply;
387                 }
388                 &setFactInfo($faqtoid, "factoid_value", $result);
389                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
390             } else {
391                 &WARN("subst: that's weird... thought we found the string ($op) in '$faqtoid'.");
392                 $error++;
393             }
394         }
395
396         if ($error) {
397             &ERROR("Some warnings/errors?");
398         }
399
400         &performReply("Ok... did s/$op/$np/ for ".
401                                 (scalar(@list) - $error)." factoids");
402
403         return $noreply;
404     }
405
406     # jump.
407     if ($message =~ /^jump(\s+(\S+))?$/i) {
408         return $noreply unless (&hasFlag("n"));
409
410         if ($2 eq "") {
411             &help("jump");
412             return $noreply;
413         }
414
415         my ($server,$port);
416         if ($2 =~ /^(\S+)(:(\d+))?$/) {
417             $server = $1;
418             $port   = $3 || 6667;
419         } else {
420             &msg($who,"invalid format.");
421             return $noreply;
422         }
423
424         &status("jumping servers... $server...");
425         &rawout("QUIT :jumping to $server");
426
427         if (&irc($server,$port) == 0) {
428             &ircloop();
429         }
430     }
431
432     # reload.
433     if ($message =~ /^reload$/i) {
434         return $noreply unless (&hasFlag("n"));
435
436         &status("USER reload $who");
437         &msg($who,"reloading...");
438         &reloadAllModules();
439         &msg($who,"reloaded.");
440
441         return $noreply;
442     }
443
444     # rehash.
445     if ($message =~ /^rehash$/) {
446         return $noreply unless (&hasFlag("n"));
447
448         &msg($who,"rehashing...");
449         &restart("REHASH");
450         &status("USER rehash $who");
451         &msg($who,"rehashed");
452
453         return $noreply;
454     }
455
456     # set.
457     if ($message =~ /^set(\s+(\S+)?(\s+(.*))?)?$/i) {
458         return $noreply unless (&hasFlag("n"));
459         my ($param,$what) = ($2,$4);
460
461         if ($param eq "" and $what eq "") {
462             &msg($who,"\002Usage\002: set <param> [what]");
463             return $noreply;
464         }
465
466         if (!exists $param{$param}) {
467             &msg($who,"error: param{$param} cannot be set");
468             return $noreply;
469         }
470
471         if ($what eq "") {
472             if ($param{$param} eq "") {
473                 &msg($who,"param{$param} has \002no value\002.");
474             } else {
475                 &msg($who,"param{$param} has value of '\002$param{$param}\002'.");
476             }
477             return $noreply;
478         }
479
480         if ($param{$param} eq $what) {
481             &msg($who,"param{$param} already has value of '\002$what\002'.");
482             return $noreply;
483         }
484
485         $param{$param} = $what;
486         &msg($who,"setting param{$param} to '\002$what\002'.");
487
488         return $noreply;
489     }
490
491     # unset.
492     if ($message =~ /^unset(\s+(\S+))?$/i) {
493         return $noreply unless (&hasFlag("n"));
494         my ($param) = $2;
495
496         if ($param eq "") {
497             &msg($who,"\002Usage\002: unset <param>");
498             return $noreply;
499         }
500
501         if (!exists $param{$param}) {
502             &msg($who,"error: \002$param\002 cannot be unset");
503             return $noreply;
504         }
505
506         if ($param{$param} == 0) {
507             &msg($who,"\002param{$param}\002 has already been unset.");
508             return $noreply;
509         }
510
511         $param{$param} = 0;
512         &msg($who,"unsetting \002param{$param}\002.");
513
514         return $noreply;
515     }
516
517     # more...
518 }
519
520 1;