em-msn 0.1 → 0.2

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/lib/msn/messenger.rb CHANGED
@@ -2,6 +2,17 @@ class Msn::Messenger
2
2
  attr_reader :username
3
3
  attr_reader :password
4
4
 
5
+ class << self
6
+ attr_accessor :logger
7
+
8
+ def log_info(message)
9
+ return unless logger
10
+
11
+ logger.info message
12
+ end
13
+ end
14
+
15
+
5
16
  def initialize(username, password)
6
17
  @username = username
7
18
  @password = password
@@ -52,11 +63,6 @@ class Msn::Messenger
52
63
  @notification_server.get_contacts
53
64
  end
54
65
 
55
- def send_contact_command(email, command, list)
56
- username, domain = email.split '@', 2
57
- @notification_server.send_payload_command_and_wait command, %Q(<ml><d n="#{domain}"><c n="#{username}" t="1" l="#{list}" /></d></ml>)
58
- end
59
-
60
66
  def on_ready(&handler)
61
67
  @on_ready_handler = handler
62
68
  end
@@ -73,6 +79,10 @@ class Msn::Messenger
73
79
  @on_message_handler = handler
74
80
  end
75
81
 
82
+ def on_message_ack(&handler)
83
+ @on_message_ack_handler = handler
84
+ end
85
+
76
86
  def on_contact_request(&handler)
77
87
  @on_contact_request = handler
78
88
  end
@@ -81,10 +91,23 @@ class Msn::Messenger
81
91
  @notification_server.send_message email, text
82
92
  end
83
93
 
94
+ def close
95
+ @notification_server.close_connection
96
+ end
97
+
98
+ def send_contact_command(email, command, list)
99
+ username, domain = email.split '@', 2
100
+ @notification_server.send_payload_command_and_wait command, %Q(<ml><d n="#{domain}"><c n="#{username}" t="1" l="#{list}" /></d></ml>)
101
+ end
102
+
84
103
  def accept_message(message)
85
104
  call_handler @on_message_handler, message
86
105
  end
87
106
 
107
+ def accept_message_ack(id, status)
108
+ call_handler @on_message_ack_handler, id, status
109
+ end
110
+
88
111
  def contact_request(email, display_name)
89
112
  call_handler @on_contact_request, email, display_name
90
113
  end
@@ -106,13 +129,5 @@ class Msn::Messenger
106
129
  Fiber.new { handler.call(*args) }.resume
107
130
  end
108
131
  end
109
-
110
- def self.debug
111
- @debug
112
- end
113
-
114
- def self.debug=(debug)
115
- @debug = debug
116
- end
117
132
  end
118
133
 
data/lib/msn/nexus.rb CHANGED
@@ -52,8 +52,8 @@ class Msn::Nexus
52
52
 
53
53
  # Check invalid login
54
54
  fault = xml.xpath("//S:Fault/faultstring")
55
- if fault
56
- raise Msn::AuthenticationError.new(fault.text)
55
+ if fault.length > 0
56
+ raise Msn::AuthenticationError.new(fault.first.text)
57
57
  end
58
58
 
59
59
  rstr = xml.xpath "//wst:RequestSecurityTokenResponse[wsp:AppliesTo/wsa:EndpointReference/wsa:Address='messengerclear.live.com']", Namespaces
@@ -11,6 +11,9 @@ class Msn::NotificationServer < EventMachine::Connection
11
11
  @guid = Guid.new.to_s
12
12
  @switchboards = {}
13
13
 
14
+ @message_id = 1
15
+ @message_ids = {}
16
+
14
17
  on_event 'ADL' do |header, data|
15
18
  data = Nokogiri::XML(data)
16
19
  domain = data.xpath('//ml/d').first['n']
@@ -26,21 +29,31 @@ class Msn::NotificationServer < EventMachine::Connection
26
29
  end
27
30
 
28
31
  def send_message(email, text)
32
+ message_id = @message_id
33
+ @message_id += 1
34
+
29
35
  switchboard = @switchboards[email]
