Permalink
Browse files

Send message to user by creating a switchboard

  • Loading branch information...
1 parent 565259c commit 0e0b02fcd6043dde57ddd3df3c630011cc9b9841 Ary Borenszweig committed Nov 1, 2012
Showing with 31 additions and 7 deletions.
  1. +6 −2 lib/msn/messenger.rb
  2. +20 −4 lib/msn/notification_server.rb
  3. +5 −1 lib/msn/protocol.rb
View
@@ -45,11 +45,15 @@ def send_message(email, text)
end
def accept_message(message)
- @on_message_handler.call(message) if @on_message_handler
+ if @on_message_handler
+ Fiber.new { @on_message_handler.call(message) }.resume
+ end
end
def ready
- @on_ready_handler.call if @on_ready_handler
+ if @on_ready_handler
+ Fiber.new { @on_ready_handler.call }.resume
+ end
end
def self.debug
@@ -11,7 +11,20 @@ def initialize(messenger)
def send_message(email, text)
switchboard = @switchboards[email]
- switchboard.send_message text
+ if switchboard
+ switchboard.send_message text
+ else
+ Fiber.new do
+ response = xfr "SB"
+ switchboard = create_switchboard email, response[3]
+ switchboard.on_event 'JOI' do
+ switchboard.clear_event 'JOI'
+ switchboard.send_message text
+ end
+ switchboard.usr messenger.username, response[5]
+ switchboard.cal email
+ end.resume
+ end
end
def username
@@ -82,9 +95,7 @@ def login_with_challenge(challenge)
end
on_event('RNG') do |header|
- host, port = header[2].split(':')
- switchboard = EM.connect host, port, Msn::Switchboard, messenger
- @switchboards[header[5]] = switchboard
+ switchboard = create_switchboard header[5], header[2]
switchboard.ans username, header[4], header[1]
end
@@ -96,6 +107,11 @@ def login_with_challenge(challenge)
@display_name = CGI.unescape response[4]
end
+ def create_switchboard(email, host_and_port)
+ host, port = host_and_port.split(':')
+ @switchboards[email] = EM.connect host, port, Msn::Switchboard, messenger
+ end
+
def unbind
if @reconnect_host
reconnect @reconnect_host, @reconnect_port.to_i
View
@@ -20,7 +20,7 @@ def receive_line(line)
size = pieces.last.to_i
set_binary_mode size
- when 'CHG', 'QRY'
+ when 'QRY'
# ignore
else
if fiber = @command_fibers.delete(pieces[1].to_i)
@@ -75,6 +75,10 @@ def on_event(kind, &block)
@event_handlers[kind] = block
end
+ def clear_event(kind)
+ @event_handlers.delete kind
+ end
+
def method_missing(name, *args)
send_command name.upcase, *args
end

0 comments on commit 0e0b02f

Please sign in to comment.