class Lita::Adapters::Xmpp::Connector

Attributes

client[R]
robot[R]
roster[R]

Public Class Methods

new(robot, jid, password, debug: false, connect_domain: nil) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 15
def initialize(robot, jid, password, debug: false, connect_domain: nil)
  @robot          = robot
  @jid            = jid
  @password       = password
  @client         = Jabber::Client.new(@jid)
  @connect_domain = connect_domain

  if debug
    Lita.logger.info("Enabling Jabber log.")
    Jabber.debug = true
  end
end

Public Instance Methods

connect() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 32
def connect
  client_connect
  load_roster
  register_message_callback
  send_presence
end
jid() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 28
def jid
  @jid.to_s
end
join_rooms(muc_domain, rooms) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 39
def join_rooms(muc_domain, rooms)
  rooms.each do |room_name|
    muc = Jabber::MUC::SimpleMUCClient.new(client)
    room_jid = normalized_jid(room_name, muc_domain, robot.name)
    mucs[room_jid.bare.to_s] = muc
    register_muc_message_callback(muc)
    Lita.logger.info("Joining room: #{room_jid}.")
    muc.join(room_jid)
  end
end
list_rooms(muc_domain) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 50
def list_rooms(muc_domain)
  Lita.logger.debug("Querying server for list of rooms.")
  browser = Jabber::MUC::MUCBrowser.new(client)
  browser.muc_rooms(muc_domain).map { |jid, name| jid.to_s }
end
message_jid(user_jid, strings) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 56
def message_jid(user_jid, strings)
  strings.each do |s|
    Lita.logger.debug("Sending message to JID #{user_jid}: #{s}")
    message = Jabber::Message.new(user_jid, s)
    message.type = :chat
    client.send(message)
  end
end
message_muc(room_jid, strings, raw = false) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 65
def message_muc(room_jid, strings, raw = false)
  if muc = mucs[room_jid]
    if raw
      Lita.logger.debug("Sending raw message to MUC #{room_jid}: #{strings}")
      muc.send(strings)
    else
      strings.each do |s|
        Lita.logger.debug("Sending message to MUC #{room_jid}: #{s}")
        muc.say(s)
      end
    end
  end
end
mucs() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 79
def mucs
  @mucs ||= {}
end
set_topic(room_jid, topic) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 83
def set_topic(room_jid, topic)
  muc = mucs[room_jid]
  if muc
    Lita.logger.debug("Setting topic for MUC #{room_jid}: #{topic}")
    muc.subject = topic
  end
end
shut_down() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 91
def shut_down
  Lita.logger.info("Disconnecting from XMPP.")
  client.close
end

Private Instance Methods

client_connect() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 103
def client_connect
  Lita.logger.info("Connecting to XMPP. #{@connect_domain}")
  client.connect(@connect_domain)
  Lita.logger.debug("Authenticating with XMPP.")
  client.auth(@password)
end
load_roster() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 118
def load_roster
  Lita.logger.debug("Loading roster.")
  @roster = Jabber::Roster::Helper.new(client, false)
  Callback.new(robot, roster).roster_update
  roster.get_roster
  roster.wait_for_roster
end
normalized_jid(jid, domain, resource) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 126
def normalized_jid(jid, domain, resource)
  jid = Jabber::JID.new(jid)
  jid.resource = resource
  unless jid.node
    jid.node = jid.domain
    jid.domain = domain
  end
  jid
end
register_message_callback() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 110
def register_message_callback
  Callback.new(robot, roster).private_message(client)
end
register_muc_message_callback(muc) click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 114
def register_muc_message_callback(muc)
  Callback.new(robot, roster).muc_message(muc)
end
send_presence() click to toggle source
# File lib/lita/adapters/xmpp/connector.rb, line 98
def send_presence
  Lita.logger.debug("Sending initial XMPP presence.")
  client.send(Jabber::Presence.new(:chat))
end