]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
- fix undefined for '.chanset'
[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
467         if (!defined $args) {
468             &help($cmd);
469             return;
470         }
471
472         my ($chan);
473         if ($args =~ s/^($mask{chan})\s*//) {
474             $chan       = $1;
475             &DEBUG("chan => $chan.");
476         } else {
477             &DEBUG("no chan arg; setting to default.");
478             $chan       = "_default";
479         }
480
481         my($what,$val) = split /[\s\t]+/, $args, 2;
482
483         ### TODO: "cannot set values without +m".
484         return unless (&hasFlag("m"));
485
486         if ($cmd eq "+chan") {
487             &DEBUG("setting _time_added...");
488             $chanconf{$chan}{_time_added}       = time();
489
490             &pSReply("Joining $chan...");
491             &joinchan($chan);
492
493             return;
494         }
495
496         if (!exists $chanconf{$chan}) {
497             &pSReply("no such channel $chan");
498             return;
499         }
500
501         my $update      = 0;
502         if ($what =~ /^\+(\S+)/) {
503             my $was     = $chanconf{$chan}{$1};
504             $was        = ($was) ? "; was '$was'" : "";
505             &pSReply("Setting $1 for $chan to '1'$was.");
506
507             $chanconf{$chan}{$1} = 1;
508
509             $update++;
510         } elsif ($what =~ /^\-(\S+)/) {
511             my $was     = $chanconf{$chan}{$1};
512             $was        = ($was) ? "; was '$was'" : "";
513             &pSReply("Setting $1 for $chan to '0'$was.");
514
515             $chanconf{$chan}{$1} = 0;
516
517             $update++;
518         } elsif (defined $val) {
519             my $was     = $chanconf{$chan}{$what};
520             $was        = ($was) ? "; was '$was'" : "";
521             &pSReply("Setting $what for $chan to '$val'$was.");
522
523             $chanconf{$chan}{$what} = $val;
524
525             $update++;
526         } elsif (defined $what) {
527             if (exists $chanconf{$chan}{$what}) {
528                 &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
529             } else {
530                 &pSReply("$what for $chan is not set.'");
531             }
532         }
533
534         if ($update) {
535             $utime_chanfile = time();
536             $ucount_chanfile++;
537             return;
538         }
539
540         if ($cmd eq "chanset" and !defined $what) {
541             &DEBUG("showing channel conf.");
542
543             foreach $chan ($chan, "_default") {
544                 &pSReply("chan: $chan");
545                 ### TODO: merge 2 or 3 per line.
546                 foreach (sort keys %{ $chanconf{$chan} }) {
547                     &pSReply("    $_ => $chanconf{$chan}{$_}");
548                 }
549             }
550
551             return;
552         }
553
554         return;
555     }
556
557     if ($message =~ /^(chanunset|\-chan)(\s+(.*))?$/) {
558         return unless (&hasFlag("m"));
559         my $args        = $3;
560
561         if (!defined $args) {
562             &help("chanunset");
563             return;
564         }
565
566         my ($chan);
567         my $delete      = 0;
568         if ($args =~ s/^(\-)?($mask{chan})\s*//) {
569             $chan       = $2;
570             $delete     = ($1) ? 1 : 0;
571             &DEBUG("chan => $chan.");
572         } else {
573             &DEBUG("no chan arg; setting to default.");
574             $chan       = "_default";
575         }
576
577         if (!exists $chanconf{$chan}) {
578             &pSReply("no such channel $chan");
579             return;
580         }
581
582         if ($args ne "") {
583             if (&getChanConf($args,$chan)) {
584                 &pSReply("Unsetting channel ($chan) option $args. (was $chanconf{$chan}{$args})");
585                 undef $chanconf{$chan}{$args};
586
587             } else {
588                 &pSReply("$args does not exist for $chan");
589             }
590
591             return;
592         }
593
594         if ($delete) {
595             &pSReply("Deleting channel $chan for sure!");
596             $utime_chanfile = time();
597             $ucount_chanfile++;
598
599             &part($chan);
600             &pSReply("Leaving $chan...");
601
602             delete $chanconf{$chan};
603         } else {
604             &pSReply("Prefix channel with '-' to delete for sure.");
605         }
606
607         return;
608     }
609
610
611     if ($message =~ /^chpass(\s+(.*))?$/) {
612         my(@args) = split /[\s\t]+/, $2 || '';
613
614         if (!scalar @args) {
615             &help("chpass");
616             return;
617         }
618
619         if (!&IsUser($args[0])) {
620             &pSReply("user $args[0] is not valid.");
621             return;
622         }
623
624         my $u = &getUser($who);
625
626         if (scalar @args == 1) {        # del pass.
627             if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
628                 &pSReply("cannto remove passwd of others.");
629                 return;
630             }
631
632             if (!exists $users{$u}{PASS}) {
633                 &pSReply("$u does not have pass set anyway.");
634                 return;
635             }
636
637             &pSReply("Deleted pass from $u.");
638
639             $utime_userfile = time();
640             $ucount_userfile++;
641
642             delete $users{$u}{PASS};
643
644             return;
645         }
646
647         my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
648         my $crypt = crypt($args[1], $salt);
649         &pSReply("Set $u's passwd to '$crypt'");
650         $users{$u}{PASS} = $crypt;
651
652         $utime_userfile = time();
653         $ucount_userfile++;
654
655         return;
656     }
657
658     if ($message =~ /^chattr(\s+(.*))?$/) {
659         my(@args) = split /[\s\t]+/, $2 || '';
660
661         if (!scalar @args) {
662             &help("chattr");
663             return;
664         }
665
666         my $user;
667         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
668             $user       = &getUser($args[0]);
669             $chflag     = $args[1];
670         } else {                                # <flags>
671             $user       = &getUser($who);
672             &DEBUG("user $who... nope.") unless (defined $user);
673             $user       = &getUser($verifyUser);
674             $chflag     = $args[0];
675         }
676
677         if (!defined $user) {
678             &pSReply("user $user does not exist.");
679             return;
680         }
681
682         my $flags = $users{$user}{FLAGS};
683         if (!defined $chflag) {
684             &pSReply("Flags for $user: $flags");
685             return;
686         }
687
688         &DEBUG("who => $who");
689         &DEBUG("verifyUser => $verifyUser");
690         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
691             &pSReply("cannto change attributes of others.");
692             return "REPLY";
693         }
694
695         my $state;
696         my $change      = 0;
697         foreach (split //, $chflag) {
698             if ($_ eq "+") { $state = 1; next; }
699             if ($_ eq "-") { $state = 0; next; }
700
701             if (!defined $state) {
702                 &pSReply("no initial + or - was found in attr.");
703                 return;
704             }
705
706             if ($state) {
707                 next if ($flags =~ /\Q$_\E/);
708                 $flags .= $_;
709             } else {
710                 if (&IsParam("owner")
711                         and $param{owner} =~ /^\Q$user\E$/i
712                         and $flags =~ /[nmo]/
713                 ) {
714                     &pSReply("not removing flag $_ for $user.");
715                     next;
716                 }
717                 next unless ($flags =~ s/\Q$_\E//);
718             }
719
720             $change++;
721         }
722
723         if ($change) {
724             $utime_userfile = time();
725             $ucount_userfile++;
726             &pSReply("Current flags: $flags");
727             $users{$user}{FLAGS} = $flags;
728         } else {
729             &pSReply("No flags changed: $flags");
730         }
731
732         return;
733     }
734
735     if ($message =~ /^chnick(\s+(.*))?$/) {
736         my(@args) = split /[\s\t]+/, $2 || '';
737
738         if ($who eq "_default") {
739             &WARN("$who or verifyuser tried to run chnick.");
740             return "REPLY";
741         }
742
743         if (!scalar @args or scalar @args > 2) {
744             &help("chnick");
745             return;
746         }
747
748         if (scalar @args == 1) {        # 1
749             $user       = &getUser($who);
750             &DEBUG("nope, not $who.") unless (defined $user);
751             $user       ||= &getUser($verifyUser);
752             $chnick     = $args[0];
753         } else {                        # 2
754             $user       = &getUser($args[0]);
755             $chnick     = $args[1];
756         }
757
758         if (!defined $user) {
759             &pSReply("user $who or $args[0] does not exist.");
760             return;
761         }
762
763         if ($user =~ /^\Q$chnick\E$/i) {
764             &pSReply("user == chnick. why should I do that?");
765             return;
766         }
767
768         if (&getUser($chnick)) {
769             &pSReply("user $chnick is already used!");
770             return;
771         }
772
773         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
774             &pSReply("cannto change nick of others.");
775             return "REPLY" if ($who eq "_default");
776             return;
777         }
778
779         foreach (keys %{ $users{$user} }) {
780             $users{$chnick}{$_} = $users{$user}{$_};
781             delete $users{$user}{$_};
782         }
783         undef $users{$user};    # ???
784
785         $utime_userfile = time();
786         $ucount_userfile++;
787
788         &pSReply("Changed '$user' to '$chnick' successfully.");
789
790         return;
791     }
792
793     if ($message =~ /^([-+])host(\s+(.*))?$/) {
794         my $cmd         = $1."host";
795         my(@args)       = split /[\s\t]+/, $3 || '';
796         my $state       = ($1 eq "+") ? 1 : 0;
797
798         if (!scalar @args) {
799             &help($cmd);
800             return;
801         }
802
803         if ($who eq "_default") {
804             &WARN("$who or verifyuser tried to run $cmd.");
805             return "REPLY";
806         }
807
808         my ($user,$mask);
809         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
810             return unless (&hasFlag("m"));
811             $user       = &getUser($args[0]);
812             $mask       = $args[1];
813         } else {                                # <mask>
814             # who or verifyUser. FIXME!!!
815             $user       = &getUser($who);
816             $mask       = $args[0];
817         }
818
819         if (!defined $user) {
820             &pSReply("user $user does not exist.");
821             return;
822         }
823
824         if (!defined $mask) {
825             ### FIXME.
826             &pSReply("Hostmasks for $user: $users{$user}{HOSTS}");
827
828             return;
829         }
830
831         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
832             &pSReply("cannto change masks of others.");
833             return;
834         }
835
836         if ($mask !~ /^$mask{nuh}$/) {
837             &pSReply("error: mask ($mask) is not a real hostmask.");
838             return;
839         }
840
841         my $count = scalar keys %{ $users{$user}{HOSTS} };
842
843         if ($state) {                           # add.
844             if (exists $users{$user}{HOSTS}{$mask}) {
845                 &pSReply("mask $mask already exists.");
846                 return;
847             }
848
849             ### TODO: override support.
850             $users{$user}{HOSTS}{$mask} = 1;
851
852             &pSReply("Added $mask to list of masks.");
853
854         } else {                                # delete.
855
856             if (!exists $users{$user}{HOSTS}{$mask}) {
857                 &pSReply("mask $mask does not exist.");
858                 return;
859             }
860
861             ### TODO: wildcard support. ?
862             delete $users{$user}{HOSTS}{$mask};
863
864             if (scalar keys %{ $users{$user}{HOSTS} } != $count) {
865                 &pSReply("Removed $mask from list of masks.");
866             } else {
867                 &pSReply("error: could not find $mask in list of masks.");
868                 return;
869             }
870         }
871
872         $utime_userfile = time();
873         $ucount_userfile++;
874
875         return;
876     }
877
878     if ($message =~ /^([-+])ban(\s+(.*))?$/) {
879         my $cmd         = $1."ban";
880         my $flatarg     = $3;
881         my(@args)       = split /[\s\t]+/, $3 || '';
882         my $state       = ($1 eq "+") ? 1 : 0;
883
884         if (!scalar @args) {
885             &help($cmd);
886             return;
887         }
888
889         my($mask,$chan,$time,$reason);
890
891         if ($flatarg =~ s/^($mask{nuh})\s*//) {
892             $mask = $1;
893         } else {
894             &DEBUG("arg does not contain nuh mask?");
895         }
896
897         if ($flatarg =~ s/^($mask{chan})\s*//) {
898             $chan = $1;
899         } else {
900             $chan = "*";        # _default instead?
901         }
902
903         if ($state == 0) {              # delete.
904             my $c = join(' ', &banDel($mask) );
905
906             if ($c) {
907                 &pSReply("Removed $mask from chans: $c");
908             } else {
909                 &pSReply("$mask was not found in ban list.");
910             }
911
912             return;
913         }
914
915         ###
916         # add ban.
917         ###
918
919         # time.
920         if ($flatarg =~ s/^(\d+)\s*//) {
921             $time = $1;
922             &DEBUG("time = $time.");
923             if ($time < 0) {
924                 &pSReply("error: time cannot be negatime?");
925                 return;
926             }
927         } else {
928             $time = 0;
929         }
930
931         if ($flatarg =~ s/^(.*)$//) {   # need length?
932             $reason     = $1;
933         }
934
935         if (!&IsFlag("m") and $who !~ /^\Q$verifyUser\E$/i) {
936             &pSReply("cannto change masks of others.");
937             return;
938         }
939
940         if ($mask !~ /^$mask{nuh}$/) {
941             &pSReply("error: mask ($mask) is not a real hostmask.");
942             return;
943         }
944
945         if ( &banAdd($mask,$chan,$time,$reason) == 2) {
946             &pSReply("ban already exists; overwriting.");
947         }
948         &pSReply("Added $mask for $chan (time => $time, reason => $reason)");
949
950         return;
951     }
952
953     if ($message =~ /^whois(\s+(.*))?$/) {
954         my $arg = $2;
955
956         if (!defined $arg) {
957             &help("whois");
958             return;
959         }
960
961         my $user = &getUser($arg);
962         if (!defined $user) {
963             &pSReply("whois: user $user does not exist.");
964             return;
965         }
966
967         ### TODO: better (eggdrop-like) output.
968         &pSReply("user: $user");
969         foreach (keys %{ $users{$user} }) {
970             my $ref = ref $users{$user}{$_};
971
972             if ($ref eq "HASH") {
973                 my $type = $_;
974                 ### DOES NOT WORK???
975                 foreach (keys %{ $users{$user}{$type} }) {
976                     &pSReply("    $type => $_");
977                 }
978                 next;
979             }
980
981             &pSReply("    $_ => $users{$user}{$_}");
982         }
983         &pSReply("End of USER whois.");
984
985         return;
986     }
987
988     if ($message =~ /^bans(\s+(.*))?$/) {
989         my $arg = $2;
990
991         if (defined $arg) {
992             if ($arg ne "_default" and !&validChan($arg) ) {
993                 &pSReply("error: chan $chan is invalid.");
994                 return;
995             }
996         }
997
998         if (!scalar keys %bans) {
999             &pSReply("Ban list is empty.");
1000             return;
1001         }
1002
1003         my $c;
1004         &pSReply("     mask: expire, time-added, count, who-by, reason");
1005         foreach $c (keys %bans) {
1006             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1007             &pSReply("  $c:");
1008
1009             foreach (keys %{ $bans{$c} }) {
1010                 my $val = $bans{$c}{$_};
1011
1012                 if (ref $val eq "ARRAY") {
1013                     my @array = @{ $val };
1014                     &pSReply("    $_: @array");
1015                 } else {
1016                     &DEBUG("unknown ban: $val");
1017                 }
1018             }
1019         }
1020         &pSReply("END of bans.");
1021
1022         return;
1023     }
1024
1025     if ($message =~ /^banlist(\s+(.*))?$/) {
1026         my $arg = $2;
1027
1028         if (defined $arg and $arg !~ /^$mask_chan$/) {
1029             &pSReply("error: chan $chan is invalid.");
1030             return;
1031         }
1032
1033         &DEBUG("bans for global or arg => $arg.");
1034         foreach (keys %bans) {                  #CHANGE!!!
1035             &DEBUG("  $_ => $bans{$_}.");
1036         }
1037
1038         &DEBUG("End of bans.");
1039         &pSReply("END of bans.");
1040
1041         return;
1042     }
1043
1044     if ($message =~ /^save$/) {
1045         return unless (&hasFlag("o"));
1046
1047         &writeUserFile();
1048         &writeChanFile();
1049
1050         return;
1051     }
1052
1053     ### ALIASES.
1054     $message =~ s/^addignore/+ignore/;
1055     $message =~ s/^(del|un)ignore/-ignore/;
1056
1057     # ignore.
1058     if ($message =~ /^(\+|\-)ignore(\s+(.*))?$/i) {
1059         return unless (&hasFlag("o"));
1060         my $state       = ($1 eq "+") ? 1 : 0;
1061         my $str         = $1."ignore";
1062         my $args        = $3;
1063
1064         if (!$args) {
1065             &help($str);
1066             return;
1067         }
1068
1069         my($mask,$chan,$time,$comment);
1070
1071         # mask.
1072         if ($args =~ s/^($mask{nuh})\s*//) {
1073             $mask = $1;
1074         } else {
1075             &ERROR("no NUH mask?");
1076             return;
1077         }
1078
1079         if (!$state) {                  # delignore.
1080             if ( &ignoreDel($mask) ) {
1081                 &pSReply("ok, deleted X ignores.");
1082             } else {
1083                 &pSReply("could not find $mask in ignore list.");
1084             }
1085             return;
1086         }
1087
1088         ###
1089         # addignore.
1090         ###
1091
1092         # chan.
1093         if ($args =~ s/^($mask{chan}|\*)\s*//) {
1094             $chan = $1;
1095         } else {
1096             $chan = "*";
1097         }
1098
1099         # time.
1100         if ($args =~ s/^(\d+)\s*//) {
1101             $time = $1*60;      # ??
1102         } else {
1103             $time = 0;
1104         }
1105
1106         # time.
1107         if ($args) {
1108             $comment = $args;
1109         } else {
1110             $comment = "added by $who";
1111         }
1112
1113         if ( &ignoreAdd($mask, $chan, $time, $comment) > 1) {
1114             &pSReply("warn: $mask already in ignore list; written over anyway. FIXME");
1115         } else {
1116             &pSReply("added $mask to ignore list.");
1117         }
1118
1119         return;
1120     }
1121
1122     if ($message =~ /^ignore(\s+(.*))?$/) {
1123         my $arg = $2;
1124
1125         if (defined $arg) {
1126             if ($arg !~ /^$mask{chan}$/) {
1127                 &pSReply("error: chan $chan is invalid.");
1128                 return;
1129             }
1130
1131             if (!&validChan($arg)) {
1132                 &pSReply("error: chan $arg is invalid.");
1133                 return;
1134             }
1135
1136             &pSReply("Showing bans for $arg only.");
1137         }
1138
1139         if (!scalar keys %ignore) {
1140             &pSReply("Ignore list is empty.");
1141             return;
1142         }
1143
1144         ### TODO: proper (eggdrop-like) formatting.
1145         my $c;
1146         &pSReply("    mask: expire, time-added, who, comment");
1147         foreach $c (keys %ignore) {
1148             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1149             &pSReply("  $c:");
1150
1151             foreach (keys %{ $ignore{$c} }) {
1152                 my $ref = ref $ignore{$c}{$_};
1153                 if ($ref eq "ARRAY") {
1154                     my @array = @{ $ignore{$c}{$_} };
1155                     &pSReply("      $_: @array");
1156                 } else {
1157                     &DEBUG("unknown ignore line?");
1158                 }
1159             }
1160         }
1161         &pSReply("END of ignore.");
1162
1163         return;
1164     }
1165
1166     # adduser/deluser.
1167     if ($message =~ /^(\+|\-|add|del)user(\s+(.*))?$/i) {
1168         my $str         = $1;
1169         my $strstr      = $1."user";
1170         my @args        = split /\s+/, $3 || '';
1171         my $args        = $3;
1172         my $state       = ($str =~ /^(\+|add)$/) ? 1 : 0;
1173
1174         if (!scalar @args) {
1175             &help($strstr);
1176             return;
1177         }
1178
1179         if ($str eq "+") {
1180             if (scalar @args != 2) {
1181                 &pSReply(".+host requires hostmask argument.");
1182                 return;
1183             }
1184         } elsif (scalar @args != 1) {
1185             &pSReply("too many arguments.");
1186             return;
1187         }
1188
1189         if ($state) {                   # adduser.
1190             if (scalar @args == 1) {
1191                 $args[1]        = &getHostMask($args[0]);
1192                 if (!defined $args[1]) {
1193                     &ERROR("could not get hostmask?");
1194                     return;
1195                 }
1196             }
1197
1198             if ( &userAdd(@args) ) {    # success.
1199                 &pSReply("Added $args[0]...");
1200
1201             } else {                    # failure.
1202                 &pSReply("User $args[0] already exists");
1203             }
1204
1205         } else {                        # deluser.
1206
1207             if ( &userDel($args[0]) ) { # success.
1208                 &pSReply("Deleted $args[0] successfully.");
1209
1210             } else {                    # failure.
1211                 &pSReply("User $args[0] does not exist.");
1212             }
1213
1214         }
1215         return;
1216     }
1217
1218     if ($message =~ /^sched$/) {
1219         my @list;
1220         my @run;
1221         foreach (keys %sched) {
1222             next unless (exists $sched{$_}{TIME});
1223             push(@list,$_);
1224
1225             next unless (exists $sched{$_}{RUNNING});
1226             push(@run,$_);
1227         }
1228
1229         &pSReply( &formListReply(0,"Scheds to run: ", sort @list ) );
1230         &pSReply( &formListReply(0, "Scheds running(should not happen?) ", sort @run ) );
1231
1232         return;
1233     }
1234
1235     return "REPLY";
1236 }
1237
1238 1;