]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/commitdiff
encoding sketch
authorDavid Bremner <bremner@unb.ca>
Sat, 28 Jun 2008 22:04:03 +0000 (00:04 +0200)
committerDavid Bremner <bremner@unb.ca>
Sat, 28 Jun 2008 22:04:03 +0000 (00:04 +0200)
encoding.pl [new file with mode: 0644]

diff --git a/encoding.pl b/encoding.pl
new file mode 100644 (file)
index 0000000..9e8a896
--- /dev/null
@@ -0,0 +1,40 @@
+use encoding "utf-8";
+
+our @digits=split "","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
+
+sub encode_num($){
+    my $num=shift;
+    my $str="";
+
+    while ($num>0){
+       $remainder=$num % 64;
+       $num=$num >> 6;
+       
+       $str = $digits[$remainder].$str;
+    }
+    
+    return $str;
+}
+sub strict_rfc2822_escape($){
+# according to rfc 2822, the following non-alphanumerics are OK for
+# the local part of an address: "!#$%&'*+-/=?^_`{|}~". On the other
+# hand, a fairly common exim configuration, for example, blocks
+# addresses having "@%!/|`#&?" in the local part.  '+' and '-' are
+# pretty widely used to attach suffixes (although usually only one
+# works on a given mail host). It seems ok to use '+-', since the first 
+# marks the beginning of a suffix, and then is a regular character.
+#  '.' also seems mostly permissable
+    my $str=shift;
+
+    # "=" we use as an escape, and '_' for space
+    $str=~ s/([^a-zA-Z0-9+\-~. ])/"=".encode_num(ord($1))."="/ge;
+    $str=~ s/ /_/g;
+
+    return $str;
+};
+
+while(<>){
+    chomp();
+    print strict_rfc2822_escape($_);
+}
+