jpmobile 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (23) hide show
  1. data/VERSION.yml +1 -1
  2. data/lib/jpmobile/rack/params_filter.rb +1 -1
  3. data/test/rails/rails_root/app/models/mobile_mailer.rb +18 -0
  4. data/test/rails/rails_root/config/initializers/secret_token.rb +1 -1
  5. data/test/rails/rails_root/spec/models/mobile_mailer_spec.rb +744 -0
  6. data/test/rails/rails_root/spec/spec_helper.rb +6 -0
  7. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile.rb +2 -0
  8. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/email.rb +17 -2
  9. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon.rb +85 -23
  10. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon/au.rb +644 -0
  11. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon/softbank.rb +488 -0
  12. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon/z_combine.rb +16 -13
  13. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_mailer.rb +22 -0
  14. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mail.rb +265 -0
  15. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/abstract_mobile.rb +35 -0
  16. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/au.rb +28 -2
  17. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/docomo.rb +24 -1
  18. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/emobile.rb +1 -1
  19. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/softbank.rb +34 -1
  20. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/vodafone.rb +1 -1
  21. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/willcom.rb +1 -1
  22. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/util.rb +214 -13
  23. metadata +11 -3
@@ -4,30 +4,33 @@ end
4
4
 
5
5
  module Jpmobile
6
6
  module Emoticon
7
+ GETA = 0x3013
8
+
7
9
  SJIS_TO_UNICODE = {}
8
10
  SJIS_TO_UNICODE.update(DOCOMO_SJIS_TO_UNICODE)
9
11
  SJIS_TO_UNICODE.update(AU_SJIS_TO_UNICODE)
10
12
  SJIS_TO_UNICODE.freeze
11
13
  UNICODE_TO_SJIS = SJIS_TO_UNICODE.invert.freeze
12
14
 
13
- if Object.const_defined?(:Encoding)
14
- sjis_regexp = Proc.new do |sjis|
15
- Regexp.compile(Regexp.escape([sjis].pack('n').force_encoding("Shift_JIS")))
16
- end
17
- else
18
- sjis_regexp = Proc.new do |sjis|
19
- Regexp.compile(Regexp.escape([sjis].pack('n'),"s"),nil,'s')
20
- end
21
- end
22
-
23
- SJIS_REGEXP = Regexp.union(*SJIS_TO_UNICODE.keys.map{|s| sjis_regexp[s]})
15
+ # for au email
16
+ SJIS_TO_EMAIL_JIS = {0x81ac => 0x222E}
17
+ SJIS_TO_EMAIL_JIS.update(AU_SJIS_TO_EMAIL_JIS)
18
+ SJIS_TO_EMAIL_JIS.freeze
19
+
20
+ SJIS_REGEXP = Regexp.union(*SJIS_TO_UNICODE.keys.map{|s| Jpmobile::Util.sjis_regexp(s)})
24
21
  SOFTBANK_WEBCODE_REGEXP = Regexp.union(*([/(?!)/n]+SOFTBANK_WEBCODE_TO_UNICODE.keys.map{|x| "\x1b\x24#{x}\x0f"}))
25
22
 
26
- DOCOMO_SJIS_REGEXP = Regexp.union(*DOCOMO_SJIS_TO_UNICODE.keys.map{|s| sjis_regexp[s]})
27
- AU_SJIS_REGEXP = Regexp.union(*AU_SJIS_TO_UNICODE.keys.map{|s| sjis_regexp[s]})
23
+ DOCOMO_SJIS_REGEXP = Regexp.union(*DOCOMO_SJIS_TO_UNICODE.keys.map{|s| Jpmobile::Util.sjis_regexp(s)})
24
+ AU_SJIS_REGEXP = Regexp.union(*AU_SJIS_TO_UNICODE.keys.map{|s| Jpmobile::Util.sjis_regexp(s)})
28
25
  SOFTBANK_UNICODE_REGEXP = Regexp.union(*SOFTBANK_UNICODE_TO_WEBCODE.keys.map{|x| [x].pack('U')}).freeze
29
26
 
30
27
  EMOTICON_UNICODES = UNICODE_TO_SJIS.keys|SOFTBANK_UNICODE_TO_WEBCODE.keys.map{|k|k+0x1000}
31
28
  UTF8_REGEXP = Regexp.union(*EMOTICON_UNICODES.map{|x| [x].pack('U')}).freeze
29
+
30
+ # for PC conversion "GETA"
31
+ CONVERSION_TABLE_TO_PC_EMAIL = Hash[*(CONVERSION_TABLE_TO_SOFTBANK.keys|CONVERSION_TABLE_TO_DOCOMO.keys|CONVERSION_TABLE_TO_AU.keys).map{|k| [k, GETA]}.flatten]
32
+
33
+ SOFTBANK_SJIS_REGEXP = Regexp.union(*SOFTBANK_SJIS_TO_UNICODE.keys.map{|s| Jpmobile::Util.sjis_regexp(s)}).freeze
34
+ AU_EMAILJIS_REGEXP = Regexp.union(*AU_EMAILJIS_TO_UNICODE.keys.map{|s| Jpmobile::Util.jis_regexp(s)})
32
35
  end
33
36
  end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'jpmobile/mail'
3
+
4
+ module Jpmobile
5
+ module Mailer
6
+ class Base < ActionMailer::Base
7
+ def mail(headers={}, &block)
8
+ m = super(headers, &block)
9
+
10
+ if m.to.size == 1
11
+ # for mobile
12
+ m.mobile = (Jpmobile::Email.detect(m.to.first) || Jpmobile::Mobile::AbstractMobile).new(nil, nil)
13
+ else
14
+ # for multi to addresses
15
+ m.mobile = Jpmobile::Mobile::AbstractMobile.new(nil, nil)
16
+ end
17
+
18
+ m
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,265 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'mail'
3
+
4
+ module Jpmobile
5
+ module Mail
6
+ module_function
7
+ end
8
+ end
9
+
10
+ module Mail
11
+ # encoding patch
12
+ if self.const_defined?(:Ruby19)
13
+ Ruby19.class_eval do
14
+ def self.b_value_decode(str)
15
+ match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
16
+ if match
17
+ encoding = match[1]
18
+ str = self.decode_base64(match[2])
19
+ str.force_encoding(fix_encoding(encoding))
20
+ end
21
+ # if str contains some emoticon, the following line raises Encoding error
22
+ str.encode("utf-8", :invalid => :replace, :replace => "") rescue Jpmobile::Util.ascii_8bit(str)
23
+ end
24
+ end
25
+ elsif self.const_defined?(:Ruby18)
26
+ Ruby18.class_eval do
27
+ def self.b_value_decode(str)
28
+ match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
29
+ if match
30
+ encoding = match[1]
31
+ str = Ruby18.decode_base64(match[2])
32
+ end
33
+ str
34
+ end
35
+ end
36
+ end
37
+
38
+ class Message
39
+ attr_accessor :mobile
40
+
41
+ def encoded_with_jpmobile
42
+ if @mobile
43
+ header['subject'].mobile = @mobile if header['subject']
44
+ self.charset = @mobile.mail_charset
45
+
46
+ ready_to_send!
47
+
48
+ self.body.mobile = @mobile
49
+ self.header['Content-Transfer-Encoding'] = '8bit'
50
+
51
+ if self.header['Content-Type']
52
+ pp self.body.charset = case self.header['Content-Type'].value
53
+ when /iso-2022-jp/i
54
+ "ISO-2022-JP"
55
+ when /shift_jis/i
56
+ "Shift_JIS"
57
+ else
58
+ "UTF-8"
59
+ end
60
+ unless self.header['Content-Type'] == self.body.charset
61
+ self.header['Content-Type'] = self.body.charset
62
+ end
63
+ pp self.header['Content-Type'].charset
64
+ pp self.header['Content-Type'].value
65
+ pp self.header['Content-Type']
66
+ pp self.body.charset
67
+ end
68
+
69
+ buffer = header.encoded
70
+ buffer << "\r\n"
71
+ buffer = @mobile.utf8_to_mail_encode(buffer)
72
+ buffer << body.encoded(content_transfer_encoding)
73
+ buffer
74
+ else
75
+ encoded_without_jpmobile
76
+ end
77
+ end
78
+
79
+ def parse_message_with_jpmobile
80
+ parse_message_without_jpmobile
81
+
82
+ if !multipart? and self.header['Content-Type']
83
+ self.body.charset = case self.header['Content-Type'].value
84
+ when /iso-2022-jp/i
85
+ "ISO-2022-JP"
86
+ when /shift_jis/i
87
+ "Shift_JIS"
88
+ else
89
+ "UTF-8"
90
+ end
91
+ end
92
+ self.header['subject'].mobile = @mobile if self.header['subject']
93
+ end
94
+
95
+ def init_with_string_with_jpmobile(string)
96
+ # Jpmobile::Mobile class from 'From: ' header
97
+ s = Jpmobile::Util.ascii_8bit(string)
98
+
99
+ mobile_class = nil
100
+ content_has_from = false
101
+ s.split(/\n|\r/).each do |line|
102
+ if line =~ /^From:/
103
+ content_has_from = true
104
+ break if mobile_class = Jpmobile::Email.detect_from_mail_header(line)
105
+ end
106
+ end
107
+
108
+ if content_has_from
109
+ @mobile = (mobile_class || Jpmobile::Mobile::AbstractMobile).new(nil, nil)
110
+
111
+ init_with_string_without_jpmobile(s)
112
+
113
+ self.body.mobile = @mobile
114
+ self.body.set_encoding_jpmobile
115
+ if self.body.multipart?
116
+ self.body.parts.each do |part|
117
+ part.body.mobile = @mobile
118
+ part.body.set_encoding_jpmobile
119
+ end
120
+ end
121
+ else
122
+ init_with_string_without_jpmobile(s)
123
+ end
124
+ end
125
+
126
+ def process_body_raw_with_jpmobile
127
+ process_body_raw_without_jpmobile
128
+
129
+ self.body.mobile = @mobile
130
+ end
131
+
132
+ alias_method :encoded_without_jpmobile, :encoded
133
+ alias_method :encoded, :encoded_with_jpmobile
134
+
135
+ alias_method :parse_message_without_jpmobile, :parse_message
136
+ alias_method :parse_message, :parse_message_with_jpmobile
137
+
138
+ alias_method :init_with_string_without_jpmobile, :init_with_string
139
+ alias_method :init_with_string, :init_with_string_with_jpmobile
140
+
141
+ alias_method :process_body_raw_without_jpmobile, :process_body_raw
142
+ alias_method :process_body_raw, :process_body_raw_with_jpmobile
143
+ end
144
+
145
+ class Part
146
+ def parse_message_with_jpmobile
147
+ parse_message_without_jpmobile
148
+
149
+ if !multipart? and self.header['Content-Type']
150
+ @charset = case self.header['Content-Type'].value
151
+ when /iso-2022-jp/i
152
+ "ISO-2022-JP"
153
+ when /shift_jis/i
154
+ "Shift_JIS"
155
+ else
156
+ "UTF-8"
157
+ end
158
+ self.body.charset = @charset
159
+ end
160
+ end
161
+
162
+ alias_method :parse_message_without_jpmobile, :parse_message
163
+ alias_method :parse_message, :parse_message_with_jpmobile
164
+ end
165
+
166
+ class Body
167
+ attr_accessor :mobile
168
+
169
+ # convert encoding
170
+ def encoded_with_jpmobile(transfer_encoding = '8bit')
171
+ if @mobile and !multipart?
172
+ if @mobile.to_mail_body_encoded?(raw_source)
173
+ raw_source
174
+ else
175
+ @mobile.to_mail_body(Jpmobile::Util.force_encode(raw_source, @charset, Jpmobile::Util::UTF8))
176
+ end
177
+ else
178
+ encoded_without_jpmobile(transfer_encoding)
179
+ end
180
+ end
181
+ def decoded_with_jpmobile
182
+ if @mobile and !multipart?
183
+ @raw_source = @mobile.to_mail_internal(@raw_source, nil) unless @jpmobile_decoded
184
+ @jpmobile_decoded = true
185
+ end
186
+
187
+ if @charset
188
+ @raw_source = Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8)
189
+ end
190
+
191
+ decoded_without_jpmobile
192
+ end
193
+
194
+ # fix charset
195
+ def set_charset_with_jpmobile
196
+ @charset ||= only_us_ascii? ? 'US-ASCII' : nil
197
+ end
198
+
199
+ # set encoding to @raw_source in init
200
+ def set_encoding_jpmobile
201
+ @raw_source = Jpmobile::Util.set_encoding(@raw_source, @charset)
202
+ end
203
+
204
+ def mobile=(m)
205
+ if self.multipart?
206
+ self.parts.each do |part|
207
+ part.body.mobile = @mobile
208
+ part.body.set_encoding_jpmobile
209
+ end
210
+ end
211
+
212
+ @mobile = m
213
+ self.set_encoding_jpmobile
214
+ end
215
+
216
+ alias_method :encoded_without_jpmobile, :encoded
217
+ alias_method :encoded, :encoded_with_jpmobile
218
+
219
+ alias_method :decoded_without_jpmobile, :decoded
220
+ alias_method :decoded, :decoded_with_jpmobile
221
+
222
+ alias_method :set_charset_without_jpmobile, :set_charset
223
+ alias_method :set_charset, :set_charset_with_jpmobile
224
+ end
225
+
226
+ class UnstructuredField
227
+ attr_accessor :mobile
228
+
229
+ def do_decode
230
+ result = value.blank? ? nil : Encodings.decode_encode(value, :decode)
231
+
232
+ result = @mobile.to_mail_internal(result, value) if @mobile
233
+
234
+ # result.encode!(value.encoding || "UTF-8") if RUBY_VERSION >= '1.9' && !result.blank?
235
+ result.blank? ? result : Jpmobile::Util.force_encode(result, nil, Jpmobile::Util::UTF8)
236
+ end
237
+ end
238
+
239
+ # for subject
240
+ class SubjectField < UnstructuredField
241
+ # FIXME: not folding subject -> folding
242
+ def encoded_with_jpmobile
243
+ if @mobile
244
+ if @mobile.to_mail_subject_encoded?(value)
245
+ "#{name}: #{value}\r\n"
246
+ else
247
+ # convert encoding
248
+ "#{name}: " + @mobile.to_mail_subject(value) + "\r\n"
249
+ end
250
+ else
251
+ encoded_without_jpmobile
252
+ end
253
+ end
254
+
255
+ # def decoded_with_jpmobile
256
+ # decoded_without_jpmobile
257
+ # end
258
+
259
+ alias_method :encoded_without_jpmobile, :encoded
260
+ alias_method :encoded, :encoded_with_jpmobile
261
+
262
+ # alias_method :decoded_without_jpmobile, :decoded
263
+ # alias_method :decoded, :decoded_with_jpmobile
264
+ end
265
+ end
@@ -65,6 +65,41 @@ module Jpmobile::Mobile
65
65
  def default_charset
66
66
  "UTF-8"
67
67
  end
68
+
69
+ # メール送信用
70
+ def to_mail_subject(str)
71
+ "=?#{mail_charset}?B?" + [to_mail_encoding(str)].pack('m').strip + "?="
72
+ end
73
+ def to_mail_body(str)
74
+ to_mail_encoding(str)
75
+ end
76
+ def mail_charset
77
+ "ISO-2022-JP"
78
+ end
79
+ def to_mail_encoding(str)
80
+ str = Jpmobile::Emoticon.unicodecr_to_external(str, Jpmobile::Emoticon::CONVERSION_TABLE_TO_PC_EMAIL, false)
81
+ Jpmobile::Util.encode(str, mail_charset)
82
+ end
83
+ def utf8_to_mail_encode(str)
84
+ case mail_charset
85
+ when /ISO-2022-JP/i
86
+ Jpmobile::Util.utf8_to_jis(str)
87
+ when /Shift_JIS/i
88
+ Jpmobile::Util.utf8_to_sjis(str)
89
+ else
90
+ str
91
+ end
92
+ end
93
+ def to_mail_internal(str, val)
94
+ str
95
+ end
96
+ def to_mail_subject_encoded?(str)
97
+ str.match(/\=\?#{mail_charset}\?B.+\?\=/i)
98
+ end
99
+ def to_mail_body_encoded?(str)
100
+ Jpmobile::Util.jis?(str)
101
+ end
102
+
68
103
  # リクエストがこのクラスに属するか調べる
69
104
  # メソッド名に関して非常に不安
70
105
  def self.check_carrier(env)
@@ -9,7 +9,7 @@ module Jpmobile::Mobile
9
9
  # User-Agent文字列中に "UP.Browser" を含むVodafoneの端末があるので注意が必要
10
10
  USER_AGENT_REGEXP = /^(?:KDDI|UP.Browser\/.+?)-(.+?) /
11
11
  # 対応するメールアドレスの正規表現
12
- MAIL_ADDRESS_REGEXP = /^.+@ezweb\.ne\.jp$/
12
+ MAIL_ADDRESS_REGEXP = /.+@ezweb\.ne\.jp/
13
13
  # 簡易位置情報取得に対応していないデバイスID
14
14
  # http://www.au.kddi.com/ezfactory/tec/spec/eznavi.html
15
15
  LOCATION_UNSUPPORTED_DEVICE_ID = ["PT21", "TS25", "KCTE", "TST9", "KCU1", "SYT5", "KCTD", "TST8", "TST7", "KCTC", "SYT4", "KCTB", "KCTA", "TST6", "KCT9", "TST5", "TST4", "KCT8", "SYT3", "KCT7", "MIT1", "MAT3", "KCT6", "TST3", "KCT5", "KCT4", "SYT2", "MAT1", "MAT2", "TST2", "KCT3", "KCT2", "KCT1", "TST1", "SYT1"]
@@ -86,7 +86,6 @@ module Jpmobile::Mobile
86
86
  str = Jpmobile::Util.sjis_to_utf8(str)
87
87
  # 数値参照を UTF-8 に変換
88
88
  Jpmobile::Emoticon::unicodecr_to_utf8(str)
89
- # 半角->全角変換
90
89
  end
91
90
  def to_external(str, content_type, charset)
92
91
  # UTF-8を数値参照に
@@ -104,5 +103,32 @@ module Jpmobile::Mobile
104
103
  def default_charset
105
104
  "Shift_JIS"
106
105
  end
106
+
107
+ # メール送信用
108
+ def to_mail_subject(str)
109
+ "=?#{mail_charset}?B?" + [to_mail_encoding(str)].pack('m').strip + "?="
110
+ end
111
+ def to_mail_body(str)
112
+ to_mail_encoding(str)
113
+ end
114
+ def mail_charset
115
+ "ISO-2022-JP"
116
+ end
117
+
118
+ def to_mail_internal(str, val)
119
+ if Jpmobile::Util.jis?(str) or Jpmobile::Util.ascii_8bit?(str)
120
+ # 絵文字を数値参照に変換
121
+ str = Jpmobile::Emoticon.external_to_unicodecr_au_mail(Jpmobile::Util.jis(str))
122
+ str = Jpmobile::Util.jis_to_utf8(Jpmobile::Util.jis(str))
123
+ end
124
+ str
125
+ end
126
+
127
+ private
128
+ def to_mail_encoding(str)
129
+ str = Jpmobile::Emoticon.utf8_to_unicodecr(str)
130
+ str = Jpmobile::Util.utf8_to_jis(str)
131
+ Jpmobile::Util.jis(Jpmobile::Emoticon.unicodecr_to_au_email(str))
132
+ end
107
133
  end
108
134
  end
@@ -7,7 +7,7 @@ module Jpmobile::Mobile
7
7
  # 対応するUser-Agentの正規表現
8
8
  USER_AGENT_REGEXP = /^DoCoMo/
9
9
  # 対応するメールアドレスの正規表現
10
- MAIL_ADDRESS_REGEXP = /^.+@docomo\.ne\.jp$/
10
+ MAIL_ADDRESS_REGEXP = /.+@docomo\.ne\.jp/
11
11
 
12
12
  # オープンiエリアがあればエリアコードを +String+ で返す。無ければ +nil+ を返す。
13
13
  def areacode
@@ -94,6 +94,29 @@ module Jpmobile::Mobile
94
94
  "Shift_JIS"
95
95
  end
96
96
 
97
+ # メール送信用
98
+ def to_mail_subject(str)
99
+ str = to_external(str, nil, nil).first
100
+ "=?#{mail_charset}?B?" + [str].pack('m').strip + "?="
101
+ end
102
+ def to_mail_body(str)
103
+ to_external(str, nil, nil).first
104
+ end
105
+ def mail_charset
106
+ "Shift_JIS"
107
+ end
108
+ def to_mail_internal(str, val)
109
+ if Jpmobile::Util.shift_jis?(str) or Jpmobile::Util.ascii_8bit?(str)
110
+ # 絵文字を数値参照に変換
111
+ str = Jpmobile::Emoticon.external_to_unicodecr_docomo(Jpmobile::Util.sjis(str))
112
+ end
113
+
114
+ str
115
+ end
116
+ def to_mail_body_encoded?(str)
117
+ Jpmobile::Util.shift_jis?(str)
118
+ end
119
+
97
120
  # i-mode ブラウザのバージョンを返す。
98
121
  # http://labs.unoh.net/2009/07/i_20.html
99
122
  def imode_browser_version