<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># mailers-lib.pl
# Functions for editing the mailertable database

# mailers_dbm(&amp;config)
# Returns the filename of the mailertable database and type, or undef if none
sub mailers_dbm
{
foreach $f (&amp;find_type("K", $_[0])) {
        if ($f-&gt;{'value'} =~ /^mailertable\s+(\S+)[^\/]+(\/\S+)$/) {
		return ($2, $1);
                }
	}
return undef;
}

# mailers_file(&amp;config)
# Returns the filename of the text mailertable file, or undef if none
sub mailers_file
{
return &amp;find_textfile($config{'mailers_file'}, &amp;mailers_dbm($_[0]));
}

# list_mailers(textfile)
sub list_mailers
{
if (!scalar(@list_mailers_cache)) {
	local $lnum = 0;
	@list_mailers_cache = ( );
	local $cmt;
	open(MAILER, $_[0]);
	while(&lt;MAILER&gt;) {
		s/\r|\n//g;     # remove newlines
		if (/^\s*#+\s*(.*)/) {
			# A comment line
			$cmt = &amp;is_table_comment($_);
			}
		elsif (/^(\S+)\s+([^: ]+):(.*)/) {
			# An actual mailer line
			local(%virt);
			$virt{'domain'} = $1;
			$virt{'mailer'} = $2;
			$virt{'dest'} = $3;
			$virt{'line'} = $cmt ? $lnum-1 : $lnum;
			$virt{'eline'} = $lnum;
			$virt{'num'} = scalar(@list_mailers_cache);
			$virt{'cmt'} = $cmt;
			push(@list_mailers_cache, \%virt);
			$cmt = undef;
			}
		else {
			$cmt = undef;
			}
		$lnum++;
		}
	close(MAILER);
	}
return @list_mailers_cache;
}

# create_mailer(&amp;details, textfile, dbmfile, dbmtype)
sub create_mailer
{
local(%mailer);
&amp;list_mailers($_[1]);	# force cache init

# Write to the file
local $lref = &amp;read_file_lines($_[1]);
$_[0]-&gt;{'line'} = scalar(@$lref);
push(@$lref, &amp;make_table_comment($_[0]-&gt;{'cmt'}));
push(@$lref, "$_[0]-&gt;{'domain'}\t$_[0]-&gt;{'mailer'}:$_[0]-&gt;{'dest'}");
$_[0]-&gt;{'eline'} = scalar(@$lref)-1;
&amp;flush_file_lines();

# Write to the DBM
if (!&amp;rebuild_map_cmd($_[1])) {
	if ($_[3] eq "dbm") {
		dbmopen(%mailer, $_[2], 0644);
		$mailer{$_[0]-&gt;{'domain'}} = "$_[0]-&gt;{'mailer'}:$_[0]-&gt;{'dest'}";
		dbmclose(%mailer);
		}
	else { &amp;run_makemap($_[1], $_[2], $_[3]); }
	}

# Update the cache
$_[0]-&gt;{'num'} = scalar(@list_mailers_cache);
$_[0]-&gt;{'file'} = $_[1];
push(@list_mailers_cache, $_[0]);
}

# modify_mailer(&amp;old, &amp;details, textfile, dbmfile, dbmtype)
sub modify_mailer
{
local(@mailer, %mailer);

# Update the file
local $lref = &amp;read_file_lines($_[2]);
local $oldlen = $_[0]-&gt;{'eline'} - $_[0]-&gt;{'line'} + 1;
local @newlines;
push(@newlines, &amp;make_table_comment($_[1]-&gt;{'cmt'}));
push(@newlines, "$_[1]-&gt;{'domain'}\t$_[1]-&gt;{'mailer'}:$_[1]-&gt;{'dest'}");
splice(@$lref, $_[0]-&gt;{'line'}, $oldlen, @newlines);
&amp;flush_file_lines($_[2]);

# Update the DBM
if (!&amp;rebuild_map_cmd($_[2])) {
	if ($_[3] eq "dbm") {
		dbmopen(%mailer, $_[3], 0644);
		delete($mailer{$_[0]-&gt;{'domain'}});
		$mailer{$_[1]-&gt;{'domain'}} = "$_[1]-&gt;{'mailer'}:$_[1]-&gt;{'dest'}";
		dbmclose(%mailer);
		}
	else { &amp;run_makemap($_[2], $_[3], $_[4]); }
	}

local $idx = &amp;indexof($_[0], @list_mailers_cache);
$_[1]-&gt;{'line'} = $_[0]-&gt;{'line'};
$_[1]-&gt;{'eline'} = $_[1]-&gt;{'cmt'} ? $_[0]-&gt;{'line'}+1 : $_[0]-&gt;{'line'};
$list_mailers_cache[$idx] = $_[1] if ($idx != -1);
&amp;renumber_list(\@list_mailers_cache, $_[0], scalar(@newlines)-$oldlen);
}

