Class: Jabot::Jabber

Inherits:
Object
  • Object
show all
Includes:
Jabber
Defined in:
lib/jabot/jabber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Jabber

Returns a new instance of Jabber.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jabot/jabber.rb', line 14

def initialize(options)
  @client_id = options[:client_id]
  @client_password = options[:client_password]

  @remote_clients = []
  @is_listen = false

  @client = Client::new JID::new(@client_id + '/bot')
  #@client.on_exception do
  #  puts 'Reconnect JabberClient';
  #  connect
  #end

  connect
end

Instance Attribute Details

#is_listenObject (readonly)

Returns the value of attribute is_listen.



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

def is_listen
  @is_listen
end

#remote_clientsObject (readonly)

Returns the value of attribute remote_clients.



11
12
13
# File 'lib/jabot/jabber.rb', line 11

def remote_clients
  @remote_clients
end

Instance Method Details

#add_remote_client(id) ⇒ Object



43
44
45
# File 'lib/jabot/jabber.rb', line 43

def add_remote_client(id)
  @remote_clients << id unless @remote_clients.include?(id)
end

#connectObject



30
31
32
33
34
35
36
# File 'lib/jabot/jabber.rb', line 30

def connect
  @client.connect
  @client.auth @client_password

  presence = Presence.new(:dnd, 'server running')
  @client.send presence
end

#disconnectObject



38
39
40
41
# File 'lib/jabot/jabber.rb', line 38

def disconnect
  @is_listen = false
  @client.close
end

#listenObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jabot/jabber.rb', line 58

def listen
  @is_listen = true

  @message_callback = @client.add_message_callback 0, @message_callback do |m|
    if m.type != :error
      sender_id = "#{m.from.node}@#{m.from.domain}"
      #puts' "Message received from "' + sender_id# + ": " + m.body
      unless m.body.nil?
        if @remote_clients.include?(sender_id)
          yield(m.body, sender_id) if block_given?
        else
          #puts 'access denied'
        end
      end
    end
  end
end

#send(to, text) ⇒ Object



53
54
55
56
# File 'lib/jabot/jabber.rb', line 53

def send(to, text)
  message = Message::new to, text
  @client.send message
end

#send_to_all(text) ⇒ Object



47
48
49
50
51
# File 'lib/jabot/jabber.rb', line 47

def send_to_all(text)
  @remote_clients.each do |to|
    send to, text
  end
end