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