Class: SipcMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/rfetion/sipc_message.rb

Defined Under Namespace

Classes: Bad, ExtentionRequired, NotFound, OK, Response, Send, Unauthoried

Constant Summary collapse

RESPONSES =
{
  200 => SipcMessage::OK,
  280 => SipcMessage::Send,
  400 => SipcMessage::Bad,
  401 => SipcMessage::Unauthoried,
  404 => SipcMessage::NotFound,
  421 => SipcMessage::ExtentionRequired
}

Class Method Summary collapse

Class Method Details

.add_buddy(fetion, options) ⇒ Object



47
48
49
50
# File 'lib/rfetion/sipc_message.rb', line 47

def self.add_buddy(fetion, options)
  body = options[:friend_mobile] ? %Q|<args><contacts><buddies><buddy uri="tel:#{options[:friend_mobile]}" buddy-lists="" desc="#{fetion.nickname}" expose-mobile-no="1" expose-name="1" addbuddy-phrase-id="0" /></buddies></contacts></args>| : %Q|<args><contacts><buddies><buddy uri="sip:#{options[:friend_sip]}" buddy-lists="" desc="#{fetion.nickname}" addbuddy-phrase-id="0" /></buddies></contacts></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => '1 S', :N => 'AddBuddyV4', :body => body)
end

.close_session(fetion, receiver_uri) ⇒ Object



104
105
106
# File 'lib/rfetion/sipc_message.rb', line 104

def self.close_session(fetion, receiver_uri)
  sipc_create(:command => 'B', :F => fetion.sid, :I => fetion.next_call, :Q => '2 B', :T => "sip:#{receiver_uri}", :with_l => false)
end

.create_session(fetion, last_sipc_response) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rfetion/sipc_message.rb', line 61

def self.create_session(fetion, last_sipc_response)
  header = {}
  last_sipc_response.sections.each do |section|
    sipc_header = section.split("\r\n\r\n").first
    if sipc_header.split("\r\n").first =~ %r|I #{fetion.sid} SIP-C/4.0|
      sipc_header.split("\r\n")[1..-1].each do |line|
        key, value = line.split(': ', 2)
        if key == 'K'
          header['K'] ? header['K'] << value : header['K'] = [value]
        else
          header[key] = value
        end
      end
      body = %Q|v=0\r\no=-0 0 IN 127.0.0.1:8001\r\ns=session\r\nc=IN IP4 127.0.0.1:8001\r\nt=0 0\r\nm=message 8001 sip sip:#{fetion.uri}\r\n|
      return sipc_response_create(:I => header['I'], :Q => header['Q'], :F => header['F'], :K => header['K'], :body => body, :f_first => false)
    end
  end
end

.get_address_list(fetion) ⇒ Object



28
29
30
31
# File 'lib/rfetion/sipc_message.rb', line 28

def self.get_address_list(fetion)
  body = %Q|<args><contacts version="0" /></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => '1 S', :N => 'GetAddressListV4', :body => body)
end

.get_contact_info(fetion, uri) ⇒ Object



52
53
54
55
# File 'lib/rfetion/sipc_message.rb', line 52

def self.get_contact_info(fetion, uri)
  body = %Q|<args><contact uri="#{uri}" /></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => '1 S', :N => 'GetContactInfoV4', :body => body)
end

.get_group_list(fetion) ⇒ Object



13
14
15
16
# File 'lib/rfetion/sipc_message.rb', line 13

def self.get_group_list(fetion)
  body = %Q|<args><group-list attributes="name;identity" /></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => '1 S', :N => 'PGGetGroupList', :body => body)
end

.get_group_topic(fetion) ⇒ Object



23
24
25
26
# File 'lib/rfetion/sipc_message.rb', line 23

def self.get_group_topic(fetion)
  body = %Q|<args><topic-list /></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => '1 S', :N => 'PGGetGroupTopic', :body => body)
end

.handle_contact_request(fetion, contact, options) ⇒ Object



113
114
115
116
# File 'lib/rfetion/sipc_message.rb', line 113

def self.handle_contact_request(fetion, contact, options)
  body = %Q|<args><contacts><buddies><buddy user-id="#{contact.uid}" uri="#{contact.uri}" result="#{options[:result]}" buddy-lists="" expose-mobile-no="0" expose-name="0" /></buddies></contacts></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => "1 S", :N => "HandleContactRequestV4", :body => body)
end

.keep_alive(fetion) ⇒ Object



108
109
110
111
# File 'lib/rfetion/sipc_message.rb', line 108

def self.keep_alive(fetion)
  body = %Q|<args><credentials domains="fetion.com.cn;m161.com.cn;www.ikuwa.cn;games.fetion.com.cn" /></args>|
  sipc_create(:command => 'R', :F => fetion.sid, :I => 1, :Q => "#{fetion.next_alive} R", :N => 'KeepAlive', :body => body)
end

.logout(fetion) ⇒ Object



57
58
59
# File 'lib/rfetion/sipc_message.rb', line 57

def self.logout(fetion)
  sipc_create(:command => 'R', :F => fetion.sid, :I => 1, :Q => "#{fetion.next_alive} R", :X => 0, :with_l => false)
