]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - Convert/YText.pm
a2b2193f30fd627ca2d90a0f7592adba18b04b75
[git.ikiwiki.info.git] / Convert / YText.pm
1 package Convert::YText;
3 use strict;
4 use warnings;
5 use vars qw/$VERSION @ISA @EXPORT_OK/;
6 @ISA = 'Exporter';
7 @EXPORT_OK = qw( encode_ytext decode_ytext );
9 use encoding "utf-8";
12 $VERSION=0.1;
14 =head1 NAME
16 Convert::YText - Quotes strings suitably for rfc2822 local part
18 =head1 VERSION
20 Version 0.1 B<BETA>
22 =head1 SYNOPSIS
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!";
32 =head1 DESCRIPTION
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
47 character code.
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.
55 =head1 METHODS
57 =cut
59 our $digit_string="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-.";
60 our @digits=split "",$digit_string;
62 sub encode_num($){
63     my $num=shift;
64     my $str="";
66     while ($num>0){
67         $remainder=$num % 64;
68         $num=$num >> 6;
69         
70         $str = $digits[$remainder].$str;
71     }
72     
73     return $str;
74 }
75 sub encode_ytext($){
76     my $str=shift;
78     # "=" we use as an escape, and '_' for space
79     $str=~ s/([^a-zA-Z0-9+\-~. ])/"=".encode_num(ord($1))."="/ge;
80     $str=~ s/ /_/g;
82     return $str;
83 };
85 sub decode_ytext($){
86     my $str = shift;
87     $str=~ s/=([a-zA-Z0-9+\-\.])+=/ decode_num($1)/eg;
88 }
90 =head1 TODO
92 Finish doc. Write tests.
94 =head1 AUTHOR
96 David Bremner, E<lt>bremner@unb.caE<gt>
98 =head1 COPYRIGHT
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.
105 =head1 CAVEAT
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
109 kind whatsoever.
111 Corrections, suggestions, bugreports and tests are welcome!
113 =head1 SEE ALSO
115 L<MIME::Base64>, L<MIME::Decoder::Base64>, L<MIME::Decoder::QuotedPrint>.
117 =cut
119 1;