Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merge mime branch

  • Loading branch information...
commit 08577463f989fc0ccafe2e9c0709e42889e53d1b 1 parent d679d94
tomi-ru authored
18 lib/Encode/JP/Mobile.pm
View
@@ -35,6 +35,12 @@ use Encode::JP::Mobile::Vodafone;
use Encode::JP::Mobile::KDDIJIS;
use Encode::JP::Mobile::AirHJIS;
use Encode::JP::Mobile::ConvertPictogramSJIS;
+
+use Encode::JP::Mobile::MIME::DoCoMo;
+use Encode::JP::Mobile::MIME::KDDI;
+use Encode::JP::Mobile::MIME::SoftBank;
+use Encode::JP::Mobile::MIME::AirH;
+
require Encode::JP::Mobile::Fallback;
require Encode::JP::Mobile::Character;
@@ -42,9 +48,9 @@ use Encode::MIME::Name;
for (Encode->encodings('JP::Mobile')) {
next if defined $Encode::MIME::Name::MIME_NAME_OF{$_};
- my $mime_name = $_ =~ /utf8/ ? 'UTF-8'
- : $_ =~ /sjis/ ? 'Shift_JIS'
- : $_ =~ /2022/ ? 'ISO-2022-JP'
+ my $mime_name = $_ =~ /utf8/i ? 'UTF-8'
+ : $_ =~ /sjis/i ? 'Shift_JIS'
+ : $_ =~ /2022/i ? 'ISO-2022-JP'
: undef;
$Encode::MIME::Name::MIME_NAME_OF{$_} = $mime_name if $mime_name;
}
@@ -273,6 +279,12 @@ x-sjis-* のエンコーディングには -raw suffix がついたバージョ
x-utf8-*-raw が用意されていないのは、utf-8 エンコーディングがそれにあたるからです。
+=item MIME-Header-JP-Mobile-DoCoMo, MIME-Header-JP-Mobile-DoCoMo-SJIS, MIME-Header-JP-Mobile-KDDI, MIME-Header-JP-Mobile-KDDI-SJIS, MIME-Header-JP-Mobile-SoftBank, MIME-Header-JP-Mobile-SoftBank-UTF8, MIME-Header-JP-Mobile-SoftBank-SJIS, MIME-Header-JP-Mobile-AirH, MIME-Header-JP-Mobile-AirH-SJIS
+
+これらはメールの件名で利用するMIMEエンコーディング用です。主に送信時に利用するためのものです。
+
+L<Encode::JP::Mobile::MIME::DoCoMo>, L<Encode::JP::Mobile::MIME::KDDI>, L<Encode::JP::Mobile::MIME::SoftBank>, L<Encode::JP::Mobile::MIME::AirH> をご覧下さい。
+
=back
=head1 UNICODE PROPERTIES
166 lib/Encode/JP/Mobile/MIME.pm
View
@@ -0,0 +1,166 @@
+package Encode::JP::Mobile::MIME;
+use strict;
+use warnings;
+use base 'Encode::Encoding';
+
+use Encode ();
+use Encode::JP::Mobile;
+use MIME::Words;
+
+sub subject_encoding {
+ Encode::find_encoding('utf-8');
+}
+
+sub charset_to_encoding {
+ my ($self, $charset) = @_;
+ Encode::find_encoding($charset);
+}
+
+sub encode($$;$){
+ my ($self, $str, $check) = @_;
+ my $encoding = $self->subject_encoding
+ or die "encoding is not found.";
+
+ $str = $encoding->encode($str, $check);
+ $str = MIME::Words::encode_mimeword($str, 'B', $encoding->mime_name);
+ return $str;
+}
+
+sub decode($$;$){
+ my ($self, $str, $check) = @_;
+
+ my $ret = "";
+ for my $part (MIME::Words::decode_mimewords($str)) {
+ my ($bytes, $charset) = @$part;
+ my $encoding = $self->charset_to_encoding($charset)
+ or die "encoding is not found for $charset.";
+
+ $ret .= $encoding->decode($bytes, $check);
+ }
+
+ return $ret;
+}
+
+1;
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+Encode::JP::Mobile::MIME - 絵文字を含んだメールのSubject用MIMEエンコーディング
+
+=head1 SYNOPSIS
+
+ Encode::decode('MIME-Header-JP-Mobile-DoCoMo', $email->header('subject'));
+ Encode::encode('MIME-Header-JP-Mobile-DoCoMo', "\x{E63E}です"); # =?SHIFT_JIS?B?+J+CxYK3?=
+ Encode::encode('MIME-Header-JP-Mobile-DoCoMo-SJIS', "\x{E63E}です"); # =?SHIFT_JIS?B?+J+CxYK3?=
+
+ Encode::decode('MIME-Header-JP-Mobile-KDDI', $email->header('subject'));
+ Encode::encode('MIME-Header-JP-Mobile-KDDI', "\x{E63E}です"); # "\xF6\x60\x82\xC5\x82\xB7"
+ Encode::encode('MIME-Header-JP-Mobile-KDDI-SJIS', "\x{E63E}です"); # "\xF6\x60\x82\xC5\x82\xB7"
+
+ Encode::decode('MIME-Header-JP-Mobile-SoftBank', $email->header('subject'));
+ Encode::encode('MIME-Header-JP-Mobile-SoftBank', "\x{E63E}です"); # =?UTF-8?B?7oGK44Gn44GZ?=
+ Encode::encode('MIME-Header-JP-Mobile-SoftBank-UTF8', "\x{E63E}です"); # =?UTF-8?B?7oGK44Gn44GZ?=
+ Encode::encode('MIME-Header-JP-Mobile-SoftBank-SJIS', "\x{E63E}です"); # =?SHIFT_JIS?B?GyRHag+CxYK3?=
+
+ Encode::decode('MIME-Header-JP-Mobile-AirH', $email->header('subject'));
+ Encode::encode('MIME-Header-JP-Mobile-AirH', "\x{E63E}です"); # =?SHIFT_JIS?B?+J+CxYK3?=
+ Encode::encode('MIME-Header-JP-Mobile-AirH-SJIS', "\x{E63E}です"); # =?SHIFT_JIS?B?+J+CxYK3?=
+
+=head1 ENCODINGS
+
+=over 4
+
+=item DoCoMo 向け
+
+=over 4
+
+=item MIME-Header-JP-Mobile-DoCoMo, MIME-Header-JP-Mobile-iMode
+
+次項の C<MIME-Header-JP-Mobile-DoCoMo-SJIS> へのエイリアスです。
+
+=item MIME-Header-JP-Mobile-DoCoMo-SJIS, MIME-Header-JP-Mobile-iMode-SJIS
+
+decode は shift_jis の場合 C<x-sjis-docomo> を利用し絵文字をマッピングします。
+が、現在実際のところ(gmail.com などの特別な場合を除き)絵文字はゲタとなって
+送られてくるので、このエンコーディングで絵文字を取ることはできないでしょう。
+
+encode は C<x-sjis-docomo> で encode してから MIME エンコードします。
+
+=back
+
+=item KDDI 向け
+
+=over 4
+
+=item MIME-Header-JP-Mobile-KDDI, MIME-Header-JP-Mobile-EZweb
+
+C<MIME-Header-JP-Mobile-KDDI-SJIS> へのエイリアスです。
+
+=item MIME-Header-JP-Mobile-KDDI-SJIS, MIME-Header-JP-Mobile-EZweb-SJIS
+
+decode は iso-2022-jp や shift_jis の場合 C<x-iso-2022-jp-kddi-auto> や
+C<x-sjis-kddi-auto> を利用し絵文字をマッピングします。
+
+encode は C<x-sjis-kddi-auto> で encode し、au は MIME エンコーディングをすると
+化けるためそのまま出力します。
+
+=back
+
+=item SoftBank 向け
+
+=over 4
+
+=item MIME-Header-JP-Mobile-SoftBank, MIME-Header-JP-Mobile-Vodafone
+
+次項の C<MIME-Header-JP-Mobile-SoftBank-UTF8> へのエイリアスです。
+
+=item MIME-Header-JP-Mobile-SoftBank-UTF8, MIME-Header-JP-Mobile-Vodafone-UTF8
+
+decode は utf-8 や shift_jis の場合 C<x-utf8-softbank> や
+C<x-sjis-softbank> を利用し絵文字をマッピングします。
+が、現在実際のところ(gmail.com などの特別な場合を除き)絵文字はゲタとなって
+送られてくるので、このエンコーディングで絵文字を取ることはできないでしょう。
+
+encode は C<x-utf8-softbank> で encode してから MIME エンコードします。
+
+=item MIME-Header-JP-Mobile-SoftBank-SJIS, MIME-Header-JP-Mobile-Vodafone-SJIS
+
+decode は C<MIME-Header-JP-Mobile-SoftBank-UTF8> と同じです。
+
+encode は C<x-sjis-softbank> で encode してから MIME エンコードします。
+fold はしません。
+
+=back
+
+=item WILLCOM 向け
+
+=over 4
+
+=item MIME-Header-JP-Mobile-AirH, MIME-Header-JP-Mobile-Airedge
+
+次項の C<MIME-Header-JP-Mobile-AirH-SJIS> へのエイリアスです。
+
+=item MIME-Header-JP-Mobile-AirH-SJIS, MIME-Header-JP-Mobile-Airedge-SJIS
+
+decode は iso-2022-jp や shift_jis の場合 C<x-iso-2022-jp-airh> や
+C<x-sjis-airh> を利用し絵文字をマッピングします。
+
+encode は C<x-sjis-airh> で encode してから MIME エンコードします。
+
+=back
+
+=back
+
+=head1 AUTHOR
+
+Naoki Tomita E<lt>tomita@cpan.orgE<gt>
+
+=head1 SEE ALSO
+
+L<Encode::JP::Mobile>, L<http://codezine.jp/a/article/aid/1262.aspx>
+
+=cut
+
31 lib/Encode/JP/Mobile/MIME/AirH.pm
View
@@ -0,0 +1,31 @@
+package Encode::JP::Mobile::MIME::AirH;
+use strict;
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-Airedge' => 'MIME-Header-JP-Mobile-AirH-SJIS');
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-AirH' => 'MIME-Header-JP-Mobile-AirH-SJIS');
+
+
+package Encode::JP::Mobile::MIME::AirH::SJIS;
+use strict;
+use base 'Encode::JP::Mobile::MIME';
+__PACKAGE__->Define('MIME-Header-JP-Mobile-AirH-SJIS');
+
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-Airedge-SJIS' => 'MIME-Header-JP-Mobile-AirH-SJIS');
+
+sub subject_encoding {
+ Encode::find_encoding('x-sjis-airh');
+}
+
+sub charset_to_encoding {
+ my ($self, $charset) = @_;
+
+ if (!$charset || $charset =~ /iso-2022-jp/i) {
+ $charset = 'x-iso-2022-jp-airh';
+ }
+ elsif ($charset =~ /shift_jis/i) {
+ $charset = 'x-sjis-airh';
+ }
+
+ Encode::find_encoding($charset);
+}
+
+1;
28 lib/Encode/JP/Mobile/MIME/DoCoMo.pm
View
@@ -0,0 +1,28 @@
+package Encode::JP::Mobile::MIME::DoCoMo;
+use strict;
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-DoCoMo' => 'MIME-Header-JP-Mobile-DoCoMo-SJIS');
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-iMode' => 'MIME-Header-JP-Mobile-DoCoMo-SJIS');
+
+
+package Encode::JP::Mobile::MIME::DoCoMo::SJIS;
+use strict;
+use base 'Encode::JP::Mobile::MIME';
+__PACKAGE__->Define('MIME-Header-JP-Mobile-DoCoMo-SJIS');
+
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-iMode-SJIS' => 'MIME-Header-JP-Mobile-DoCoMo-SJIS');
+
+sub subject_encoding {
+ Encode::find_encoding('x-sjis-docomo');
+}
+
+sub charset_to_encoding {
+ my ($self, $charset) = @_;
+
+ if (!$charset || $charset =~ /shift_jis/i) {
+ $charset = 'x-sjis-docomo';
+ }
+
+ Encode::find_encoding($charset);
+}
+
+1;
42 lib/Encode/JP/Mobile/MIME/KDDI.pm
View
@@ -0,0 +1,42 @@
+package Encode::JP::Mobile::MIME::KDDI;
+use strict;
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-KDDI' => 'MIME-Header-JP-Mobile-KDDI-SJIS');
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-EZweb' => 'MIME-Header-JP-Mobile-KDDI-SJIS');
+
+sub charset_to_encoding {
+ my ($self, $charset) = @_;
+
+ if (!$charset || $charset =~ /iso-2022-jp/i) {
+ $charset = 'x-iso-2022-jp-kddi-auto';
+ }
+ elsif ($charset =~ /shift_jis/i) {
+ $charset = 'x-sjis-kddi-auto';
+ }
+
+ Encode::find_encoding($charset);
+}
+
+
+package Encode::JP::Mobile::MIME::KDDI::SJIS;
+use strict;
+use base 'Encode::JP::Mobile::MIME';
+__PACKAGE__->Define('MIME-Header-JP-Mobile-KDDI-SJIS');
+
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-EZweb-SJIS' => 'MIME-Header-JP-Mobile-KDDI-SJIS');
+
+sub subject_encoding {
+ Encode::find_encoding('x-sjis-kddi-auto');
+}
+
+sub charset_to_encoding {
+ Encode::JP::Mobile::MIME::KDDI::charset_to_encoding(@_);
+}
+
+sub encode($$;$){
+ my ($self, $str, $check) = @_;
+
+ $str = $self->subject_encoding->encode($str, $check);
+ return $str;
+}
+
+1;
51 lib/Encode/JP/Mobile/MIME/SoftBank.pm
View
@@ -0,0 +1,51 @@
+package Encode::JP::Mobile::MIME::SoftBank;
+use strict;
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-SoftBank' => 'MIME-Header-JP-Mobile-SoftBank-UTF8');
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-Vodafone' => 'MIME-Header-JP-Mobile-SoftBank-UTF8');
+
+sub charset_to_encoding {
+ my ($self, $charset) = @_;
+
+ if (!$charset || $charset =~ /utf-8/i) {
+ $charset = 'x-utf8-softbank';
+ }
+ elsif ($charset =~ /shift_jis/i) {
+ $charset = 'x-sjis-softbank';
+ }
+
+ Encode::find_encoding($charset);
+}
+
+
+package Encode::JP::Mobile::MIME::SoftBank::UTF8;
+use strict;
+use base 'Encode::JP::Mobile::MIME';
+__PACKAGE__->Define('MIME-Header-JP-Mobile-SoftBank-UTF8');
+
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-Vodafone-UTF8' => 'MIME-Header-JP-Mobile-SoftBank-UTF8');
+
+sub subject_encoding {
+ Encode::find_encoding('x-utf8-softbank');
+}
+
+sub charset_to_encoding {
+ Encode::JP::Mobile::MIME::SoftBank::charset_to_encoding(@_);
+}
+
+
+package Encode::JP::Mobile::MIME::SoftBank::SJIS;
+use strict;
+use base 'Encode::JP::Mobile::MIME';
+__PACKAGE__->Define('MIME-Header-JP-Mobile-SoftBank-SJIS');
+
+Encode::Alias::define_alias('MIME-Header-JP-Mobile-Vodafone-SJIS' => 'MIME-Header-JP-Mobile-SoftBank-SJIS');
+
+sub subject_encoding {
+ Encode::find_encoding('x-sjis-softbank');
+}
+
+sub charset_to_encoding {
+ Encode::JP::Mobile::MIME::SoftBank::charset_to_encoding(@_);
+}
+
+1;
75 t/mime_encoding.t
View
@@ -0,0 +1,75 @@
+use strict;
+use warnings;
+use Test::More tests => 10;
+
+use Encode;
+use Encode::JP::Mobile;
+use utf8;
+
+is(
+ Encode::encode('MIME-Header-JP-Mobile-DoCoMo-SJIS', "\x{E63E}です"),
+ '=?SHIFT_JIS?B?+J+CxYK3?=',
+ 'docomo mime encode'
+);
+is(
+ Encode::decode('MIME-Header-JP-Mobile-DoCoMo', '=?SHIFT_JIS?B?+J+CxYK3?='),
+ "\x{E63E}です",
+ 'docomo mime decode'
+);
+
+is(
+ Encode::encode('MIME-Header-JP-Mobile-KDDI-SJIS', "\x{E63E}です"),
+ "\xF6\x60\x82\xC5\x82\xB7",
+ 'kddi mime encode'
+);
+is(
+ Encode::decode('MIME-Header-JP-Mobile-KDDI', "\xF6\x60\x82\xC5\x82\xB7"),
+ "\x{EF60}です",
+ 'kddi mime decode (raw)'
+);
+is(
+ Encode::decode('MIME-Header-JP-Mobile-KDDI', '=?iso-2022-jp?B?GyRCdUEkRyQ5GyhC?='),
+ "\x{EF60}です",
+ 'kddi mime decode (iso-2022-jp)'
+);
+
+is(
+ Encode::encode('MIME-Header-JP-Mobile-SoftBank-UTF8', "\x{E63E}です"),
+ '=?UTF-8?B?7oGK44Gn44GZ?=',
+ 'softbank mime encode'
+);
+is(
+ Encode::encode('MIME-Header-JP-Mobile-SoftBank-SJIS', "\x{E63E}です"),
+ '=?SHIFT_JIS?B?GyRHag+CxYK3?=',
+ 'softbank mime encode (shift_jis)'
+);
+is(
+ Encode::decode('MIME-Header-JP-Mobile-SoftBank', '=?UTF-8?B?7oGK44Gn44GZ?='),
+ "\x{E04A}です",
+ 'softbank mime decode (utf-8)'
+);
+
+# このtestはx-iso-2022-jp-softbankというencodingを作ろうかとしていた時の名残です。
+# この文字列はsoftbankから送ってgmailから転送された時の晴れ(昼)なんだけど
+# encodingはx-iso-2022-jp-kddiになってて、これは恐らくgmail依存な動作なので
+# MIME-Header-JP-Mobile-SoftBankのdecodeでこれを正しく晴れに戻せるのは
+# 若干おかしい気がする。なのでdecodeサポートは無しでいいんじゃないかなぁと。
+# というわけでコメントアウトにしとく。
+# MIME-Header-JP-Mobile-Softbank-Gmailとかを作ってもいいかもだけど
+# ref: http://mobilehacker.g.hatena.ne.jp/nihen/20090104/1231081872
+# is(
+# Encode::decode('MIME-Header-JP-Mobile-SoftBank', '=?ISO-2022-JP?B?GyRCdUEbKEIbJEIkRyQ5GyhC?='),
+# "\x{E04A}です",
+# 'softbank mime decode (iso-2022-jp)'
+# );
+
+is(
+ Encode::encode('MIME-Header-JP-Mobile-Airedge-SJIS', "\x{E63E}です"),
+ '=?SHIFT_JIS?B?+J+CxYK3?=',
+ 'willcom mime encode'
+);
+is(
+ Encode::decode('MIME-Header-JP-Mobile-Airedge', '=?SHIFT_JIS?B?+J+CxYK3?='),
+ "\x{E63E}です",
+ 'willcom mime decode'
+);
5 t/mime_name.t
View
@@ -8,6 +8,7 @@ my @utf8_encodings = qw(
x-utf8-docomo
x-utf8-softbank
x-utf8-kddi
+ MIME-Header-JP-Mobile-SoftBank-UTF8
);
my @sjis_encodings = qw(
x-sjis-imode
@@ -22,6 +23,10 @@ my @sjis_encodings = qw(
x-sjis-kddi-cp932-raw
x-sjis-kddi-auto-raw
x-sjis-airh-raw
+ MIME-Header-JP-Mobile-DoCoMo-SJIS
+ MIME-Header-JP-Mobile-KDDI-SJIS
+ MIME-Header-JP-Mobile-SoftBank-SJIS
+ MIME-Header-JP-Mobile-Airedge-SJIS
);
my @jis_encodings = qw(
x-iso-2022-jp-kddi
Please sign in to comment.
Something went wrong with that request. Please try again.