From: martin f. krafft Date: Fri, 10 Sep 2021 00:39:07 +0000 (+0200) Subject: Put the type arrays into a hash for easier use X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=d5a9d34691f18d23e333fa86857066b7e9a0dc32;p=irssi-scripts.irssi.org.git Put the type arrays into a hash for easier use Signed-off-by: martin f. krafft --- diff --git a/scripts/ctrlact.pl b/scripts/ctrlact.pl index 8991943..717b7aa 100644 --- a/scripts/ctrlact.pl +++ b/scripts/ctrlact.pl @@ -160,6 +160,14 @@ sig_setup_changed(); my $changed_since_last_save = 0; +my @window_thresholds; +my @channel_thresholds; +my @query_thresholds; +my %THRESHOLDARRAYS = ('window' => \@window_thresholds, + 'channel' => \@channel_thresholds, + 'query' => \@query_thresholds + ); + my @DATALEVEL_KEYWORDS = ('all', 'messages', 'hilights', 'none'); ### HELPERS #################################################################### @@ -208,10 +216,6 @@ sub error { say("ERROR: ".$msg, MSGLEVEL_CLIENTERROR, $inwin); } -my @window_thresholds; -my @channel_thresholds; -my @query_thresholds; - sub match { my ($pat, $text) = @_; if ($pat =~ m/^(\W)(.+)\1$/) { @@ -276,14 +280,8 @@ sub get_mappings_table { sub get_specific_threshold { my ($type, $name, $net) = @_; $type = lc($type); - if ($type eq 'window') { - return walk_match_array($name, $net, $type, \@window_thresholds); - } - elsif ($type eq 'channel') { - return walk_match_array($name, $net, $type, \@channel_thresholds); - } - elsif ($type eq 'query') { - return walk_match_array($name, $net, $type, \@query_thresholds); + if (exists $THRESHOLDARRAYS{$type}) { + return walk_match_array($name, $net, $type, $THRESHOLDARRAYS{$type}); } else { croak "ctrlact: can't look up threshold for type: $type"; @@ -440,11 +438,11 @@ sub load_mappings { next if m/^\s*(?:#|$)/; my ($type, @matchers) = m/$linesplitter/; @matchers = ['*', @matchers] if ($version eq "1.0"); - push @matchers, $l; - push @window_thresholds, [@matchers] if match($type, 'window'); - push @channel_thresholds, [@matchers] if match($type, 'channel'); - push @query_thresholds, [@matchers] if match($type, 'query'); - $cnt += 1; + push @matchers, sprintf('line %3d', $l); + if (exists $THRESHOLDARRAYS{$type}) { + push @{$THRESHOLDARRAYS{$type}}, [@matchers]; + $cnt += 1; + } } close($fh) || croak "Cannot close mappings file: $!"; return $cnt; @@ -471,14 +469,10 @@ sub save_mappings { # type server name min.level EOF - my %types = ( 'window' => \@window_thresholds, - 'channel' => \@channel_thresholds, - 'query' => \@query_thresholds - ); - while (my ($type, $arr) = each %types) { - while (my ($idx, $elem) = each @{$arr}) { + foreach my $type (sort keys %THRESHOLDARRAYS) { + foreach my $arr (@{$THRESHOLDARRAYS{$type}}) { print FH "$type\t"; - print FH join "\t", @{$elem}[0..2]; + print FH join "\t", @{$arr}[0..2]; print FH "\n"; } }