From: David Bremner Date: Tue, 1 Jul 2008 07:53:11 +0000 (+0200) Subject: create Convert::YText X-Git-Tag: 0.1~47 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=beb1f39be5710c7afa8dbb2716257a2d55593a4f;p=ikiwiki.git create Convert::YText --- diff --git a/Convert/YText.pm b/Convert/YText.pm new file mode 100644 index 000000000..a2b2193f3 --- /dev/null +++ b/Convert/YText.pm @@ -0,0 +1,119 @@ +package Convert::YText; + +use strict; +use warnings; +use vars qw/$VERSION @ISA @EXPORT_OK/; +@ISA = 'Exporter'; +@EXPORT_OK = qw( encode_ytext decode_ytext ); + +use encoding "utf-8"; + + +$VERSION=0.1; + +=head1 NAME + +Convert::YText - Quotes strings suitably for rfc2822 local part + +=head1 VERSION + +Version 0.1 B + +=head1 SYNOPSIS + +use Convert::YText qw(encode_ytext decode_ytext); + +$encoded=encode_ytext($string); +$decoded=decode_ytext($encoded); + +($decoded eq $string) || die "this should never happen!"; + + +=head1 DESCRIPTION + +Convert::YText converts strings to and from "YText", a format inspired +by xtext defined in RFC1894, the MIME base64 and quoted-printable +types (RFC 1394). The main goal is encode a UTF8 string into something safe +for use as the local part in an internet email address (RFC2822). + +According to RFC 2822, the following non-alphanumerics are OK for the +local part of an address: "!#$%&'*+-/=?^_`{|}~". On the other hand, it +seems common in practice to block addresses having "%!/|`#&?" in the +local part. The idea is to restrict ourselves to basic ASCII +alphanumerics, plus a small set of printable ASCII, namely "=_+-~.". +Spaces are replaced with "_", the characters "A-Za-z0-9.\+\-~" encode +as themselves, and everything else is written "=USTR=" where USTR is +the base64 (using "A-Za-z0-9\+\-\." as digits) encoding of the unicode +character code. + +The characters '+' 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. The character '.' also seems mostly permissable. + + +=head1 METHODS + +=cut + +our $digit_string="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-."; +our @digits=split "",$digit_string; + +sub encode_num($){ + my $num=shift; + my $str=""; + + while ($num>0){ + $remainder=$num % 64; + $num=$num >> 6; + + $str = $digits[$remainder].$str; + } + + return $str; +} +sub encode_ytext($){ + 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; +}; + +sub decode_ytext($){ + my $str = shift; + $str=~ s/=([a-zA-Z0-9+\-\.])+=/ decode_num($1)/eg; +} + +=head1 TODO + +Finish doc. Write tests. + +=head1 AUTHOR + +David Bremner, Ebremner@unb.caE + +=head1 COPYRIGHT + +Copyright (C) 2008 David Bremner. All Rights Reserved. + +This module is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=head1 CAVEAT + +This module is currently in B condition. It should not be used +in a production environment, and is released with no warranty of any +kind whatsoever. + +Corrections, suggestions, bugreports and tests are welcome! + +=head1 SEE ALSO + +L, L, L. + +=cut + +1; diff --git a/IkiWiki/Plugin/postal.pm b/IkiWiki/Plugin/postal.pm index 57f879148..bc3410280 100644 --- a/IkiWiki/Plugin/postal.pm +++ b/IkiWiki/Plugin/postal.pm @@ -31,8 +31,7 @@ package IkiWiki::Plugin::postal; use warnings; use strict; use IkiWiki 2.00; -use Compress::LZF ; -use MIME::Base64::URLSafe; + sub import @@ -43,7 +42,7 @@ sub import sub pagetemplate (@) { my %params = @_; - my $page = $params{page}; + my $page = IkiWiki::pagetitle($params{page}); my $destpage = $params{destpage}; my $template = $params{template}; @@ -52,7 +51,7 @@ sub pagetemplate (@) ! defined $template->param ('comments')) { debug("adding comments to ".$page); - my $key = urlsafe_b64encode(compress($page)); + my $key = uri_escape_utf8($page); debug("using key ".$key); @@ -74,4 +73,18 @@ sub pagetemplate (@) } } -1 +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). + my $str=shift; + + $str=s/[^a-zA-Z0-9+\-~_]/= ord($1)/; + +}; + + +1; diff --git a/encoding.pl b/encoding.pl deleted file mode 100644 index 9e8a896a9..000000000 --- a/encoding.pl +++ /dev/null @@ -1,40 +0,0 @@ -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($_); -} - diff --git a/filters/expand-iki-address.pl b/filters/expand-iki-address.pl index a11e6d9d0..e4e541cee 100644 --- a/filters/expand-iki-address.pl +++ b/filters/expand-iki-address.pl @@ -1,8 +1,7 @@ #!/usr/bin/perl use Mail::Internet; -use Compress::LZF ; -use MIME::Base64::URLSafe; +use URI::Escape; my $prefix="-comment-"; my $mail = Mail::Internet->new([<>]); @@ -11,7 +10,7 @@ my $to = $mail->get('To:'); if ($to =~ m/$prefix([A-Za-z0-9\-_]+)\@/){ my $key=$1; - my $page=decompress(urlsafe_b64decode($key)); + my $page=uri_unescape($key); $mail->replace('X-IkiWiki-Page:',$page); }