# -*- mode: ruby; -*- #=begin header #Command Executer # This agent responds to privmsg's from the operator that start with "!" and execute the commands. # # $Author: knu $ # $Date: 2001/01/31 10:55:28 $ # # Copyright (C) 1998-2000 Hiroshi IGARASHI #=end agent = IRC::PassiveAgent::new("CommandExecuter") class << agent include IRC def start(client) super @operator = User.parse(Operator) report("Hello, sir.") end def stop super end def notifyMessage(message) case message.command when CMD_PRIVMSG from = message.prefix to = message.params[0] user = User::parse(from) #putlog("debug", "recieved PRIVMSG from #{user}.") str = message.trailing if to == @client.nick if user.nick == @operator.nick operator(str) else report("#{user} commanded me \"#{str}\".") end end end end private def report(str) privmsg("!"+str, @operator.nick) end def operator(str) if str =~ /\A[\s ]*[!!](.*)[\s ]*\Z/ putlog("debug", $1) str = $1 case str when /\Aquit\Z/ doQuit when /\Areboot\Z/ doReboot when /\AlistAgent\Z/ doListAgent when /\ArestartAgent[\s ]+(\w+)\Z/ doRestartAgent($1) when /\Achannels\Z/ doMychannel when /\Ainvite[\s ]+(\S+)\Z/ doInvite($1) else report("Beg your pardon, sir?") end end end # 終了 def doQuit report("I am leaving, sir!") @client.stop end # 再起動(to be fixed) def doReboot unless fork exec($0) end doQuit end # List agents def doListAgent report("List of agents:") @client.agents.keys.sort.each do |name| agent = @client.agents[name] putlog("debug", agent.inspect) report(" #{agent.script_name.ljust(24)}: #{agent.nick}") end report("(End of list)") end # Restart an agent def doRestartAgent(name) new_agent = @client.restartAgent(name) unless new_agent.nil? report("Agent [#{new_agent.nick}] has been restarted.") end end # List channels it's joining def doMychannel channels = @client.join_channels unless channels.empty? report("I am joining the channels: ") report(" " + channels.join(",")) else report("I am not joining any channels.") end end def doInvite(channel) report("I'm coming to "+channel) @client.join(channel, []) privmsg("Hello, excuse me for interrupting you..", channel) putlog("join", channel) end end agent