OSDN Git Service

encoding handling massive update
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 22 May 2010 20:20:11 +0000 (05:20 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 22 May 2010 20:20:11 +0000 (05:20 +0900)
14 files changed:
data/plugins/00all
data/plugins/00index
data/plugins/00location
data/plugins/00location_receiver
data/plugins/00nick
data/plugins/00postme
data/plugins/00recent
data/plugins/00topic
data/plugins/00unread
data/plugins/32address
keitairc
lib/Keitairc/IrcBuffer.pm
lib/Keitairc/IrcCallback.pm
lib/Keitairc/View.pm

index a91c069..950453d 100644 (file)
@@ -40,7 +40,7 @@ $plugin = {
                                        }
                                        $message =~ s/\s+$//;
                                        $message =~ s/\s+/ /g;
-                                       $p->{message} = Encode::encode($::cf->web_charset(), $message);
+                                       $p->{message} = $message;
                                        $p->{read} = @{$rp}[$lp];
                                        @{$rp}[$lp] = 1;
                                        push @loop, $p;
@@ -50,7 +50,7 @@ $plugin = {
 
                my $ci = new Keitairc::ClientInfo($request);
                my $view = new Keitairc::View($::cf, $ci);
-               my $cname = Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid)));
+               my $cname = $::ib->simple_escape($::ib->compact_channel_name($cid));
                return $view->render('all.html', {
                        loop => \@loop,
                        channel_compact => $cname,
index fbee73a..f5cffdd 100644 (file)
@@ -43,7 +43,7 @@ $plugin = {
                my @loop;
                for my $cid ($::ib->channels()){
                        my $p = {};
-                       my $cname = Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid)));
+                       my $cname = $::ib->simple_escape($::ib->compact_channel_name($cid));
                        $p->{sid} = $session_id;
                        $p->{cid} = $cid;
                        $p->{cname} = $cname;
@@ -72,7 +72,7 @@ $plugin = {
                                $unread_channels++;
                        }
 
-                       $p->{mtime} = Encode::encode($::cf->web_charset(), $format_mtime->($::ib->mtime($cid)));
+                       $p->{mtime} = $format_mtime->($::ib->mtime($cid));
                        push @loop, $p;
                }
 
index afd03ce..b8f5105 100644 (file)
@@ -18,7 +18,7 @@ $plugin = {
                my $ci = new Keitairc::ClientInfo($request);
                my $view = new Keitairc::View($::cf, $ci);
                my $cid = $param_string;
-               my $cname = Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid)));
+               my $cname = $::ib->simple_escape($::ib->compact_channel_name($cid));
                return $view->render('location.html', {
                        web_schema => $::cf->web_schema(),
                        web_host => $::cf->web_host(),
index 4370fcc..b8a401c 100644 (file)
@@ -8,7 +8,7 @@
 
 use Net::HTTP;
 use XML::Simple;
-use Encode;
+use Encode qw(is_utf8 _utf8_off decode);
 
 # refits.cgk.affrc.go.jp を使ってgeocode逆変換
 # ただし 2009 年から www.finds.jp に移転した
@@ -78,8 +78,8 @@ sub get_rgeocode{
        }else{
                return;
        }
-       if(Encode::is_utf8($s)){
-               Encode::_utf8_off($s);
+       if(is_utf8($s)){
+               _utf8_off($s);
        }
        return $s;
 }
@@ -195,7 +195,7 @@ $plugin = {
                        $p->{tx} = $tx;
                        $p->{ty} = $ty;
                        $p->{map_image_url} = google_map_image($wx, $wy, 'gif');
-                       Encode::from_to($p->{address}, 'utf8', $::cf->web_charset());
+                       $p->{address} = decode('utf8', $p->{address});
                        return $view->render('location_receiver.html', $p);
                }
 
@@ -231,7 +231,7 @@ $plugin = {
                        $p->{tx} = $tx;
                        $p->{ty} = $ty;
                        $p->{map_image_url} = google_map_image($wx, $wy, 'png');
-                       Encode::from_to($p->{address}, 'utf8', $::cf->web_charset());
+                       $p->{address} = decode('utf8', $p->{address});
                        return $view->render('location_receiver.html', $p);
                }
 
@@ -256,7 +256,7 @@ $plugin = {
                        $p->{tx} = $tx;
                        $p->{ty} = $ty;
                        $p->{map_image_url} = google_map_image($wx, $wy, 'png');
-                       Encode::from_to($p->{address}, 'utf8', $::cf->web_charset());
+                       $p->{address} = decode('utf8', $p->{address});
                        return $view->render('location_receiver.html', $p);
                }
 
@@ -287,7 +287,7 @@ $plugin = {
                        $p->{tx} = $tx;
                        $p->{ty} = $ty;
                        $p->{map_image_url} = google_map_image($wx, $wy, 'gif');
-                       Encode::from_to($p->{address}, 'utf8', $::cf->web_charset());
+                       $p->{address} = decode('utf8', $p->{address});
                        return $view->render('location_receiver.html', $p);
                }
 
index 58246c8..2fbe68d 100644 (file)
@@ -18,13 +18,12 @@ $plugin = {
                        $p->{nick} = $nick;
                        $p->{op} = $::ib->get_nick_op($cid, $nick);
                        $p->{realname} = $::ib->get_nick_realname($cid, $nick);
-                       Encode::from_to($p->{realname}, $::cf->irc_charset(), $::cf->web_charset());
                        push @loop, $p;
                }
                my $ci = new Keitairc::ClientInfo($request);
                my $view = new Keitairc::View($::cf, $ci);
                return $view->render('nick.html', {
-                       channel_compact => Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid))),
+                       channel_compact => $::ib->simple_escape($::ib->compact_channel_name($cid)),
                        loop => \@loop,
                        cid => $cid
                             });