30
36
  if switchboard
31
- switchboard.send_message text
37
+ trid = switchboard.send_message text
38
+ @message_ids[trid] = message_id
32
39
  else
33
40
  Fiber.new do
34
41
  response = xfr "SB"
35
42
  switchboard = create_switchboard email, response[3]
36
43
  switchboard.on_event 'JOI' do
37
44
  switchboard.clear_event 'JOI'
38
- switchboard.send_message text
45
+ trid = switchboard.send_message text
46
+ @message_ids[trid] = message_id
39
47
  end
40
48
  switchboard.usr username_guid, response[5]
41
- switchboard.cal email
49
+ cal_response = switchboard.cal email
50
+ if cal_response[0] == '217'
51
+ messenger.accept_message_ack message_id, :offline
52
+ end
42
53
  end.resume
43
54
  end
55
+
56
+ message_id
44
57
  end
45
58
 
46
59
  def username
@@ -137,6 +150,12 @@ class Msn::NotificationServer < EventMachine::Connection
137
150
  def create_switchboard(email, host_and_port)
138
151
  host, port = host_and_port.split(':')
139
152
  switchboard = EM.connect host, port, Msn::Switchboard, messenger
153
+ switchboard.on_event 'ACK' do |header|
154
+ notify_ack header[1].to_i, :ack
155
+ end
156
+ switchboard.on_event 'NAK' do |header|
157
+ notify_ack header[1].to_i, :nak
158
+ end
140
159
  switchboard.on_event 'BYE' do |header|
141
160
  destroy_switchboard email if header[1] =~ /#{email}/
142
161
  end
@@ -148,6 +167,11 @@ class Msn::NotificationServer < EventMachine::Connection
148
167
  switchboard.close_connection if switchboard
149
168
  end
150
169
 
170
+ def notify_ack(trid, status)
171
+ id = @message_ids.delete(trid)
172
+ messenger.accept_message_ack id, status if id
173
+ end
174
+
151
175
  def unbind
152
176
  if @reconnect_host
153
177
  reconnect @reconnect_host, @reconnect_port.to_i
data/lib/msn/protocol.rb CHANGED
@@ -7,7 +7,7 @@ module Msn::Protocol
7
7
  end
8
8
 
9
9
  def receive_line(line)
10
- puts "<< #{line}" if Msn::Messenger.debug
10
+ Msn::Messenger.log_info "<< #{line}"
11
11
 
12
12
  pieces = line.split(' ')
13
13
 
@@ -47,7 +47,7 @@ module Msn::Protocol
47
47
  end
48
48
 
49
49
  def receive_binary_data(data)
50
- puts "<<* #{data}" if Msn::Messenger.debug
50
+ Msn::Messenger.log_info "<<* #{data}"
51
51
 
52
52
  handle_event @header, data
53
53
  end
@@ -91,9 +91,10 @@ module Msn::Protocol
91
91
  end
92
92
 
93
93
  def send_command_internal(text)
94
- puts ">> #{text}" if Msn::Messenger.debug
94
+ Msn::Messenger.log_info ">> #{text}"
95
95
  send_data text
96
96
  @trid += 1
97
+ @trid - 1
97
98
  end
98
99
 
99
100
  def on_event(kind, &block)
@@ -17,7 +17,7 @@ class Msn::Switchboard < EventMachine::Connection
17
17
  end
18
18
 
19
19
  def send_message(text)
20
- send_payload_command "MSG", "N", "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nUser-Agent: pidgin/2.10.5devel\r\nX-MMS-IM-Format: FN=Segoe%20UI; EF=; CO=0; PF=0; RL=0\r\n\r\n#{text}"
20
+ send_payload_command "MSG", "A", "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nUser-Agent: pidgin/2.10.5devel\r\nX-MMS-IM-Format: FN=Segoe%20UI; EF=; CO=0; PF=0; RL=0\r\n\r\n#{text}"
21
21
  end
22
22
  end
23
23
 
data/lib/msn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Msn
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-msn
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: