blib/lib/MIME/AltWords0.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 40 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package MIME::AltWords0;
2 # at Wed Sep 27 07:52:29 CEST 2006
3  
4 =head1 NAME
5  
6 MIME::AltWords0 - copy of MIME::Words (not for direct use)
7  
8  
9 =head1 SYNOPSIS
10  
11 L<MIME::AltWords0> is an auxilary package used by L<MIME::AltWords>.
12  
13 If you want to encode or decode MIME words (such as
14 C<=?ISO-8859-2?Q?_=E1ll_e=E1r?=>) found in e-mail message headers (mostly
15 Subject, From and To), the recommended Perl module to use is
16 L<MIME::AltWords>. Please don't use L<MIME::AltWords0> (which is
17 useless by itself anyway) or L<MIME::Words> (version 5.420 has several
18 serious bugs both with encoding and decoding).
19  
20 Most users shouldn't read on, but they should read L<MIME::AltWords>
21 instead.
22  
23 =head1 DESCRIPTION
24  
25 The following functions have been moved to L<MIME::AltWords>:
26 decode_mimewords() (some code stays here as decode_mimewords_wantarray()),
27  
28 use MIME::AltWords0 qw(:all);
29
30 ### Split string into array of decoded [DATA,CHARSET] pairs:
31 @decoded = decode_mimewords_wantarray(
32 'To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
33 );
34
35 Fellow Americans, you probably won't know what the hell this module
36 is for. Europeans, Russians, et al, you probably do. C<:-)>.
37  
38 For example, here's a valid MIME header you might get:
39  
40 From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
41 To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
42 CC: =?ISO-8859-1?Q?Andr=E9_?= Pirard <PIRARD@vm1.ulg.ac.be>
43 Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
44 =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
45 =?US-ASCII?Q?.._cool!?=
46  
47 The fields basically decode to (sorry, I can only approximate the
48 Latin characters with 7 bit sequences /o and 'e):
49  
50 From: Keith Moore <moore@cs.utk.edu>
51 To: Keld J/orn Simonsen <keld@dkuug.dk>
52 CC: Andr'e Pirard <PIRARD@vm1.ulg.ac.be>
53 Subject: If you can read this you understand the example... cool!
54  
55  
56 =head1 PUBLIC INTERFACE
57  
58 =over 4
59  
60 =cut
61  
62 require 5.001;
63  
64 ### Pragmas:
65 use strict;
66 use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA);
67  
68 ### Exporting:
69 use Exporter;
70 %EXPORT_TAGS = (all => [qw(decode_mimewords encode_mimewords encode_mimeword
71 decode_mimewords_wantarray)]);
72 Exporter::export_ok_tags('all');
73  
74 ### Inheritance:
75 @ISA = qw(Exporter);
76  
77 ### Other modules:
78 use MIME::Base64;
79 use MIME::QuotedPrint;
80  
81  
82  
83 #------------------------------
84 #
85 # Globals...
86 #
87 #------------------------------
88  
89 #------------------------------
90  
91  
92  
93 #------------------------------
94  
95 =item decode_mimewords_wantarray ENCODED, [OPTS...]
96  
97 I<Function.>
98 Go through the string looking for RFC-1522-style "Q"
99 (quoted-printable, sort of) or "B" (base64) encoding, and decode them.
100  
101 B<In an array context,> splits the ENCODED string into a list of decoded
102 C<[DATA, CHARSET]> pairs, and returns that list. Unencoded
103 data are returned in a 1-element array C<[DATA]>, giving an effective
104 CHARSET of C<undef>.
105  
106 $enc = '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>';
107 foreach (decode_mimewords($enc)) {
108 print "", ($_[1] || 'US-ASCII'), ": ", $_[0], "\n";
109 }
110  
111 B<In a scalar context,> the result is undocumented, please see
112 L<MIME::AltWords/decode_mimewords> instead.
113  
114 In the event of a syntax error, $@ will be set to a description
115 of the error, but parsing will continue as best as possible (so as to
116 get I<something> back when decoding headers).
117 $@ will be false if no error was detected.
118  
119 Any arguments past the ENCODED string are taken to define a hash of options:
120  
121 =over 4
122  
123 =item Field
124  
125 Name of the mail field this string came from. I<Currently ignored.>
126  
127 =back
128  
129 =cut
130  
131 #------------------------------
132  
133 # vvv buggy implementation of encode_mimeword() commented out,
134 # see MIME::AltWords
135  
136 =begin comment
137  
138 =item encode_mimeword RAW, [ENCODING], [CHARSET]
139  
140 I<Function.>
141 Encode a single RAW "word" that has unsafe characters.
142 The "word" will be encoded in its entirety.
143  
144 ### Encode "<<Franc,ois>>":
145 $encoded = encode_mimeword("\xABFran\xE7ois\xBB");
146  
147 You may specify the ENCODING (C<"Q"> or C<"B">), which defaults to C<"Q">.
148 You may specify the CHARSET, which defaults to C<iso-8859-1>.
149  
150 =cut
151  
152 =begin commentcode
153  
154 sub encode_mimeword {
155 my $word = shift;
156 my $encoding = uc(shift || 'Q');
157 my $charset = uc(shift || 'ISO-8859-1');
158 my $encfunc = (($encoding eq 'Q') ? \&_encode_Q : \&_encode_B);
159 "=?$charset?$encoding?" . &$encfunc($word) . "?=";
160 }
161  
162 =cut
163  
164 #------------------------------
165  
166 # vvv buggy implementation of encode_mimeword() commented out,
167 # see MIME::AltWords
168  
169 =begin comment
170  
171 =item encode_mimewords RAW, [OPTS]
172  
173 I<Function.>
174 Given a RAW string, try to find and encode all "unsafe" sequences
175 of characters:
176  
177 ### Encode a string with some unsafe "words":
178 $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB");
179  
180 Returns the encoded string.
181 Any arguments past the RAW string are taken to define a hash of options:
182  
183 =over 4
184  
185 =item Charset
186  
187 Encode all unsafe stuff with this charset. Default is 'ISO-8859-1',
188 a.k.a. "Latin-1".
189  
190 =item Encoding
191  
192 The encoding to use, C<"q"> or C<"b">. The default is C<"q">.
193  
194 =item Field
195  
196 Name of the mail field this string will be used in. I<Currently ignored.>
197  
198 =back
199  
200 B<Warning:> this is a quick-and-dirty solution, intended for character
201 sets which overlap ASCII. B<It does not comply with the RFC-1522
202 rules regarding the use of encoded words in message headers>.
203 You may want to roll your own variant,
204 using C<encoded_mimeword()>, for your application.
205 I<Thanks to Jan Kasprzak for reminding me about this problem.>
206  
207 =cut
208  
209 =begin commentcode
210  
211 sub encode_mimewords {
212 my ($rawstr, %params) = @_;
213 my $charset = $params{Charset} || 'ISO-8859-1';
214 my $encoding = lc($params{Encoding} || 'q');
215  
216 ### Encode any "words" with unsafe characters.
217 ### We limit such words to 18 characters, to guarantee that the
218 ### worst-case encoding give us no more than 54 + ~10 < 75 characters
219 my $word;
220 $rawstr =~ s{([a-zA-Z0-9\x7F-\xFF]{1,18})}{ ### get next "word"
221 $word = $1;
222 (($word !~ /[$NONPRINT]/o)
223 ? $word ### no unsafe chars
224 : encode_mimeword($word, $encoding, $charset)); ### has unsafe chars
225 }xeg;
226 $rawstr;
227 }
228  
229 1;
230 __END__