]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
add a do <chan> <what> to go with say
[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         &FIXME("userDCC: quit called.");
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
340         &DEBUG("chan => '$1', msg => '$msg'.");
341
342         # TODO: add nick destination.
343         if (&validChan($chan)) {
344             &msg($chan, $msg);
345         } else {
346             &msg($who,"i'm not on \002$chan\002, sorry.");
347         }
348
349         return;
350     }
351
352     # do.
353     if ($message =~ s/^do\s+(\S+)\s+(.*)//) {
354         return unless (&hasFlag("o"));
355         my ($chan,$msg) = (lc $1, $2);
356
357         &DEBUG("chan => '$1', msg => '$msg'.");
358
359         # TODO: add nick destination.
360         if (&validChan($chan)) {
361             &action($chan, $msg);
362         } else {
363             &msg($who,"i'm not on \002$chan\002, sorry.");
364         }
365
366         return;
367     }
368
369     # die.
370     if ($message =~ /^die$/) {
371         return unless (&hasFlag("n"));
372
373         &doExit();
374
375         &status("Dying by $who\'s request");
376         exit 0;
377     }
378
379     # global factoid substitution.
380     if ($message =~ m|^s([/,#])(.+?)\1(.*?)\1;?\s*$|) {
381         my ($delim,$op,$np) = ($1, $2, $3);
382         return unless (&hasFlag("n"));
383         ### TODO: support flags to do full-on global.
384
385         # incorrect format.
386         if ($np =~ /$delim/) {
387             &performReply("looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
388             return;
389         }
390
391         ### TODO: fix up $op to support mysql/sqlite/pgsql
392         ### TODO: => add db/sql specific function to fix this.
393         my @list = &searchTable("factoids", "factoid_key",
394                         "factoid_value", $op);
395
396         if (!scalar @list) {
397             &performReply("Expression didn't match anything.");
398             return;
399         }
400
401         if (scalar @list > 100) {
402             &performReply("regex found more than 100 matches... not doing.");
403             return;
404         }
405
406         &status("gsubst: going to alter ".scalar(@list)." factoids.");
407         &performReply("going to alter ".scalar(@list)." factoids.");
408
409         my $error = 0;
410         foreach (@list) {
411             my $faqtoid = $_;
412
413             next if (&IsLocked($faqtoid) == 1);
414             my $result = &getFactoid($faqtoid);
415             my $was = $result;
416             &DEBUG("was($faqtoid) => '$was'.");
417
418             # global global
419             # we could support global local (once off).
420             if ($result =~ s/\Q$op/$np/gi) {
421                 if (length $result > $param{'maxDataSize'}) {
422                     &performReply("that's too long (or was long)");
423                     return;
424                 }
425                 &setFactInfo($faqtoid, "factoid_value", $result);
426                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
427             } else {
428                 &WARN("subst: that's weird... thought we found the string ($op) in '$faqtoid'.");
429                 $error++;
430             }
431         }
432
433         if ($error) {
434             &ERROR("Some warnings/errors?");
435         }
436
437         &performReply("Ok... did s/$op/$np/ for ".
438                                 (scalar(@list) - $error)." factoids");
439
440         return;
441     }
442
443     # jump.
444     if ($message =~ /^jump(\s+(\S+))?$/i) {
445         return unless (&hasFlag("n"));
446
447         if ($2 eq "") {
448             &help("jump");
449             return;
450         }
451
452         my ($server,$port);
453         if ($2 =~ /^(\S+)(:(\d+))?$/) {
454             $server = $1;
455             $port   = $3 || 6667;
456         } else {
457             &msg($who,"invalid format.");
458             return;
459         }
460
461         &status("jumping servers... $server...");
462         $conn->quit("jumping to $server");
463
464         if (&irc($server,$port) == 0) {
465             &ircloop();
466         }
467     }
468
469     # reload.
470     if ($message =~ /^reload$/i) {
471         return unless (&hasFlag("n"));
472
473         &status("USER reload $who");
474         &pSReply("reloading...");
475         &reloadAllModules();
476         &pSReply("reloaded.");
477
478         return;
479     }
480
481     # reset.
482     if ($message =~ /^reset$/i) {
483         return unless (&hasFlag("n"));
484
485         &msg($who,"resetting...");
486         my @done;
487         foreach ( keys %channels, keys %chanconf ) {
488             my $c = $_;
489             next if (grep /^\Q$c\E$/i, @done);
490
491             &part($_);
492
493             push(@done, $_);
494             sleep 1;
495         }
496         &DEBUG("before clearircvars");
497         &clearIRCVars();
498         &DEBUG("before joinnextchan");
499         &joinNextChan();
500         &DEBUG("after joinnextchan");
501
502         &status("USER reset $who");
503         &msg($who,"reset complete");
504
505         return;
506     }
507
508     # rehash.
509     if ($message =~ /^rehash$/) {
510         return unless (&hasFlag("n"));
511
512         &msg($who,"rehashing...");
513         &restart("REHASH");
514         &status("USER rehash $who");
515         &msg($who,"rehashed");
516
517         return;
518     }
519
520     #####
521     ##### USER//CHAN SPECIFIC CONFIGURATION COMMANDS
522     #####
523
524     if ($message =~ /^chaninfo(\s+(.*))?$/) {
525         my @args = split /[\s\t]+/, $2; # hrm.
526
527         if (scalar @args != 1) {
528             &help("chaninfo");
529             return;
530         }
531
532         if (!exists $chanconf{$args[0]}) {
533             &pSReply("no such channel $args[0]");
534             return;
535         }
536
537         &pSReply("showing channel conf.");
538         foreach (sort keys %{ $chanconf{$args[0]} }) {
539             &pSReply("$chan: $_ => $chanconf{$args[0]}{$_}");
540         }
541         &pSReply("End of chaninfo.");
542
543         return;
544     }
545
546     # +chan.
547     if ($message =~ /^(chanset|\+chan)(\s+(.*?))?$/) {
548         my $cmd         = $1;
549         my $args        = $3;
550         my $no_chan     = 0;
551
552         if (!defined $args) {
553             &help($cmd);
554             return;
555         }
556
557         my @chans;
558         while ($args =~ s/^($mask{chan})\s*//) {
559             push(@chans, lc($1));
560         }
561
562         if (!scalar @chans) {
563             push(@chans, "_default");
564             $no_chan    = 1;
565         }
566
567         my($what,$val) = split /[\s\t]+/, $args, 2;
568
569         ### TODO: "cannot set values without +m".
570         return unless (&hasFlag("n"));
571
572         # READ ONLY.
573         if (defined $what and $what !~ /^[-+]/ and !defined $val and $no_chan) {
574             &pSReply("Showing $what values on all channels...");
575
576             my %vals;
577             foreach (keys %chanconf) {
578                 my $val;
579                 if (defined $chanconf{$_}{$what}) {
580                     $val = $chanconf{$_}{$what};
581                 } else {
582                     $val = "NOT-SET";
583                 }
584                 $vals{$val}{$_} = 1;
585             }
586
587             foreach (keys %vals) {
588                 &pSReply("  $what = $_(" . scalar(keys %{$vals{$_}}) . "): ".join(' ', keys %{ $vals{$_} } ) );
589             }
590
591             &pSReply("End of list.");
592
593             return;
594         }
595
596         ### TODO: move to UserDCC again.
597         if ($cmd eq "chanset" and !defined $what) {
598             &DEBUG("showing channel conf.");
599
600             foreach $chan (@chans) {
601                 if ($chan eq '_default') {
602                     &pSReply("Default channel settings");
603                 } else {
604                     &pSReply("chan: $chan (see _default also)");
605                 }
606                 my @items;
607                 my $str = "";
608                 foreach (sort keys %{ $chanconf{$chan} }) {
609                     my $newstr = join(', ', @items);
610                     ### TODO: make length use channel line limit?
611                     if (length $newstr > 370) {
612                         &pSReply(" $str");
613                         @items = ();
614                     }
615                     $str = $newstr;
616                     push(@items, "$_ => $chanconf{$chan}{$_}");
617                 }
618                 &pSReply(" $str") if (@items);
619             }
620             return;
621         }
622
623         $cache{confvars}{$what} = $val;
624         &rehashConfVars();
625
626         foreach (@chans) {
627             &chanSet($cmd, $_, $what, $val);
628         }
629
630         return;
631     }
632
633     if ($message =~ /^(chanunset|\-chan)(\s+(.*))?$/) {
634         return unless (&hasFlag("n"));
635         my $args        = $3;
636         my $no_chan     = 0;
637
638         if (!defined $args) {
639             &help("chanunset");
640             return;
641         }
642
643         my ($chan);
644         my $delete      = 0;
645         if ($args =~ s/^(\-)?($mask{chan})\s*//) {
646             $chan       = $2;
647             $delete     = ($1) ? 1 : 0;
648         } else {
649             &VERB("no chan arg; setting to default.",2);
650             $chan       = "_default";
651             $no_chan    = 1;
652         }
653
654         if (!exists $chanconf{$chan}) {
655             &pSReply("no such channel $chan");
656             return;
657         }
658
659         if ($args ne "") {
660
661             if (!&getChanConf($args,$chan)) {
662                 &pSReply("$args does not exist for $chan");
663                 return;
664             }
665
666             my @chans = &ChanConfList($args);
667             &DEBUG("scalar chans => ".scalar(@chans) );
668             if (scalar @chans == 1 and $chans[0] eq "_default" and !$no_chan) {
669                 &psReply("ok, $args was set only for _default; unsetting for _defaul but setting for other chans.");
670
671                 my $val = $chanconf{$_}{_default};
672                 foreach (keys %chanconf) {
673                     $chanconf{$_}{$args} = $val;
674                 }
675                 delete $chanconf{_default}{$args};
676                 $cache{confvars}{$args} = 0;
677                 &rehashConfVars();
678
679                 return;
680             }
681
682             if ($no_chan and !exists($chanconf{_default}{$args})) {
683                 &pSReply("ok, $args for _default does not exist, removing from all chans.");
684
685                 foreach (keys %chanconf) {
686                     next unless (exists $chanconf{$_}{$args});
687                     &DEBUG("delete chanconf{$_}{$args};");
688                     delete $chanconf{$_}{$args};
689                 }
690                 $cache{confvars}{$args} = 0;
691                 &rehashConfVars();
692
693                 return;
694             }
695
696             &pSReply("Unsetting channel ($chan) option $args. (was $chanconf{$chan}{$args})");
697             delete $chanconf{$chan}{$args};
698
699             return;
700         }
701
702         if ($delete) {
703             &pSReply("Deleting channel $chan for sure!");
704             $utime_chanfile = time();
705             $ucount_chanfile++;
706
707             &part($chan);
708             &pSReply("Leaving $chan...");
709
710             delete $chanconf{$chan};
711         } else {
712             &pSReply("Prefix channel with '-' to delete for sure.");
713         }
714
715         return;
716     }
717
718     if ($message =~ /^newpass(\s+(.*))?$/) {
719         my(@args) = split /[\s\t]+/, $2 || '';
720
721         if (scalar @args != 1) {
722             &help("newpass");
723             return;
724         }
725
726         my $u           = &getUser($who);
727         my $crypt       = &mkcrypt($args[0]);
728
729         &pSReply("Set your passwd to '$crypt'");
730         $users{$u}{PASS} = $crypt;
731
732         $utime_userfile = time();
733         $ucount_userfile++;
734
735         return;
736     }
737
738     if ($message =~ /^chpass(\s+(.*))?$/) {
739         my(@args) = split /[\s\t]+/, $2 || '';
740
741         if (!scalar @args) {
742             &help("chpass");
743             return;
744         }
745
746         if (!&IsUser($args[0])) {
747             &pSReply("user $args[0] is not valid.");
748             return;
749         }
750
751         my $u = &getUser($args[0]);
752         if (!defined $u) {
753             &pSReply("Internal error, u = NULL.");
754             return;
755         }
756
757         if (scalar @args == 1) {        # del pass.
758             if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
759                 &pSReply("cannot remove passwd of others.");
760                 return;
761             }
762
763             if (!exists $users{$u}{PASS}) {
764                 &pSReply("$u does not have pass set anyway.");
765                 return;
766             }
767
768             &pSReply("Deleted pass from $u.");
769
770             $utime_userfile = time();
771             $ucount_userfile++;
772
773             delete $users{$u}{PASS};
774
775             return;
776         }
777
778         my $crypt       = &mkcrypt($args[1]);
779         &pSReply("Set $u's passwd to '$crypt'");
780         $users{$u}{PASS} = $crypt;
781
782         $utime_userfile = time();
783         $ucount_userfile++;
784
785         return;
786     }
787
788     if ($message =~ /^chattr(\s+(.*))?$/) {
789         my(@args) = split /[\s\t]+/, $2 || '';
790
791         if (!scalar @args) {
792             &help("chattr");
793             return;
794         }
795
796         my $chflag;
797         my $user;
798         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
799             $user       = &getUser($args[0]);
800             $chflag     = $args[1];
801         } else {                                # <flags>
802             $user       = &getUser($who);
803             &DEBUG("user $who... nope.") unless (defined $user);
804             $user       = &getUser($verifyUser);
805             $chflag     = $args[0];
806         }
807
808         if (!defined $user) {
809             &pSReply("user does not exist.");
810             return;
811         }
812
813         my $flags = $users{$user}{FLAGS};
814         if (!defined $chflag) {
815             &pSReply("Flags for $user: $flags");
816             return;
817         }
818
819         &DEBUG("who => $who");
820         &DEBUG("verifyUser => $verifyUser");
821         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
822             &pSReply("cannto change attributes of others.");
823             return "REPLY";
824         }
825
826         my $state;
827         my $change      = 0;
828         foreach (split //, $chflag) {
829             if ($_ eq "+") { $state = 1; next; }
830             if ($_ eq "-") { $state = 0; next; }
831
832             if (!defined $state) {
833                 &pSReply("no initial + or - was found in attr.");
834                 return;
835             }
836
837             if ($state) {
838                 next if ($flags =~ /\Q$_\E/);
839                 $flags .= $_;
840             } else {
841                 if (&IsParam("owner")
842                         and $param{owner} =~ /^\Q$user\E$/i
843                         and $flags =~ /[nmo]/
844                 ) {
845                     &pSReply("not removing flag $_ for $user.");
846                     next;
847                 }
848                 next unless ($flags =~ s/\Q$_\E//);
849             }
850
851             $change++;
852         }
853
854         if ($change) {
855             $utime_userfile = time();
856             $ucount_userfile++;
857             &pSReply("Current flags: $flags");
858             $users{$user}{FLAGS} = $flags;
859         } else {
860             &pSReply("No flags changed: $flags");
861         }
862
863         return;
864     }
865
866     if ($message =~ /^chnick(\s+(.*))?$/) {
867         my(@args) = split /[\s\t]+/, $2 || '';
868
869         if ($who eq "_default") {
870             &WARN("$who or verifyuser tried to run chnick.");
871             return "REPLY";
872         }
873
874         if (!scalar @args or scalar @args > 2) {
875             &help("chnick");
876             return;
877         }
878
879         if (scalar @args == 1) {        # 1
880             $user       = &getUser($who);
881             &DEBUG("nope, not $who.") unless (defined $user);
882             $user       ||= &getUser($verifyUser);
883             $chnick     = $args[0];
884         } else {                        # 2
885             $user       = &getUser($args[0]);
886             $chnick     = $args[1];
887         }
888
889         if (!defined $user) {
890             &pSReply("user $who or $args[0] does not exist.");
891             return;
892         }
893
894         if ($user =~ /^\Q$chnick\E$/i) {
895             &pSReply("user == chnick. why should I do that?");
896             return;
897         }
898
899         if (&getUser($chnick)) {
900             &pSReply("user $chnick is already used!");
901             return;
902         }
903
904         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
905             &pSReply("cannto change nick of others.");
906             return "REPLY" if ($who eq "_default");
907             return;
908         }
909
910         foreach (keys %{ $users{$user} }) {
911             $users{$chnick}{$_} = $users{$user}{$_};
912             delete $users{$user}{$_};
913         }
914         undef $users{$user};    # ???
915
916         $utime_userfile = time();
917         $ucount_userfile++;
918
919         &pSReply("Changed '$user' to '$chnick' successfully.");
920
921         return;
922     }
923
924     if ($message =~ /^([-+])host(\s+(.*))?$/) {
925         my $cmd         = $1."host";
926         my(@args)       = split /[\s\t]+/, $3 || '';
927         my $state       = ($1 eq "+") ? 1 : 0;
928
929         if (!scalar @args) {
930             &help($cmd);
931             return;
932         }
933
934         if ($who eq "_default") {
935             &WARN("$who or verifyuser tried to run $cmd.");
936             return "REPLY";
937         }
938
939         my ($user,$mask);
940         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
941             return unless (&hasFlag("n"));
942             $user       = &getUser($args[0]);
943             $mask       = $args[1];
944         } else {                                # <mask>
945             # FIXME: who or verifyUser. (don't remember why)
946             $user       = &getUser($who);
947             $mask       = $args[0];
948         }
949
950         if (!defined $user) {
951             &pSReply("user $user does not exist.");
952             return;
953         }
954
955         if (!defined $mask) {
956             &pSReply("Hostmasks for $user: " . join(" ", keys %{$users{$user}{HOSTS}}));
957             return;
958         }
959
960         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
961             &pSReply("cannto change masks of others.");
962             return;
963         }
964
965         if ($mask !~ /^$mask{nuh}$/) {
966             &pSReply("error: mask ($mask) is not a real hostmask.");
967             return;
968         }
969
970         my $count = scalar keys %{ $users{$user}{HOSTS} };
971
972         if ($state) {                           # add.
973             if (exists $users{$user}{HOSTS}{$mask}) {
974                 &pSReply("mask $mask already exists.");
975                 return;
976             }
977
978             ### TODO: override support.
979             $users{$user}{HOSTS}{$mask} = 1;
980
981             &pSReply("Added $mask to list of masks.");
982
983         } else {                                # delete.
984
985             if (!exists $users{$user}{HOSTS}{$mask}) {
986                 &pSReply("mask $mask does not exist.");
987                 return;
988             }
989
990             ### TODO: wildcard support. ?
991             delete $users{$user}{HOSTS}{$mask};
992
993             if (scalar keys %{ $users{$user}{HOSTS} } != $count) {
994                 &pSReply("Removed $mask from list of masks.");
995             } else {
996                 &pSReply("error: could not find $mask in list of masks.");
997                 return;
998             }
999         }
1000
1001         $utime_userfile = time();
1002         $ucount_userfile++;
1003
1004         return;
1005     }
1006
1007     if ($message =~ /^([-+])ban(\s+(.*))?$/) {
1008         my $cmd         = $1."ban";
1009         my $flatarg     = $3;
1010         my(@args)       = split /[\s\t]+/, $3 || '';
1011         my $state       = ($1 eq "+") ? 1 : 0;
1012
1013         if (!scalar @args) {
1014             &help($cmd);
1015             return;
1016         }
1017
1018         my($mask,$chan,$time,$reason);
1019
1020         if ($flatarg =~ s/^($mask{nuh})\s*//) {
1021             $mask = $1;
1022         } else {
1023             &DEBUG("arg does not contain nuh mask?");
1024         }
1025
1026         if ($flatarg =~ s/^($mask{chan})\s*//) {
1027             $chan = $1;
1028         } else {
1029             $chan = "*";        # _default instead?
1030         }
1031
1032         if ($state == 0) {              # delete.
1033             my @c = &banDel($mask);
1034
1035             foreach (@c) {
1036                 &unban($mask, $_);
1037             }
1038
1039             if (@c) {
1040                 &pSReply("Removed $mask from chans: @c");
1041             } else {
1042                 &pSReply("$mask was not found in ban list.");
1043             }
1044
1045             return;
1046         }
1047
1048         ###
1049         # add ban.
1050         ###
1051
1052         # time.
1053         if ($flatarg =~ s/^(\d+)\s*//) {
1054             $time = $1;
1055             &DEBUG("time = $time.");
1056             if ($time < 0) {
1057                 &pSReply("error: time cannot be negatime?");
1058                 return;
1059             }
1060         } else {
1061             $time = 0;
1062         }
1063
1064         if ($flatarg =~ s/^(.*)$//) {   # need length?
1065             $reason     = $1;
1066         }
1067
1068         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
1069             &pSReply("cannto change masks of others.");
1070             return;
1071         }
1072
1073         if ($mask !~ /^$mask{nuh}$/) {
1074             &pSReply("error: mask ($mask) is not a real hostmask.");
1075             return;
1076         }
1077
1078         if ( &banAdd($mask,$chan,$time,$reason) == 2) {
1079             &pSReply("ban already exists; overwriting.");
1080         }
1081         &pSReply("Added $mask for $chan (time => $time, reason => $reason)");
1082
1083         return;
1084     }
1085
1086     if ($message =~ /^whois(\s+(.*))?$/) {
1087         my $arg = $2;
1088
1089         if (!defined $arg) {
1090             &help("whois");
1091             return;
1092         }
1093
1094         my $user = &getUser($arg);
1095         if (!defined $user) {
1096             &pSReply("whois: user $user does not exist.");
1097             return;
1098         }
1099
1100         ### TODO: better (eggdrop-like) output.
1101         &pSReply("user: $user");
1102         foreach (keys %{ $users{$user} }) {
1103             my $ref = ref $users{$user}{$_};
1104
1105             if ($ref eq "HASH") {
1106                 my $type = $_;
1107                 ### DOES NOT WORK???
1108                 foreach (keys %{ $users{$user}{$type} }) {
1109                     &pSReply("    $type => $_");
1110                 }
1111                 next;
1112             }
1113
1114             &pSReply("    $_ => $users{$user}{$_}");
1115         }
1116         &pSReply("End of USER whois.");
1117
1118         return;
1119     }
1120
1121     if ($message =~ /^bans(\s+(.*))?$/) {
1122         my $arg = $2;
1123
1124         if (defined $arg) {
1125             if ($arg ne "_default" and !&validChan($arg) ) {
1126                 &pSReply("error: chan $chan is invalid.");
1127                 return;
1128             }
1129         }
1130
1131         if (!scalar keys %bans) {
1132             &pSReply("Ban list is empty.");
1133             return;
1134         }
1135
1136         my $c;
1137         &pSReply("     mask: expire, time-added, count, who-by, reason");
1138         foreach $c (keys %bans) {
1139             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1140             &pSReply("  $c:");
1141
1142             foreach (keys %{ $bans{$c} }) {
1143                 my $val = $bans{$c}{$_};
1144
1145                 if (ref $val eq "ARRAY") {
1146                     my @array = @{ $val };
1147                     &pSReply("    $_: @array");
1148                 } else {
1149                     &DEBUG("unknown ban: $val");
1150                 }
1151             }
1152         }
1153         &pSReply("END of bans.");
1154
1155         return;
1156     }
1157
1158     if ($message =~ /^banlist(\s+(.*))?$/) {
1159         my $arg = $2;
1160
1161         if (defined $arg and $arg !~ /^$mask{chan}$/) {
1162             &pSReply("error: chan $chan is invalid.");
1163             return;
1164         }
1165
1166         &DEBUG("bans for global or arg => $arg.");
1167         foreach (keys %bans) {                  #CHANGE!!!
1168             &DEBUG("  $_ => $bans{$_}.");
1169         }
1170
1171         &DEBUG("End of bans.");
1172         &pSReply("END of bans.");
1173
1174         return;
1175     }
1176
1177     if ($message =~ /^save$/) {
1178         return unless (&hasFlag("o"));
1179
1180         &writeUserFile();
1181         &writeChanFile();
1182         &pSReply("saved user and chan files");
1183
1184         return;
1185     }
1186
1187     ### ALIASES.
1188     $message =~ s/^addignore/+ignore/;
1189     $message =~ s/^(del|un)ignore/-ignore/;
1190
1191     # ignore.
1192     if ($message =~ /^(\+|\-)ignore(\s+(.*))?$/i) {
1193         return unless (&hasFlag("o"));
1194         my $state       = ($1 eq "+") ? 1 : 0;
1195         my $str         = $1."ignore";
1196         my $args        = $3;
1197
1198         if (!$args) {
1199             &help($str);
1200             return;
1201         }
1202
1203         my($mask,$chan,$time,$comment);
1204
1205         # mask.
1206         if ($args =~ s/^($mask{nuh})\s*//) {
1207             $mask = $1;
1208         } else {
1209             &ERROR("no NUH mask?");
1210             return;
1211         }
1212
1213         if (!$state) {                  # delignore.
1214             if ( &ignoreDel($mask) ) {
1215                 &pSReply("ok, deleted ignores for $mask.");
1216             } else {
1217                 &pSReply("could not find $mask in ignore list.");
1218             }
1219             return;
1220         }
1221
1222         ###
1223         # addignore.
1224         ###
1225
1226         # chan.
1227         if ($args =~ s/^($mask{chan}|\*)\s*//) {
1228             $chan = $1;
1229         } else {
1230             $chan = "*";
1231         }
1232
1233         # time.
1234         if ($args =~ s/^(\d+)\s*//) {
1235             $time = $1; # time is in minutes
1236         } else {
1237             $time = 0;
1238         }
1239
1240         # time.
1241         if ($args) {
1242             $comment = $args;
1243         } else {
1244             $comment = "added by $who";
1245         }
1246
1247         if ( &ignoreAdd($mask, $chan, $time, $comment) > 1) {
1248             &pSReply("FIXME: $mask already in ignore list; written over anyway.");
1249         } else {
1250             &pSReply("added $mask to ignore list.");
1251         }
1252
1253         return;
1254     }
1255
1256     if ($message =~ /^ignore(\s+(.*))?$/) {
1257         my $arg = $2;
1258
1259         if (defined $arg) {
1260             if ($arg !~ /^$mask{chan}$/) {
1261                 &pSReply("error: chan $chan is invalid.");
1262                 return;
1263             }
1264
1265             if (!&validChan($arg)) {
1266                 &pSReply("error: chan $arg is invalid.");
1267                 return;
1268             }
1269
1270             &pSReply("Showing bans for $arg only.");
1271         }
1272
1273         if (!scalar keys %ignore) {
1274             &pSReply("Ignore list is empty.");
1275             return;
1276         }
1277
1278         ### TODO: proper (eggdrop-like) formatting.
1279         my $c;
1280         &pSReply("    mask: expire, time-added, who, comment");
1281         foreach $c (keys %ignore) {
1282             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1283             &pSReply("  $c:");
1284
1285             foreach (keys %{ $ignore{$c} }) {
1286                 my $ref = ref $ignore{$c}{$_};
1287                 if ($ref eq "ARRAY") {
1288                     my @array = @{ $ignore{$c}{$_} };
1289                     &pSReply("      $_: @array");
1290                 } else {
1291                     &DEBUG("unknown ignore line?");
1292                 }
1293             }
1294         }
1295         &pSReply("END of ignore.");
1296
1297         return;
1298     }
1299
1300     # adduser/deluser.
1301     if ($message =~ /^(\+|\-|add|del)user(\s+(.*))?$/i) {
1302         my $str         = $1;
1303         my $strstr      = $1."user";
1304         my @args        = split /\s+/, $3 || '';
1305         my $args        = $3;
1306         my $state       = ($str =~ /^(\+|add)$/) ? 1 : 0;
1307
1308         if (!scalar @args) {
1309             &help($strstr);
1310             return;
1311         }
1312
1313         if ($str eq "+") {
1314             if (scalar @args != 2) {
1315                 &pSReply(".+host requires hostmask argument.");
1316                 return;
1317             }
1318         } elsif (scalar @args != 1) {
1319             &pSReply("too many arguments.");
1320             return;
1321         }
1322
1323         if ($state) {                   # adduser.
1324             if (scalar @args == 1) {
1325                 $args[1]        = &getHostMask($args[0]);
1326                 &pSReply("Attemping to guess $args[0]'s hostmask...");
1327
1328                 # crude hack... crappy Net::IRC
1329                 $conn->schedule(5, sub {
1330         # hopefully this is right.
1331         my $nick = (keys %{ $cache{nuhInfo} })[0];
1332         if (!defined $nick) {
1333             &pSReply("couldn't get nuhinfo... adding user without a hostmask.");
1334             &userAdd($nick);
1335             return;
1336         }
1337
1338         my $mask = &makeHostMask( $cache{nuhInfo}{$nick}{NUH} );
1339
1340         if ( &userAdd($nick, $mask) ) { # success.
1341                 &pSReply("Added $nick with flags $users{$nick}{FLAGS}");
1342                 my @hosts = keys %{ $users{$nick}{HOSTS} };
1343                 &pSReply("hosts: @hosts");
1344         }
1345 });
1346                 return;
1347             }
1348
1349             &DEBUG("args => @args");
1350             if ( &userAdd(@args) ) {    # success.
1351                 &pSReply("Added $args[0] with flags $users{$args[0]}{FLAGS}");
1352                 my @hosts = keys %{ $users{$args[0]}{HOSTS} };
1353                 &pSReply("hosts: @hosts");
1354
1355             } else {                    # failure.
1356                 &pSReply("User $args[0] already exists");
1357             }
1358
1359         } else {                        # deluser.
1360
1361             if ( &userDel($args[0]) ) { # success.
1362                 &pSReply("Deleted $args[0] successfully.");
1363
1364             } else {                    # failure.
1365                 &pSReply("User $args[0] does not exist.");
1366             }
1367
1368         }
1369         return;
1370     }
1371
1372     if ($message =~ /^sched$/) {
1373         my @list;
1374         my @run;
1375
1376         my %time;
1377         foreach (keys %sched) {
1378             next unless (exists $sched{$_}{TIME});
1379             $time{ $sched{$_}{TIME}-time() }{$_} = 1;
1380             push(@list,$_);
1381
1382             next unless (exists $sched{$_}{RUNNING});
1383             push(@run,$_);
1384         }
1385
1386         my @time;
1387         foreach (sort { $a <=> $b } keys %time) {
1388             my $str = join(", ", sort keys %{ $time{$_} });
1389             &DEBUG("time => $_, str => $str");
1390             push(@time, "$str (".&Time2String($_).")");
1391         }
1392
1393         &pSReply( &formListReply(0, "Schedulers: ", @time ) );
1394         &pSReply( &formListReply(0, "Scheds to run: ", sort @list ) );
1395         &pSReply( &formListReply(0, "Scheds running(should not happen?) ", sort @run ) );
1396
1397         return;
1398     }
1399
1400     # quite a cool hack: reply in DCC CHAT.
1401     $msgType = "chat" if (exists $dcc{'CHAT'}{$who});
1402
1403     my $done = 0;
1404     $done++ if &parseCmdHook("main", $message);
1405     $done++ if &parseCmdHook("extra", $message);
1406     $done++ unless (&Modules());
1407
1408     if ($done) {
1409         &DEBUG("running non DCC CHAT command inside DCC CHAT!");
1410         return;
1411     }
1412
1413     return "REPLY";
1414 }
1415
1416 1;