2 # DynaConfig.pl: Read/Write configuration files dynamically.
4 # Version: v0.1 (20010120)
6 # NOTE: Merged from User.pl
9 if (&IsParam("useStrict")) { use strict; }
12 ##### USERFILE CONFIGURATION READER/WRITER
16 if (!open IN,"$bot_misc_dir/blootbot.users") {
17 &ERROR("cannot read userfile.");
24 &ERROR("old or invalid user file found.");
36 if (/^--(\S+)[\s\t]+(.*)$/) { # user: middle entry.
37 my ($what,$val) = ($1,$2);
40 if ($what eq "HOSTS") {
41 $users{$nick}{$1}{$2} = 1;
43 $users{$nick}{$1} = $2;
46 } elsif (/^(\S+)$/) { # user: start entry.
49 } elsif (/^::(\S+) ignore$/) { # ignore: start entry.
52 } elsif (/^- (\S+):+(\d+):(\S+):(\d+):(.*)$/) {
53 ### ignore: middle entry.
54 # $mask, $count?, $whoby, $atime, $comment.
55 my(@array) = ($2,$3,$4,$5);
56 $ignore{$chan}{$1} = \@array;
58 } elsif (/^::(\S+) bans$/) { # bans: start entry.
61 } elsif (/^- (\S+):+(\d+):+(\d+):(\d+):(\S+):(.*)$/) {
62 ### bans: middle entry.
63 # $btime, $atime, $count, $whoby, $reason.
64 my(@array) = ($2,$3,$4,$5,$6);
65 $bans{$chan}{$1} = \@array;
68 &WARN("unknown line: $_");
73 &status( sprintf("USERFILE: Loaded: %d users, %d bans, %d ignore",
74 scalar(keys %users)-1,
82 if (!open OUT,">$bot_misc_dir/blootbot.users") {
83 &ERROR("cannot write to userfile.");
87 my $time = scalar(localtime);
89 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
93 foreach (sort keys %users) {
96 my $count = scalar keys %{ $users{$user} };
98 &WARN("user $user has no other attributes; skipping.");
104 foreach (sort keys %{ $users{$user} }) {
106 my $val = $users{$user}{$_};
108 if (ref($val) eq "HASH") {
109 foreach (sort keys %{ $users{$user}{$_} }) {
110 print OUT "--$what\t\t$_\n";
114 print OUT "--$_\t\t$val\n";
122 foreach (keys %bans) {
126 my $count = scalar keys %{ $bans{$chan} };
128 &WARN("bans: chan $chan has no other attributes; skipping.");
132 print OUT "::$chan bans\n";
133 &DEBUG("::$chan bans");
134 foreach (keys %{ $bans{$chan} }) {
135 printf OUT "- %s:+%d:+%d:%d:%s:%s\n",
136 $_, @{ $bans{$chan}{$_} };
137 &DEBUG( sprintf("- %s:+%d:+%d:%d:%s:%s\n",
138 $_, @{ $bans{$chan}{$_} } ));
141 print OUT "\n" if ($cbans);
145 foreach (keys %ignore) {
149 my $count = scalar keys %{ $ignore{$chan} };
151 &WARN("ignore: chan $chan has no other attributes; skipping.");
155 print OUT "::$chan ignore\n";
156 &DEBUG("::$chan ignore");
157 foreach (keys %{ $ignore{$chan} }) {
158 printf OUT "- %s:+%d:%s:%d:%s\n",
159 $_, @{ $bans{$chan}{$_} };
160 &DEBUG( sprintf("- %s:+%d:%s:%d:%s\n",
161 $_, @{ $bans{$chan}{$_} } ));
167 $wtime_userfile = time();
168 &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
169 if (defined $msgType and $msgType =~ /^chat$/) {
170 &performStrictReply("--- Writing user file...");
175 ##### CHANNEL CONFIGURATION READER/WRITER
179 if (!open IN,"$bot_misc_dir/blootbot.chan") {
180 &ERROR("cannot erad chanfile.");
184 $_ = <IN>; # version string.
196 next unless (defined $chan);
198 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
199 $chanconf{$chan}{$1} = 1;
201 } elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
202 $chanconf{$chan}{$1} = 0;
204 } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
205 $chanconf{$chan}{$1} = $2;
208 &WARN("unknown line: $_") unless (/^#/);
213 &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
217 if (!open OUT,">$bot_misc_dir/blootbot.chan") {
218 &ERROR("cannot write chanfile.");
222 my $time = scalar(localtime);
223 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
227 ### Process 1: if defined in _default, remove same definition
228 ### from non-default channels.
229 foreach (keys %{ $chanconf{_default} }) {
231 my $val = $chanconf{_default}{$opt};
234 foreach (keys %chanconf) {
237 next if ($chan eq "_default");
238 next unless (exists $chanconf{$chan}{$opt});
239 next unless ($val eq $chanconf{$chan}{$opt});
241 delete $chanconf{$chan}{$opt};
245 &DEBUG("Removed config $opt to @chans since it's defiend in '_default'");
249 ### Process 2: if defined in all chans but _default, set in
250 ### _default and remove all others.
251 my (%optsval, %opts);
252 foreach (keys %chanconf) {
254 next if ($chan eq "_default");
257 foreach (keys %{ $chanconf{$chan} }) {
259 if (exists $optsval{$opt} and $optsval{$opt} eq $chanconf{$chan}{$opt}) {
263 $optsval{$opt} = $chanconf{$chan}{$opt};
268 &DEBUG("chans => ".scalar(keys %chanconf)." - 1");
269 foreach (keys %opts) {
270 &DEBUG(" opts{$_} => $opts{$_}");
273 ### other optimizations are in UserDCC.pl
277 foreach (sort keys %chanconf) {
282 foreach (sort keys %{ $chanconf{$chan} }) {
283 my $val = $chanconf{$chan}{$_};
285 if ($val =~ /^0$/) { # bool, false.
288 } elsif ($val =~ /^1$/) { # bool, true.
291 } else { # what = val.
292 print OUT " $_ $val\n";
302 $wtime_chanfile = time();
303 &status("--- Saved CHANFILE (".scalar(keys %chanconf).
306 if (defined $msgType and $msgType =~ /^chat$/) {
307 &performStrictReply("--- Writing chan file...");
317 my ($ret, $f, $o) = "";
319 foreach $f (split //, $users{$userHandle}{FLAGS}) {
320 foreach $o ( split //, $flags ) {
321 next unless ($f eq $o);
332 my ($nick, $lnuh) = @_;
337 foreach $user (keys %users) {
338 next if ($user eq "_default");
340 foreach $m (keys %{$users{$user}{HOSTS}}) {
343 $m =~ s/([\@\(\)\[\]])/\\$1/g;
345 next unless ($lnuh =~ /^$m$/i);
347 if ($user !~ /^\Q$nick\E$/i) {
348 &status("vU: host matched but diff nick ($nick != $user).");
355 last if ($userHandle ne "");
357 if ($user =~ /^\Q$nick\E$/i) {
358 &status("vU: nick matched but host is not in list ($lnuh).");
362 $userHandle ||= "_default";
363 $talkWho{$talkchannel} = $who if (defined $talkchannel);
370 # returns true if arg1 encrypts to arg2
371 my ($plain, $encrypted) = @_;
372 if ($encrypted eq "") {
373 ($plain, $encrypted) = split(/\s+/, $plain, 2);
375 return 0 unless ($plain ne "" and $encrypted ne "");
377 # MD5 // DES. Bobby Billingsley++.
378 my $salt = substr($encrypted, 0, 2);
379 if ($encrypted =~ /^\$\d\$(\w\w)\$/) {
383 return ($encrypted eq crypt($plain, $salt));
386 # mainly for dcc chat... hrm.
390 if (&IsFlag($flag) eq $flag) {
393 &status("DCC CHAT: <$who> $message -- not enough flags.");
394 &performStrictReply("error: you do not have enough flags for that. ($flag required)");
400 my($mask,$chan,$expire,$comment) = @_;
402 $chan ||= "*"; # global if undefined.
403 $comment ||= ""; # optional.
404 $expire ||= 0; # permament.
408 $expire = $expire*60 + time();
413 $ignore{$chan}{$mask} = [$expire, $count, $who, time(), $comment];
415 if (exists $ignore{$chan}{$mask}) {
418 $utime_userfile = time();
429 ### TODO: support wildcards.
430 foreach (keys %ignore) {
433 foreach (grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} }) {
434 delete $ignore{$chan}{$mask};
438 &DEBUG("iD: scalar => ".scalar(keys %{ $ignore{$chan} }) );
442 $utime_userfile = time();
450 my($nick,$mask) = @_;
452 if (exists $users{$nick}) {
456 $utime_userfile = time();
459 $users{$nick}{HOSTS}{$mask} = 1;
460 $users{$nick}{FLAGS} = $users{_default}{FLAGS};
468 if (!exists $users{$nick}) {
472 $utime_userfile = time();
475 delete $users{$nick};
481 my($mask,$chan,$expire,$reason) = @_;
487 $expire = $expire*60 + time();
491 $exist++ if (exists $bans{$chan}{$mask} or
492 exists $bans{_default}{$mask});
493 $bans{$chan}{$mask} = [$expire, 0, $who, time(), $reason];
496 $utime_userfile = time();
507 foreach (keys %bans) {
510 foreach (grep /^\Q$mask\E$/i, keys %{ $bans{$chan} }) {
511 delete $bans{$chan}{$_};
515 &DEBUG("bans: scalar => ".scalar(keys %{ $bans{$chan} }) );
519 $utime_userfile = time();
529 if ( &getUser($user) ) {
539 if (!defined $user) {
540 &WARN("getUser: user == NULL.");
544 if (my @retval = grep /^\Q$user\E$/i, keys %users) {
545 if ($retval[0] ne $user) {
546 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
548 my $count = scalar keys %{ $users{$retval[0]} };
549 &DEBUG("count => $count.");
560 "limitcheckInterval",
564 ### TODO: finish off this list.
567 my @regFlagsUser = ("mno"); # todo...