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