# delete_mailer(&amp;old, textfile, dbmfile, dbmtype)
sub delete_mailer
{
local(@mailer, %mailer);

# Delete from the file
local $len = $_[0]-&gt;{'eline'} - $_[0]-&gt;{'line'} + 1;
local $lref = &amp;read_file_lines($_[1]);
splice(@$lref, $_[0]-&gt;{'line'}, $len);
&amp;flush_file_lines($_[1]);

# Delete f rom the DBM
if (!&amp;rebuild_map_cmd($_[1])) {
	if ($_[3] eq "dbm") {
		dbmopen(%mailer, $_[2], 0644);
		delete($mailer{$_[0]-&gt;{'domain'}});
		dbmclose(%mailer);
		}
	else { &amp;run_makemap($_[1], $_[2], $_[3]); }
	}

# Update the cache
local $idx = &amp;indexof($_[0], @list_mailers_cache);
splice(@list_mailers_cache, $idx, 1) if ($idx != -1);
&amp;renumber_list(\@list_mailers_cache, $_[0], -$len);
}

# mailer_form([&amp;details])
sub mailer_form
{
local ($m) = @_;
local ($mode, $addr, $conf, $ml, $dest, $nomx);

print &amp;ui_form_start("save_mailer.cgi", "post");
if ($m) {
	print &amp;ui_hidden("num", $m-&gt;{'num'});
	}
else {
	print &amp;ui_hidden("new", 1);
	}
print &amp;ui_table_start($m ? $text{'mform_edit'} : $text{'mform_create'},
		      undef, 2);

# Description
print &amp;ui_table_row($text{'vform_cmt'},
		    &amp;ui_textbox("cmt", $m ? $m-&gt;{'cmt'} : undef, 50));

# Show 'mail for' input
local $dom = $m &amp;&amp; $m-&gt;{'domain'} =~ /^\.(\S+)$/ ? $1 : undef;
print &amp;ui_table_row($text{'mform_for'},
    &amp;ui_radio_table("from_type", $dom ? 1 : 0,
	[ [ 0, $text{'mform_host2'},
	       &amp;ui_textbox("from_host", $dom || !$m ? "" : $m-&gt;{'domain'},
			   20) ],
	  [ 1, $text{'mform_domain2'},
	       &amp;ui_textbox("from_dom", $dom, 20) ],
	  $m ? ( ) : ( [ 2, $text{'mform_domain3'},
			    &amp;ui_textbox("from_all", undef ,20) ] ) ]));

# Show delivery input
$conf = &amp;get_sendmailcf();
local @mailers = ( { 'value' =&gt; 'error' }, &amp;find_type("M", $conf) );
print &amp;ui_table_row($text{'mform_delivery'},
    &amp;ui_select("mailer",
	       $m ? $m-&gt;{'mailer'} : "smtp",
	       [ map { $_-&gt;{'value'} =~ /^([^ ,]+)/;
		       [ $1, $text{"mform_$1"} ] } @mailers ]));

# Show send to input
$dest = $m-&gt;{'dest'};
if ($dest =~ s/\[([^\]:]+)\]/$1/g) {
	$nomx = 1;
	}
else {
	$dest = $m-&gt;{'dest'};
	}
print &amp;ui_table_row($text{'mform_to'},
    &amp;ui_textbox("dest", $dest, 30)."&lt;br&gt;".
    &amp;ui_checkbox("nomx", 1, $text{'mform_ignore'}, $nomx));

print &amp;ui_table_end();
print &amp;ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
			     [ "delete", $text{'delete'} ] ]
		         : [ [ "create", $text{'create'} ] ]);
}

1;

</pre></body></html>