index 6028b1f..cfb569b 100644 (file)
@@ -40,7 +40,7 @@ $plugin = {
                        $view->template('postme.eml', {
                                from => $::cf->smtp_from(),
                                to => $::cf->smtp_to(),
-                               message => $message,                    
+                               message => $message,
                                })
                        );
                $smtp->dataend();
index 910ecf8..d5b7a53 100644 (file)
@@ -15,11 +15,9 @@ $plugin = {
                        my $channel = $::ib->cid2name($cid);
                        if($::ib->unread_lines($cid)){
                                my $name = $::ib->cid2name($cid);
-                               Encode::from_to($name, $::cf->irc_charset(), $::cf->web_charset());
                                my $p = {};
                                $p->{cid} = $cid;
-#                              $p->{cname} = $::ib->compact_channel_name($name);
-                               $p->{cname} = Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid)));
+                               $p->{cname} = $::ib->simple_escape($::ib->compact_channel_name($cid));
                                my ($tp, $np, $mp, $rp) = $::ib->buffer_ptr($cid);
                                my @looop;
                                if(defined $tp){
@@ -44,7 +42,7 @@ $plugin = {
                                                }
                                                $message =~ s/\s+$//;
                                                $message =~ s/\s+/ /g;
-                                               $pp->{message} = Encode::encode($::cf->web_charset(), $message);
+                                               $pp->{message} = $message;
                                                @{$rp}[$lp] = 1;
                                                push @looop, $pp;
                                        }
index a4a22b2..1d9e6be 100644 (file)
@@ -14,8 +14,8 @@ $plugin = {
                for my $cid ($::ib->channels()){
                        my $p = {};
                        $p->{cid} = $cid;
-                       $p->{cname} = Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid)));
-                       $p->{topic} = Encode::encode($::cf->web_charset(), $::ib->simple_escape(Encode::decode($::cf->irc_charset(), $::ib->topic($cid))));
+                       $p->{cname} = $::ib->simple_escape($::ib->compact_channel_name($cid));
+                       $p->{topic} = $::ib->simple_escape($::ib->topic($cid));
                        push @loop, $p;
                }
 
index 6949ae4..3b32983 100644 (file)
@@ -41,7 +41,7 @@ $plugin = {
                                        }
                                        $message =~ s/\s+$//;
                                        $message =~ s/\s+/ /g;
-                                       $p->{message} = Encode::encode($::cf->web_charset(), $message);
+                                       $p->{message} = $message;
                                        @{$rp}[$lp] = 1;
                                        push @loop, $p;
                                }
