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