rfetion 0.3.14 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile CHANGED
@@ -21,14 +21,9 @@ gem install rfetion
21
21
 
22
22
  h2. Usage
23
23
 
24
- send sms to yourself
24
+ send sms to friends or yourself
25
25
  <pre><code>
26
- Fetion.send_sms_to_self(mobile_no, password, content)
27
- </code></pre>
28
-
29
- send sms to friends
30
- <pre><code>
31
- Fetion.send_sms_to_friends(mobile_no, password, friends_mobile_or_fetion_number, content)
26
+ Fetion.send_sms(mobile_no, password, mobile_or_fetion_numbers, content)
32
27
  </code></pre>
33
28
 
34
29
  add friend with mobile
@@ -41,6 +36,13 @@ add friend with sip
41
36
  Fetion.add_buddy_with_sip(mobile_no, password, friend_sip)
42
37
  </code></pre>
43
38
 
39
+ send schedule sms to friends
40
+ <pre><code>
41
+ Fetion.schedule_sms(mobile_no, password, mobile_or_fetion_number, content, time)
42
+ </code></pre>
43
+
44
+ Time format is %Y-%m-%d %H:%M:%S
45
+
44
46
  **************************************************************************
45
47
 
46
48
  h2. Shell command
@@ -50,20 +52,22 @@ you can use it in shell command directly
50
52
  <pre><code>
51
53
  Usage: rfetion [options]
52
54
 
53
- Example: rfetion -m mobile -p password -f friends_mobile_or_fetion_number -c sms_content
54
-
55
+ Example: rfetion -m mobile -p password -c sms_content
56
+ rfetion -m mobile -p password -r mobile_or_fetion_numbers -c sms_content
57
+ rfetion -m mobile -p password -r mobile_or_fetion_numbers -c sms_content -t time
55
58
  rfetion -m mobile -p password --add-buddy-with-mobile friend_mobile
56
-
57
59
  rfetion -m mobile -p password --add-buddy-with-sip friend_sip
58
60
 
59
61
  -m, --mobile MOBILE Fetion mobile number
60
62
  -p, --password PASSWORD Fetion password
61
63
  -c, --content CONTENT Fetion message content
62
- -f, --friends MOBILE,FETION (optional) Fetion friends mobile number or fetion number, if no friends mobile number and fetion number, send message to yourself
64
+ -r, --receivers MOBILE,SIP (optional) Receivers' Fetion mobile numbers or fetion sip numbers, if no recievers, send sms to yourself
65
+ -t, --time TIME Schedule time to send sms, format is "2009-12-10 20:00:00"
63
66
  --add-buddy-with-mobile MOBILE
64
67
  Add friend mobile as fetion friend
65
68
  --add-buddy-with-sip SIP Add friend fetion sip as fetion friend
66
69
 
70
+
67
71
  different mode:
68
72
  --debug debug mode
69
73
  --silence silence mode
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.14
1
+ 0.4.0
data/lib/rfetion.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'guid'
3
+ require 'time'
3
4
  require 'net/http'
4
5
  require 'net/https'
5
6
  require 'rexml/document'
@@ -8,34 +8,40 @@ OptionParser.new do |opts|
8
8
 
9
9
  opts.separator ""
10
10
  opts.separator <<-EOF
11
- Example: rfetion -m mobile -p password -f friends_mobile_or_fetion_number -c sms_content
11
+ Example: rfetion -m mobile -p password -c sms_content
12
+ rfetion -m mobile -p password -r mobile_or_fetion_numbers -c sms_content
13
+ rfetion -m mobile -p password -r mobile_or_fetion_numbers -c sms_content -t time
12
14
  rfetion -m mobile -p password --add-buddy-with-mobile friend_mobile
13
15
  rfetion -m mobile -p password --add-buddy-with-sip friend_sip
16
+
14
17
  EOF
15
18
 
16
19
  opts.on('-m', '--mobile MOBILE', 'Fetion mobile number') do |mobile|
17
20
  options[:mobile_no] = mobile
18
21
  end
19
22
 
20
- opts.on('-p', '--password PASSWORD', 'Fetion password') do |f|
21
- options[:password] = f
23
+ opts.on('-p', '--password PASSWORD', 'Fetion password') do |password|
24
+ options[:password] = password
22
25
  end
23
26
 
24
- opts.on('-c', '--content CONTENT', 'Fetion message content') do |f|
25
- options[:content] = f
27
+ opts.on('-c', '--content CONTENT', 'Fetion message content') do |content|
28
+ options[:content] = content
26
29
  end
