]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
- Moved most of chanset to DynaConfig
[infobot.git] / src / Modules / UserDCC.pl
1 #
2 #  UserDCC.pl: User Commands, DCC CHAT.
3 #      Author: dms
4 #     Version: v0.2 (20010119)
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         &dcc_close($who);
20         &status("hrmm....");
21
22         return;
23     }
24
25     # who.
26     if ($message =~ /^who$/) {
27         my $count = scalar(keys %{$dcc{'CHAT'}});
28         my $dccCHAT = $message;
29
30         &pSReply("Start of who ($count users).");
31         foreach (keys %{$dcc{'CHAT'}}) {
32             &pSReply("=> $_");
33         }
34         &pSReply("End of who.");
35
36         return;
37     }
38
39     ### for those users with enough flags.
40
41     # 4op.
42     if ($message =~ /^4op(\s+($mask{chan}))?$/i) {
43         return unless (&hasFlag("o"));
44
45         my $chan = $2;
46
47         if ($chan eq "") {
48             &help("4op");
49             return;
50         }
51
52         if (!$channels{$chan}{'o'}{$ident}) {
53             &msg($who, "i don't have ops on $chan to do that.");
54             return;
55         }
56
57         # on non-4mode(<4) servers, this may be exploited.
58         if ($channels{$chan}{'o'}{$who}) {
59             rawout("MODE $chan -o+o-o+o". (" $who" x 4));
60         } else {
61             rawout("MODE $chan +o-o+o-o". (" $who" x 4));
62         }
63
64         return;
65     }
66
67     # backlog.
68     if ($message =~ /^backlog(\s+(.*))?$/i) {
69         return unless (&hasFlag("o"));
70         return unless (&hasParam("backlog"));
71         my $num = $2;
72         my $max = $param{'backlog'};
73
74         if (!defined $num) {
75             &help("backlog");
76             return;
77         } elsif ($num !~ /^\d+/) {
78             &msg($who, "error: argument is not positive integer.");
79             return;
80         } elsif ($num > $max or $num < 0) {
81             &msg($who, "error: argument is out of range (max $max).");
82             return;
83         }
84
85         &msg($who, "Start of backlog...");
86         for (0..$num-1) {
87             sleep 1 if ($_ % 4 == 0 and $_ != 0);
88             $conn->privmsg($who, "[".($_+1)."]: $backlog[$max-$num+$_]");
89         }
90         &msg($who, "End of backlog.");
91
92         return;
93     }
94
95     # dump variables.
96     if ($message =~ /^dumpvars$/i) {
97         return unless (&hasFlag("o"));
98         return unless (&IsParam("dumpvars"));
99
100         &status("Dumping all variables...");
101         &dumpallvars();
102
103         return;
104     }
105
106     # kick.
107     if ($message =~ /^kick(\s+(\S+)(\s+(\S+))?)?/) {
108         return unless (&hasFlag("o"));
109         my ($nick,$chan) = (lc $2,lc $4);
110
111         if ($nick eq "") {
112             &help("kick");
113             return;
114         }
115
116         if (&validChan($chan) == 0) {
117             &msg($who,"error: invalid channel \002$chan\002");
118             return;
119         }
120
121         if (&IsNickInChan($nick,$chan) == 0) {
122             &msg($who,"$nick is not in $chan.");
123             return;
124         }
125
126         &kick($nick,$chan);
127
128         return;
129     }
130
131     # kick.
132     if ($message =~ /^mode(\s+(.*))?$/) {
133         return unless (&hasFlag("m"));
134         my ($chan,$mode) = split /\s+/,$2,2;
135
136         if ($chan eq "") {
137             &help("mode");
138             return;
139         }
140
141         if (&validChan($chan) == 0) {
142             &msg($who,"error: invalid channel \002$chan\002");
143             return;
144         }
145
146         if (!$channels{$chan}{o}{$ident}) {
147             &msg($who,"error: don't have ops on \002$chan\002");
148             return;
149         }
150
151         &mode($chan, $mode);
152
153         return;
154     }
155
156     # part.
157     if ($message =~ /^part(\s+(\S+))?$/i) {
158         return unless (&hasFlag("o"));
159         my $jchan = $2;
160
161         if ($jchan !~ /^$mask{chan}$/) {
162             &msg($who, "error, invalid chan.");
163             &help("part");
164             return;
165         }
166
167         if (!&validChan($jchan)) {
168             &msg($who, "error, I'm not on that chan.");
169             return;
170         }
171
172         &msg($jchan, "Leaving. (courtesy of $who).");
173         &part($jchan);
174         return;
175     }
176
177     # lobotomy. sometimes we want the bot to be _QUIET_.
178     if ($message =~ /^(lobotomy|bequiet)$/i) {
179         return unless (&hasFlag("o"));
180
181         if ($lobotomized) {
182             &performReply("i'm already lobotomized");
183         } else {
184             &performReply("i have been lobotomized");
185             $lobotomized = 1;
186         }
187
188         return;
189     }
190
191     # unlobotomy.
192     if ($message =~ /^(unlobotomy|benoisy)$/i) {
193         return unless (&hasFlag("o"));
194         if ($lobotomized) {
195             &performReply("i have been unlobotomized, woohoo");
196             $lobotomized = 0;
197         } else {
198             &performReply("i'm not lobotomized");
199         }
200         return;
201     }
202
203     # op.
204     if ($message =~ /^op(\s+(.*))?$/i) {
205         return unless (&hasFlag("o"));
206         my ($opee) = lc $2;
207         my @chans;
208
209         if ($opee =~ / /) {
210             if ($opee =~ /^(\S+)\s+(\S+)$/) {
211                 $opee  = $1;
212                 @chans = ($2);
213                 if (!&validChan($2)) {
214                     &msg($who,"error: invalid chan ($2).");
215                     return;
216                 }
217             } else {
218                 &msg($who,"error: invalid params.");
219                 return;
220             }
221         } else {
222             @chans = keys %channels;
223         }
224
225         my $found = 0;
226         my $op = 0;
227         foreach (@chans) {
228             next unless (&IsNickInChan($opee,$_));
229             $found++;
230             if ($channels{$_}{'o'}{$opee}) {
231                 &status("op: $opee already has ops on $_");
232                 next;
233             }
234             $op++;
235
236             &status("opping $opee on $_ at ${who}'s request");
237             &pSReply("opping $opee on $_");
238             &op($_, $opee);
239         }
240
241         if ($found != $op) {
242             &status("op: opped on all possible channels.");
243         } else {
244             &DEBUG("found => '$found'.");
245             &DEBUG("op => '$op'.");
246         }
247
248         return;
249     }
250
251     # deop.
252     if ($message =~ /^deop(\s+(.*))?$/i) {
253         return unless (&hasFlag("o"));
254         my ($opee) = lc $2;
255         my @chans;
256
257         if ($opee =~ / /) {
258             if ($opee =~ /^(\S+)\s+(\S+)$/) {
259                 $opee  = $1;
260                 @chans = ($2);
261                 if (!&validChan($2)) {
262                     &msg($who,"error: invalid chan ($2).");
263                     return;
264                 }
265             } else {
266                 &msg($who,"error: invalid params.");
267                 return;
268             }
269         } else {
270             @chans = keys %channels;
271         }
272
273         my $found = 0;
274         my $op = 0;
275         foreach (@chans) {
276             next unless (&IsNickInChan($opee,$_));
277             $found++;
278             if (!exists $channels{$_}{'o'}{$opee}) {
279                 &status("deop: $opee already has no ops on $_");
280                 next;
281             }
282             $op++;
283
284             &status("deopping $opee on $_ at ${who}'s request");
285             &deop($_, $opee);
286         }
287
288         if ($found != $op) {
289             &status("deop: deopped on all possible channels.");
290         } else {
291             &DEBUG("deop: found => '$found'.");
292             &DEBUG("deop: op => '$op'.");
293         }
294
295         return;
296     }
297
298     # say.
299     if ($message =~ s/^say\s+(\S+)\s+(.*)//) {
300         return unless (&hasFlag("o"));
301         my ($chan,$msg) = (lc $1, $2);
302         &DEBUG("chan => '$1', msg => '$msg'.");
303
304         if (&validChan($chan)) {
305             &msg($chan, $2);
306         } else {
307             &msg($who,"i'm not on \002$1\002, sorry.");
308         }
309         return;
310     }
311
312     # die.
313     if ($message =~ /^die$/) {
314         return unless (&hasFlag("n"));
315
316         &doExit();
317
318         &status("Dying by $who\'s request");
319         exit 0;
320     }
321
322     # global factoid substitution.
323     if ($message =~ m|^s([/,#])(.+?)\1(.*?)\1;?\s*$|) {
324         my ($delim,$op,$np) = ($1, $2, $3);
325         return unless (&hasFlag("n"));
326         ### TODO: support flags to do full-on global.
327
328         # incorrect format.
329         if ($np =~ /$delim/) {
330             &performReply("looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
331             return;
332         }
333
334         ### TODO: fix up $op to support mysql/pgsql/dbm(perl)
335         ### TODO: => add db/sql specific function to fix this.
336         my @list = &searchTable("factoids", "factoid_key",
337                         "factoid_value", $op);
338
339         if (!scalar @list) {
340             &performReply("Expression didn't match anything.");
341             return;
342         }
343
344         if (scalar @list > 100) {
345             &performReply("regex found more than 100 matches... not doing.");
346             return;
347         }
348
349         &status("gsubst: going to alter ".scalar(@list)." factoids.");
350         &performReply("going to alter ".scalar(@list)." factoids.");
351
352         my $error = 0;
353         foreach (@list) {
354             my $faqtoid = $_;
355
356             next if (&IsLocked($faqtoid) == 1);
357             my $result = &getFactoid($faqtoid);
358             my $was = $result;
359             &DEBUG("was($faqtoid) => '$was'.");
360
361             # global global
362             # we could support global local (once off).
363             if ($result =~ s/\Q$op/$np/gi) {
364                 if (length $result > $param{'maxDataSize'}) {
365                     &performReply("that's too long (or was long)");
366                     return;
367                 }
368                 &setFactInfo($faqtoid, "factoid_value", $result);
369                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
370             } else {
371                 &WARN("subst: that's weird... thought we found the string ($op) in '$faqtoid'.");
372                 $error++;
373             }
374         }
375
376         if ($error) {
377             &ERROR("Some warnings/errors?");
378         }
379
380         &performReply("Ok... did s/$op/$np/ for ".
381                                 (scalar(@list) - $error)." factoids");
382
383         return;
384     }
385
386     # jump.
387     if ($message =~ /^jump(\s+(\S+))?$/i) {
388         return unless (&hasFlag("n"));
389
390         if ($2 eq "") {
391             &help("jump");
392             return;
393         }
394
395         my ($server,$port);
396         if ($2 =~ /^(\S+)(:(\d+))?$/) {
397             $server = $1;
398             $port   = $3 || 6667;
399         } else {
400             &msg($who,"invalid format.");
401             return;
402         }
403
404         &status("jumping servers... $server...");
405         &rawout("QUIT :jumping to $server");
406
407         if (&irc($server,$port) == 0) {
408             &ircloop();
409         }
410     }
411
412     # reload.
413     if ($message =~ /^reload$/i) {
414         return unless (&hasFlag("n"));
415
416         &status("USER reload $who");
417         &pSReply("reloading...");
418         &reloadAllModules();
419         &pSReply("reloaded.");
420
421         return;
422     }
423
424     # rehash.
425     if ($message =~ /^rehash$/) {
426         return unless (&hasFlag("n"));
427
428         &msg($who,"rehashing...");
429         &restart("REHASH");
430         &status("USER rehash $who");
431         &msg($who,"rehashed");
432
433         return;
434     }
435
436     #####
437     ##### USER//CHAN SPECIFIC CONFIGURATION COMMANDS
438     #####
439
440     if ($message =~ /^chaninfo(\s+(.*))?$/) {
441         my @args = split /[\s\t]+/, $2; # hrm.
442
443         if (scalar @args != 1) {
444             &help("chaninfo");
445             return;
446         }
447
448         if (!exists $chanconf{$args[0]}) {
449             &pSReply("no such channel $args[0]");
450             return;
451         }
452
453         &pSReply("showing channel conf.");
454         foreach (sort keys %{ $chanconf{$args[0]} }) {
455             &pSReply("$chan: $_ => $chanconf{$args[0]}{$_}");
456         }
457         &pSReply("End of chaninfo.");
458
459         return;
460     }
461
462     # +chan.
463     if ($message =~ /^(chanset|\+chan)(\s+(.*?))?$/) {
464         my $cmd         = $1;
465         my $args        = $3;
466         my $no_chan     = 0;
467
468         if (!defined $args) {
469             &help($cmd);
470             return;
471         }
472
473         my @chans;
474         while ($args =~ s/^($mask{chan})\s*//) {
475             push(@chans, $1);
476         }
477
478         if (!scalar @chans) {
479             push(@chans, "_default");
480             $no_chan    = 1;
481         }
482
483         my($what,$val) = split /[\s\t]+/, $args, 2;
484
485         ### TODO: "cannot set values without +m".
486         return unless (&hasFlag("m"));
487
488         # READ ONLY.
489         if (defined $what and $what !~ /^[-+]/ and !defined $val and $no_chan) {
490             &pSReply("Showing $what values on all channels...");
491
492             my %vals;
493             foreach (keys %chanconf) {
494                 my $val = $chanconf{$_}{$what} || "NOT-SET";
495                 $vals{$val}{$_} = 1;
496             }
497
498             foreach (keys %vals) {
499                 &pSReply("  $what = $_: ".join(' ', keys %{ $vals{$_} } ) );
500             }
501
502             &pSReply("End of list.");
503
504             return;
505         }
506
507         foreach (@chans) {
508             &chanSet($cmd, $_, $what, $val);
509         }
510
511         return;
512     }
513
514     if ($message =~ /^(chanunset|\-chan)(\s+(.*))?$/) {
515         return unless (&hasFlag("m"));
516         my $args        = $3;
517         my $no_chan     = 0;
518
519         if (!defined $args) {
520             &help("chanunset");
521             return;
522         }
523
524         my ($chan);
525         my $delete      = 0;
526         if ($args =~ s/^(\-)?($mask{chan})\s*//) {
527             $chan       = $2;
528             $delete     = ($1) ? 1 : 0;
529             &DEBUG("chan => $chan.");
530         } else {
531             &VERB("no chan arg; setting to default.",2);
532             $chan       = "_default";
533             $no_chan    = 1;
534         }
535
536         if (!exists $chanconf{$chan}) {
537             &pSReply("no such channel $chan");
538             return;
539         }
540
541         if ($args ne "") {
542
543             if (!&getChanConf($args,$chan)) {
544                 &pSReply("$args does not exist for $chan");
545                 return;
546             }
547
548             my @chans = &ChanConfList($args);
549             &DEBUG("scalar chans => ".scalar(@chans) );
550             if (scalar @chans == 1 and $chans[0] eq "_default" and !$no_chan) {
551                 &psReply("ok, $args was set only for _default; unsetting for _defaul but setting for other chans.");
552
553                 my $val = $chanconf{$_}{_default};
554                 foreach (keys %chanconf) {
555                     $chanconf{$_}{$args} = $val;
556                 }
557                 delete $chanconf{_default}{$args};
558
559                 return;
560             }
561
562             if ($no_chan and !exists($chanconf{_default}{$args})) {
563                 &pSReply("ok, $args for _default does not exist, removing from all chans.");
564
565                 foreach (keys %chanconf) {
566                     next unless (exists $chanconf{$_}{$args});
567                     &DEBUG("delete chanconf{$_}{$args};");
568                     delete $chanconf{$_}{$args};
569                 }
570
571                 return;
572             }
573
574             &pSReply("Unsetting channel ($chan) option $args. (was $chanconf{$chan}{$args})");
575             delete $chanconf{$chan}{$args};
576
577             return;
578         }
579
580         if ($delete) {
581             &pSReply("Deleting channel $chan for sure!");
582             $utime_chanfile = time();
583             $ucount_chanfile++;
584
585             &part($chan);
586             &pSReply("Leaving $chan...");
587
588             delete $chanconf{$chan};
589         } else {
590             &pSReply("Prefix channel with '-' to delete for sure.");
591         }
592
593         return;
594     }
595
596
597     if ($message =~ /^chpass(\s+(.*))?$/) {
598         my(@args) = split /[\s\t]+/, $2 || '';
599
600         if (!scalar @args) {
601             &help("chpass");
602             return;
603         }
604
605         if (!&IsUser($args[0])) {
606             &pSReply("user $args[0] is not valid.");
607             return;
608         }
609
610         my $u = &getUser($who);
611
612         if (scalar @args == 1) {        # del pass.
613             if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
614                 &pSReply("cannto remove passwd of others.");
615                 return;
616             }
617
618             if (!exists $users{$u}{PASS}) {
619                 &pSReply("$u does not have pass set anyway.");
620                 return;
621             }
622
623             &pSReply("Deleted pass from $u.");
624
625             $utime_userfile = time();
626             $ucount_userfile++;
627
628             delete $users{$u}{PASS};
629
630             return;
631         }
632
633         my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
634         my $crypt = crypt($args[1], $salt);
635         &pSReply("Set $u's passwd to '$crypt'");
636         $users{$u}{PASS} = $crypt;
637
638         $utime_userfile = time();
639         $ucount_userfile++;
640
641         return;
642     }
643
644     if ($message =~ /^chattr(\s+(.*))?$/) {
645         my(@args) = split /[\s\t]+/, $2 || '';
646
647         if (!scalar @args) {
648             &help("chattr");
649             return;
650         }
651
652         my $user;
653         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
654             $user       = &getUser($args[0]);
655             $chflag     = $args[1];
656         } else {                                # <flags>
657             $user       = &getUser($who);
658             &DEBUG("user $who... nope.") unless (defined $user);
659             $user       = &getUser($verifyUser);
660             $chflag     = $args[0];
661         }
662
663         if (!defined $user) {
664             &pSReply("user $user does not exist.");
665             return;
666         }
667
668         my $flags = $users{$user}{FLAGS};
669         if (!defined $chflag) {
670             &pSReply("Flags for $user: $flags");
671             return;
672         }
673
674         &DEBUG("who => $who");
675         &DEBUG("verifyUser => $verifyUser");
676         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
677             &pSReply("cannto change attributes of others.");
678             return "REPLY";
679         }
680
681         my $state;
682         my $change      = 0;
683         foreach (split //, $chflag) {
684             if ($_ eq "+") { $state = 1; next; }
685             if ($_ eq "-") { $state = 0; next; }
686
687             if (!defined $state) {
688                 &pSReply("no initial + or - was found in attr.");
689                 return;
690             }
691
692             if ($state) {
693                 next if ($flags =~ /\Q$_\E/);
694                 $flags .= $_;
695             } else {
696                 if (&IsParam("owner")
697                         and $param{owner} =~ /^\Q$user\E$/i
698                         and $flags =~ /[nmo]/
699                 ) {
700                     &pSReply("not removing flag $_ for $user.");
701                     next;
702                 }
703                 next unless ($flags =~ s/\Q$_\E//);
704             }
705
706             $change++;
707         }
708
709         if ($change) {
710             $utime_userfile = time();
711             $ucount_userfile++;
712             &pSReply("Current flags: $flags");
713             $users{$user}{FLAGS} = $flags;
714         } else {
715             &pSReply("No flags changed: $flags");
716         }
717
718         return;
719     }
720
721     if ($message =~ /^chnick(\s+(.*))?$/) {
722         my(@args) = split /[\s\t]+/, $2 || '';
723
724         if ($who eq "_default") {
725             &WARN("$who or verifyuser tried to run chnick.");
726             return "REPLY";
727         }
728
729         if (!scalar @args or scalar @args > 2) {
730             &help("chnick");
731             return;
732         }
733
734         if (scalar @args == 1) {        # 1
735             $user       = &getUser($who);
736             &DEBUG("nope, not $who.") unless (defined $user);
737             $user       ||= &getUser($verifyUser);
738             $chnick     = $args[0];
739         } else {                        # 2
740             $user       = &getUser($args[0]);
741             $chnick     = $args[1];
742         }
743
744         if (!defined $user) {
745             &pSReply("user $who or $args[0] does not exist.");
746             return;
747         }
748
749         if ($user =~ /^\Q$chnick\E$/i) {
750             &pSReply("user == chnick. why should I do that?");
751             return;
752         }
753
754         if (&getUser($chnick)) {
755             &pSReply("user $chnick is already used!");
756             return;
757         }
758
759         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
760             &pSReply("cannto change nick of others.");
761             return "REPLY" if ($who eq "_default");
762             return;
763         }
764
765         foreach (keys %{ $users{$user} }) {
766             $users{$chnick}{$_} = $users{$user}{$_};
767             delete $users{$user}{$_};
768         }
769         undef $users{$user};    # ???
770
771         $utime_userfile = time();
772         $ucount_userfile++;
773
774         &pSReply("Changed '$user' to '$chnick' successfully.");
775
776         return;
777     }
778
779     if ($message =~ /^([-+])host(\s+(.*))?$/) {
780         my $cmd         = $1."host";
781         my(@args)       = split /[\s\t]+/, $3 || '';
782         my $state       = ($1 eq "+") ? 1 : 0;
783
784         if (!scalar @args) {
785             &help($cmd);
786             return;
787         }
788
789         if ($who eq "_default") {
790             &WARN("$who or verifyuser tried to run $cmd.");
791             return "REPLY";
792         }
793
794         my ($user,$mask);
795         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
796             return unless (&hasFlag("m"));
797             $user       = &getUser($args[0]);
798             $mask       = $args[1];
799         } else {                                # <mask>
800             # who or verifyUser. FIXME!!!
801             $user       = &getUser($who);
802             $mask       = $args[0];
803         }
804
805         if (!defined $user) {
806             &pSReply("user $user does not exist.");
807             return;
808         }
809
810         if (!defined $mask) {
811             ### FIXME.
812             &pSReply("Hostmasks for $user: $users{$user}{HOSTS}");
813
814             return;
815         }
816
817         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
818             &pSReply("cannto change masks of others.");
819             return;
820         }
821
822         if ($mask !~ /^$mask{nuh}$/) {
823             &pSReply("error: mask ($mask) is not a real hostmask.");
824             return;
825         }
826
827         my $count = scalar keys %{ $users{$user}{HOSTS} };
828
829         if ($state) {                           # add.
830             if (exists $users{$user}{HOSTS}{$mask}) {
831                 &pSReply("mask $mask already exists.");
832                 return;
833             }
834
835             ### TODO: override support.
836             $users{$user}{HOSTS}{$mask} = 1;
837
838             &pSReply("Added $mask to list of masks.");
839
840         } else {                                # delete.
841
842             if (!exists $users{$user}{HOSTS}{$mask}) {
843                 &pSReply("mask $mask does not exist.");
844                 return;
845             }
846
847             ### TODO: wildcard support. ?
848             delete $users{$user}{HOSTS}{$mask};
849
850             if (scalar keys %{ $users{$user}{HOSTS} } != $count) {
851                 &pSReply("Removed $mask from list of masks.");
852             } else {
853                 &pSReply("error: could not find $mask in list of masks.");
854                 return;
855             }
856         }
857
858         $utime_userfile = time();
859         $ucount_userfile++;
860
861         return;
862     }
863
864     if ($message =~ /^([-+])ban(\s+(.*))?$/) {
865         my $cmd         = $1."ban";
866         my $flatarg     = $3;
867         my(@args)       = split /[\s\t]+/, $3 || '';
868         my $state       = ($1 eq "+") ? 1 : 0;
869
870         if (!scalar @args) {
871             &help($cmd);
872             return;
873         }
874
875         my($mask,$chan,$time,$reason);
876
877         if ($flatarg =~ s/^($mask{nuh})\s*//) {
878             $mask = $1;
879         } else {
880             &DEBUG("arg does not contain nuh mask?");
881         }
882
883         if ($flatarg =~ s/^($mask{chan})\s*//) {
884             $chan = $1;
885         } else {
886             $chan = "*";        # _default instead?
887         }
888
889         if ($state == 0) {              # delete.
890             my $c = join(' ', &banDel($mask) );
891
892             if ($c) {
893                 &pSReply("Removed $mask from chans: $c");
894             } else {
895                 &pSReply("$mask was not found in ban list.");
896             }
897
898             return;
899         }
900
901         ###
902         # add ban.
903         ###
904
905         # time.
906         if ($flatarg =~ s/^(\d+)\s*//) {
907             $time = $1;
908             &DEBUG("time = $time.");
909             if ($time < 0) {
910                 &pSReply("error: time cannot be negatime?");
911                 return;
912             }
913         } else {
914             $time = 0;
915         }
916
917         if ($flatarg =~ s/^(.*)$//) {   # need length?
918             $reason     = $1;
919         }
920
921         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
922             &pSReply("cannto change masks of others.");
923             return;
924         }
925
926         if ($mask !~ /^$mask{nuh}$/) {
927             &pSReply("error: mask ($mask) is not a real hostmask.");
928             return;
929         }
930
931         if ( &banAdd($mask,$chan,$time,$reason) == 2) {
932             &pSReply("ban already exists; overwriting.");
933         }
934         &pSReply("Added $mask for $chan (time => $time, reason => $reason)");
935
936         return;
937     }
938
939     if ($message =~ /^whois(\s+(.*))?$/) {
940         my $arg = $2;
941
942         if (!defined $arg) {
943             &help("whois");
944             return;
945         }
946
947         my $user = &getUser($arg);
948         if (!defined $user) {
949             &pSReply("whois: user $user does not exist.");
950             return;
951         }
952
953         ### TODO: better (eggdrop-like) output.
954         &pSReply("user: $user");
955         foreach (keys %{ $users{$user} }) {
956             my $ref = ref $users{$user}{$_};
957
958             if ($ref eq "HASH") {
959                 my $type = $_;
960                 ### DOES NOT WORK???
961                 foreach (keys %{ $users{$user}{$type} }) {
962                     &pSReply("    $type => $_");
963                 }
964                 next;
965             }
966
967             &pSReply("    $_ => $users{$user}{$_}");
968         }
969         &pSReply("End of USER whois.");
970
971         return;
972     }
973
974     if ($message =~ /^bans(\s+(.*))?$/) {
975         my $arg = $2;
976
977         if (defined $arg) {
978             if ($arg ne "_default" and !&validChan($arg) ) {
979                 &pSReply("error: chan $chan is invalid.");
980                 return;
981             }
982         }
983
984         if (!scalar keys %bans) {
985             &pSReply("Ban list is empty.");
986             return;
987         }
988
989         my $c;
990         &pSReply("     mask: expire, time-added, count, who-by, reason");
991         foreach $c (keys %bans) {
992             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
993             &pSReply("  $c:");
994
995             foreach (keys %{ $bans{$c} }) {
996                 my $val = $bans{$c}{$_};
997
998                 if (ref $val eq "ARRAY") {
999                     my @array = @{ $val };
1000                     &pSReply("    $_: @array");
1001                 } else {
1002                     &DEBUG("unknown ban: $val");
1003                 }
1004             }
1005         }
1006         &pSReply("END of bans.");
1007
1008         return;
1009     }
1010
1011     if ($message =~ /^banlist(\s+(.*))?$/) {
1012         my $arg = $2;
1013
1014         if (defined $arg and $arg !~ /^$mask_chan$/) {
1015             &pSReply("error: chan $chan is invalid.");
1016             return;
1017         }
1018
1019         &DEBUG("bans for global or arg => $arg.");
1020         foreach (keys %bans) {                  #CHANGE!!!
1021             &DEBUG("  $_ => $bans{$_}.");
1022         }
1023
1024         &DEBUG("End of bans.");
1025         &pSReply("END of bans.");
1026
1027         return;
1028     }
1029
1030     if ($message =~ /^save$/) {
1031         return unless (&hasFlag("o"));
1032
1033         &writeUserFile();
1034         &writeChanFile();
1035
1036         return;
1037     }
1038
1039     ### ALIASES.
1040     $message =~ s/^addignore/+ignore/;
1041     $message =~ s/^(del|un)ignore/-ignore/;
1042
1043     # ignore.
1044     if ($message =~ /^(\+|\-)ignore(\s+(.*))?$/i) {
1045         return unless (&hasFlag("o"));
1046         my $state       = ($1 eq "+") ? 1 : 0;
1047         my $str         = $1."ignore";
1048         my $args        = $3;
1049
1050         if (!$args) {
1051             &help($str);
1052             return;
1053         }
1054
1055         my($mask,$chan,$time,$comment);
1056
1057         # mask.
1058         if ($args =~ s/^($mask{nuh})\s*//) {
1059             $mask = $1;
1060         } else {
1061             &ERROR("no NUH mask?");
1062             return;
1063         }
1064
1065         if (!$state) {                  # delignore.
1066             if ( &ignoreDel($mask) ) {
1067                 &pSReply("ok, deleted X ignores.");
1068             } else {
1069                 &pSReply("could not find $mask in ignore list.");
1070             }
1071             return;
1072         }
1073
1074         ###
1075         # addignore.
1076         ###
1077
1078         # chan.
1079         if ($args =~ s/^($mask{chan}|\*)\s*//) {
1080             $chan = $1;
1081         } else {
1082             $chan = "*";
1083         }
1084
1085         # time.
1086         if ($args =~ s/^(\d+)\s*//) {
1087             $time = $1*60;      # ??
1088         } else {
1089             $time = 0;
1090         }
1091
1092         # time.
1093         if ($args) {
1094             $comment = $args;
1095         } else {
1096             $comment = "added by $who";
1097         }
1098
1099         if ( &ignoreAdd($mask, $chan, $time, $comment) > 1) {
1100             &pSReply("warn: $mask already in ignore list; written over anyway. FIXME");
1101         } else {
1102             &pSReply("added $mask to ignore list.");
1103         }
1104
1105         return;
1106     }
1107
1108     if ($message =~ /^ignore(\s+(.*))?$/) {
1109         my $arg = $2;
1110
1111         if (defined $arg) {
1112             if ($arg !~ /^$mask{chan}$/) {
1113                 &pSReply("error: chan $chan is invalid.");
1114                 return;
1115             }
1116
1117             if (!&validChan($arg)) {
1118                 &pSReply("error: chan $arg is invalid.");
1119                 return;
1120             }
1121
1122             &pSReply("Showing bans for $arg only.");
1123         }
1124
1125         if (!scalar keys %ignore) {
1126             &pSReply("Ignore list is empty.");
1127             return;
1128         }
1129
1130         ### TODO: proper (eggdrop-like) formatting.
1131         my $c;
1132         &pSReply("    mask: expire, time-added, who, comment");
1133         foreach $c (keys %ignore) {
1134             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1135             &pSReply("  $c:");
1136
1137             foreach (keys %{ $ignore{$c} }) {
1138                 my $ref = ref $ignore{$c}{$_};
1139                 if ($ref eq "ARRAY") {
1140                     my @array = @{ $ignore{$c}{$_} };
1141                     &pSReply("      $_: @array");
1142                 } else {
1143                     &DEBUG("unknown ignore line?");
1144                 }
1145             }
1146         }
1147         &pSReply("END of ignore.");
1148
1149         return;
1150     }
1151
1152     # adduser/deluser.
1153     if ($message =~ /^(\+|\-|add|del)user(\s+(.*))?$/i) {
1154         my $str         = $1;
1155         my $strstr      = $1."user";
1156         my @args        = split /\s+/, $3 || '';
1157         my $args        = $3;
1158         my $state       = ($str =~ /^(\+|add)$/) ? 1 : 0;
1159
1160         if (!scalar @args) {
1161             &help($strstr);
1162             return;
1163         }
1164
1165         if ($str eq "+") {
1166             if (scalar @args != 2) {
1167                 &pSReply(".+host requires hostmask argument.");
1168                 return;
1169             }
1170         } elsif (scalar @args != 1) {
1171             &pSReply("too many arguments.");
1172             return;
1173         }
1174
1175         if ($state) {                   # adduser.
1176             if (scalar @args == 1) {
1177                 $args[1]        = &getHostMask($args[0]);
1178                 if (!defined $args[1]) {
1179                     &ERROR("could not get hostmask?");
1180                     return;
1181                 }
1182             }
1183
1184             if ( &userAdd(@args) ) {    # success.
1185                 &pSReply("Added $args[0]...");
1186
1187             } else {                    # failure.
1188                 &pSReply("User $args[0] already exists");
1189             }
1190
1191         } else {                        # deluser.
1192
1193             if ( &userDel($args[0]) ) { # success.
1194                 &pSReply("Deleted $args[0] successfully.");
1195
1196             } else {                    # failure.
1197                 &pSReply("User $args[0] does not exist.");
1198             }
1199
1200         }
1201         return;
1202     }
1203
1204     if ($message =~ /^sched$/) {
1205         my @list;
1206         my @run;
1207         foreach (keys %sched) {
1208             next unless (exists $sched{$_}{TIME});
1209             push(@list,$_);
1210
1211             next unless (exists $sched{$_}{RUNNING});
1212             push(@run,$_);
1213         }
1214
1215         &pSReply( &formListReply(0,"Scheds to run: ", sort @list ) );
1216         &pSReply( &formListReply(0, "Scheds running(should not happen?) ", sort @run ) );
1217
1218         return;
1219     }
1220
1221     return "REPLY";
1222 }
1223
1224 1;