Created
March 23, 2010 13:00
-
-
Save stuffmc/341137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class XMPPD | |
def initialize(username, password) | |
begin | |
@mainthread = Thread.current | |
login(username, password) | |
listen_for_presence_notifications | |
send_initial_presence | |
rescue Exception => e | |
puts "*** ERROR (initialize): #{e}" | |
end | |
end | |
def login(username, password) | |
begin | |
@jid = Jabber::JID.new(username) | |
@client = Jabber::Client.new(@jid) | |
@client.connect | |
@client.auth(password) | |
rescue Exception => e | |
puts "*** ERROR (login): #{e}" | |
end | |
end | |
def send_initial_presence | |
@client.send(Jabber::Presence.new.set_status("XMPP4R at #{Time.now.utc}")) | |
end | |
def listen_for_presence_notifications | |
@client.add_presence_callback do |m| | |
case m.type | |
when nil # status: available | |
puts "PRESENCE: #{m.from} is online" | |
msg = Jabber::Message.new(m.from, "Welcome!") | |
msg.type = :chat | |
@client.send(msg) | |
when :unavailable | |
puts "PRESENCE: #{m.from} is offline" | |
end | |
end | |
end | |
end | |
DRb.start_service("druby://localhost:7777", XMPPD.new("[email protected]", "secret")) | |
DRb.thread.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment