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