OSDN Git Service

link fix
[keitairc/keitairc.git] / lib / Keitairc / ClientInfo.pm
1 # -*- mode: perl; coding: utf-8 -*-
2 # Keitairc::ClientInfo
3 #
4 # Copyright (c) 2008 Jun Morimoto <morimoto@mrmt.net>
5 # This program is covered by the GNU General Public License 2
6
7 package Keitairc::ClientInfo;
8 use strict;
9 use warnings;
10
11 ################################################################
12 sub new{
13         my $proto = shift;
14         my $arg = shift;
15         my $me = {};
16
17         for my $key (keys %{$arg->{_headers}}){
18                 my $ukey = $key;
19                 $ukey =~ s/-/_/g;
20                 $me->{header}->{$ukey} = $arg->{_headers}->{$key};
21         }
22
23         if(defined $me->{header}->{cookie}){
24                 for my $set (split(/; */, $me->{header}->{cookie})){
25                         my ($name, $value) = split(/=/, $set);
26                         $value =~ s/%([0-9A-Fa-f]{2})/pack('C', hex($1))/eg;
27                         $me->{cookie}->{$name} = $value if !defined $me->{cookie}->{$name};
28                 }
29         }
30
31         bless $me;
32         $me->{serial_key} = $me->serial_key();
33         $me;
34 }
35
36 ################################################################
37 sub user_agent{
38         my $me = shift;
39         $me->{header}->{user_agent};
40 }
41
42 ################################################################
43 sub is_ezweb{
44         my $me = shift;
45         # old au phones (WAP1.0 era) does not match with this regexp,
46         # we don't care
47         defined $me->{header}->{user_agent} && $me->{header}->{user_agent} =~ /^KDDI-/;
48 }
49
50 ################################################################
51 sub is_docomo{
52         my $me = shift;
53         defined $me->{header}->{user_agent} && $me->{header}->{user_agent} =~ /^DoCoMo/;
54 }
55
56 ################################################################
57 sub is_webkit{
58         my $me = shift;
59
60         return $ENV{IS_WEBKIT} if (defined $ENV{IS_WEBKIT});
61
62         # Detect Apple Webkit based Mobile browser
63         defined $me->{header}->{user_agent} && $me->{header}->{user_agent} =~ /AppleWebKit.* Mobile/;
64 }
65
66 ################################################################
67 sub is_softbank{
68         my $me = shift;
69         defined $me->{header}->{user_agent} && $me->{header}->{user_agent} =~ /^(SoftBank|Vodafone|J-PHONE|MOT-)/;
70 }
71
72 ################################################################
73 sub is_emobile{
74         my $me = shift;
75         $me->is_emobile_mobilebrowser() || $me->is_emobile_openbrowser();
76 }
77
78 ################################################################
79 sub is_emobile_mobilebrowser{
80         my $me = shift;
81         defined $me->{header}->{user_agent} && $me->{header}->{user_agent} =~ /^emobile/;
82 }
83
84 ################################################################
85 sub is_emobile_openbrowser{
86         my $me = shift;
87         # http://developer.emnet.ne.jp/useragent.html
88         defined $me->{header}->{user_agent} && $me->{header}->{user_agent} =~ /(H11T; like Gecko|\) S11HT$)/;
89 }
90
91 ################################################################
92 sub is_mobile{
93         my $me = shift;
94         $me->is_ezweb() || $me->is_docomo() ||
95                 $me->is_softbank() || $me->is_emobile_mobilebrowser();
96 }
97
98 ################################################################
99 sub softbank_serial{
100         my $me = shift;
101         if($me->is_softbank()){
102                 if($me->{header}->{user_agent} =~ m|/(SN[0-9A-Z]+)|){
103                         return $1;
104                 }
105         }
106         undef;
107 }
108
109 ################################################################
110 sub docomo_foma_icc{
111         my $me = shift;
112         if($me->is_docomo()){
113                 # NTT DoCoMoのFOMA端末製造番号 (15桁のユニークな英数字) および
114                 # FOMAカード製造番号 (20桁のユニークな英数字)
115                 # http://www.nttdocomo.co.jp/service/imode/make/content/html/tag/utn.html
116                 # 実際にはlower caseも使われているようなので注意
117                 # DoCoMo/2.0 P703imyu(c100;TB;W30H15)
118                 # DoCoMo/2.0 P703imyu(c100;TB;W30H15;ser12345ABCDE12345;icc12345ABCDE12345abcde)
119                 if($me->{header}->{user_agent} =~ /;(icc[0-9a-zA-Z]{20})/){
120                         return $1;
121                 }
122         }
123         undef;
124 }
125
126 ################################################################
127 # http://www.nttdocomo.co.jp/service/imode/make/content/ip/#imodeid
128 sub docomo_imodeid{
129         my $me = shift;
130         $me->{header}->{x_dcmguid};
131 }
132
133 ################################################################
134 sub au_subscriber_id{
135         my $me = shift;
136         $me->{header}->{x_up_subno};
137 }
138
139 ################################################################
140 sub emobile_userid{
141         my $me = shift;
142         $me->{header}->{x_em_uid};
143 }
144
145 ################################################################
146 # we don't treat imodeid nor ser/icc of NTT DoCoMo,
147 # as they only send us them when they are forced to do it
148 sub serial_key{
149         my $me = shift;
150         if($me->is_softbank()){
151                 return $me->softbank_serial();
152         }
153         if($me->is_ezweb()){
154                 return $me->au_subscriber_id();
155         }
156         if($me->is_emobile()){
157                 return $me->emobile_userid();
158         }
159         undef;
160 }
161
162 ################################################################
163 # Cookieを扱えるかどうか
164 # とりあえず Cookie を確実に扱えない DoCoMo を判定してるだけ
165 sub cookie_available{
166         my $me = shift;
167         unless($me->is_docomo()){
168                 return 1;
169         }
170         0;
171 }
172
173 1;