27
30
 
28
- options[:friends] = []
29
- opts.on('-f', '--friends MOBILE,FETION', Array, '(optional) Fetion friends mobile number or fetion number, if no friends mobile number and fetion number, send message to yourself') do |f|
30
- options[:friends] = f
31
+ opts.on('-r', '--receivers MOBILE,SIP', Array, "(optional) Receivers' Fetion mobile numbers or fetion sip numbers, if no recievers, send sms to yourself") do |receivers|
32
+ options[:receivers] = receivers
31
33
  end
32
34
 
33
- opts.on('--add-buddy-with-mobile MOBILE', 'Add friend mobile as fetion friend') do |f|
34
- options[:add_mobile] = f
35
+ opts.on('-t', '--time TIME', 'Schedule time to send sms, format is "2009-12-10 20:00:00"') do |time|
36
+ options[:time] = time
35
37
  end
36
38
 
37
- opts.on('--add-buddy-with-sip SIP', 'Add friend fetion sip as fetion friend') do |f|
38
- options[:add_sip] = f
39
+ opts.on('--add-buddy-with-mobile MOBILE', 'Add friend mobile as fetion friend') do |mobile|
40
+ options[:add_mobile] = mobile
41
+ end
42
+
43
+ opts.on('--add-buddy-with-sip SIP', 'Add friend fetion sip as fetion friend') do |sip|
44
+ options[:add_sip] = sip
39
45
  end
40
46
 
41
47
  opts.separator ""
@@ -80,10 +86,10 @@ begin
80
86
  end
81
87
 
82
88
  raise FetionException.new('You must input your mobile number, password and content') unless options[:mobile_no] and options[:password] and options[:content]
83
- if options[:friends].empty?
84
- Fetion.send_sms_to_self(options[:mobile_no], options[:password], options[:content], level(options))
89
+ if options[:time]
90
+ Fetion.schedule_sms(options[:mobile_no], options[:password], options[:receivers], options[:content], options[:time], level(options))
85
91
  else
86
- Fetion.send_sms_to_friends(options[:mobile_no], options[:password], options[:friends], options[:content], level(options))
92
+ Fetion.send_sms(options[:mobile_no], options[:password], options[:receivers], options[:content], level(options))
87
93
  end
88
94
  rescue FetionException => e
89
95
  puts e.message
@@ -30,20 +30,31 @@ class Fetion
30
30
  @cat = cat
31
31
  end
32
32
 
33
- def Fetion.send_sms_to_self(mobile_no, password, content, level = Logger::INFO)
33
+ def Fetion.send_sms(mobile_no, password, receivers, content, level = Logger::INFO)
34
34
  fetion = Fetion.new
35
35
  fetion.logger_level = level
36
36
  fetion.mobile_no = mobile_no
37
37
  fetion.password = password
38
38
  fetion.login
39
39
  fetion.register
40
- fetion.send_sms(fetion.uri, content)
40
+ if receivers
41
+ receivers = Array(receivers)
42
+ receivers.collect! {|receiver| receiver.to_s}
43
+ fetion.get_buddy_list
44
+ fetion.get_contacts_info
45
+ fetion.contacts.each do |contact|
46
+ if receivers.include? contact.mobile_no.to_s or receivers.any? { |receiver| contact.uri.index(receiver) }
47
+ fetion.send_sms(contact.uri, content)
48
+ end
49
+ end
50
+ fetion.send_sms(fetion.uri, content) if receivers.any? { |receiver| fetion.self? receiver }
51
+ else
52
+ fetion.send_sms(fetion.uri, content)
53
+ end
41
54
  fetion.logout
42
55
  end
43
56
 
44
- def Fetion.send_sms_to_friends(mobile_no, password, friends, content, level = Logger::INFO)
45
- friends = Array(friends)
46
- friends.collect! {|friend| friend.to_s}
57
+ def Fetion.schedule_sms(mobile_no, password, receivers, content, time, level = Logger::INFO)
47
58
  fetion = Fetion.new
48
59
  fetion.logger_level = level
49
60
  fetion.mobile_no = mobile_no
@@ -52,10 +63,18 @@ class Fetion
52
63
  fetion.register
53
64
  fetion.get_buddy_list
54
65
  fetion.get_contacts_info
