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