Advertisement
Guest User

Untitled

a guest
Jul 6th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.03 KB | None | 0 0
  1. # get_current_value(parameter_name)
  2. # returns a scalar corresponding to the value of the parameter
  3. ## modified to allow main_parameter:subparameter
  4. sub get_current_value
  5. {
  6. # First try to get the value from main.cf directly
  7. my ($name,$key)=split /:/,$_[0];
  8. my $lref = &read_file_lines($config{'postfix_config_file'});
  9. my $out;
  10. my ($begin_flag, $end_flag);
  11. foreach my $l (@$lref) {
  12.     # changes made to this loop by Dan Hartman of Rae Internet /
  13.     # Message Partners for multi-line parsing 2007-06-04
  14.     if ($begin_flag == 1 && $l =~ /\S/ && $l =~ /^(\s+[^#].+)/) {
  15.         # non-comment continuation line, and replace tabs with spaces
  16.         $out .= $1;
  17.         $out =~ s/^\s+/ /;
  18.         }
  19.      if ($l =~ /^\s*([a-z0-9\_]+)\s*=\s*(.*)|^\s*([a-z0-9\_]+)\s*=\s*$/ &&
  20.          $1 . $3 eq $name) {
  21.         # Found the one we're looking for, set a flag
  22.         $out = $2;
  23.         $begin_flag = 1;
  24.         }
  25.      if ($l =~ /^\s*([a-z0-9\_]+)\s*=\s*(.*)|^\s*([a-z0-9\_]+)\s*=\s*$/ &&
  26.          $1 . $3 ne $name && $begin_flag == 1) {
  27.         # after the beginning, another configuration variable
  28.         # found!  Stop!
  29.         $end_flag = 1;
  30.         last;
  31.         }
  32.     }
  33. if (!defined($out)) {
  34.     # Fall back to asking Postfix
  35.     # -h tells postconf not to output the name of the parameter
  36.     $out = &backquote_command("$config{'postfix_config_command'} -c $config_dir -h ".
  37.                   quotemeta($name)." 2>/dev/null", 1);
  38.     if ($?) {
  39.         &error(&text('query_get_efailed', $name, $out));
  40.         }
  41.     elsif ($out =~ /warning:.*unknown\s+parameter/) {
  42.         return undef;
  43.         }
  44.     chop($out);
  45.     }
  46. else {
  47.     # Trim trailing whitespace
  48.     $out =~ s/\s+$//;
  49.     }
  50. if ($key) {
  51.     # If the value asked for was like foo:bar, extract from the value
  52.     # the parts after bar
  53.     my @res = ( );
  54.         while($out =~ /^(.*?)\Q$key\E\s+(\S+)(.*)$/) {
  55.         my $v = $2;
  56.         $out = $3;
  57.         $v =~ s/,$//;
  58.         push(@res, $v);
  59.         }
  60.     return join(" ", @res);
  61.     }
  62. return $out;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement