ccd767102a88b1c1321cd52b7bd367619a3d9e51
[PerlMail.git] / PerlMail / Lib.pm
1 #! /usr/bin/perl
2
3 #       $Id$
4 # Copyright (C) 2002-2003 Jan Kratochvil <short@ucw.cz>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 package PerlMail::Lib;
22 use vars qw($VERSION);
23 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
24 use strict;
25 use warnings;
26
27 require Exporter;
28 use vars qw(@ISA @EXPORT);
29 @ISA=qw(Exporter);
30 @EXPORT=qw(
31                 &parseone
32                 &muttrc &muttrc_get);
33
34 use PerlMail::Config;
35
36 require Mail::Address;
37
38
39 # return: Mail::Address instance or undef()
40 sub parseone
41 {
42 my($line)=@_;
43
44         return undef() if !defined $line;
45         my @r=Mail::Address->parse($line);
46         warn "Got ".scalar(@r)." addresses while wanting just one; when parsing: $line" if 1!=@r;
47         return $r[0];
48 }
49
50 our %muttrc_pending=(); # not exported, just for local()
51 sub muttrc
52 {
53 my($muttrc)=@_;
54
55         $muttrc||="$HOME/.muttrc";
56         $muttrc=~s/^\~/$HOME/;
57         do { warn "Looping muttrc, ignoring: $muttrc"; return (); } if $muttrc_pending{$muttrc};
58         local $muttrc_pending{$muttrc}=1;
59         local *MUTTRC;
60         open MUTTRC,$muttrc or do { warn "open \"$muttrc\": $!"; return (); };
61         local $/="\n";
62         local $_;
63         my @r=();
64         # far emulation mutt/init.c/mutt_parse_rc_line()
65         while (<MUTTRC>) {
66                 s/^[\s;]*//s;
67                 s/[#;].*$//s;
68                 s/\s*$//s;
69                 next if !/^(\S+)\s*/s;
70                 if ($1 eq "source") {
71                         $_=$';
72                         do { warn "Wrong 'source' parameters at $muttrc:$.: $_"; next; } if !/^\S+$/;
73                         push @r,muttrc($_);
74                         next;
75                         }
76                 push @r,$_;
77                 }
78         close MUTTRC or warn "close \"$muttrc\": $!";
79         return wantarray() ? @r : join("",map("$_\n",@r));
80 }
81
82 my %mutteval_charmap=(          # WARNING: Don't use "" or "0" here, see below for "|| warn"!
83                 '\\'=>"\\",
84                 'r'=>"\r",
85                 'n'=>"\n",
86                 't'=>"\t",
87                 'f'=>"\f",
88                 'e'=>"\e",
89                 );
90 # mutt/init.c/mutt_extract_token()
91 sub mutteval
92 {
93         local $_=$_[0];
94         return $_ if !s/^"//;
95         do { warn "Missing trailing quote in: $_"; return $_; } if !s/"$//;
96         s/\\(.)/$mutteval_charmap{$1} || warn "Undefined '\\$1' sequence in: $_";/ges;
97         return $_;
98 }
99
100 sub muttrc_get
101 {
102 my(@headers)=@_;
103
104         my @r=map({ (ref $_ ? $_ : qr/^\s*set\s+\Q$_\E\s*=\s*(.*?)\s*$/si); } @headers);
105         my %r=map(($_=>undef()),@r);
106         for (muttrc()) {
107                 for my $ritem (@r) {
108                         /$ritem/si or next;
109                         $r{$ritem}=mutteval $1;
110                         }
111                 }
112         for my $var (grep { !defined($r{$_}) } @r) {
113                 warn "Variable '$var' not found in muttrc";
114                 return undef();
115                 }
116         return wantarray() ? %r : $r{$r[0]};
117 }
118
119 1;