Class: XmppBot

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

Instance Method Summary collapse

Constructor Details

#initializeXmppBot

Returns a new instance of XmppBot.



8
9
10
11
12
13
14
15
16
17
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/notify/xmpp_bot.rb', line 8

def initialize()
  #Jabber::debug = true
  if $config.im_daemon && ($config. != "")
    @buff=MsgBuffer.new(1)
    $log.info("Starting XMPP bot")
    begin
      timeout(10) {@messenger = Jabber::Simple.new($config., $config.im_password)}
    rescue Timeout::Error
      $log.error("XMPP bot error: can't connect to #{$config.}")
      return false
    rescue Exception=> msg
      $log.error("XMPP bot error: #{msg}")
      return false
    end
    @sender = Thread.start do
      while $config.im_daemon
        send_messages if @buff.full?
        sleep(2)
        Thread.pass
        @messenger.received_messages do |msg|
          $log.debug("Send from "+ msg.from.node+"@"+msg.from.domain + " \"#{msg.body}\"")
          case msg.body
          when /^help/i
            @messenger.deliver(msg.from, "status - display stats regarding host alived\nstatus ip - display the status of the given ip")
          when /^beer/i
            @messenger.deliver(msg.from, "Gimme a free beeeeeeeer")
          when /^status/i
            hup=0
            hdown=0
            begin
              $host.each_key {|ip|
                if $host[ip] && ($host[ip].status == NORMAL)
                  hup=hup+1
                else
                  hdown=hdown+1
                end
              }
            rescue Exception => msg
              $log.error(msg)
            end
            @messenger.deliver(msg.from, "Active Host" + ($host.size<2?" ":"s ") + "#{hup}/#{$host.size}")
          when /^info\s+/i
            ipasked=msg.body.gsub(/info\s+/,"")
            if isValidIPv4(ipasked)
              if $host[ipasked]
                @messenger.deliver(msg.from, "Ip node #{ipasked} status is #{$status[$status_value.index($host[ipasked].status)]}")
              elsif $network[ipasked]
                @messenger.deliver(msg.from, "Network node #{ipasked} status is #{$status[$status_value.index($network[ipasked].status)]}")
              else
                @messenger.deliver(msg.from, "Unkown ip address #{ipasked}")
              end
            else
              @messenger.deliver(msg.from, "#{ipasked} is not a valid ip address")
            end

          else
            @messenger.deliver(msg.from, "Sorry , I didn't understand that. Message me with 'help' for a list of commands")
          end
        end
      end
      $log.debug("end of XmppBot thread")
    end
  end
end

Instance Method Details

#add_in_queue(im_recipient, msg) ⇒ Object



73
74
75
76
77
78
# File 'lib/notify/xmpp_bot.rb', line 73

def add_in_queue(im_recipient, msg)
  $log.debug("xmpp add message in queue")
  if  $config.im_daemon
    @buff.put im_recipient+"|"+msg unless !@buff
  end
end

#stopObject



80
81
82
83
84
85
86
87
# File 'lib/notify/xmpp_bot.rb', line 80

def stop()
  if defined?(@sender) && (@sender != nil)
    $log.info("Stopping XMPP bot")
    @messenger.disconnect unless !@messenger
    Thread.kill(@sender)
    $log.debug("end of XmppBot thread")
  end
end