Class: Msn::Messenger
- Inherits:
-
Object
- Object
- Msn::Messenger
- Defined in:
- lib/msn/messenger.rb
Overview
Main class to communicate with MSN.
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.log_info(message) ⇒ Object
:nodoc:.
-
.logger ⇒ Object
:nodoc:.
-
.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).
Instance Method Summary collapse
-
#accept_message(message) ⇒ Object
:nodoc:.
-
#accept_message_ack(id, status) ⇒ Object
:nodoc:.
-
#add_to_allowed_list(email) ⇒ Object
Adds a contact to your allowed list.
-
#add_to_friends_list(email) ⇒ Object
Adds a contact to your friends list.
-
#call_handler(handler, *args) ⇒ Object
:nodoc:.
-
#close ⇒ Object
Closes the connection to the MSN server.
-
#connect ⇒ Object
Connects to the MSN server.
-
#contact_request(email, display_name) ⇒ Object
:nodoc:.
-
#disconnected ⇒ Object
:nodoc:.
-
#get_contacts ⇒ Object
Returns all contacts associated to this Messenger account.
-
#initialize(username, password) ⇒ Messenger
constructor
Create an MSN connection with a username (email) and password.
-
#login_failed(message) ⇒ Object
:nodoc:.
-
#on_contact_request(&handler) ⇒ Object
Invoked when there is a contact request.
-
#on_disconnect(&handler) ⇒ Object
Invoked when this Messenger gets disconnected from the server.
-
#on_login_failed(&handler) ⇒ Object
Invoked when the username/password are incorrect.
-
#on_message(&handler) ⇒ Object
Invoked when somebody sends a messages to this account, with an Msn::Message.
-
#on_message_ack(&handler) ⇒ Object
Invoked after a message is sent by this Messenger, to know what happened with it.
-
#on_ready(&handler) ⇒ Object
Invoked when this Messenger gets connected to the server.
-
#ready ⇒ Object
:nodoc:.
-
#remove_from_allowed_list(email) ⇒ Object
Removes a contact from your allowed list.
-
#remove_from_friends_list(email) ⇒ Object
Removes a contact from your friends list.
-
#send_contact_command(email, command, list) ⇒ Object
:nodoc:.
-
#send_message(email, text) ⇒ Object
Sends a message to the given email with the given text.
-
#set_online_status(status) ⇒ Object
Sets your online status.
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
#password ⇒ Object (readonly)
Returns the value of attribute password.
4 5 6 |
# File 'lib/msn/messenger.rb', line 4 def password @password end |
#username ⇒ Object (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() return unless logger logger.info end |
.logger ⇒ Object
: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 () call_handler @on_message_handler, end |
#accept_message_ack(id, status) ⇒ Object
:nodoc:
156 157 158 |
# File 'lib/msn/messenger.rb', line 156 def (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 |
#close ⇒ Object
Closes the connection to the MSN server.
140 141 142 |
# File 'lib/msn/messenger.rb', line 140 def close @notification_server.close_connection end |
#connect ⇒ Object
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 |
#disconnected ⇒ Object
:nodoc:
176 177 178 |
# File 'lib/msn/messenger.rb', line 176 def disconnected call_handler @on_disconnect end |
#get_contacts ⇒ Object
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 login_failed() call_handler @on_login_failed, 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.on_login_failed do |reason|
puts "Login failed: #{reason} :-("
msn.close
end
89 90 91 |
# File 'lib/msn/messenger.rb', line 89 def on_login_failed(&handler) @on_login_failed = handler end |
#on_message(&handler) ⇒ Object
Invoked when somebody sends a messages to this account, with an Msn::Message.
msn. do |msg|
# msg is an Msn:Message instance
end
108 109 110 |
# File 'lib/msn/messenger.rb', line 108 def (&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. do |, 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 (&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 |
#ready ⇒ Object
: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 (email, text) @notification_server. 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 |