]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
converted %{$blah{$blah}} to %{ $blah{$blah} }
[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("userDCC: after dcc_close!");
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("n"));
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
195         if ($lobotomized) {
196             &performReply("i have been unlobotomized, woohoo");
197             $lobotomized = 0;
198             delete $cache{lobotomy};
199 #           undef $cache{lobotomy};     # ??
200         } else {
201             &performReply("i'm not lobotomized");
202         }
203
204         return;
205     }
206
207     # op.
208     if ($message =~ /^op(\s+(.*))?$/i) {
209         return unless (&hasFlag("o"));
210         my ($opee) = lc $2;
211         my @chans;
212
213         if ($opee =~ / /) {
214             if ($opee =~ /^(\S+)\s+(\S+)$/) {
215                 $opee  = $1;
216                 @chans = ($2);
217                 if (!&validChan($2)) {
218                     &msg($who,"error: invalid chan ($2).");
219                     return;
220                 }
221             } else {
222                 &msg($who,"error: invalid params.");
223                 return;
224             }
225         } else {
226             @chans = keys %channels;
227         }
228
229         my $found = 0;
230         my $op = 0;
231         foreach (@chans) {
232             next unless (&IsNickInChan($opee,$_));
233             $found++;
234             if ($channels{$_}{'o'}{$opee}) {
235                 &pSReply("op: $opee already has ops on $_");
236                 next;
237             }
238             $op++;
239
240             &pSReply("opping $opee on $_");
241             &op($_, $opee);
242         }
243
244         if ($found != $op) {
245             &pSReply("op: opped on all possible channels.");
246         } else {
247             &DEBUG("op: found => '$found'.");
248             &DEBUG("op:    op => '$op'.");
249         }
250
251         return;
252     }
253
254     # deop.
255     if ($message =~ /^deop(\s+(.*))?$/i) {
256         return unless (&hasFlag("o"));
257         my ($opee) = lc $2;
258         my @chans;
259
260         if ($opee =~ / /) {
261             if ($opee =~ /^(\S+)\s+(\S+)$/) {
262                 $opee  = $1;
263                 @chans = ($2);
264                 if (!&validChan($2)) {
265                     &msg($who,"error: invalid chan ($2).");
266                     return;
267                 }
268             } else {
269                 &msg($who,"error: invalid params.");
270                 return;
271             }
272         } else {
273             @chans = keys %channels;
274         }
275
276         my $found = 0;
277         my $op = 0;
278         foreach (@chans) {
279             next unless (&IsNickInChan($opee,$_));
280             $found++;
281             if (!exists $channels{$_}{'o'}{$opee}) {
282                 &status("deop: $opee already has no ops on $_");
283                 next;
284             }
285             $op++;
286
287             &status("deopping $opee on $_ at ${who}'s request");
288             &deop($_, $opee);
289         }
290
291         if ($found != $op) {
292             &status("deop: deopped on all possible channels.");
293         } else {
294             &DEBUG("deop: found => '$found'.");
295             &DEBUG("deop: op => '$op'.");
296         }
297
298         return;
299     }
300
301     # say.
302     if ($message =~ s/^say\s+(\S+)\s+(.*)//) {
303         return unless (&hasFlag("o"));
304         my ($chan,$msg) = (lc $1, $2);
305         &DEBUG("chan => '$1', msg => '$msg'.");
306
307         if (&validChan($chan)) {
308             &msg($chan, $2);
309         } else {
310             &msg($who,"i'm not on \002$1\002, sorry.");
311         }
312         return;
313     }
314
315     # die.
316     if ($message =~ /^die$/) {
317         return unless (&hasFlag("n"));
318
319         &doExit();
320
321         &status("Dying by $who\'s request");
322         exit 0;
323     }
324
325     # global factoid substitution.
326     if ($message =~ m|^s([/,#])(.+?)\1(.*?)\1;?\s*$|) {
327         my ($delim,$op,$np) = ($1, $2, $3);
328         return unless (&hasFlag("n"));
329         ### TODO: support flags to do full-on global.
330
331         # incorrect format.
332         if ($np =~ /$delim/) {
333             &performReply("looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
334             return;
335         }
336
337         ### TODO: fix up $op to support mysql/pgsql/dbm(perl)
338         ### TODO: => add db/sql specific function to fix this.
339         my @list = &searchTable("factoids", "factoid_key",
340                         "factoid_value", $op);
341
342         if (!scalar @list) {
343             &performReply("Expression didn't match anything.");
344             return;
345         }
346
347         if (scalar @list > 100) {
348             &performReply("regex found more than 100 matches... not doing.");
349             return;
350         }
351
352         &status("gsubst: going to alter ".scalar(@list)." factoids.");
353         &performReply("going to alter ".scalar(@list)." factoids.");
354
355         my $error = 0;
356         foreach (@list) {
357             my $faqtoid = $_;
358
359             next if (&IsLocked($faqtoid) == 1);
360             my $result = &getFactoid($faqtoid);
361             my $was = $result;
362             &DEBUG("was($faqtoid) => '$was'.");
363
364             # global global
365             # we could support global local (once off).
366             if ($result =~ s/\Q$op/$np/gi) {
367                 if (length $result > $param{'maxDataSize'}) {
368                     &performReply("that's too long (or was long)");
369                     return;
370                 }
371                 &setFactInfo($faqtoid, "factoid_value", $result);
372                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
373             } else {
374                 &WARN("subst: that's weird... thought we found the string ($op) in '$faqtoid'.");
375                 $error++;
376             }
377         }
378
379         if ($error) {
380             &ERROR("Some warnings/errors?");
381         }
382
383         &performReply("Ok... did s/$op/$np/ for ".
384                                 (scalar(@list) - $error)." factoids");
385
386         return;
387     }
388
389     # jump.
390     if ($message =~ /^jump(\s+(\S+))?$/i) {
391         return unless (&hasFlag("n"));
392
393         if ($2 eq "") {
394             &help("jump");
395             return;
396         }
397
398         my ($server,$port);
399         if ($2 =~ /^(\S+)(:(\d+))?$/) {
400             $server = $1;
401             $port   = $3 || 6667;
402         } else {
403             &msg($who,"invalid format.");
404             return;
405         }
406
407         &status("jumping servers... $server...");
408         &rawout("QUIT :jumping to $server");
409
410         if (&irc($server,$port) == 0) {
411             &ircloop();
412         }
413     }
414
415     # reload.
416     if ($message =~ /^reload$/i) {
417         return unless (&hasFlag("n"));
418
419         &status("USER reload $who");
420         &pSReply("reloading...");
421         &reloadAllModules();
422         &pSReply("reloaded.");
423
424         return;
425     }
426
427     # rehash.
428     if ($message =~ /^rehash$/) {
429         return unless (&hasFlag("n"));
430
431         &msg($who,"rehashing...");
432         &restart("REHASH");
433         &status("USER rehash $who");
434         &msg($who,"rehashed");
435
436         return;
437     }
438
439     #####
440     ##### USER//CHAN SPECIFIC CONFIGURATION COMMANDS
441     #####
442
443     if ($message =~ /^chaninfo(\s+(.*))?$/) {
444         my @args = split /[\s\t]+/, $2; # hrm.
445
446         if (scalar @args != 1) {
447             &help("chaninfo");
448             return;
449         }
450
451         if (!exists $chanconf{$args[0]}) {
452             &pSReply("no such channel $args[0]");
453             return;
454         }
455
456         &pSReply("showing channel conf.");
457         foreach (sort keys %{ $chanconf{$args[0]} }) {
458             &pSReply("$chan: $_ => $chanconf{$args[0]}{$_}");
459         }
460         &pSReply("End of chaninfo.");
461
462         return;
463     }
464
465     # +chan.
466     if ($message =~ /^(chanset|\+chan)(\s+(.*?))?$/) {
467         my $cmd         = $1;
468         my $args        = $3;
469         my $no_chan     = 0;
470
471         if (!defined $args) {
472             &help($cmd);
473             return;
474         }
475
476         my @chans;
477         while ($args =~ s/^($mask{chan})\s*//) {
478             push(@chans, $1);
479         }
480
481         if (!scalar @chans) {
482             push(@chans, "_default");
483             $no_chan    = 1;
484         }
485
486         my($what,$val) = split /[\s\t]+/, $args, 2;
487
488         ### TODO: "cannot set values without +m".
489         return unless (&hasFlag("n"));
490
491         # READ ONLY.
492         if (defined $what and $what !~ /^[-+]/ and !defined $val and $no_chan) {
493             &pSReply("Showing $what values on all channels...");
494
495             my %vals;
496             foreach (keys %chanconf) {
497                 my $val = $chanconf{$_}{$what} || "NOT-SET";
498                 $vals{$val}{$_} = 1;
499             }
500
501             foreach (keys %vals) {
502                 &pSReply("  $what = $_: ".join(' ', keys %{ $vals{$_} } ) );
503             }
504
505             &pSReply("End of list.");
506
507             return;
508         }
509
510         ### TODO: move to UserDCC again.
511         if ($cmd eq "chanset" and !defined $what) {
512             &DEBUG("showing channel conf.");
513
514             foreach $chan ($chan, "_default") {
515                 &pSReply("chan: $chan");
516                 ### TODO: merge 2 or 3 per line.
517                 my @items;
518                 my $str = "";
519                 foreach (sort keys %{ $chanconf{$chan} }) {
520                     my $newstr = join(', ', @items);
521                     if (length $newstr > 60) {
522                         &pSReply("    $str");
523                         @items = ();
524                     }
525                     $str = $newstr;
526                     push(@items, "$_ => $chanconf{$chan}{$_}");
527                 }
528                 &pSReply("    $str") if (@items);
529             }
530             return;
531         }
532
533         foreach (@chans) {
534             &chanSet($cmd, $_, $what, $val);
535         }
536
537         return;
538     }
539
540     if ($message =~ /^(chanunset|\-chan)(\s+(.*))?$/) {
541         return unless (&hasFlag("n"));
542         my $args        = $3;
543         my $no_chan     = 0;
544
545         if (!defined $args) {
546             &help("chanunset");
547             return;
548         }
549
550         my ($chan);
551         my $delete      = 0;
552         if ($args =~ s/^(\-)?($mask{chan})\s*//) {
553             $chan       = $2;
554             $delete     = ($1) ? 1 : 0;
555             &DEBUG("chan => $chan.");
556         } else {
557             &VERB("no chan arg; setting to default.",2);
558             $chan       = "_default";
559             $no_chan    = 1;
560         }
561
562         if (!exists $chanconf{$chan}) {
563             &pSReply("no such channel $chan");
564             return;
565         }
566
567         if ($args ne "") {
568
569             if (!&getChanConf($args,$chan)) {
570                 &pSReply("$args does not exist for $chan");
571                 return;
572             }
573
574             my @chans = &ChanConfList($args);
575             &DEBUG("scalar chans => ".scalar(@chans) );
576             if (scalar @chans == 1 and $chans[0] eq "_default" and !$no_chan) {
577                 &psReply("ok, $args was set only for _default; unsetting for _defaul but setting for other chans.");
578
579                 my $val = $chanconf{$_}{_default};
580                 foreach (keys %chanconf) {
581                     $chanconf{$_}{$args} = $val;
582                 }
583                 delete $chanconf{_default}{$args};
584
585                 return;
586             }
587
588             if ($no_chan and !exists($chanconf{_default}{$args})) {
589                 &pSReply("ok, $args for _default does not exist, removing from all chans.");
590
591                 foreach (keys %chanconf) {
592                     next unless (exists $chanconf{$_}{$args});
593                     &DEBUG("delete chanconf{$_}{$args};");
594                     delete $chanconf{$_}{$args};
595                 }
596
597                 return;
598             }
599
600             &pSReply("Unsetting channel ($chan) option $args. (was $chanconf{$chan}{$args})");
601             delete $chanconf{$chan}{$args};
602
603             return;
604         }
605
606         if ($delete) {
607             &pSReply("Deleting channel $chan for sure!");
608             $utime_chanfile = time();
609             $ucount_chanfile++;
610
611             &part($chan);
612             &pSReply("Leaving $chan...");
613
614             delete $chanconf{$chan};
615         } else {
616             &pSReply("Prefix channel with '-' to delete for sure.");
617         }
618
619         return;
620     }
621
622     if ($message =~ /^newpass(\s+(.*))?$/) {
623         my(@args) = split /[\s\t]+/, $2 || '';
624
625         if (scalar @args != 1) {
626             &help("newpass");
627             return;
628         }
629
630         my $u           = &getUser($who);
631         my $crypt       = &mkcrypt($args[0]);
632
633         &pSReply("Set your passwd to '$crypt'");
634         $users{$u}{PASS} = $crypt;
635
636         $utime_userfile = time();
637         $ucount_userfile++;
638
639         return;
640     }
641
642     if ($message =~ /^chpass(\s+(.*))?$/) {
643         my(@args) = split /[\s\t]+/, $2 || '';
644
645         if (!scalar @args) {
646             &help("chpass");
647             return;
648         }
649
650         if (!&IsUser($args[0])) {
651             &pSReply("user $args[0] is not valid.");
652             return;
653         }
654
655         my $u = &getUser($args[0]);
656         if (!defined $u) {
657             &pSReply("Internal error, u = NULL.");
658             return;
659         }
660
661         if (scalar @args == 1) {        # del pass.
662             if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
663                 &pSReply("cannto remove passwd of others.");
664                 return;
665             }
666
667             if (!exists $users{$u}{PASS}) {
668                 &pSReply("$u does not have pass set anyway.");
669                 return;
670             }
671
672             &pSReply("Deleted pass from $u.");
673
674             $utime_userfile = time();
675             $ucount_userfile++;
676
677             delete $users{$u}{PASS};
678
679             return;
680         }
681
682         my $crypt       = &mkcrypt($args[1]);
683         &pSReply("Set $u's passwd to '$crypt'");
684         $users{$u}{PASS} = $crypt;
685
686         $utime_userfile = time();
687         $ucount_userfile++;
688
689         return;
690     }
691
692     if ($message =~ /^chattr(\s+(.*))?$/) {
693         my(@args) = split /[\s\t]+/, $2 || '';
694
695         if (!scalar @args) {
696             &help("chattr");
697             return;
698         }
699
700         my $user;
701         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
702             $user       = &getUser($args[0]);
703             $chflag     = $args[1];
704         } else {                                # <flags>
705             $user       = &getUser($who);
706             &DEBUG("user $who... nope.") unless (defined $user);
707             $user       = &getUser($verifyUser);
708             $chflag     = $args[0];
709         }
710
711         if (!defined $user) {
712             &pSReply("user does not exist.");
713             return;
714         }
715
716         my $flags = $users{$user}{FLAGS};
717         if (!defined $chflag) {
718             &pSReply("Flags for $user: $flags");
719             return;
720         }
721
722         &DEBUG("who => $who");
723         &DEBUG("verifyUser => $verifyUser");
724         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
725             &pSReply("cannto change attributes of others.");
726             return "REPLY";
727         }
728
729         my $state;
730         my $change      = 0;
731         foreach (split //, $chflag) {
732             if ($_ eq "+") { $state = 1; next; }
733             if ($_ eq "-") { $state = 0; next; }
734
735             if (!defined $state) {
736                 &pSReply("no initial + or - was found in attr.");
737                 return;
738             }
739
740             if ($state) {
741                 next if ($flags =~ /\Q$_\E/);
742                 $flags .= $_;
743             } else {
744                 if (&IsParam("owner")
745                         and $param{owner} =~ /^\Q$user\E$/i
746                         and $flags =~ /[nmo]/
747                 ) {
748                     &pSReply("not removing flag $_ for $user.");
749                     next;
750                 }
751                 next unless ($flags =~ s/\Q$_\E//);
752             }
753
754             $change++;
755         }
756
757         if ($change) {
758             $utime_userfile = time();
759             $ucount_userfile++;
760             &pSReply("Current flags: $flags");
761             $users{$user}{FLAGS} = $flags;
762         } else {
763             &pSReply("No flags changed: $flags");
764         }
765
766         return;
767     }
768
769     if ($message =~ /^chnick(\s+(.*))?$/) {
770         my(@args) = split /[\s\t]+/, $2 || '';
771
772         if ($who eq "_default") {
773             &WARN("$who or verifyuser tried to run chnick.");
774             return "REPLY";
775         }
776
777         if (!scalar @args or scalar @args > 2) {
778             &help("chnick");
779             return;
780         }
781
782         if (scalar @args == 1) {        # 1
783             $user       = &getUser($who);
784             &DEBUG("nope, not $who.") unless (defined $user);
785             $user       ||= &getUser($verifyUser);
786             $chnick     = $args[0];
787         } else {                        # 2
788             $user       = &getUser($args[0]);
789             $chnick     = $args[1];
790         }
791
792         if (!defined $user) {
793             &pSReply("user $who or $args[0] does not exist.");
794             return;
795         }
796
797         if ($user =~ /^\Q$chnick\E$/i) {
798             &pSReply("user == chnick. why should I do that?");
799             return;
800         }
801
802         if (&getUser($chnick)) {
803             &pSReply("user $chnick is already used!");
804             return;
805         }
806
807         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
808             &pSReply("cannto change nick of others.");
809             return "REPLY" if ($who eq "_default");
810             return;
811         }
812
813         foreach (keys %{ $users{$user} }) {
814             $users{$chnick}{$_} = $users{$user}{$_};
815             delete $users{$user}{$_};
816         }
817         undef $users{$user};    # ???
818
819         $utime_userfile = time();
820         $ucount_userfile++;
821
822         &pSReply("Changed '$user' to '$chnick' successfully.");
823
824         return;
825     }
826
827     if ($message =~ /^([-+])host(\s+(.*))?$/) {
828         my $cmd         = $1."host";
829         my(@args)       = split /[\s\t]+/, $3 || '';
830         my $state       = ($1 eq "+") ? 1 : 0;
831
832         if (!scalar @args) {
833             &help($cmd);
834             return;
835         }
836
837         if ($who eq "_default") {
838             &WARN("$who or verifyuser tried to run $cmd.");
839             return "REPLY";
840         }
841
842         my ($user,$mask);
843         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
844             return unless (&hasFlag("n"));
845             $user       = &getUser($args[0]);
846             $mask       = $args[1];
847         } else {                                # <mask>
848             # who or verifyUser. FIXME!!!
849             $user       = &getUser($who);
850             $mask       = $args[0];
851         }
852
853         if (!defined $user) {
854             &pSReply("user $user does not exist.");
855             return;
856         }
857
858         if (!defined $mask) {
859             ### FIXME.
860             &pSReply("Hostmasks for $user: $users{$user}{HOSTS}");
861
862             return;
863         }
864
865         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
866             &pSReply("cannto change masks of others.");
867             return;
868         }
869
870         if ($mask !~ /^$mask{nuh}$/) {
871             &pSReply("error: mask ($mask) is not a real hostmask.");
872             return;
873         }
874
875         my $count = scalar keys %{ $users{$user}{HOSTS} };
876
877         if ($state) {                           # add.
878             if (exists $users{$user}{HOSTS}{$mask}) {
879                 &pSReply("mask $mask already exists.");
880                 return;
881             }
882
883             ### TODO: override support.
884             $users{$user}{HOSTS}{$mask} = 1;
885
886             &pSReply("Added $mask to list of masks.");
887
888         } else {                                # delete.
889
890             if (!exists $users{$user}{HOSTS}{$mask}) {
891                 &pSReply("mask $mask does not exist.");
892                 return;
893             }
894
895             ### TODO: wildcard support. ?
896             delete $users{$user}{HOSTS}{$mask};
897
898             if (scalar keys %{ $users{$user}{HOSTS} } != $count) {
899                 &pSReply("Removed $mask from list of masks.");
900             } else {
901                 &pSReply("error: could not find $mask in list of masks.");
902                 return;
903             }
904         }
905
906         $utime_userfile = time();
907         $ucount_userfile++;
908
909         return;
910     }
911
912     if ($message =~ /^([-+])ban(\s+(.*))?$/) {
913         my $cmd         = $1."ban";
914         my $flatarg     = $3;
915         my(@args)       = split /[\s\t]+/, $3 || '';
916         my $state       = ($1 eq "+") ? 1 : 0;
917
918         if (!scalar @args) {
919             &help($cmd);
920             return;
921         }
922
923         my($mask,$chan,$time,$reason);
924
925         if ($flatarg =~ s/^($mask{nuh})\s*//) {
926             $mask = $1;
927         } else {
928             &DEBUG("arg does not contain nuh mask?");
929         }
930
931         if ($flatarg =~ s/^($mask{chan})\s*//) {
932             $chan = $1;
933         } else {
934             $chan = "*";        # _default instead?
935         }
936
937         if ($state == 0) {              # delete.
938             my @c = &banDel($mask);
939
940             foreach (@c) {
941                 &unban($mask, $_);
942             }
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("n") 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         &News::writeNews() if (&ChanConfList("news"));
1088
1089         return;
1090     }
1091
1092     ### ALIASES.
1093     $message =~ s/^addignore/+ignore/;
1094     $message =~ s/^(del|un)ignore/-ignore/;
1095
1096     # ignore.
1097     if ($message =~ /^(\+|\-)ignore(\s+(.*))?$/i) {
1098         return unless (&hasFlag("o"));
1099         my $state       = ($1 eq "+") ? 1 : 0;
1100         my $str         = $1."ignore";
1101         my $args        = $3;
1102
1103         if (!$args) {
1104             &help($str);
1105             return;
1106         }
1107
1108         my($mask,$chan,$time,$comment);
1109
1110         # mask.
1111         if ($args =~ s/^($mask{nuh})\s*//) {
1112             $mask = $1;
1113         } else {
1114             &ERROR("no NUH mask?");
1115             return;
1116         }
1117
1118         if (!$state) {                  # delignore.
1119             if ( &ignoreDel($mask) ) {
1120                 &pSReply("ok, deleted X ignores.");
1121             } else {
1122                 &pSReply("could not find $mask in ignore list.");
1123             }
1124             return;
1125         }
1126
1127         ###
1128         # addignore.
1129         ###
1130
1131         # chan.
1132         if ($args =~ s/^($mask{chan}|\*)\s*//) {
1133             $chan = $1;
1134         } else {
1135             $chan = "*";
1136         }
1137
1138         # time.
1139         if ($args =~ s/^(\d+)\s*//) {
1140             $time = $1*60;      # ??
1141         } else {
1142             $time = 0;
1143         }
1144
1145         # time.
1146         if ($args) {
1147             $comment = $args;
1148         } else {
1149             $comment = "added by $who";
1150         }
1151
1152         if ( &ignoreAdd($mask, $chan, $time, $comment) > 1) {
1153             &pSReply("warn: $mask already in ignore list; written over anyway. FIXME");
1154         } else {
1155             &pSReply("added $mask to ignore list.");
1156         }
1157
1158         return;
1159     }
1160
1161     if ($message =~ /^ignore(\s+(.*))?$/) {
1162         my $arg = $2;
1163
1164         if (defined $arg) {
1165             if ($arg !~ /^$mask{chan}$/) {
1166                 &pSReply("error: chan $chan is invalid.");
1167                 return;
1168             }
1169
1170             if (!&validChan($arg)) {
1171                 &pSReply("error: chan $arg is invalid.");
1172                 return;
1173             }
1174
1175             &pSReply("Showing bans for $arg only.");
1176         }
1177
1178         if (!scalar keys %ignore) {
1179             &pSReply("Ignore list is empty.");
1180             return;
1181         }
1182
1183         ### TODO: proper (eggdrop-like) formatting.
1184         my $c;
1185         &pSReply("    mask: expire, time-added, who, comment");
1186         foreach $c (keys %ignore) {
1187             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1188             &pSReply("  $c:");
1189
1190             foreach (keys %{ $ignore{$c} }) {
1191                 my $ref = ref $ignore{$c}{$_};
1192                 if ($ref eq "ARRAY") {
1193                     my @array = @{ $ignore{$c}{$_} };
1194                     &pSReply("      $_: @array");
1195                 } else {
1196                     &DEBUG("unknown ignore line?");
1197                 }
1198             }
1199         }
1200         &pSReply("END of ignore.");
1201
1202         return;
1203     }
1204
1205     # adduser/deluser.
1206     if ($message =~ /^(\+|\-|add|del)user(\s+(.*))?$/i) {
1207         my $str         = $1;
1208         my $strstr      = $1."user";
1209         my @args        = split /\s+/, $3 || '';
1210         my $args        = $3;
1211         my $state       = ($str =~ /^(\+|add)$/) ? 1 : 0;
1212
1213         if (!scalar @args) {
1214             &help($strstr);
1215             return;
1216         }
1217
1218         if ($str eq "+") {
1219             if (scalar @args != 2) {
1220                 &pSReply(".+host requires hostmask argument.");
1221                 return;
1222             }
1223         } elsif (scalar @args != 1) {
1224             &pSReply("too many arguments.");
1225             return;
1226         }
1227
1228         if ($state) {                   # adduser.
1229             if (scalar @args == 1) {
1230                 $args[1]        = &getHostMask($args[0]);
1231                 if (!defined $args[1]) {
1232                     &ERROR("could not get hostmask?");
1233                     return;
1234                 }
1235             }
1236
1237             if ( &userAdd(@args) ) {    # success.
1238                 &pSReply("Added $args[0]...");
1239
1240             } else {                    # failure.
1241                 &pSReply("User $args[0] already exists");
1242             }
1243
1244         } else {                        # deluser.
1245
1246             if ( &userDel($args[0]) ) { # success.
1247                 &pSReply("Deleted $args[0] successfully.");
1248
1249             } else {                    # failure.
1250                 &pSReply("User $args[0] does not exist.");
1251             }
1252
1253         }
1254         return;
1255     }
1256
1257     if ($message =~ /^sched$/) {
1258         my @list;
1259         my @run;
1260
1261         my %time;
1262         foreach (keys %sched) {
1263             next unless (exists $sched{$_}{TIME});
1264             $time{ $sched{$_}{TIME}-time() }{$_} = 1;
1265             push(@list,$_);
1266
1267             next unless (exists $sched{$_}{RUNNING});
1268             push(@run,$_);
1269         }
1270
1271         my @time;
1272         foreach (sort { $a <=> $b } keys %time) {
1273             my $str = join(", ", sort keys %{ $time{$_} });
1274             &DEBUG("time => $_, str => $str");
1275             push(@time, "$str (".&Time2String($_).")");
1276         }
1277
1278         &pSReply( &formListReply(0, "Schedulers: ", @time ) );
1279         &pSReply( &formListReply(0, "Scheds to run: ", sort @list ) );
1280         &pSReply( &formListReply(0, "Scheds running(should not happen?) ", sort @run ) );
1281
1282         return;
1283     }
1284
1285     return "REPLY";
1286 }
1287
1288 1;