Class: JabberBot

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber_bot.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ JabberBot



6
7
8
9
10
11
12
13
# File 'lib/jabber_bot.rb', line 6

def initialize(config)
  @config = config
  @keep_alive_status = false
  @commands = {}
  @jabber = Client.new(JID::new(@config['JID'] + '/' + @config['resource']))
  #add_default_commands
  keep_alive
end

Instance Method Details

#add_command(command, syntax, description, public = false, &callback) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/jabber_bot.rb', line 45

def add_command(command, syntax, description, public = false, &callback)
  @commands[command.downcase] = {
      'syntax' => syntax,
      'description' => description,
      'callback' => callback,
      'public' => public
  }
end

#connectObject



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

def connect
  begin
    if not @jabber.is_connected?
      @keep_alive_status = false
      @jabber.connect(@config['host'])
      @jabber.auth(@config['password'])
      @jabber.send(Presence.new.set_type(:available))
    end
  rescue Exception => e
    puts "Error connecting: #{e} (#{e.class})!"
  ensure
    @keep_alive_status = true
  end
end

#disconnectObject



30
31
32
33
# File 'lib/jabber_bot.rb', line 30

def disconnect
  @keep_alive_status = false;
  @jabber.close
end

#listenObject



35
36
37
38
39
# File 'lib/jabber_bot.rb', line 35

def listen
  @jabber.add_message_callback do |message|
    handle_message(message) unless message.body.to_s == ''
  end
end

#run_command(command, params) ⇒ Object



54
55
56
# File 'lib/jabber_bot.rb', line 54

def run_command(command, params)
  return @commands[command]['callback'].call(params)
end

#say(to, message) ⇒ Object



41
42
43
# File 'lib/jabber_bot.rb', line 41

def say(to, message)
  @jabber.send(Message.new(to, message).set_type(:chat))
end