2 # Functions for the domains table
5 # Returns the filename and type of the domains database, or undef if none
8 foreach $f (&find_type("K", $_[0])) {
9 if ($f->{'value'} =~ /^domaintable\s+(\S+)[^\/]+(\S+)$/) {
16 # domains_file(&config)
17 # Returns the filename of the text domains file, or undef if none
20 return &find_textfile($config{'domains_file'}, &domains_dbm($_[0]));
23 # list_domains(textfile)
26 if (!scalar(@list_domains_cache)) {
27 @list_domains_cache = ( );
32 s/\r|\n//g; # remove newlines
33 if (/^\s*#+\s*(.*)/) {
35 $cmt = &is_table_comment($_);
37 elsif (/^(\S+)\s+(.*)/) {
42 $dom{'line'} = $cmt ? $lnum-1 : $lnum;
43 $dom{'eline'} = $lnum;
44 $dom{'num'} = scalar(@list_domains_cache);
46 push(@list_domains_cache, \%dom);
56 return @list_domains_cache;
59 # create_domain(&details, textfile, dbmfile, dbmtype)
60 # Create a new domain mapping
63 &list_domains($_[1]); # force cache init
67 local $lref = &read_file_lines($_[1]);
68 $_[0]->{'line'} = scalar(@$lref);
69 push(@$lref, &make_table_comment($_[0]->{'cmt'}));
70 push(@$lref, "$_[0]->{'from'}\t$_[0]->{'to'}");
71 $_[0]->{'eline'} = scalar(@$lref)-1;
72 &flush_file_lines($_[1]);
75 if (!&rebuild_map_cmd($_[1])) {
77 dbmopen(%dom, $_[2], 0644);
78 $dom{$_[0]->{'from'}} = $_[0]->{'to'};
81 else { &run_makemap($_[1], $_[2], $_[3]); }
85 $_[0]->{'num'} = scalar(@list_domains_cache);
86 $_[0]->{'file'} = $_[1];
87 push(@list_domains_cache, $_[0]);
90 # delete_domain(&details, textfile, dbmfile, dbmtype)
91 # Delete an existing domain mapping
97 local $lref = &read_file_lines($_[1]);
98 local $len = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
99 splice(@$lref, $_[0]->{'line'}, $len);
100 &flush_file_lines($_[1]);
103 if (!&rebuild_map_cmd($_[1])) {
104 if ($_[3] eq "dbm") {
105 dbmopen(%dom, $_[2], 0644);
106 delete($dom{$_[0]->{'from'}});
109 else { &run_makemap($_[1], $_[2], $_[3]); }
113 local $idx = &indexof($_[0], @list_domains_cache);
114 splice(@list_domains_cache, $idx, 1) if ($idx != -1);
115 &renumber_list(\@list_domains_cache, $_[0], -$len);
118 # modify_domain(&old, &details, textfile, dbmfile, dbmtype)
119 # Change an existing domain
125 local $lref = &read_file_lines($_[2]);
126 local $oldlen = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
128 push(@newlines, &make_table_comment($_[1]->{'cmt'}));
129 push(@newlines, "$_[1]->{'from'}\t$_[1]->{'to'}");
130 splice(@$lref, $_[0]->{'line'}, $oldlen, @newlines);
131 &flush_file_lines($_[2]);
134 if (!&rebuild_map_cmd($_[2])) {
135 if ($_[4] eq "dbm") {
136 dbmopen(%dom, $_[3], 0644);
137 delete($dom{$_[0]->{'from'}});
138 $dom{$_[1]->{'from'}} = $_[1]->{'to'};
141 else { &run_makemap($_[2], $_[3], $_[4]); }
145 local $idx = &indexof($_[0], @list_domains_cache);
146 $_[1]->{'line'} = $_[0]->{'line'};
147 $_[1]->{'eline'} = $_[1]->{'cmt'} ? $_[0]->{'line'}+1 : $_[0]->{'line'};
148 $list_domains_cache[$idx] = $_[1] if ($idx != -1);
149 &renumber_list(\@list_domains_cache, $_[0], scalar(@newlines)-$oldlen);
152 # domain_form([&details])
157 print &ui_form_start("save_domain.cgi", "post");
159 print &ui_hidden("num", $g->{'num'}),"\n";
162 print &ui_hidden("new", 1),"\n";
164 print &ui_table_start($g ? $text{'gform_edit'} : $text{'gform_create'},
167 print &ui_table_row($text{'vform_cmt'},
168 &ui_textbox("cmt", $g->{'cmt'}, 50));
170 print &ui_table_row($text{'dform_from'},
171 &ui_textbox("from", $g->{'from'}, 30));
173 print &ui_table_row($text{'dform_to'},
174 &ui_textbox("to", $g->{'to'}, 30));
176 print &ui_table_end();
177 print &ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
178 [ "delete", $text{'delete'} ] ]
179 : [ [ "create", $text{'create'} ] ]);