]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
sort flags on chattr
[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         &performStrictReply("Start of who ($count users).");
38         foreach (keys %{ $dcc{'CHAT'} }) {
39             &performStrictReply("=> $_");
40         }
41         &performStrictReply("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         &performStrictReply($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 (&IsParam("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                 &performStrictReply("op: $opee already has ops on $_");
288                 next;
289             }
290             $op++;
291
292             &performStrictReply("opping $opee on $_");
293             &op($_, $opee);
294         }
295
296         if ($found != $op) {
297             &performStrictReply("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         &performStrictReply("reloading...");
493         &reloadAllModules();
494         &performStrictReply("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             &performStrictReply("no such channel $args[0]");
552             return;
553         }
554
555         &performStrictReply("showing channel conf.");
556         foreach (sort keys %{ $chanconf{$args[0]} }) {
557             &performStrictReply("$chan: $_ => $chanconf{$args[0]}{$_}");
558         }
559         &performStrictReply("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             &performStrictReply("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                 &performStrictReply("  $what = $_(" . scalar(keys %{$vals{$_}}) . "): ".join(' ', sort keys %{ $vals{$_} } ) );
607             }
608
609             &performStrictReply("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                     &performStrictReply("Default channel settings");
621                 } else {
622                     &performStrictReply("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                         &performStrictReply(" $str");
631                         @items = ();
632                     }
633                     $str = $newstr;
634                     push(@items, "$_ => $chanconf{$chan}{$_}");
635                 }
636                 &performStrictReply(" $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             &performStrictReply("no such channel $chan");
674             return;
675         }
676
677         if ($args ne "") {
678
679             if (!&getChanConf($args,$chan)) {
680                 &performStrictReply("$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                 &performStrictReply("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                 &performStrictReply("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             &performStrictReply("Unsetting channel ($chan) option $args. (was $chanconf{$chan}{$args})");
715             delete $chanconf{$chan}{$args};
716
717             return;
718         }
719
720         if ($delete) {
721             &performStrictReply("Deleting channel $chan for sure!");
722             $utime_chanfile = time();
723             $ucount_chanfile++;
724
725             &part($chan);
726             &performStrictReply("Leaving $chan...");
727
728             delete $chanconf{$chan};
729         } else {
730             &performStrictReply("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         &performStrictReply("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             &performStrictReply("user $args[0] is not valid.");
766             return;
767         }
768
769         my $u = &getUser($args[0]);
770         if (!defined $u) {
771             &performStrictReply("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                 &performStrictReply("cannot remove passwd of others.");
779                 return;
780             }
781
782             if (!exists $users{$u}{PASS}) {
783                 &performStrictReply("$u does not have pass set anyway.");
784                 return;
785             }
786
787             &performStrictReply("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         &performStrictReply("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             &performStrictReply("user does not exist.");
831             return;
832         }
833
834         my $flags = $users{$user}{FLAGS};
835         if (!defined $chflag) {
836             &performStrictReply("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             &performStrictReply("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                 &performStrictReply("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                     &performStrictReply("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             #$flags.*FLAGS sort
879             $flags = join('', sort split('', $flags));
880             &performStrictReply("Current flags: $flags");
881             $users{$user}{FLAGS} = $flags;
882         } else {
883             &performStrictReply("No flags changed: $flags");
884         }
885
886         return;
887     }
888
889     if ($message =~ /^chnick(\s+(.*))?$/) {
890         my(@args) = split /[\s\t]+/, $2 || '';
891
892         if ($who eq "_default") {
893             &WARN("$who or verifyuser tried to run chnick.");
894             return "REPLY";
895         }
896
897         if (!scalar @args or scalar @args > 2) {
898             &help("chnick");
899             return;
900         }
901
902         if (scalar @args == 1) {        # 1
903             $user       = &getUser($who);
904             &DEBUG("nope, not $who.") unless (defined $user);
905             $user       ||= &getUser($verifyUser);
906             $chnick     = $args[0];
907         } else {                        # 2
908             $user       = &getUser($args[0]);
909             $chnick     = $args[1];
910         }
911
912         if (!defined $user) {
913             &performStrictReply("user $who or $args[0] does not exist.");
914             return;
915         }
916
917         if ($user =~ /^\Q$chnick\E$/i) {
918             &performStrictReply("user == chnick. why should I do that?");
919             return;
920         }
921
922         if (&getUser($chnick)) {
923             &performStrictReply("user $chnick is already used!");
924             return;
925         }
926
927         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
928             &performStrictReply("cannto change nick of others.");
929             return "REPLY" if ($who eq "_default");
930             return;
931         }
932
933         foreach (keys %{ $users{$user} }) {
934             $users{$chnick}{$_} = $users{$user}{$_};
935             delete $users{$user}{$_};
936         }
937         undef $users{$user};    # ???
938
939         $utime_userfile = time();
940         $ucount_userfile++;
941
942         &performStrictReply("Changed '$user' to '$chnick' successfully.");
943
944         return;
945     }
946
947     if ($message =~ /^([-+])host(\s+(.*))?$/) {
948         my $cmd         = $1."host";
949         my(@args)       = split /[\s\t]+/, $3 || '';
950         my $state       = ($1 eq "+") ? 1 : 0;
951
952         if (!scalar @args) {
953             &help($cmd);
954             return;
955         }
956
957         if ($who eq "_default") {
958             &WARN("$who or verifyuser tried to run $cmd.");
959             return "REPLY";
960         }
961
962         my ($user,$mask);
963         if ($args[0] =~ /^$mask{nick}$/i) {     # <nick>
964             return unless (&hasFlag("n"));
965             $user       = &getUser($args[0]);
966             $mask       = $args[1];
967         } else {                                # <mask>
968             # FIXME: who or verifyUser. (don't remember why)
969             $user       = &getUser($who);
970             $mask       = $args[0];
971         }
972
973         if (!defined $user) {
974             &performStrictReply("user $user does not exist.");
975             return;
976         }
977
978         if (!defined $mask) {
979             &performStrictReply("Hostmasks for $user: " . join(" ", keys %{$users{$user}{HOSTS}}));
980             return;
981         }
982
983         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
984             &performStrictReply("cannto change masks of others.");
985             return;
986         }
987
988         my $count = scalar keys %{ $users{$user}{HOSTS} };
989
990         if ($state) {                           # add.
991             if ($mask !~ /^$mask{nuh}$/) {
992                 &performStrictReply("error: mask ($mask) is not a real hostmask.");
993                 return;
994             }
995
996             if (exists $users{$user}{HOSTS}{$mask}) {
997                 &performStrictReply("mask $mask already exists.");
998                 return;
999             }
1000
1001             ### TODO: override support.
1002             $users{$user}{HOSTS}{$mask} = 1;
1003
1004             &performStrictReply("Added $mask to list of masks.");
1005
1006         } else {                                # delete.
1007
1008             if (!exists $users{$user}{HOSTS}{$mask}) {
1009                 &performStrictReply("mask $mask does not exist.");
1010                 return;
1011             }
1012
1013             ### TODO: wildcard support. ?
1014             delete $users{$user}{HOSTS}{$mask};
1015
1016             if (scalar keys %{ $users{$user}{HOSTS} } != $count) {
1017                 &performStrictReply("Removed $mask from list of masks.");
1018             } else {
1019                 &performStrictReply("error: could not find $mask in list of masks.");
1020                 return;
1021             }
1022         }
1023
1024         $utime_userfile = time();
1025         $ucount_userfile++;
1026
1027         return;
1028     }
1029
1030     if ($message =~ /^([-+])ban(\s+(.*))?$/) {
1031         my $cmd         = $1."ban";
1032         my $flatarg     = $3;
1033         my(@args)       = split /[\s\t]+/, $3 || '';
1034         my $state       = ($1 eq "+") ? 1 : 0;
1035
1036         if (!scalar @args) {
1037             &help($cmd);
1038             return;
1039         }
1040
1041         my($mask,$chan,$time,$reason);
1042
1043         if ($flatarg =~ s/^($mask{nuh})\s*//) {
1044             $mask = $1;
1045         } else {
1046             &DEBUG("arg does not contain nuh mask?");
1047         }
1048
1049         if ($flatarg =~ s/^($mask{chan})\s*//) {
1050             $chan = $1;
1051         } else {
1052             $chan = "*";        # _default instead?
1053         }
1054
1055         if ($state == 0) {              # delete.
1056             my @c = &banDel($mask);
1057
1058             foreach (@c) {
1059                 &unban($mask, $_);
1060             }
1061
1062             if (@c) {
1063                 &performStrictReply("Removed $mask from chans: @c");
1064             } else {
1065                 &performStrictReply("$mask was not found in ban list.");
1066             }
1067
1068             return;
1069         }
1070
1071         ###
1072         # add ban.
1073         ###
1074
1075         # time.
1076         if ($flatarg =~ s/^(\d+)\s*//) {
1077             $time = $1;
1078             &DEBUG("time = $time.");
1079             if ($time < 0) {
1080                 &performStrictReply("error: time cannot be negatime?");
1081                 return;
1082             }
1083         } else {
1084             $time = 0;
1085         }
1086
1087         if ($flatarg =~ s/^(.*)$//) {   # need length?
1088             $reason     = $1;
1089         }
1090
1091         if (!&IsFlag("n") and $who !~ /^\Q$verifyUser\E$/i) {
1092             &performStrictReply("cannto change masks of others.");
1093             return;
1094         }
1095
1096         if ($mask !~ /^$mask{nuh}$/) {
1097             &performStrictReply("error: mask ($mask) is not a real hostmask.");
1098             return;
1099         }
1100
1101         if ( &banAdd($mask,$chan,$time,$reason) == 2) {
1102             &performStrictReply("ban already exists; overwriting.");
1103         }
1104         &performStrictReply("Added $mask for $chan (time => $time, reason => $reason)");
1105
1106         return;
1107     }
1108
1109     if ($message =~ /^whois(\s+(.*))?$/) {
1110         my $arg = $2;
1111
1112         if (!defined $arg) {
1113             &help("whois");
1114             return;
1115         }
1116
1117         my $user = &getUser($arg);
1118         if (!defined $user) {
1119             &performStrictReply("whois: user $user does not exist.");
1120             return;
1121         }
1122
1123         ### TODO: better (eggdrop-like) output.
1124         &performStrictReply("user: $user");
1125         foreach (keys %{ $users{$user} }) {
1126             my $ref = ref $users{$user}{$_};
1127
1128             if ($ref eq "HASH") {
1129                 my $type = $_;
1130                 ### DOES NOT WORK???
1131                 foreach (keys %{ $users{$user}{$type} }) {
1132                     &performStrictReply("    $type => $_");
1133                 }
1134                 next;
1135             }
1136
1137             &performStrictReply("    $_ => $users{$user}{$_}");
1138         }
1139         &performStrictReply("End of USER whois.");
1140
1141         return;
1142     }
1143
1144     if ($message =~ /^bans(\s+(.*))?$/) {
1145         my $arg = $2;
1146
1147         if (defined $arg) {
1148             if ($arg ne "_default" and !&validChan($arg) ) {
1149                 &performStrictReply("error: chan $chan is invalid.");
1150                 return;
1151             }
1152         }
1153
1154         if (!scalar keys %bans) {
1155             &performStrictReply("Ban list is empty.");
1156             return;
1157         }
1158
1159         my $c;
1160         &performStrictReply("     mask: expire, time-added, count, who-by, reason");
1161         foreach $c (keys %bans) {
1162             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1163             &performStrictReply("  $c:");
1164
1165             foreach (keys %{ $bans{$c} }) {
1166                 my $val = $bans{$c}{$_};
1167
1168                 if (ref $val eq "ARRAY") {
1169                     my @array = @{ $val };
1170                     &performStrictReply("    $_: @array");
1171                 } else {
1172                     &DEBUG("unknown ban: $val");
1173                 }
1174             }
1175         }
1176         &performStrictReply("END of bans.");
1177
1178         return;
1179     }
1180
1181     if ($message =~ /^banlist(\s+(.*))?$/) {
1182         my $arg = $2;
1183
1184         if (defined $arg and $arg !~ /^$mask{chan}$/) {
1185             &performStrictReply("error: chan $chan is invalid.");
1186             return;
1187         }
1188
1189         &DEBUG("bans for global or arg => $arg.");
1190         foreach (keys %bans) {                  #CHANGE!!!
1191             &DEBUG("  $_ => $bans{$_}.");
1192         }
1193
1194         &DEBUG("End of bans.");
1195         &performStrictReply("END of bans.");
1196
1197         return;
1198     }
1199
1200     if ($message =~ /^save$/) {
1201         return unless (&hasFlag("o"));
1202
1203         &writeUserFile();
1204         &writeChanFile();
1205         &performStrictReply("saved user and chan files");
1206
1207         return;
1208     }
1209
1210     ### ALIASES.
1211     $message =~ s/^addignore/+ignore/;
1212     $message =~ s/^(del|un)ignore/-ignore/;
1213
1214     # ignore.
1215     if ($message =~ /^(\+|\-)ignore(\s+(.*))?$/i) {
1216         return unless (&hasFlag("o"));
1217         my $state       = ($1 eq "+") ? 1 : 0;
1218         my $str         = $1."ignore";
1219         my $args        = $3;
1220
1221         if (!$args) {
1222             &help($str);
1223             return;
1224         }
1225
1226         my($mask,$chan,$time,$comment);
1227
1228         # mask.
1229         if ($args =~ s/^($mask{nuh})\s*//) {
1230             $mask = $1;
1231         } else {
1232             &ERROR("no NUH mask?");
1233             return;
1234         }
1235
1236         if (!$state) {                  # delignore.
1237             if ( &ignoreDel($mask) ) {
1238                 &performStrictReply("ok, deleted ignores for $mask.");
1239             } else {
1240                 &performStrictReply("could not find $mask in ignore list.");
1241             }
1242             return;
1243         }
1244
1245         ###
1246         # addignore.
1247         ###
1248
1249         # chan.
1250         if ($args =~ s/^($mask{chan}|\*)\s*//) {
1251             $chan = $1;
1252         } else {
1253             $chan = "*";
1254         }
1255
1256         # time.
1257         if ($args =~ s/^(\d+)\s*//) {
1258             $time = $1; # time is in minutes
1259         } else {
1260             $time = 0;
1261         }
1262
1263         # time.
1264         if ($args) {
1265             $comment = $args;
1266         } else {
1267             $comment = "added by $who";
1268         }
1269
1270         if ( &ignoreAdd($mask, $chan, $time, $comment) > 1) {
1271             &performStrictReply("FIXME: $mask already in ignore list; written over anyway.");
1272         } else {
1273             &performStrictReply("added $mask to ignore list.");
1274         }
1275
1276         return;
1277     }
1278
1279     if ($message =~ /^ignore(\s+(.*))?$/) {
1280         my $arg = $2;
1281
1282         if (defined $arg) {
1283             if ($arg !~ /^$mask{chan}$/) {
1284                 &performStrictReply("error: chan $chan is invalid.");
1285                 return;
1286             }
1287
1288             if (!&validChan($arg)) {
1289                 &performStrictReply("error: chan $arg is invalid.");
1290                 return;
1291             }
1292
1293             &performStrictReply("Showing bans for $arg only.");
1294         }
1295
1296         if (!scalar keys %ignore) {
1297             &performStrictReply("Ignore list is empty.");
1298             return;
1299         }
1300
1301         ### TODO: proper (eggdrop-like) formatting.
1302         my $c;
1303         &performStrictReply("    mask: expire, time-added, who, comment");
1304         foreach $c (keys %ignore) {
1305             next unless (!defined $arg or $arg =~ /^\Q$c\E$/i);
1306             &performStrictReply("  $c:");
1307
1308             foreach (keys %{ $ignore{$c} }) {
1309                 my $ref = ref $ignore{$c}{$_};
1310                 if ($ref eq "ARRAY") {
1311                     my @array = @{ $ignore{$c}{$_} };
1312                     &performStrictReply("      $_: @array");
1313                 } else {
1314                     &DEBUG("unknown ignore line?");
1315                 }
1316             }
1317         }
1318         &performStrictReply("END of ignore.");
1319
1320         return;
1321     }
1322
1323     # adduser/deluser.
1324     if ($message =~ /^(add|del)user(\s+(.*))?$/i) {
1325         my $str         = $1;
1326         my $strstr      = $1."user";
1327         my @args        = split /\s+/, $3 || '';
1328         my $args        = $3;
1329         my $state       = ($str =~ /^(add)$/) ? 1 : 0;
1330
1331         if (!scalar @args) {
1332             &help($strstr);
1333             return;
1334         }
1335
1336         if ($str eq 'add') {
1337             if (scalar @args != 2) {
1338                 &performStrictReply('adduser requires hostmask argument.');
1339                 return;
1340             }
1341         } elsif (scalar @args != 1) {
1342             &performStrictReply('too many arguments.');
1343             return;
1344         }
1345
1346         if ($state) {
1347             # adduser.
1348             if (scalar @args == 1) {
1349                 $args[1]        = &getHostMask($args[0]);
1350                 &performStrictReply("Attemping to guess $args[0]'s hostmask...");
1351
1352                 # crude hack... crappy Net::IRC
1353                 $conn->schedule(5, sub {
1354                     # hopefully this is right.
1355                     my $nick = (keys %{ $cache{nuhInfo} })[0];
1356                     if (!defined $nick) {
1357                         &performStrictReply("couldn't get nuhinfo... adding user without a hostmask.");
1358                         &userAdd($nick);
1359                         return;
1360                     }
1361                     my $mask = &makeHostMask( $cache{nuhInfo}{$nick}{NUH} );
1362
1363                     if ( &userAdd($nick, $mask) ) {
1364                         # success.
1365                         &performStrictReply("Added $nick with flags $users{$nick}{FLAGS}");
1366                         my @hosts = keys %{ $users{$nick}{HOSTS} };
1367                         &performStrictReply("hosts: @hosts");
1368                     }
1369                 });
1370                 return;
1371             }
1372
1373             &DEBUG("args => @args");
1374             if ( &userAdd(@args) ) {    # success.
1375                 &performStrictReply("Added $args[0] with flags $users{$args[0]}{FLAGS}");
1376                 my @hosts = keys %{ $users{$args[0]}{HOSTS} };
1377                 &performStrictReply("hosts: @hosts");
1378
1379             } else {                    # failure.
1380                 &performStrictReply("User $args[0] already exists");
1381             }
1382
1383         } else {                        # deluser.
1384
1385             if ( &userDel($args[0]) ) { # success.
1386                 &performStrictReply("Deleted $args[0] successfully.");
1387
1388             } else {                    # failure.
1389                 &performStrictReply("User $args[0] does not exist.");
1390             }
1391
1392         }
1393         return;
1394     }
1395
1396     if ($message =~ /^sched$/) {
1397         my @list;
1398         my @run;
1399
1400         my %time;
1401         foreach (keys %sched) {
1402             next unless (exists $sched{$_}{TIME});
1403             $time{ $sched{$_}{TIME}-time() }{$_} = 1;
1404             push(@list,$_);
1405
1406             next unless (exists $sched{$_}{RUNNING});
1407             push(@run,$_);
1408         }
1409
1410         my @time;
1411         foreach (sort { $a <=> $b } keys %time) {
1412             my $str = join(", ", sort keys %{ $time{$_} });
1413             &DEBUG("time => $_, str => $str");
1414             push(@time, "$str (".&Time2String($_).")");
1415         }
1416
1417         &performStrictReply( &formListReply(0, "Schedulers: ", @time ) );
1418         &performStrictReply( &formListReply(0, "Scheds to run: ", sort @list ) );
1419         &performStrictReply( &formListReply(0, "Scheds running(should not happen?) ", sort @run ) );
1420
1421         return;
1422     }
1423
1424     # quite a cool hack: reply in DCC CHAT.
1425     $msgType = "chat" if (exists $dcc{'CHAT'}{$who});
1426
1427     my $done = 0;
1428     $done++ if &parseCmdHook("main", $message);
1429     $done++ if &parseCmdHook("extra", $message);
1430     $done++ unless (&Modules());
1431
1432     if ($done) {
1433         &DEBUG("running non DCC CHAT command inside DCC CHAT!");
1434         return;
1435     }
1436
1437     return "REPLY";
1438 }
1439
1440 1;