end

.msg_received(fetion, message_response) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/rfetion/sipc_message.rb', line 95

def self.msg_received(fetion, message_response)
  header = {}
  message_response.split("\r\n\r\n").first.split("\r\n")[1..-1].each do |line|
    key, value = line.split(': ', 2)
    header[key] = value
  end
  sipc_response_create(:F => header['F'], :I => header['I'], :Q => header['Q'], :with_l => false)
end

.presence(fetion) ⇒ Object



18
19
20
21
# File 'lib/rfetion/sipc_message.rb', line 18

def self.presence(fetion)
  body = %Q|<args><subscription self="v4default;mail-count" buddy="v4default" version="0" /></args>|
  sipc_create(:command => 'SUB', :F => fetion.sid, :I => fetion.next_call, :Q => '1 SUB', :N => 'PresenceV4', :body => body)
end

.register_first(fetion) ⇒ Object



4
5
6
# File 'lib/rfetion/sipc_message.rb', line 4

def self.register_first(fetion)
  sipc_create(:command => 'R', :F => fetion.sid, :I => 1, :Q => "#{fetion.next_alive} R", :CN => ::Guid.new.hexdigest.upcase, :CL => %Q|type="pc" ,version="#{Fetion::VERSION}"|, :with_l => false)
end

.register_second(fetion) ⇒ Object



8
9
10
11
# File 'lib/rfetion/sipc_message.rb', line 8

def self.register_second(fetion)
  body = %Q|<args><device machine-code="#{fetion.machine_code}" /><caps value="ff" /><events value="7f" /><user-info mobile-no="#{fetion.mobile_no}" user-id="#{fetion.uid}"><personal version="0" attributes="v4default" /><custom-config version="0" /><contact-list version="0"   buddy-attributes="v4default" /></user-info><credentials domains="fetion.com.cn;m161.com.cn;www.ikuwa.cn;games.fetion.com.cn" /><presence><basic value="400" desc="" /></presence></args>|
  sipc_create(:command => 'R', :F => fetion.sid, :I => 1, :Q => "#{fetion.next_alive} R", :A => %Q|Digest response="#{fetion.response}",algorithm="SHA1-sess-v4"|, :AK => 'ak-value', :body => body)
end

.send_cat_msg(fetion, receiver, content) ⇒ Object



37
38
39
# File 'lib/rfetion/sipc_message.rb', line 37

def self.send_cat_msg(fetion, receiver, content)
  sipc_create(:command => 'M', :F => fetion.sid, :I => fetion.next_call, :Q => '2 M', :T => receiver, :K => 'SaveHistory', :N => 'CatMsg', :body => content)
end

.send_cat_sms(fetion, receiver, content) ⇒ Object



33
34
35
# File 'lib/rfetion/sipc_message.rb', line 33

def self.send_cat_sms(fetion, receiver, content)
  sipc_create(:command => 'M', :F => fetion.sid, :I => fetion.next_call, :Q => '1 M', :T => receiver, :N => 'SendCatSMS', :body => content)
end

.session_connected(fetion, last_sipc_response) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rfetion/sipc_message.rb', line 80

def self.session_connected(fetion, last_sipc_response)
  header = {}
  last_sipc_response.sections.each do |section|
    sipc_header = section.split("\r\n\r\n").first
    if sipc_header.split("\r\n").first =~ %r|O #{fetion.sid} SIP-C/4.0|
      sipc_header.split("\r\n")[1..-1].each do |line|
        key, value = line.split(': ', 2)
        header[key] = value
      end
      header['K'] = ['text/html-fragment', 'text/plain']
      return sipc_response_create(:F => header['F'], :I => header['I'], :Q => header['Q'], :K => header['K'], :with_l => false)
    end
  end
end

.set_schedule_sms(fetion, receivers, content, time) ⇒ Object



41
42
43
44
45
# File 'lib/rfetion/sipc_message.rb', line 41

def self.set_schedule_sms(fetion, receivers, content, time)
  receivers_str = receivers.collect { |receiver| %Q[<receiver uri="#{receiver}" />] }.join('')
  body = %Q|<args><schedule-sms send-time="#{time}" type="0"><message>test</message><receivers>#{receivers_str}</receivers></schedule-sms></args>|
  sipc_create(:command => 'S', :F => fetion.sid, :I => fetion.next_call, :Q => '1 S', :N => 'SSSetScheduleCatSms', :SV => '1', :body => body)
end

.sipc_response(http_response_body, fetion) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rfetion/sipc_message.rb', line 118

def self.sipc_response(http_response_body, fetion)
  return if http_response_body == Fetion::SIPP
  
  sections = []
  body = http_response_body
  while true
    index = body.index(%r{(BN|M|I|O) #{fetion.sid} SIP-C/4.0}, 1)
    if index
      sections << body[0...index]
      body = body[index..-1]
    else
      sections << body
      break
    end
  end
  SipcMessage::Response.new(sections)
rescue NoMethodError
  raise FetionException.new("Fetion error: No response to #{sections.first.split("\r\n").first}")
end