]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - Convert/YText.pm
e8ea42abef20f1fe16d26d33d42d37bf10e567f5
[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 "_", "/" with "~", the characters
45 "A-Za-z0-9.\+\-~" encode as themselves, and everything else is written
46 "=USTR=" where USTR is the base64 (using "A-Za-z0-9\+\-\." as digits)
47 encoding of the unicode 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         my $remainder=$num % 64;
68         $num=$num >> 6;
69         
70         $str = $digits[$remainder].$str;
71     }
72     
73     return $str;
74 }
75 sub decode_str($){
76     my $str=shift;
77     my @chars=split "",$str;
78     my $num=0;
80     while (scalar(@chars)>0){
81         my $remainder=index $digit_string,$chars[0];
82         
83         # convert this to carp or something
84         die if ($remainder <0);
86         $num=$num << 6;
87         $num+=$remainder;
88         print STDERR "num=$num\n";
89         shift @chars;
90     }
91     
92     return chr($num);
93 }
94 sub encode_ytext($){
95     my $str=shift;
96     # "=" we use as an escape, and '_' for space
97     $str=~ s/([^a-zA-Z0-9+\-\/. ])/"=".encode_num(ord($1))."="/ge;
98     
99     $str=~ s|/|~|g;
100     $str=~ s/ /_/g;
101     
102     return $str;
103 };
105 sub decode_ytext($){
106     my $str = shift;
107     $str=~ s/=([a-zA-Z0-9+\-\.])+=/ decode_str($1)/eg;
108     $str=~ s/_/ /g;
109     $str=~ s|~|/|g;
110     return $str;
113 =head1 TODO
115 Finish doc. Write tests.
117 =head1 AUTHOR
119 David Bremner, E<lt>bremner@unb.caE<gt>
121 =head1 COPYRIGHT
123 Copyright (C) 2008 David Bremner.  All Rights Reserved.
125 This module is free software; you can redistribute it and/or modify it
126 under the same terms as Perl itself.
128 =head1 CAVEAT
130 This module is currently in B<BETA> condition.  It should not be used
131 in a production environment, and is released with no warranty of any
132 kind whatsoever.
134 Corrections, suggestions, bugreports and tests are welcome!
136 =head1 SEE ALSO
138 L<MIME::Base64>, L<MIME::Decoder::Base64>, L<MIME::Decoder::QuotedPrint>.
140 =cut
142 1;