1 package Convert::YText;
5 use vars qw/$VERSION @ISA @EXPORT_OK/;
7 @EXPORT_OK = qw( encode_ytext decode_ytext );
16 Convert::YText - Quotes strings suitably for rfc2822 local part
24 use Convert::YText qw(encode_ytext decode_ytext);
26 $encoded=encode_ytext($string);
27 $decoded=decode_ytext($encoded);
29 ($decoded eq $string) || die "this should never happen!";
34 Convert::YText converts strings to and from "YText", a format inspired
35 by xtext defined in RFC1894, the MIME base64 and quoted-printable
36 types (RFC 1394). The main goal is encode a UTF8 string into something safe
37 for use as the local part in an internet email address (RFC2822).
39 According to RFC 2822, the following non-alphanumerics are OK for the
40 local part of an address: "!#$%&'*+-/=?^_`{|}~". On the other hand, it
41 seems common in practice to block addresses having "%!/|`#&?" in the
42 local part. The idea is to restrict ourselves to basic ASCII
43 alphanumerics, plus a small set of printable ASCII, namely "=_+-~.".
44 Spaces are replaced with "_", the characters "A-Za-z0-9.\+\-~" encode
45 as themselves, and everything else is written "=USTR=" where USTR is
46 the base64 (using "A-Za-z0-9\+\-\." as digits) encoding of the unicode
49 The characters '+' and '-' are pretty widely used to attach suffixes
50 (although usually only one works on a given mail host). It seems ok to
51 use '+-', since the first marks the beginning of a suffix, and then is
52 a regular character. The character '.' also seems mostly permissable.
59 our $digit_string="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-.";
60 our @digits=split "",$digit_string;
70 $str = $digits[$remainder].$str;
78 # "=" we use as an escape, and '_' for space
79 $str=~ s/([^a-zA-Z0-9+\-~. ])/"=".encode_num(ord($1))."="/ge;
87 $str=~ s/=([a-zA-Z0-9+\-\.])+=/ decode_num($1)/eg;
92 Finish doc. Write tests.
96 David Bremner, E<lt>bremner@unb.caE<gt>
100 Copyright (C) 2008 David Bremner. All Rights Reserved.
102 This module is free software; you can redistribute it and/or modify it
103 under the same terms as Perl itself.
107 This module is currently in B<BETA> condition. It should not be used
108 in a production environment, and is released with no warranty of any
111 Corrections, suggestions, bugreports and tests are welcome!
115 L<MIME::Base64>, L<MIME::Decoder::Base64>, L<MIME::Decoder::QuotedPrint>.