]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - encoding.pl
9e8a896a986667463a3bc70aa9556a20e9499acb
[git.ikiwiki.info.git] / encoding.pl
1 use encoding "utf-8";
3 our @digits=split "","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
5 sub encode_num($){
6     my $num=shift;
7     my $str="";
9     while ($num>0){
10         $remainder=$num % 64;
11         $num=$num >> 6;
12         
13         $str = $digits[$remainder].$str;
14     }
15     
16     return $str;
17 }
18 sub strict_rfc2822_escape($){
19 # according to rfc 2822, the following non-alphanumerics are OK for
20 # the local part of an address: "!#$%&'*+-/=?^_`{|}~". On the other
21 # hand, a fairly common exim configuration, for example, blocks
22 # addresses having "@%!/|`#&?" in the local part.  '+' and '-' are
23 # pretty widely used to attach suffixes (although usually only one
24 # works on a given mail host). It seems ok to use '+-', since the first 
25 # marks the beginning of a suffix, and then is a regular character.
26 #  '.' also seems mostly permissable
27     my $str=shift;
29     # "=" we use as an escape, and '_' for space
30     $str=~ s/([^a-zA-Z0-9+\-~. ])/"=".encode_num(ord($1))."="/ge;
31     $str=~ s/ /_/g;
33     return $str;
34 };
36 while(<>){
37     chomp();
38     print strict_rfc2822_escape($_);
39 }