class Jabber::MUC::HipchatClient

Constants

CALLBACKS

Callbacks

Attributes

my_jid[R]

Public Class Methods

new(jid, conference_host = nil) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 6
def initialize jid, conference_host = nil
  @my_jid     = JID.new(jid)

  @presence   = HipChat::Presence.new(my_jid)
  @message    = HipChat::Message.new(my_jid)

  @conference_host = conference_host
end

Public Instance Methods

activate_callbacks() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 105
def activate_callbacks
  stream.add_stanza_callback(0, self) do |stanza|
    case stanza.name
    when 'message'
      handle_message(HipChat::ReceivedMessage.new(stanza))
    when 'presence'
      handle_presence(HipChat::ReceivedPresence.new(stanza, chat_host))
    end
  end
  Jabber::debuglog "Callbacks activated"
end
connect(password) click to toggle source

Connection

# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 78
def connect password
  stream.connect
  Jabber::debuglog "Connected to stream"
  stream.auth(password)
  Jabber::debuglog "Authenticated"
  true
end
deactivate_callbacks() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 117
def deactivate_callbacks
  stream.delete_stanza_callback(self)
  Jabber::debuglog "Callbacks deactivated"
end
exit(room_id, reason = nil) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 31
def exit room_id, reason = nil
  jid = JID.new(room_id, conference_host)
  Jabber::debuglog "Exiting #{jid}"
  @presence.get_leave(jid, reason).send_to(stream)
end
get_rooms() click to toggle source

Fetching

# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 64
def get_rooms
  HipChat::RoomData.get_rooms_data(stream, conference_host)
end
get_user_details(user_id) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 72
def get_user_details user_id
  HipChat::VCard.get_details(stream, user_id)
end
get_users() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 68
def get_users
  HipChat::UserData.get_users_data(stream)
end
invite(user_ids, room_id) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 50
def invite user_ids, room_id
  room_jid = JID.new(room_id, conference_host)
  user_jids = user_ids.map{ |id| JID.new(id, chat_host) }
  Jabber::debuglog "Inviting #{user_jids} to #{room_jid}"
  @message.get_invite(room_jid, user_jids).send_to(stream)
end
join(room_id, fetch_history = false) click to toggle source

Actions

# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 25
def join room_id, fetch_history = false
  jid = JID.new(room_id, conference_host)
  Jabber::debuglog "Joining #{jid}"
  @presence.get_join(jid, fetch_history).send_to(stream)
end
keep_alive(password) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 86
def keep_alive password
  if stream.is_disconnected?
    Jabber::debuglog "Stream disconnected. Connecting again..."
    connect(password)
  end
end
kick(user_ids, room_id) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 43
def kick user_ids, room_id
  room_jid = JID.new(room_id, conference_host)
  user_jids = user_ids.map{ |id| JID.new(id, chat_host) }
  Jabber::debuglog "Kicking #{user_jids} from #{room_jid}"
  HipChat::KickMessage.new(my_jid).make(room_jid, user_jids).send_to(stream)
end
name() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 15
def name
  my_jid.resource
end
name=(resource) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 19
def name= resource
  my_jid.resource = resource
end
send_message(type, recipient_id, text, subject = nil) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 57
def send_message type, recipient_id, text, subject = nil
  jid = JID.new(recipient_id, type == :chat ? chat_host : conference_host)
  @message.get_text(type, jid, text, subject).send_to(stream)
end
set_presence(status = nil, type = :available, room_id = nil) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 37
def set_presence status = nil, type = :available, room_id = nil
  room_jid = room_id ? JID.new(room_id, conference_host) : nil
  Jabber::debuglog "Setting presence to #{type} in #{room_jid} with #{status}"
  @presence.get_status(type, room_jid, status).send_to(stream)
end

Private Instance Methods

callbacks() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 141
def callbacks
  @callbacks ||= Hash.new { |hash, key| hash[key] = CallbackList.new }
end
chat_host() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 124
def chat_host
  my_jid.domain
end
conference_host() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 128
def conference_host
  @conference_host ||= begin
    MUCBrowser.new(stream).muc_rooms(chat_host).keys.first.domain
  end
rescue => e
  Jabber.logger.error("Conference host not found")
  nil
end
handle_error(message) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 208
def handle_error message
  callbacks[:error].process(
    message.room_id,
    message.user_id,
    message.body,
    message.topic,
  )
end
handle_group_message(message) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 193
def handle_group_message message
  callbacks[:room_message].process(
    message.room_id,
    message.sender_name,
    message.body,
  )
end
handle_invite(message) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 179
def handle_invite message
  callbacks[:room_invite].process(
    message.room_id,
    message.room_name,
  )
end
handle_message(message) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 162
def handle_message message
  case message.type
  when :chat
    handle_private_message(message)
  when :groupchat
    if message.topic?
      handle_room_topic(message)
    else
      handle_group_message(message)
    end
  when :error
    handle_error(message)
  else
    handle_invite(message)
  end
end
handle_presence(presence) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 145
def handle_presence presence
  if presence.lobby?
    callbacks[:lobby_presence].process(
      presence.sender_id,
      presence.type
    )
  else
    callbacks[:room_presence].process(
      presence.room_id,
      # presence.user_id,
      presence.sender_name,
      presence.type,
      presence.role,
    )
  end
end
handle_private_message(message) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 186
def handle_private_message message
  callbacks[:private_message].process(
    message.sender_id,
    message.body,
  )
end
handle_room_topic(message) click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 201
def handle_room_topic message
  callbacks[:room_topic].process(
    message.room_id,
    message.topic,
  )
end
stream() click to toggle source
# File lib/xmpp4r/muc/helper/hipchat_client.rb, line 137
def stream
  @stream ||= Client.new(my_jid.strip) # TODO: Error Handling
end