55
- fetion.contacts.each do |contact|
56
- if friends.include? contact.mobile_no.to_s or friends.any? { |friend| contact.uri.index(friend) }
57
- fetion.send_sms(contact.uri, content)
58
- end
66
+ if receivers
67
+ receivers = Array(receivers)
68
+ receivers.collect! {|receiver| receiver.to_s}
69
+ new_receivers = fetion.contacts.collect do |contact|
70
+ if receivers.include? contact.mobile_no.to_s or receivers.any? { |receiver| contact.uri.index(receiver) }
71
+ contact.uri
72
+ end
73
+ end.compact!
74
+ new_receivers << fetion.uri if receivers.any? { |receiver| fetion.self? receiver }
75
+ fetion.schedule_sms(new_receivers, content, time)
76
+ else
77
+ fetion.schedule_sms([fetion.uri], content, time)
59
78
  end
60
79
  fetion.logout
61
80
  end
@@ -122,7 +141,7 @@ class Fetion
122
141
  def register
123
142
  @logger.info "fetion http register"
124
143
  call = next_call
125
- arg = '<args><device type="PC" version="44" client-version="3.2.0540" /><caps value="simple-im;im-session;temp-group;personal-group" /><events value="contact;permission;system-message;personal-group" /><user-info attributes="all" /><presence><basic value="400" desc="" /></presence></args>'
144
+ arg = '<args><device type="PC" version="284488270" client-version="3.2.0540" /><caps value="simple-im;im-session;temp-group;personal-group;im-relay;xeno-im;direct-sms;sms2fetion" /><events value="contact;permission;system-message;personal-group;compact" /><user-info attributes="all" /><presence><basic value="400" desc="" /></presence></args>'
126
145
 
127
146
  register_first(call, arg)
128
147
 
@@ -172,14 +191,15 @@ class Fetion
172
191
 
173
192
  def get_buddy_list
174
193
  @logger.info "fetion get buddy list"
175
- arg = '<args><contacts><buddy-lists /><buddies attributes="all" /><mobile-buddies attributes="all" /><chat-friends /><blacklist /></contacts></args>'
194
+ arg = '<args><contacts><buddy-lists /><buddies attributes="all" /><mobile-buddies attributes="all" /><chat-friends /><blacklist /><allow-list /></contacts></args>'
176
195
  msg = sip_create('S fetion.com.cn SIP-C/2.0', {'F' => @sid, 'I' => next_call, 'Q' => '1 S', 'N' => 'GetContactList'}, arg) + FETION_SIPP
177
196
  curl_exec(next_url, @ssic, msg)
178
197
  response = curl_exec(next_url, @ssic, FETION_SIPP)
179
198
  raise FetionException.new("Fetion Error: Get buddy list error") unless response.is_a? Net::HTTPSuccess
180
199
 
181
- response.body.scan(/uri="([^"]+)"/).each do |buddy|
182
- @buddies << {:uri => buddy[0]}
200
+ doc = REXML::Document.new(response.body.chomp(FETION_SIPP))
201
+ doc.elements.each("results/contacts/allow-list/contact") do |contact|
202
+ @buddies << {:uri => contact.attributes["uri"]}
183
203
  end
184
204
  @logger.debug "buddies: #{@buddies.inspect}"
185
205
  @logger.info "fetion get buddy list success"
@@ -187,7 +207,7 @@ class Fetion
187
207
 
188
208
  def get_contacts_info
189
209
  @logger.info "fetion get contacts info"
190
- arg = '<args><contacts attributes="all">'
210
+ arg = '<args><contacts attributes="provisioning;impresa;mobile-no;nickname;name;gender;portrait-crc;ivr-enabled" extended-attributes="score-level">'
191
211
  @buddies.each do |buddy|
192
212
  arg += "<contact uri=\"#{buddy[:uri]}\" />"
193
213
  end
@@ -195,28 +215,49 @@ class Fetion
195
215
 
196
216
  msg = sip_create('S fetion.com.cn SIP-C/2.0', {'F' => @sid, 'I' => next_call, 'Q' => '1 S', 'N' => 'GetContactsInfo'}, arg) + FETION_SIPP
197
217
  curl_exec(next_url, @ssic, msg)
198
- response = curl_exec(next_url, @ssic, FETION_SIPP)
199
- raise FetionException.new("Fetion Error: Get contacts info error") unless response.is_a? Net::HTTPSuccess
218
+ while true do
219
+ sleep 1
220
+ response = curl_exec(next_url, @ssic, FETION_SIPP)
221
+ raise FetionException.new("Fetion Error: Get contacts info error") unless response.is_a? Net::HTTPSuccess
222
+ break if response.body.size > FETION_SIPP.size
223
+ end
200
224
 
