summaryrefslogtreecommitdiff
path: root/fml/lib/Mail/Message/String.pm
diff options
context:
space:
mode:
authorfukachan <fukachan>2004-02-06 13:42:40 +0000
committerfukachan <fukachan>2004-02-06 13:42:40 +0000
commit103199ac84ee546c349e7cbc14a7a6ed4fa7c484 (patch)
treeb98d9df8783feabe0491120e06a3f2052e648cbb /fml/lib/Mail/Message/String.pm
parent3a26895db9cec6a3a76aa862ac3b4e96a53f7b66 (diff)
downloadfml8-103199ac84ee546c349e7cbc14a7a6ed4fa7c484.tar.gz
fml8-103199ac84ee546c349e7cbc14a7a6ed4fa7c484.tar.bz2
fml8-103199ac84ee546c349e7cbc14a7a6ed4fa7c484.zip
use mothod-style more even internally.
speculate in and out code/charset as could as possible. implement method: charcode_convert(), get_charcode().
Diffstat (limited to 'fml/lib/Mail/Message/String.pm')
-rw-r--r--fml/lib/Mail/Message/String.pm148
1 files changed, 133 insertions, 15 deletions
diff --git a/fml/lib/Mail/Message/String.pm b/fml/lib/Mail/Message/String.pm
index 967c93f3..86c16e58 100644
--- a/fml/lib/Mail/Message/String.pm
+++ b/fml/lib/Mail/Message/String.pm
@@ -4,7 +4,7 @@
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
-# $FML: String.pm,v 1.1 2004/01/31 06:32:58 fukachan Exp $
+# $FML: String.pm,v 1.2 2004/02/04 15:09:55 fukachan Exp $
#
package Mail::Message::String;
@@ -41,9 +41,17 @@ sub new
my ($self, $str) = @_;
my ($type) = ref($self) || $self;
+ # init
+ $str ||= '';
+
my $me = {};
set($me, $str);
- $me->{ _orig_string } = $str;
+ bless $me, $type;
+
+ # save hints for further use.
+ $me->{ _orig_string } = $str;
+ $me->{ _orig_mime_charset } = $me->get_mime_charset();
+
return bless $me, $type;
}
@@ -128,15 +136,18 @@ return charset information.
sub mime_encode
{
my ($self, $encode, $out_code, $in_code) = @_;
- my $str = $self->{ _string };
+ my $str = $self->as_str();
# base64 encoding by default.
- $encode ||= 'base64';
+ $encode ||= 'base64';
+
+ # speculate charset by a few hints.
+ $out_code ||= $self->_speculate_external_charset();
use Mail::Message::Encode;
my $obj = new Mail::Message::Encode;
$str = $obj->encode_mime_string($str, $encode, $out_code, $in_code);
- $self->{ _string } = $str;
+ $self->set($str);
return $str;
}
@@ -149,18 +160,19 @@ sub mime_encode
sub mime_decode
{
my ($self, $out_code, $in_code) = @_;
- my $str = $self->{ _string };
+ my $str = $self->as_str();
use Mail::Message::Encode;
my $encode = new Mail::Message::Encode;
my $dec_string = $encode->decode_mime_string($str, $out_code, $in_code);
- $self->{ _string } = $dec_string;
+ $self->set($dec_string);
return $dec_string;
}
-# Descriptions: dummy now. enforce charset to handle.
+# Descriptions: dummy now.
+# enforce charset to handle.
# Arguments: OBJ($self) STR($charset)
# Side Effects: none
# Return Value: none
@@ -179,7 +191,7 @@ sub set_mime_charset
sub get_mime_charset
{
my ($self) = @_;
- my $str = $self->{ _string };
+ my $str = $self->as_str();
if ($str =~ /\=\?([-\w\d]+)\?/o) {
my $charset = $1;
@@ -192,13 +204,109 @@ sub get_mime_charset
}
+=head1 CHAR CODE CONVERSION UTILITIES
+
+=head2 charcode_convert($out_code, [$in_code])
+
+convert charactor code to $out_code and return it.
+
+if $in_code is given, it is used as a hint.
+if $in_code is not given, speculate the code.
+
+if $out_code is not given, try to resolve the out code
+based on the initial data (e.g. =?ISO-2022-JP? part).
+
+=cut
+
+
+# Descriptions: convert charactor code to $out_code and return it.
+# if $in_code is given, it is used as a hint.
+# Arguments: OBJ($self) STR($out_code) STR($in_code)
+# Side Effects: none
+# Return Value: none
+sub charcode_convert
+{
+ my ($self, $out_code, $in_code) = @_;
+ my $str = $self->as_str();
+
+ # speculate internal code we should use for this string.
+ $out_code ||= $self->_speculate_internal_code();
+
+ use Mail::Message::Encode;
+ my $encode = new Mail::Message::Encode;
+ $encode->convert_str_ref(\$str, $out_code, $in_code);
+ $self->set($str);
+ return $str;
+}
+
+
+# Descriptions: get char code of $str.
+# Arguments: OBJ($self) STR($str)
+# Side Effects: none
+# Return Value: STR
+sub get_charcode
+{
+ my ($self, $str) = @_;
+
+ # init
+ $str ||= $self->as_str();
+
+ use Mail::Message::Encode;
+ my $encode = new Mail::Message::Encode;
+ return $encode->detect_code($str);
+}
+
+
+# Descriptions: speculate internal code to handle in fml.
+# Arguments: OBJ($self)
+# Side Effects: none
+# Return Value: STR
+sub _speculate_internal_code
+{
+ my ($self) = @_;
+ my $hint_code = $self->{ _orig_mime_charset };
+
+ # speculate fml internal code: iso-2022-jp -> euc-jp.
+ use Mail::Message::Charset;
+ my $charset = new Mail::Message::Charset;
+ my $language = $charset->message_charset_to_language($hint_code);
+ return $charset->language_to_internal_charset($language);
+}
+
+
+# Descriptions: speculate public charset to handle.
+# Arguments: OBJ($self)
+# Side Effects: none
+# Return Value: STR
+sub _speculate_external_charset
+{
+ my ($self) = @_;
+ my $hint_code = $self->{ _orig_mime_charset };
+ my $str = $self->as_str();
+
+ # speculate public code: euc-jp -> iso-2022-jp.
+ if ($hint_code) {
+ return $hint_code;
+ }
+ else {
+ use Mail::Message::Encode;
+ use Mail::Message::Charset;
+ my $encode = new Mail::Message::Encode;
+ my $charset = new Mail::Message::Charset;
+ my $cur_code = $encode->detect_code($str); # e.g. euc
+ my $language = $charset->message_charset_to_language($cur_code); #ja
+ return $charset->language_to_message_charset($language); # iso-2022-jp
+ }
+}
+
+
=head1 UTILITIES
=head2 unfold()
unfold in-core data.
-=cut
+=cut
# Descriptions: unfold in-core data.
@@ -208,13 +316,13 @@ unfold in-core data.
sub unfold
{
my ($self) = @_;
- my $str = $self->{ _string };
+ my $str = $self->as_str();
$str =~ s/\s*\n/ /g;
$str =~ s/\s+/ /g;
# save and return it.
- $self->{ _string } = $str;
+ $self->set($str);
return $str;
}
@@ -225,9 +333,19 @@ sub unfold
# debug
#
if ($0 eq __FILE__) {
- my $str = '=?ISO-2022-JP?B?GyRCJDckRCRiJHMbKEI=?=';
- my $obj = new Mail::Message::String $str;
- print $obj->get_mime_charset() ,"\n";
+ my $_str = '=?ISO-2022-JP?B?GyRCJDckRCRiJHMbKEI=?=';
+ my $str = new Mail::Message::String $_str;
+ print $str->get_mime_charset() ,"\n";
+ $str->mime_decode();
+ $str->charcode_convert();
+ print $str->as_str(), "\t(CONVERTED TO ";
+ print $str->get_charcode(), ")\n";
+ $str->mime_encode();
+ print $str->as_str(), "\n";
+ print $_str, " (ORIGINAL)\n";
+ print $str->get_charcode(), "\n";
+ $str->mime_decode();
+ print $str->get_charcode(), "\n";
}