1 # Functions for editing /etc/mailcap
3 BEGIN { push(@INC, ".."); };
7 if ($module_info{'usermin'}) {
8 &switch_to_remote_user();
9 $mailcap_file = $userconfig{'mailcap'};
10 if ($mailcap_file !~ /^\//) {
11 $mailcap_file = "$remote_user_info[7]/$mailcap_file";
15 $mailcap_file = $config{'mailcap'};
19 # Returns a list of /etc/mailcap entries
22 if (!scalar(@list_mailcap_cache)) {
23 @list_mailcap_cache = ( );
24 open(CAP, $mailcap_file);
27 local ($slnum, $elnum) = ($lnum, $lnum);
30 # Continuation line! Read the next one and append it
37 if (/^(#*)\s*([a-z0-9\-]+)\/([a-z0-9\-\*]+);\s*([^;]*)(;(.*))?/) {
39 local @cmtlines = split(/\n/, $cmt);
40 local $cap = { 'type' => $2."/".$3,
45 'line' => $slnum-scalar(@cmtlines),
47 'index' => scalar(@list_mailcap_cache),
49 # Parse ; separated args after the command
51 local @args = split(/\s*;\s*/, $args);
52 foreach my $arg (@args) {
53 if ($arg =~ /^\s*(\S+)\s*=\s*(.*)/) {
55 $cap->{'args'}->{$1} = $2;
57 elsif ($arg =~ /^\s*(\S+)\s*$/) {
58 $cap->{'args'}->{$1} = "";
61 push(@list_mailcap_cache, $cap);
65 # Found a comment before a rule
80 return @list_mailcap_cache;
83 # create_mailcap(&mailcap)
84 # Adds a mailcap entry
87 local ($mailcap) = @_;
88 &list_mailcap(); # init cache
89 local $lref = &read_file_lines($mailcap_file);
90 local @lines = &mailcap_lines($mailcap);
91 $mailcap->{'line'} = scalar(@$lref);
92 $mailcap->{'eline'} = scalar(@$lref)+scalar(@lines)-1;
93 $mailcap->{'index'} = scalar(@list_mailcap_cache);
95 &flush_file_lines($mailcap_file);
96 push(@list_mailcap_cache, $mailcap);
99 # modify_mailcap(&mailcap)
100 # Updates one mailcap entry in the file
103 local ($mailcap) = @_;
104 local $lref = &read_file_lines($mailcap_file);
105 local @lines = &mailcap_lines($mailcap);
106 local $oldlen = $mailcap->{'eline'} - $mailcap->{'line'} + 1;
107 splice(@$lref, $mailcap->{'line'}, $oldlen, @lines);
108 &flush_file_lines($mailcap_file);
109 local $diff = scalar(@lines)-$oldlen;
110 foreach my $c (grep { $c ne $mailcap } @list_mailcap_cache) {
111 $c->{'line'} += $diff if ($c->{'line'} > $mailcap->{'line'});
112 $c->{'eline'} += $diff if ($c->{'eline'} > $mailcap->{'line'});
116 # delete_mailcap(&mailcap)
117 # Removes one mailcap entry from the file
120 local ($mailcap) = @_;
121 local $lref = &read_file_lines($mailcap_file);
122 local $len = $mailcap->{'eline'} - $mailcap->{'line'} + 1;
123 splice(@$lref, $mailcap->{'line'}, $len);
124 &flush_file_lines($mailcap_file);
125 @list_mailcap_cache = grep { $_ ne $mailcap } @list_mailcap_cache;
126 foreach my $c (@list_mailcap_cache) {
127 $c->{'line'} -= $len if ($c->{'line'} > $mailcap->{'line'});
128 $c->{'eline'} -= $len if ($c->{'eline'} > $mailcap->{'line'});
129 $c->{'index'}-- if ($c->{'index'} > $mailcap->{'index'});
133 # mailcap_lines(&mailcap)
134 # Returns an array of lines for a mailcap entry
137 local ($mailcap) = @_;
140 foreach my $a (keys %{$mailcap->{'args'}}) {
141 local $v = $mailcap->{'args'}->{$a};
149 foreach my $l (split(/\n/, $mailcap->{'cmt'})) {
152 push(@rv, ($mailcap->{'enabled'} ? "" : "#").
153 "$mailcap->{'type'}; $mailcap->{'program'}".$args);