201
- response.body.scan(%r{<events>.*?</events>}).each do |events|
202
- doc = REXML::Document.new(events)
203
- doc.elements.each("events/event/results/contacts/contact") do |contact|
204
- attrs = contact.children.size == 0 ? {} : contact.children.first.attributes
205
- @contacts << Contact.new(contact.attributes["uri"], attrs)
206
- end
225
+ doc = REXML::Document.new(response.body.chomp(FETION_SIPP))
226
+ doc.elements.each("results/contacts/contact") do |contact|
227
+ attrs = contact.children.size == 0 ? {} : contact.children.first.attributes
228
+ @contacts << Contact.new(contact.attributes["uri"], attrs)
207
229
  end
208
230
  @logger.debug @contacts.inspect
209
231
  @logger.info "fetion get contacts info success"
210
232
  end
211
233
 
212
- def send_sms(to, content)
213
- @logger.info "fetion #{send_command} to #{to}"
214
- msg = sip_create('M fetion.com.cn SIP-C/2.0', {'F' => @sid, 'I' => next_call, 'Q' => '1 M', 'T' => to, 'N' => send_command}, content) + FETION_SIPP
234
+ def send_sms(receiver, content)
235
+ @logger.info "fetion #{send_command} to #{receiver}"
236
+ msg = sip_create('M fetion.com.cn SIP-C/2.0', {'F' => @sid, 'I' => next_call, 'Q' => '1 M', 'T' => receiver, 'N' => send_command}, content) + FETION_SIPP
215
237
  curl_exec(next_url, @ssic, msg)
216
238
  response = curl_exec(next_url, @ssic, FETION_SIPP)
217
239
 
218
240
  raise FetionException.new("Fetion Error: Send sms error") unless response.is_a? Net::HTTPSuccess
219
- @logger.info "fetion #{send_command} to #{to} success"
241
+ @logger.info "fetion #{send_command} to #{receiver} success"
242
+ end
243
+
244
+ def schedule_sms(receivers, content, time)
245
+ receivers = Array(receivers)
246
+ time = time.is_a?(Time) ? time : Time.parse(time)
247
+ now = Time.now
248
+ one_year = Time.local(now.year + 1, now.month, now.day, now.hour, now.min, now.sec)
249
+ raise FetionException.new("Can't schedule send sms to more than 32 receivers") if receivers.size > 32
250
+ raise FetionException.new("Schedule time must between #{(now + 600).strftime('%Y-%m-%d %H:%M:%S')} and #{one_year.strftime('%Y-%m-%d %H:%M:%S')}") if time < (now + 600) or time > one_year
251
+ @logger.info "fetion schedule send sms to #{receivers.join(', ')}"
252
+
253
+ receivers_str = receivers.collect { |receiver| %Q[<receiver uri="#{receiver}" />] }.join('')
254
+ arg = %Q{<args><schedule-sms send-time="#{time.getutc.strftime('%Y-%m-%d %H:%M:%S')}"><message>#{content}</message><receivers>#{receivers_str}</receivers></schedule-sms></args>}
255
+ msg = sip_create('S fetion.com.cn SIP-C/2.0', {'F' => @sid, 'I' => next_call, 'Q' => '1 S', 'N' => 'SSSetScheduleCatSms'}, arg) + FETION_SIPP
256
+ curl_exec(next_url, @ssic, msg)
257
+ response = curl_exec(next_url, @ssic, FETION_SIPP)
258
+
259
+ raise FetionException.new("Fetion Error: Schedule sms error") unless response.is_a? Net::HTTPSuccess
260
+ @logger.info "fetion schedule send sms to #{receivers.join(', ')} success"
220
261
  end
221
262
 
222
263
  def add_buddy_with_mobile(mobile, nickname = nil)
@@ -307,5 +348,9 @@ class Fetion
307
348
  def send_command
308
349
  @cat ? 'SendCatSMS' : 'SendSMS'
309
350
  end
351
+
352
+ def self?(mobile_or_sid)
353
+ mobile_or_sid == @mobile_no or mobile_or_sid == @sid
354
+ end
310
355
  end
311
356
 
data/rfetion.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rfetion}
8
- s.version = "0.3.14"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
12
- s.date = %q{2009-12-08}
12
+ s.date = %q{2009-12-10}
13
13
  s.description = %q{rfetion is a ruby gem for China Mobile fetion service that you can send SMS free.}
14
14
  s.email = %q{flyerhzm@gmail.com}
15
15
  s.executables = ["rfetion", "rfetion"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rfetion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.14
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-08 00:00:00 +08:00
12
+ date: 2009-12-10 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency