Class: Zoe

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/zoe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZoe

Returns a new instance of Zoe.



14
15
16
# File 'lib/zoe.rb', line 14

def initialize
  @callbacks = []
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



12
13
14
# File 'lib/zoe.rb', line 12

def callbacks
  @callbacks
end

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/zoe.rb', line 12

def client
  @client
end

#roomsObject (readonly)

Returns the value of attribute rooms.



12
13
14
# File 'lib/zoe.rb', line 12

def rooms
  @rooms
end

#rosterObject (readonly)

Returns the value of attribute roster.



12
13
14
# File 'lib/zoe.rb', line 12

def roster
  @roster
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object



74
75
76
# File 'lib/zoe.rb', line 74

def self.method_missing(method, *args, &block)
  instance.__send__(method, *args, &block)
end

Instance Method Details

#connect(jid: nil, password: nil, rooms: []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zoe.rb', line 18

def connect(jid: nil, password: nil, rooms: [])
  @jid = jid
  @client = Jabber::Client.new(jid + '/bot')

  @client.connect
  @client.auth(password)
  @client.send(Jabber::Presence.new.set_type(:available))

  @roster = Jabber::Roster::Helper.new(@client)
  @roster.wait_for_roster

  @rooms = rooms.map do |room_name|
    room = Jabber::MUC::MUCClient.new(@client)
    room.join(room_name + '/' + self.jid.iname)
    room
  end

  @client.add_message_callback(200) do |m|
    if m.type == :chat # if PM
      m.reply_to = m.from
    elsif m.type == :groupchat && m.from.resource # if GM
      m.reply_to = m.from
      m.from = get_jid_by_name(m.from.resource)
    end

    if m.body
      @callbacks.each{ |c| c.call(m) }
    end
  end
end

#debug!Object



69
70
71
72
# File 'lib/zoe.rb', line 69

def debug!
  Jabber.logger = Logger.new(STDOUT)
  Jabber.debug = true
end

#get_jid_by_name(name) ⇒ Object



53
54
55
# File 'lib/zoe.rb', line 53

def get_jid_by_name(name)
  @roster.items.values.find{ |v| v.iname == name }
end

#jidObject



49
50
51
# File 'lib/zoe.rb', line 49

def jid
  @roster[@jid]
end

#on_message(&block) ⇒ Object



65
66
67
# File 'lib/zoe.rb', line 65

def on_message(&block)
  @callbacks << block
end

#send(to, message) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/zoe.rb', line 57

def send(to, message)
  to = to.is_a?(Jabber::JID) ? to : Jabber::JID.new(to.to_s)
  message = Jabber::Message.new(to, message)
  message.type = (to.domain == "conf.hipchat.com" ? :groupchat : :chat)

  @client.send(message)
end