]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
- userAdd, if no mask is given, don't add it.
[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         foreach (@chans) {
570             &chanSet($cmd, $_, $what, $val);
571         }
572
573         return;
574     }
575
576     if ($message =~ /^(chanunset|\-chan)(\s+(.*))?$/) {
577         return unless (&hasFlag("n"));
578         my $args        = $3;
579         my $no_chan     = 0;
580
581         if (!defined $args) {
582             &help("chanunset");
583             return;
584         }
585
586         my ($chan);
587         my $delete      = 0;
588         if ($args =~ s/^(\-)?($mask{chan})\s*//) {
589             $chan       = $2;
590             $delete     = ($1) ? 1 : 0;
591             &DEBUG("chan => $chan.");
592         } else {
593             &VERB("no chan arg; setting to default.",2);
594             $chan       = "_default";
595             $no_chan    = 1;
596         }
597
598         if (!exists $chanconf{$chan}) {
599             &pSReply("no such channel $chan");
600             return;
601         }
602
603         if ($args ne "") {
604
605             if (!&getChanConf($args,$chan)) {
606                 &pSReply("$args does not exist for $chan");
607                 return;
608             }
609
610             my @chans = &ChanConfList($args);
611             &DEBUG("scalar chans => ".scalar(@chans) );
612             if (scalar @chans == 1 and $chans[0] eq "_default" and !$no_chan) {
613                 &psReply("ok, $args was set only for _default; unsetting for _defaul but setting for other chans.");
614
615                 my $val = $chanconf{$_}{_default};
616                 foreach (keys %chanconf) {
617                     $chanconf{$_}{$args} = $val;
618                 }
619                 delete $chanconf{_default}{$args};
620
621                 return;
622             }
623
624             if ($no_chan and !exists($chanconf{_default}{$args})) {
625                 &pSReply("ok, $args for _default does not exist, removing from all chans.");
626
627                 foreach (keys %chanconf) {
628                     next unless (exists $chanconf{$_}{$args});
629                     &DEBUG("delete chanconf{$_}{$args};");
630                     delete $chanconf{$_}{$args};
631                 }
632
633                 return;
634             }
635
636             &pSReply("Unsetting channel ($chan) option $args. (was $chanconf{$chan}{$args})");
637             delete $chanconf{$chan}{$args};
638
639             return;
640         }
641
642         if ($delete) {
643             &pSReply("Deleting channel $chan for sure!");
644             $utime_chanfile = time();
645             $ucount_chanfile++;
646
647             &part($chan);
648             &pSReply("Leaving $chan...");
649
650             delete $chanconf{$chan};
651         } else {
652             &pSReply("Prefix channel with '-' to delete for sure.");
653         }
654
655         return;
656     }
657
658     if ($message =~ /^newpass(\s+(.*))?$/) {
659         my(@args) = split /[\s\t]+/, $2 || '';
660
661         if (scalar @args != 1) {
662             &help("newpass");
663             return;
664         }
665
666         my $u           = &getUser($who);
667         my $crypt       = &mkcrypt($args[0]);
668
669         &pSReply("Set your passwd to '$crypt'");
670         $users{$u}{PASS} = $crypt;
671
672         $utime_userfile = time();
673         $ucount_userfile++;
674
675         return;
676     }
677
678     if ($message =~ /^chpass(\s+(.*))?$/) {
679         my(@args) = split /[\s\t]+/, $2 || '';
680
681         if (!scalar @args) {
682             &help("chpass");
683             return;
684         }
685
686         if (!&IsUser($args[0])) {
687             &pSReply("user $args[0] is not valid.");
688             return;
689         }
690
691         my $u = &getUser($args[0]);
692         if (!defined $u) {
693             &pSReply("Internal error, u = NULL.");
694             return;
695         }
696
697         if (scalar @args == 1) {        # del pass.
698             if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
699                 &pSReply("cannot remove passwd of others.");
700                 return;
701             }
702
703             if (!exists $users{$u}{PASS}) {
704                 &pSReply("$u does not have pass set anyway.");
705                 return;
706             }
707
708             &pSReply("Deleted pass from $u.");
709
710             $utime_userfile = time();
711             $ucount_userfile++;
712
713             delete $users{$u}{PASS};
714
715             return;
716         }
717
718         my $crypt       = &mkcrypt($args[1]);
719         &pSReply("Set $u's passwd to '$crypt'");
720         $users{$u}{PASS} = $crypt;
721
722         $utime_userfile = time();
723         $ucount_userfile++;
724
725         return;
726     }
727
728     if ($message =~ /^chattr(\s+(.*))?$/) {
729         my(@args) = split /[\s\t]+/, $2 || '';
730
731         if (!scalar @args) {
732             &help("chattr");
733             return;
734         }
735
736         my $user;
737         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
738             $user       = &getUser($args[0]);
739             $chflag     = $args[1];
740         } else {                                # <flags>
741             $user       = &getUser($who);
742             &DEBUG("user $who... nope.") unless (defined $user);
743             $user       = &getUser($verifyUser);
744             $chflag     = $args[0];
745         }
746
747         if (!defined $user) {
748             &pSReply("user does not exist.");
749             return;
750         }
751
752         my $flags = $users{$user}{FLAGS};
753         if (!defined $chflag) {
754             &pSReply("Flags for $user: $flags");
755             return;
756         }
757
758         &DEBUG("who => $who");
759         &DEBUG("verifyUser => $verifyUser");
760         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
761             &pSReply("cannto change attributes of others.");
762             return "REPLY";
763         }
764
765         my $state;
766         my $change      = 0;
767         foreach (split //, $chflag) {
768             if ($_ eq "+") { $state = 1; next; }
769             if ($_ eq "-") { $state = 0; next; }
770
771             if (!defined $state) {
772                 &pSReply("no initial + or - was found in attr.");
773                 return;
774             }
775
776             if ($state) {
777                 next if ($flags =~ /\Q$_\E/);
778                 $flags .= $_;
779             } else {
780                 if (&IsParam("owner")
781                         and $param{owner} =~ /^\Q$user\E$/i
782                         and $flags =~ /[nmo]/
783                 ) {
784                     &pSReply("not removing flag $_ for $user.");
785                     next;
786                 }
787                 next unless ($flags =~ s/\Q$_\E//);
788             }
789
790             $change++;
791         }
792
793         if ($change) {
794             $utime_userfile = time();
795             $ucount_userfile++;
796             &pSReply("Current flags: $flags");
797             $users{$user}{FLAGS} = $flags;
798         } else {
799             &pSReply("No flags changed: $flags");
800         }
801
802         return;
803     }
804
805     if ($message =~ /^chnick(\s+(.*))?$/) {
806         my(@args) = split /[\s\t]+/, $2 || '';
807
808         if ($who eq "_default") {
809             &WARN("$who or verifyuser tried to run chnick.");
810             return "REPLY";
811         }
812
813         if (!scalar @args or scalar @args > 2) {
814             &help("chnick");
815             return;
816         }
817
818         if (scalar @args == 1) {        # 1
819             $user       = &getUser($who);
820             &DEBUG("nope, not $who.") unless (defined $user);
821             $user       ||= &getUser($verifyUser);
822             $chnick     = $args[0];
823         } else {                        # 2
824             $user       = &getUser($args[0]);
825             $chnick     = $args[1];
826         }
827
828         if (!defined $user) {
829             &pSReply("user $who or $args[0] does not exist.");
830             return;
831         }
832
833         if ($user =~ /^\Q$chnick\E$/i) {
834             &pSReply("user == chnick. why should I do that?");
835             return;
836         }
837
838         if (&getUser($chnick)) {
839             &pSReply("user $chnick is already used!");
840             return;
841         }
842
843         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
844             &pSReply("cannto change nick of others.");
845             return "REPLY" if ($who eq "_default");
846             return;
847         }
848
849         foreach (keys %{ $users{$user} }) {
850             $users{$chnick}{$_} = $users{$user}{$_};
851             delete $users{$user}{$_};
852         }
853         undef $users{$user};    # ???
854
855         $utime_userfile = time();
856         $ucount_userfile++;
857
858         &pSReply("Changed '$user' to '$chnick' successfully.");
859
860         return;
861     }
862
863     if ($message =~ /^([-+])host(\s+(.*))?$/) {
864         my $cmd         = $1."host";
865         my(@args)       = split /[\s\t]+/, $3 || '';
866         my $state       = ($1 eq "+") ? 1 : 0;
867
868         if (!scalar @args) {
869             &help($cmd);
870             return;
871         }
872
873         if ($who eq "_default") {
874             &WARN("$who or verifyuser tried to run $cmd.");
875             return "REPLY";
876         }
877
878         my ($user,$mask);
879         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
880             return unless (&hasFlag("n"));
881             $user       = &getUser($args[0]);
882             $mask       = $args[1];
883         } else {                                # <mask>
884             # who or verifyUser. FIXME!!!
885             $user       = &getUser($who);
886             $mask       = $args[0];
887         }
888
889         if (!defined $user) {
890             &pSReply("user $user does not exist.");
891             return;
892         }
893
894         if (!defined $mask) {
895             ### FIXME.
896             &pSReply("Hostmasks for $user: $users{$user}{HOSTS}");
897
898             return;
899         }
900
901         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
902             &pSReply("cannto change masks of others.");
903             return;
904         }
905
906         if ($mask !~ /^$mask{nuh}$/) {
907             &pSReply("error: mask ($mask) is not a real hostmask.");
908             return;
909         }
910
911         my $count = scalar keys %{ $users{$user}{HOSTS} };
912
913         if ($state) {                           # add.
914             if (exists $users{$user}{HOSTS}{$mask}) {
915                 &pSReply("mask $mask already exists.");
916                 return;
917             }
918
919             ### TODO: override support.
920             $users{$user}{HOSTS}{$mask} = 1;
921
922             &pSReply("Added $mask to list of masks.");
923
924         } else {                                # delete.
925
926             if (!exists $users{$user}{HOSTS}{$mask}) {
927                 &pSReply("mask $mask does not exist.");
928                 return;
929             }
930
931             ### TODO: wildcard support. ?
932             delete $users{$user}{HOSTS}{$mask};
933
934             if (scalar keys %{ $users{$user}{HOSTS} } != $count) {
935                 &pSReply("Removed $mask from list of masks.");
936             } else {
937                 &pSReply("error: could not find $mask in list of masks.");
938                 return;
939             }
940         }
941
942         $utime_userfile = time();
943         $ucount_userfile++;
944
945         return;
946     }
947
948     if ($message =~ /^([-+])ban(\s+(.*))?$/) {
949         my $cmd         = $1."ban";
950         my $flatarg     = $3;
951         my(@args)       = split /[\s\t]+/, $3 || '';
952         my $state       = ($1 eq "+") ? 1 : 0;
953
954         if (!scalar @args) {
955             &help($cmd);
956             return;
957         }
958
959         my($mask,$chan,$time,$reason);
960
961         if ($flatarg =~ s/^($mask{nuh})\s*//) {
962             $mask = $1;
963         } else {
964             &DEBUG("arg does not contain nuh mask?");
965         }
966
967         if ($flatarg =~ s/^($mask{chan})\s*//) {
968             $chan = $1;
969         } else {
970             $chan = "*";        # _default instead?
971         }
972
973         if ($state == 0) {              # delete.
974             my @c = &banDel($mask);
975
976             foreach (@c) {
977                 &unban($mask, $_);
978             }
979
980             if ($c) {
981                 &pSReply("Removed $mask from chans: @c");
982             } else {
983                 &pSReply("$mask was not found in ban list.");
984             }
985
986             return;
987         }
988
989         ###
990         # add ban.
991         ###
992
993         # time.
994         if ($flatarg =~ s/^(\d+)\s*//) {
995             $time = $1;
996             &DEBUG("time = $time.");
997             if ($time < 0) {
998                 &pSReply("error: time cannot be negatime?");
999                 return;
1000             }
1001         } else {
1002             $time = 0;
1003         }
1004
1005         if ($flatarg =~ s/^(.*)$//) {   # need length?
1006             $reason     = $1;
1007         }
1008
1009         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
1010             &pSReply("cannto change masks of others.");
1011             return;
1012         }
1013
1014         if ($mask !~ /^$mask{nuh}$/) {
1015             &pSReply("error: mask ($mask) is not a real hostmask.");
1016             return;
1017         }
1018
1019         if ( &banAdd($mask,$chan,$time,$reason) == 2) {
1020             &pSReply("ban already exists; overwriting.");
1021         }
1022         &pSReply("Added $mask for $chan (time => $time, reason => $reason)");
1023
1024         return;
1025     }
1026
1027     if ($message =~ /^whois(\s+(.*))?$/) {
1028         my $arg = $2;
1029
1030         if (!defined $arg) {
1031             &help("whois");
1032             return;
1033         }
1034
1035         my $user = &getUser($arg);
1036         if (!defined $user) {
1037             &pSReply("whois: user $user does not exist.");
1038             return;
1039         }
1040
1041         ### TODO: better (eggdrop-like) output.
1042         &pSReply("user: $user");
1043         foreach (keys %{ $users{$user} }) {
1044             my $ref = ref $users{$user}{$_};
1045
1046             if ($ref eq "HASH") {
1047                 my $type = $_;
1048                 ### DOES NOT WORK???
1049                 foreach (keys %{ $users{$user}{$type} }) {
1050                     &pSReply("    $type => $_");
1051                 }
1052                 next;
1053             }
1054
1055             &pSReply("    $_ => $users{$user}{$_}");
1056         }
1057         &pSReply("End of USER whois.");
1058
1059         return;
1060     }
1061
1062     if ($message =~ /^bans(\s+(.*))?$/) {
1063         my $arg = $2;
1064
1065         if (defined $arg) {
1066             if ($arg ne "_default" and !&validChan($arg) ) {
1067                 &pSReply("error: chan $chan is invalid.");
1068                 return;
1069             }
1070         }
1071
1072         if (!scalar keys %bans) {
1073             &pSReply("Ban list is empty.");
1074             return;
1075         }
1076
1077         my $c;
1078         &pSReply("     mask: expire, time-added, count, who-by, reason");
1079         foreach $c (keys %bans) {
1080             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1081             &pSReply("  $c:");
1082
1083             foreach (keys %{ $bans{$c} }) {
1084                 my $val = $bans{$c}{$_};
1085
1086                 if (ref $val eq "ARRAY") {
1087                     my @array = @{ $val };
1088                     &pSReply("    $_: @array");
1089                 } else {
1090                     &DEBUG("unknown ban: $val");
1091                 }
1092             }
1093         }
1094         &pSReply("END of bans.");
1095
1096         return;
1097     }
1098
1099     if ($message =~ /^banlist(\s+(.*))?$/) {
1100         my $arg = $2;
1101
1102         if (defined $arg and $arg !~ /^$mask_chan$/) {
1103             &pSReply("error: chan $chan is invalid.");
1104             return;
1105         }
1106
1107         &DEBUG("bans for global or arg => $arg.");
1108         foreach (keys %bans) {                  #CHANGE!!!
1109             &DEBUG("  $_ => $bans{$_}.");
1110         }
1111
1112         &DEBUG("End of bans.");
1113         &pSReply("END of bans.");
1114
1115         return;
1116     }
1117
1118     if ($message =~ /^save$/) {
1119         return unless (&hasFlag("o"));
1120
1121         &writeUserFile();
1122         &writeChanFile();
1123         &News::writeNews() if (&ChanConfList("news"));
1124
1125         return;
1126     }
1127
1128     ### ALIASES.
1129     $message =~ s/^addignore/+ignore/;
1130     $message =~ s/^(del|un)ignore/-ignore/;
1131
1132     # ignore.
1133     if ($message =~ /^(\+|\-)ignore(\s+(.*))?$/i) {
1134         return unless (&hasFlag("o"));
1135         my $state       = ($1 eq "+") ? 1 : 0;
1136         my $str         = $1."ignore";
1137         my $args        = $3;
1138
1139         if (!$args) {
1140             &help($str);
1141             return;
1142         }
1143
1144         my($mask,$chan,$time,$comment);
1145
1146         # mask.
1147         if ($args =~ s/^($mask{nuh})\s*//) {
1148             $mask = $1;
1149         } else {
1150             &ERROR("no NUH mask?");
1151             return;
1152         }
1153
1154         if (!$state) {                  # delignore.
1155             if ( &ignoreDel($mask) ) {
1156                 &pSReply("ok, deleted X ignores.");
1157             } else {
1158                 &pSReply("could not find $mask in ignore list.");
1159             }
1160             return;
1161         }
1162
1163         ###
1164         # addignore.
1165         ###
1166
1167         # chan.
1168         if ($args =~ s/^($mask{chan}|\*)\s*//) {
1169             $chan = $1;
1170         } else {
1171             $chan = "*";
1172         }
1173
1174         # time.
1175         if ($args =~ s/^(\d+)\s*//) {
1176             $time = $1*60;      # ??
1177         } else {
1178             $time = 0;
1179         }
1180
1181         # time.
1182         if ($args) {
1183             $comment = $args;
1184         } else {
1185             $comment = "added by $who";
1186         }
1187
1188         if ( &ignoreAdd($mask, $chan, $time, $comment) > 1) {
1189             &pSReply("warn: $mask already in ignore list; written over anyway. FIXME");
1190         } else {
1191             &pSReply("added $mask to ignore list.");
1192         }
1193
1194         return;
1195     }
1196
1197     if ($message =~ /^ignore(\s+(.*))?$/) {
1198         my $arg = $2;
1199
1200         if (defined $arg) {
1201             if ($arg !~ /^$mask{chan}$/) {
1202                 &pSReply("error: chan $chan is invalid.");
1203                 return;
1204             }
1205
1206             if (!&validChan($arg)) {
1207                 &pSReply("error: chan $arg is invalid.");
1208                 return;
1209             }
1210
1211             &pSReply("Showing bans for $arg only.");
1212         }
1213
1214         if (!scalar keys %ignore) {
1215             &pSReply("Ignore list is empty.");
1216             return;
1217         }
1218
1219         ### TODO: proper (eggdrop-like) formatting.
1220         my $c;
1221         &pSReply("    mask: expire, time-added, who, comment");
1222         foreach $c (keys %ignore) {
1223             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1224             &pSReply("  $c:");
1225
1226             foreach (keys %{ $ignore{$c} }) {
1227                 my $ref = ref $ignore{$c}{$_};
1228                 if ($ref eq "ARRAY") {
1229                     my @array = @{ $ignore{$c}{$_} };
1230                     &pSReply("      $_: @array");
1231                 } else {
1232                     &DEBUG("unknown ignore line?");
1233                 }
1234             }
1235         }
1236         &pSReply("END of ignore.");
1237
1238         return;
1239     }
1240
1241     # adduser/deluser.
1242     if ($message =~ /^(\+|\-|add|del)user(\s+(.*))?$/i) {
1243         my $str         = $1;
1244         my $strstr      = $1."user";
1245         my @args        = split /\s+/, $3 || '';
1246         my $args        = $3;
1247         my $state       = ($str =~ /^(\+|add)$/) ? 1 : 0;
1248
1249         if (!scalar @args) {
1250             &help($strstr);
1251             return;
1252         }
1253
1254         if ($str eq "+") {
1255             if (scalar @args != 2) {
1256                 &pSReply(".+host requires hostmask argument.");
1257                 return;
1258             }
1259         } elsif (scalar @args != 1) {
1260             &pSReply("too many arguments.");
1261             return;
1262         }
1263
1264         if ($state) {                   # adduser.
1265             if (scalar @args == 1) {
1266                 $args[1]        = &getHostMask($args[0]);
1267                 &pSReply("Attemping to guess $args[0]'s hostmask...");
1268
1269                 # crude hack... crappy Net::IRC
1270                 $conn->schedule(5, sub {
1271         # hopefully this is right.
1272         my $nick = (keys %{ $cache{nuhInfo} })[0];
1273         if (!defined $nick) {
1274             &pSReply("couldn't get nuhinfo... adding user without a hostmask.");
1275             &userAdd($nick);
1276             return;
1277         }
1278
1279         my $mask = &makeHostMask( $cache{nuhInfo}{$nick}{NUH} );
1280
1281         if ( &userAdd($nick, $mask) ) { # success.
1282                 &pSReply("Added $nick with flags $users{$nick}{FLAGS}");
1283                 my @hosts = keys %{ $users{$nick}{HOSTS} };
1284                 &pSReply("hosts: @hosts");
1285         }
1286 });
1287                 return;
1288             }
1289
1290             &DEBUG("args => @args");
1291             if ( &userAdd(@args) ) {    # success.
1292                 &pSReply("Added $args[0] with flags $users{$args[0]}{FLAGS}");
1293                 my @hosts = keys %{ $users{$args[0]}{HOSTS} };
1294                 &pSReply("hosts: @hosts");
1295
1296             } else {                    # failure.
1297                 &pSReply("User $args[0] already exists");
1298             }
1299
1300         } else {                        # deluser.
1301
1302             if ( &userDel($args[0]) ) { # success.
1303                 &pSReply("Deleted $args[0] successfully.");
1304
1305             } else {                    # failure.
1306                 &pSReply("User $args[0] does not exist.");
1307             }
1308
1309         }
1310         return;
1311     }
1312
1313     if ($message =~ /^sched$/) {
1314         my @list;
1315         my @run;
1316
1317         my %time;
1318         foreach (keys %sched) {
1319             next unless (exists $sched{$_}{TIME});
1320             $time{ $sched{$_}{TIME}-time() }{$_} = 1;
1321             push(@list,$_);
1322
1323             next unless (exists $sched{$_}{RUNNING});
1324             push(@run,$_);
1325         }
1326
1327         my @time;
1328         foreach (sort { $a <=> $b } keys %time) {
1329             my $str = join(", ", sort keys %{ $time{$_} });
1330             &DEBUG("time => $_, str => $str");
1331             push(@time, "$str (".&Time2String($_).")");
1332         }
1333
1334         &pSReply( &formListReply(0, "Schedulers: ", @time ) );
1335         &pSReply( &formListReply(0, "Scheds to run: ", sort @list ) );
1336         &pSReply( &formListReply(0, "Scheds running(should not happen?) ", sort @run ) );
1337
1338         return;
1339     }
1340
1341     # quite a cool hack: reply in DCC CHAT.
1342     $msgType = "chat";
1343
1344     my $done = 0;
1345     $done++ if &parseCmdHook("main", $message);
1346     $done++ if &parseCmdHook("extra", $message);
1347     $done++ unless (&Modules());
1348
1349     if ($done) {
1350         &DEBUG("running non DCC CHAT command inside DCC CHAT!");
1351         return;
1352     }
1353
1354     return "REPLY";
1355 }
1356
1357 1;