@@ -50,7 +50,7 @@ $plugin = {
 
                my $ci = new Keitairc::ClientInfo($request);
                my $view = new Keitairc::View($::cf, $ci);
-               my $cname = Encode::encode($::cf->web_charset(), $::ib->simple_escape($::ib->compact_channel_name($cid)));
+               my $cname = $::ib->simple_escape($::ib->compact_channel_name($cid));
                return $view->render('unread.html', {
                        loop => \@loop,
                        channel_compact => $cname,
index 5b9de18..0b32808 100644 (file)
@@ -64,9 +64,7 @@ $plugin = {
                my ($request, $name, $session_id, $param) = @_;
                my $ci = new Keitairc::ClientInfo($request);
                my $view = new Keitairc::View($::cf, $ci);
-               my $address = URI::Escape::uri_unescape($param);
-               # $address is utf8 string w/o utf flag, so we use from_to()
-               Encode::from_to($address, 'utf8', $::cf->web_charset());
+               my $address = Encode::deconde('utf8', URI::Escape::uri_unescape($param));
                return $view->render('address.html', {
                        session_id => $session_id,
                        address => $address,
index eac773c..9ba865f 100755 (executable)
--- a/keitairc
+++ b/keitairc
@@ -435,11 +435,9 @@ sub send_message{
        my ($message, $timestamp) = parse_message($request);
 
        if(length($message) && length($channel)){
-               my $irc_external = $message;
-               Encode::from_to($irc_external, $cf->web_charset(), $cf->irc_charset());
                my $internal = Encode::decode($cf->web_charset(), $message);
                if($ib->update_timestamp($timestamp)){
-                       $irc->yield(privmsg => $channel => $irc_external);
+                       $irc->yield(privmsg => $channel => $internal);
                        my $cid = $ib->name2cid($channel);
                        $ib->add_message($cid, $internal, $cf->irc_nick());
                }
@@ -452,7 +450,7 @@ sub send_command{
        my ($message, $timestamp) = parse_message($request);
 
        if(length($message)){
-               Encode::from_to($message, $cf->web_charset(), $cf->irc_charset());
+               $message = Encode::decode($cf->web_charset(), $message);
                if($message =~ s|^/||) {
                        my ($params, $trailing) = split(/ :/, $message, 2);
                        my @postcmd = split(/ /, $params);
index d4ee957..6dc4949 100644 (file)
@@ -5,7 +5,7 @@
 # This program is covered by the GNU General Public License 2
 
 package Keitairc::IrcBuffer;
-use Encode;
+use Encode qw(decode);
 use strict;
 use warnings;
 
@@ -19,9 +19,7 @@ sub new{
 
        # join しているchannelの名称を記録するハッシュ。
        # - cid および name2cid ハッシュに格納されている値は整数。
-       # - cid2nameハッシュに格納されている文字列、および name2cid の
-       #   ハッシュキーは iso-2022-jp (ないしはそのircチャネルで使われ
-       #   ているcharset)のまま。これは歴史的理由でこうするしかない。
+       # - cid2nameハッシュに格納されているのは perl decoded な チャネル名
        $me->{cid2name} = {};
        $me->{name2cid} = {};
 
@@ -126,11 +124,11 @@ sub format_date{
 }
 
 ################################################################
-# 引数の $name (チャネル名) は、生の iso-2022-jp のまま、ないしは irc
-# チャネルで扱われている charset のまま扱うこと。こういう慣習なので
-# しょうがない。
 sub name2cid{
        my($me, $name) = @_;
+       # name2cid だけ $name が irc_charset のまま
+       # 直すべきな気がするな...
+       $name = decode($::cf->irc_charset(), $name);
        $name =~ tr/A-Z[\\]^/a-z{|}~/;
 
        unless(defined $me->{name2cid}->{$name}){
@@ -166,7 +164,6 @@ sub part{
 sub join{
        my ($me, $name) = @_;
        my $cid = $me->name2cid($name);
-       $me->{cid2name}->{$cid} = $name;
 }
 
 ################################################################
@@ -199,7 +196,6 @@ sub buffer_ptr{
 
 ################################################################
 # 引数の $msg の charset は perl internal
-# $channel は iso-2022-jp または irc channel specific
 sub add_message{
        my($me, $cid, $message, $who) = @_;
 
@@ -251,11 +247,6 @@ sub compact_channel_name{
 
        return undef unless defined $name;
 
-       # この後の置換処理は、チャネル名の文字列が実際に運用されている
-       # charset で行う必要がある。日本語が用いられている従来の irc チャ
-       # ネルであれば iso-2022-jp-1 だし、ないしは utf8 のまま。
-       $name = Encode::decode($::cf->irc_charset(), $name);
-
        # #name:*.jp を %name に
        if($name =~ s/:\*\.jp$//){
                $name =~ s/^#/%/;
index 7eb703a..74484fd 100644 (file)
@@ -6,7 +6,7 @@
 
 package Keitairc::IrcCallback;
 use POE;
-use Encode;
+use Encode qw(decode);
 use strict;
 use warnings;
 
@@ -46,7 +46,7 @@ sub irc_public{
        my $ib = $heap->{IrcBuffer};
        my $cf = $heap->{Config};
        $who =~ s/!.*//;
-       $msg = Encode::decode($cf->irc_charset(), $msg);
+       $msg = decode($cf->irc_charset(), $msg);
        my $cid = $ib->name2cid($channel->[0]);
        $heap->{IrcBuffer}->add_message($cid, $msg, $who);
        $heap->{seen_traffic} = 1;
@@ -117,7 +117,7 @@ sub irc_quit{
        my $cf = $heap->{Config};
 
        $who =~ s/!.*//;
-       $message = Encode::decode($cf->irc_charset(), $message);
+       $message = decode($cf->irc_charset(), $message);
 
        for my $cid ($ib->channels()) {
                for my $name ($ib->list_nick($cid)) {
@@ -151,10 +151,11 @@ sub irc_whoreply{
        my ($channel, $user, $host, $server, $nick, $hg, $hop, @realname) =
                split(/\s/, $arg);
        my $ib = $heap->{IrcBuffer};
+       my $cf = $heap->{Config};
        my $op = ($hg =~ /@/ ? 1 : 0);
        my $cid = $ib->name2cid($channel);
-       # @realname may be iso-2022-jp (or irc channel's charset)
-       $ib->add_nick($cid, $nick, $op, join(' ', @realname));
+       # @realname may be irc channel's charset
+       $ib->add_nick($cid, $nick, $op, decode($cf->irc_charset(), join(' ', @realname)));
 }
 
 ################################################################
@@ -220,7 +221,7 @@ sub irc_notice{
 
        $who =~ s/!.*//;
        my $cid = $ib->name2cid($channel->[0]);
-       $msg = Encode::decode($cf->irc_charset(), $msg);
+       $msg = decode($cf->irc_charset(), $msg);
 
        $ib->add_message($cid, $msg, $who);
        $heap->{seen_traffic} = 1;
@@ -233,9 +234,9 @@ sub irc_topic{
        my $ib = $heap->{IrcBuffer};
        my $cf = $heap->{Config};
        my $cid = $ib->name2cid($channel);
+       $topic = decode($cf->irc_charset(), $topic);
        $ib->topic($cid, $topic);
        $who =~ s/!.*//;
-       $topic = Encode::decode($cf->irc_charset(), $topic);
        $ib->add_message($cid, "$who set topic: $topic");
        $heap->{seen_traffic} = 1;
        $heap->{disconnect_msg} = 1;
@@ -246,8 +247,9 @@ sub irc_topicraw{
        my ($kernel, $heap, $raw) = @_[KERNEL, HEAP, ARG1];
        my ($channel, $topic) = ($raw =~ m/(\S+)\s+:?(.*)$/);
        my $ib = $heap->{IrcBuffer};
+       my $cf = $heap->{Config};
        my $cid = $ib->name2cid($channel);
-       $ib->topic($cid, $topic);
+       $ib->topic($cid, decode($cf->irc_charset(), $topic));
        $heap->{seen_traffic} = 1;
        $heap->{disconnect_msg} = 1;
 }
@@ -269,7 +271,7 @@ sub irc_msg{
        }
        $ib->join($buddy); # XXX: join $buddy if not joined
        my $cid = $ib->name2cid($buddy);
-       $msg = Encode::decode($cf->irc_charset(), $msg);
+       $msg = decode($cf->irc_charset(), $msg);
        $ib->add_message($cid, $msg, $who);
        $heap->{seen_traffic} = 1;
        $heap->{disconnect_msg} = 1;
@@ -283,7 +285,7 @@ sub irc_ctcp_action{
 
        $who =~ s/!.*//;
        my $cid = $ib->name2cid($channel->[0]);
-       $msg = Encode::decode($cf->irc_charset(), $msg);
+       $msg = decode($cf->irc_charset(), $msg);
        $ib->add_message($cid, sprintf('* %s %s', $who, $msg));
        $heap->{seen_traffic} = 1;
        $heap->{disconnect_msg} = 1;
index b472746..462b277 100644 (file)
@@ -112,18 +112,13 @@ sub template{
                }
        }
 
-       # Note: ishikawa
-       # 現状、varsの中は既にencodeされた文字列が入ってきているはずだけど
-       # 内部コードのままで、最後の output を encode するのが本来はいい
-       # のではないかと思う。
-       # が、今いじるとあちこち影響でそうなので 2.0 出てから直す。
        if (my $filename = $me->template_path("$arch/$template")) {
                my $t = new HTML::Template(
                        filename => $filename,
                        die_on_bad_params => 0,
                        filter => sub {
                                my $text_ref = shift;
-                               Encode::from_to($$text_ref, 'utf8', $me->{Config}->web_charset());
+                               $$text_ref = Encode::decode('utf8', $$text_ref);
                        },
                        cache => 1);
                $t->param($p);
@@ -132,7 +127,7 @@ sub template{
                        $t->param($vars);
                }
 
-               $t->output();
+               Encode::encode($me->{Config}->web_charset(), $t->output());
        }
 }