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