From b68bc95f4062a0694ca45d2335c2c34d35e9b341 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Tue, 22 Dec 2020 22:26:31 -0800 Subject: [PATCH] Add new function to add a new section --- dovecot/dovecot-lib.pl | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/dovecot/dovecot-lib.pl b/dovecot/dovecot-lib.pl index b0778ef09..b0272f0f7 100755 --- a/dovecot/dovecot-lib.pl +++ b/dovecot/dovecot-lib.pl @@ -295,7 +295,6 @@ elsif (!$dir && defined($value)) { # save_section(&conf, §ion) # Updates one section in the config file -# XXX when adding a section, the part to replace shouldn't depend on eline sub save_section { local ($conf, $section) = @_; @@ -322,6 +321,47 @@ foreach my $m (@{$section->{'members'}}) { } } +# create_section(&conf, §ion, [&parent]) +# Adds a section to the config file +sub create_section +{ +local ($conf, $section, $parent) = @_; +local $indent = " " x $section->{'indent'}; +local @newlines; +push(@newlines, $indent.$section->{'name'}." ".$section->{'value'}." {"); +foreach my $m (@{$section->{'members'}}) { + push(@newlines, $indent." ".$m->{'name'}." = ".$m->{'value'}); + } +push(@newlines, $indent."}"); +my $file; +my $lref; +if ($parent) { + # Add to another config block + $file = $parent->{'file'}; + $lref = &read_file_lines($file); + $section->{'line'} = $parent->{'eline'}; + } +else { + # Add to the end of the global config file + $file = &get_config_file(); + $lref = &read_file_lines($file); + $section->{'line'} = scalar(@$lref); + } +splice(@$lref, $section->{'line'}, 0, @newlines); +&renumber($conf, $section->{'eline'}, $section->{'file'}, + scalar(@newlines)-$oldlen); +$section->{'eline'} = $section->{'line'} + scalar(@newlines) - 1; +my $i = 1; +foreach my $m (@{$section->{'members'}}) { + $m->{'line'} = $m->{'eline'} = $section->{'line'} + $i++; + $m->{'space'} = " "; + $m->{'indent'} = $section->{'indent'} + 1; + $m->{'file'} = $section->{'file'}; + $m->{'sectionname'} = $section->{'name'}; + $m->{'sectionvalue'} = $section->{'value'}; + } +} + # renumber(&conf, line, file, offset) sub renumber {