Class: Msn::Messenger

Inherits:
Object
  • Object
show all
Defined in:
lib/msn/messenger.rb

Overview

Main class to communicate with MSN.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Messenger

Create an MSN connection with a username (email) and password.



7
8
9
10
# File 'lib/msn/messenger.rb', line 7

def initialize(username, password)
  @username = username
  @password = password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'lib/msn/messenger.rb', line 4

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



3
4
5
# File 'lib/msn/messenger.rb', line 3

def username
  @username
end

Class Method Details

.log_info(message) ⇒ Object

:nodoc:



199
200
201
202
203
# File 'lib/msn/messenger.rb', line 199

def self.log_info(message)
  return unless logger

  logger.info message
end

.loggerObject

:nodoc:



194
195
196
# File 'lib/msn/messenger.rb', line 194

def self.logger
  @logger
end

.logger=(logger) ⇒ Object

Sets a logger that will get logged as info all communication to the MSN server (but not all communication to the MSN nexus server).



189
190
191
# File 'lib/msn/messenger.rb', line 189

def self.logger=(logger)
  @logger = logger
end

Instance Method Details

#accept_message(message) ⇒ Object

:nodoc:



151
152
153
# File 'lib/msn/messenger.rb', line 151

def accept_message(message)
  call_handler @on_message_handler, message
end

#accept_message_ack(id, status) ⇒ Object

:nodoc:



156
157
158
# File 'lib/msn/messenger.rb', line 156

def accept_message_ack(id, status)
  call_handler @on_message_ack_handler, id, status
end

#add_to_allowed_list(email) ⇒ Object

Adds a contact to your allowed list.



59
60
61
# File 'lib/msn/messenger.rb', line 59

def add_to_allowed_list(email)
  send_contact_command email, 'ADL', '2'
end

#add_to_friends_list(email) ⇒ Object

Adds a contact to your friends list.



49
50
51
# File 'lib/msn/messenger.rb', line 49

def add_to_friends_list(email)
  send_contact_command email, 'ADL', '1'
end

#call_handler(handler, *args) ⇒ Object

:nodoc:



181
182
183
184
185
# File 'lib/msn/messenger.rb', line 181

def call_handler(handler, *args)
  if handler
    Fiber.new { handler.call(*args) }.resume
  end
end

#closeObject

Closes the connection to the MSN server.



140
141
142
# File 'lib/msn/messenger.rb', line 140

def close
  @notification_server.close_connection
end

#connectObject

Connects to the MSN server. Event handlers should be set before calling this method.



13
14
15
# File 'lib/msn/messenger.rb', line 13

def connect
  @notification_server = EM.connect 'messenger.hotmail.com', 1863, Msn::NotificationServer, self
end

#contact_request(email, display_name) ⇒ Object

:nodoc:



161
162
163
# File 'lib/msn/messenger.rb', line 161

def contact_request(email, display_name)
  call_handler @on_contact_request, email, display_name
end

#disconnectedObject

:nodoc:



176
177
178
# File 'lib/msn/messenger.rb', line 176

def disconnected
  call_handler @on_disconnect
end

#get_contactsObject

Returns all contacts associated to this Messenger account. This is an array of Msn::Contact.



70
71
72
# File 'lib/msn/messenger.rb', line 70

def get_contacts
  @notification_server.get_contacts
end

#login_failed(message) ⇒ Object

:nodoc:



171
172
173
# File 'lib/msn/messenger.rb', line 171

def (message)
  call_handler @on_login_failed, message
end

#on_contact_request(&handler) ⇒ Object

Invoked when there is a contact request.

msn.on_contact_request do |email, display_name|
  ...
end


128
129
130
# File 'lib/msn/messenger.rb', line 128

def on_contact_request(&handler)
  @on_contact_request = handler
end

#on_disconnect(&handler) ⇒ Object

Invoked when this Messenger gets disconnected from the server.

msn.on_disconnect do
  # Try to reconnect
  msn.connect
end


99
100
101
# File 'lib/msn/messenger.rb', line 99

def on_disconnect(&handler)
  @on_disconnect = handler
end

#on_login_failed(&handler) ⇒ Object

Invoked when the username/password are incorrect.

msn. do |reason|
  puts "Login failed: #{reason} :-("
  msn.close
end


89
90
91
# File 'lib/msn/messenger.rb', line 89

def (&handler)
  @on_login_failed = handler
end

#on_message(&handler) ⇒ Object

Invoked when somebody sends a messages to this account, with an Msn::Message.

msn.on_message do |msg|
  # msg is an Msn:Message instance
end


108
109
110
# File 'lib/msn/messenger.rb', line 108

def on_message(&handler)
  @on_message_handler = handler
end

#on_message_ack(&handler) ⇒ Object

Invoked after a message is sent by this Messenger, to know what happened with it.

msn.on_message_ack do |message_id, status|
  # status can be :ack, :nak or :offline
  # message_id is the one you got from send_message
end


119
120
121
# File 'lib/msn/messenger.rb', line 119

def on_message_ack(&handler)
  @on_message_ack_handler = handler
end

#on_ready(&handler) ⇒ Object

Invoked when this Messenger gets connected to the server.

msn.on_ready do
  msn.set_online_status :online
end


79
80
81
# File 'lib/msn/messenger.rb', line 79

def on_ready(&handler)
  @on_ready_handler = handler
end

#readyObject

:nodoc:



166
167
168
# File 'lib/msn/messenger.rb', line 166

def ready
  call_handler @on_ready_handler
end

#remove_from_allowed_list(email) ⇒ Object

Removes a contact from your allowed list.



64
65
66
# File 'lib/msn/messenger.rb', line 64

def remove_from_allowed_list(email)
  send_contact_command email, 'RML', '2'
end

#remove_from_friends_list(email) ⇒ Object

Removes a contact from your friends list.



54
55
56
# File 'lib/msn/messenger.rb', line 54

def remove_from_friends_list(email)
  send_contact_command email, 'RML', '1'
end

#send_contact_command(email, command, list) ⇒ Object

:nodoc:



145
146
147
148
# File 'lib/msn/messenger.rb', line 145

def send_contact_command(email, command, list)
  username, domain = email.split '@', 2
  @notification_server.send_payload_command_and_wait command, %Q(<ml><d n="#{domain}"><c n="#{username}" t="1" l="#{list}" /></d></ml>)
end

#send_message(email, text) ⇒ Object

Sends a message to the given email with the given text. Returns an ID (a number) that can be used to relate the send messages to ACKs.



135
136
137
# File 'lib/msn/messenger.rb', line 135

def send_message(email, text)
  @notification_server.send_message email, text
end

#set_online_status(status) ⇒ Object

Sets your online status. Status can be:

  • :available, :online

  • :busy

  • :idle

  • :brb, :be_right_back

  • :away

  • :phone, :on_the_phone

  • :lunch, :out_to_lunch

It is advisable to call this method as soon as you connect, otherwise you won’t be able to perform certain actions (like sending messages).



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/msn/messenger.rb', line 27

def set_online_status(status)
  case status
  when :available, :online
    @notification_server.chg "NLN", 0
  when :busy
    @notification_server.chg "BSY", 0
  when :idle
    @notification_server.chg "IDL", 0
  when :brb, :be_right_back
    @notification_server.chg "BRB", 0
  when :away
    @notification_server.chg "AWY", 0
  when :phone, :on_the_phone
    @notification_server.chg "PHN", 0
  when :lunch, :out_to_lunch
    @notification_server.chg "LUN", 0
  else
    raise "Wrong online status: #{status}"
  end
end