Class: IrcBot
- Inherits:
-
Object
- Object
- IrcBot
- Defined in:
- lib/notify/irc_bot.rb
Instance Method Summary collapse
- #add_in_queue(msg) ⇒ Object
-
#initialize ⇒ IrcBot
constructor
A new instance of IrcBot.
- #quit ⇒ Object
- #run ⇒ Object
- #say(msg) ⇒ Object
- #say_to_chan(msg) ⇒ Object
- #say_to_user(user, msg) ⇒ Object
- #stop ⇒ Object
- #try_join(nm) ⇒ Object
Constructor Details
#initialize ⇒ IrcBot
Returns a new instance of IrcBot.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/notify/irc_bot.rb', line 8 def initialize() if $config.irc_daemon && ($config.irc_server != "") @buff = MsgBuffer.new(1) @connected = 0 @channel = $config.irc_channel if !@channel or @channel == "" $log.error("IRC bot error: channel not set") return false end @server = $config.irc_server @port = isInteger($config.irc_port) ? $config.irc_port : 6667 @botname = "gnmsbot" $log.info("Starting IRC bot #{@server}:#{@port} on channel ##{@channel}") begin @socket = TCPSocket.open(@server, @port) rescue Exception => msg $log.error("IRC bot error: #{msg}") return false end try_join(@botname) run() end end |
Instance Method Details
#add_in_queue(msg) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/notify/irc_bot.rb', line 125 def add_in_queue(msg) $log.debug("irc message added in queue") if $config.irc_daemon @buff.put msg unless !@buff end end |
#quit ⇒ Object
145 146 147 148 |
# File 'lib/notify/irc_bot.rb', line 145 def quit say "PART ##{@channel} :Bye" say 'QUIT' end |
#run ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/notify/irc_bot.rb', line 54 def run @sender = Thread.start do while $config.irc_daemon if @buff.full? Thread.pass sleep(2) end $log.debug("end of IrcBot sender thread") end @receiver = Thread.start do until $config.irc_daemon and @socket.eof? do msg = @socket.gets if (msg) msg.chomp! $log.debug("IRC bot received: #{msg}") if msg.match(/^PING :(.*)$/) say "PONG #{$~[1]}" next end if msg.match(/:(.+)!~.+ PRIVMSG #{@botname} :(.*)$/) sender = $~[1] content = $~[2] if sender != "" if content and content.match(/^status$/) $host.each_value {|node| say_to_user(sender, "IP #{node.ip} status is #{$status[$status_value.index(node.status)]}") } else say_to_user(sender, "status - display stats regarding known hosts") end end next end if msg.match(/PRIVMSG ##{@channel} :(.*)$/) content = $~[1] if content.match(/^beer$/) say_to_chan "Gimme a free beeeeeeeer" end next end if msg.match(/\s433\s/) if @connected == 0 #nickname may be already in use @botname += "-#{rand(2**256).to_s(36)[0..4]}" $log.info("Irc bot renamed to #{@botname}") try_join(@botname) @connected = 1 else $log.fatal("Irc bot can't connect #{msg}") stop() end next end if msg.match(/:End of \/MOTD/) #as there was a MOTD the join may have not succeed say "JOIN ##{@channel}" @connected = 1 next end end Thread.pass end $log.debug("end of IrcBot receiver thread") end end |
#say(msg) ⇒ Object
39 40 41 42 |
# File 'lib/notify/irc_bot.rb', line 39 def say(msg) $log.debug("IRC bot sent: #{msg}") @socket.puts msg end |
#say_to_chan(msg) ⇒ Object
44 45 46 47 |
# File 'lib/notify/irc_bot.rb', line 44 def say_to_chan(msg) $log.debug("IRC bot sent: #{msg}") say "PRIVMSG ##{@channel} :#{msg}" end |
#say_to_user(user, msg) ⇒ Object
49 50 51 52 |
# File 'lib/notify/irc_bot.rb', line 49 def say_to_user(user, msg) $log.debug("IRC bot sent to #{user}: #{msg}") say "PRIVMSG #{user} :#{msg}" end |
#stop ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/notify/irc_bot.rb', line 132 def stop() if defined?(@sender) && (@sender != nil) $log.info("Stopping IRC bot") quit() sleep(1) Thread.kill(@sender) Thread.kill(@receiver) if @receiver != nil @socket.close @connected = 0 $log.debug("end of IrcBot threads") end end |
#try_join(nm) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/notify/irc_bot.rb', line 32 def try_join(nm) say "NICK #{nm}" say "USER #{nm} 0 * #{nm}" #try to join say "JOIN ##{@channel}" end |