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)
-
- (Object) password
readonly
Returns the value of attribute password.
-
- (Object) username
readonly
Returns the value of attribute username.
Class Method Summary (collapse)
-
+ (Object) log_info(message)
:nodoc:.
-
+ (Object) logger
:nodoc:.
-
+ (Object) logger=(logger)
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)
-
- (Object) accept_message(message)
:nodoc:.
-
- (Object) accept_message_ack(id, status)
:nodoc:.
-
- (Object) add_to_allowed_list(email)
Adds a contact to your allowed list.
-
- (Object) add_to_friends_list(email)
Adds a contact to your friends list.
-
- (Object) call_handler(handler, *args)
:nodoc:.
-
- (Object) close
Closes the connection to the MSN server.
-
- (Object) connect
Connects to the MSN server.
-
- (Object) contact_request(email, display_name)
:nodoc:.
-
- (Object) disconnected
:nodoc:.
-
- (Object) get_contacts
Returns all contacts associated to this Messenger account.
-
- (Messenger) initialize(username, password)
constructor
Create an MSN connection with a username (email) and password.
-
- (Object) login_failed(message)
:nodoc:.
-
- (Object) on_contact_request(&handler)
Invoked when there is a contact request.
-
- (Object) on_disconnect(&handler)
Invoked when this Messenger gets disconnected from the server.
-
- (Object) on_login_failed(&handler)
Invoked when the username/password are incorrect.
-
- (Object) on_message(&handler)
Invoked when somebody sends a messages to this account, with an Msn::Message.
-
- (Object) on_message_ack(&handler)
Invoked after a message is sent by this Messenger, to know what happened with it.
-
- (Object) on_ready(&handler)
Invoked when this Messenger gets connected to the server.
-
- (Object) ready
:nodoc:.
-
- (Object) remove_from_allowed_list(email)
Removes a contact from your allowed list.
-
- (Object) remove_from_friends_list(email)
Removes a contact from your friends list.
-
- (Object) send_contact_command(email, command, list)
:nodoc:.
-
- (Object) send_message(email, text)
Sends a message to the given email with the given text.
-
- (Object) set_online_status(status)
Sets your online status.
Constructor Details
- (Messenger) initialize(username, password)
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
- (Object) password (readonly)
Returns the value of attribute password
4 5 6 |
# File 'lib/msn/messenger.rb', line 4 def password @password end |
- (Object) username (readonly)
Returns the value of attribute username
3 4 5 |
# File 'lib/msn/messenger.rb', line 3 def username @username end |
Class Method Details
+ (Object) log_info(message)
:nodoc:
199 200 201 202 203 |
# File 'lib/msn/messenger.rb', line 199 def self.log_info() return unless logger logger.info end |
+ (Object) logger
:nodoc:
194 195 196 |
# File 'lib/msn/messenger.rb', line 194 def self.logger @logger end |
+ (Object) logger=(logger)
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
- (Object) accept_message(message)
:nodoc:
151 152 153 |
# File 'lib/msn/messenger.rb', line 151 def () call_handler @on_message_handler, end |
- (Object) accept_message_ack(id, status)
:nodoc:
156 157 158 |
# File 'lib/msn/messenger.rb', line 156 def (id, status) call_handler @on_message_ack_handler, id, status end |
- (Object) add_to_allowed_list(email)
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 |
- (Object) add_to_friends_list(email)
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 |
- (Object) call_handler(handler, *args)
: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 |
- (Object) close
Closes the connection to the MSN server.
140 141 142 |
# File 'lib/msn/messenger.rb', line 140 def close @notification_server.close_connection end |
- (Object) connect
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 |
- (Object) contact_request(email, display_name)
: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 |
- (Object) disconnected
:nodoc:
176 177 178 |
# File 'lib/msn/messenger.rb', line 176 def disconnected call_handler @on_disconnect end |
- (Object) get_contacts
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 |
- (Object) login_failed(message)
:nodoc:
171 172 173 |
# File 'lib/msn/messenger.rb', line 171 def login_failed() call_handler @on_login_failed, end |
- (Object) on_contact_request(&handler)
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 |
- (Object) on_disconnect(&handler)
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 |
- (Object) on_login_failed(&handler)
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 |
- (Object) on_message(&handler)
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 |
- (Object) on_message_ack(&handler)
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 |
- (Object) on_ready(&handler)
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 |
- (Object) ready
:nodoc:
166 167 168 |
# File 'lib/msn/messenger.rb', line 166 def ready call_handler @on_ready_handler end |
- (Object) remove_from_allowed_list(email)
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 |
- (Object) remove_from_friends_list(email)
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 |
- (Object) send_contact_command(email, command, list)
: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 |
- (Object) send_message(email, text)
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 |
- (Object) set_online_status(status)
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 |