-# Copyright (C) 2002 Tom Gilbert.
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to
-# deal in the Software without restriction, including without limitation the
-# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-# sell copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies of the Software and its documentation and acknowledgment shall be
-# given in the documentation and software packages that this Software was
-# used.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
require 'thread'
require 'etc'
require 'fileutils'
+$debug = false unless $debug
+# print +message+ if debugging is enabled
+def debug(message=nil)
+ print "DEBUG: #{message}\n" if($debug && message)
+ #yield
+end
+
# these first
require 'rbot/rbotconfig'
require 'rbot/config'
module Irc
-# Main bot class, which receives messages, handles them or passes them to
-# plugins, and stores runtime data
+# Main bot class, which manages the various components, receives messages,
+# handles them or passes them to plugins, and contains core functionality.
class IrcBot
# the bot's current nickname
attr_reader :nick
# channel info for channels the bot is in
attr_reader :channels
+ # bot's irc socket
+ attr_reader :socket
+
# bot's object registry, plugins get an interface to this for persistant
# storage (hash interface tied to a bdb file, plugins use Accessors to store
# and restore objects in their own namespaces.)
@argv = params[:argv]
- unless FileTest.directory? Config::DATADIR
- puts "data directory '#{Config::DATADIR}' not found, did you install.rb?"
+ unless FileTest.directory? Config::datadir
+ puts "data directory '#{Config::datadir}' not found, did you install.rb?"
exit 2
end
- botclass = "/home/#{Etc.getlogin}/.rbot" unless botclass
+ botclass = "#{Etc.getpwnam(Etc.getlogin).dir}/.rbot" unless botclass
@botclass = botclass.gsub(/\/$/, "")
unless FileTest.directory? botclass
puts "Error: file #{botclass} exists but isn't a directory"
exit 2
end
- FileUtils.cp_r Config::DATADIR+'/templates', botclass
+ FileUtils.cp_r Config::datadir+'/templates', botclass
end
Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
@startup_time = Time.new
@config = BotConfig.new(self)
+# TODO background self after botconfig has a chance to run wizard
@timer = Timer::Timer.new(1.0) # only need per-second granularity
@registry = BotRegistry.new self
@timer.add(@config['core.save_every']) { save } if @config['core.save_every']
@socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
@nick = @config['irc.nick']
- if @config['core.address_prefix']
- @addressing_prefixes = @config['core.address_prefix'].split(" ")
- else
- @addressing_prefixes = Array.new
- end
@client = IrcClient.new
- @client["PRIVMSG"] = proc { |data|
- message = PrivMessage.new(self, data["SOURCE"], data["TARGET"], data["MESSAGE"])
+ @client[:privmsg] = proc { |data|
+ message = PrivMessage.new(self, data[:source], data[:target], data[:message])
onprivmsg(message)
}
- @client["NOTICE"] = proc { |data|
- message = NoticeMessage.new(self, data["SOURCE"], data["TARGET"], data["MESSAGE"])
+ @client[:notice] = proc { |data|
+ message = NoticeMessage.new(self, data[:source], data[:target], data[:message])
# pass it off to plugins that want to hear everything
@plugins.delegate "listen", message
}
- @client["MOTD"] = proc { |data|
- data['MOTD'].each_line { |line|
+ @client[:motd] = proc { |data|
+ data[:motd].each_line { |line|
log "MOTD: #{line}", "server"
}
}
- @client["NICKTAKEN"] = proc { |data|
- nickchg "#{@nick}_"
+ @client[:nicktaken] = proc { |data|
+ nickchg "#{data[:nick]}_"
}
- @client["BADNICK"] = proc {|data|
- puts "WARNING, bad nick (#{data['NICK']})"
+ @client[:badnick] = proc {|data|
+ puts "WARNING, bad nick (#{data[:nick]})"
}
- @client["PING"] = proc {|data|
+ @client[:ping] = proc {|data|
# (jump the queue for pongs)
- @socket.puts "PONG #{data['PINGID']}"
+ @socket.puts "PONG #{data[:pingid]}"
}
- @client["NICK"] = proc {|data|
- sourcenick = data["SOURCENICK"]
- nick = data["NICK"]
- m = NickMessage.new(self, data["SOURCE"], data["SOURCENICK"], data["NICK"])
+ @client[:nick] = proc {|data|
+ sourcenick = data[:sourcenick]
+ nick = data[:nick]
+ m = NickMessage.new(self, data[:source], data[:sourcenick], data[:nick])
if(sourcenick == @nick)
+ debug "my nick is now #{nick}"
@nick = nick
end
@channels.each {|k,v|
@plugins.delegate("listen", m)
@plugins.delegate("nick", m)
}
- @client["QUIT"] = proc {|data|
- source = data["SOURCE"]
- sourcenick = data["SOURCENICK"]
- sourceurl = data["SOURCEADDRESS"]
- message = data["MESSAGE"]
- m = QuitMessage.new(self, data["SOURCE"], data["SOURCENICK"], data["MESSAGE"])
- if(data["SOURCENICK"] =~ /#{@nick}/i)
+ @client[:quit] = proc {|data|
+ source = data[:source]
+ sourcenick = data[:sourcenick]
+ sourceurl = data[:sourceaddress]
+ message = data[:message]
+ m = QuitMessage.new(self, data[:source], data[:sourcenick], data[:message])
+ if(data[:sourcenick] =~ /#{@nick}/i)
else
@channels.each {|k,v|
if(v.users.has_key?(sourcenick))
@plugins.delegate("listen", m)
@plugins.delegate("quit", m)
}
- @client["MODE"] = proc {|data|
- source = data["SOURCE"]
- sourcenick = data["SOURCENICK"]
- sourceurl = data["SOURCEADDRESS"]
- channel = data["CHANNEL"]
- targets = data["TARGETS"]
- modestring = data["MODESTRING"]
+ @client[:mode] = proc {|data|
+ source = data[:source]
+ sourcenick = data[:sourcenick]
+ sourceurl = data[:sourceaddress]
+ channel = data[:channel]
+ targets = data[:targets]
+ modestring = data[:modestring]
log "@ Mode #{modestring} #{targets} by #{sourcenick}", channel
}
- @client["WELCOME"] = proc {|data|
- log "joined server #{data['SOURCE']} as #{data['NICK']}", "server"
- debug "I think my nick is #{@nick}, server thinks #{data['NICK']}"
- if data['NICK'] && data['NICK'].length > 0
- @nick = data['NICK']
- end
- if(@config['irc.quser'])
- # TODO move this to a plugin
- debug "authing with Q using #{@config['quakenet.user']} #{@config['quakenet.auth']}"
- @socket.puts "PRIVMSG Q@CServe.quakenet.org :auth #{@config['quakenet.user']} #{@config['quakenet.auth']}"
+ @client[:welcome] = proc {|data|
+ log "joined server #{data[:source]} as #{data[:nick]}", "server"
+ debug "I think my nick is #{@nick}, server thinks #{data[:nick]}"
+ if data[:nick] && data[:nick].length > 0
+ @nick = data[:nick]
end
+ @plugins.delegate("connect")
+
@config['irc.join_channels'].each {|c|
debug "autojoining channel #{c}"
if(c =~ /^(\S+)\s+(\S+)$/i)
end
}
}
- @client["JOIN"] = proc {|data|
- m = JoinMessage.new(self, data["SOURCE"], data["CHANNEL"], data["MESSAGE"])
+ @client[:join] = proc {|data|
+ m = JoinMessage.new(self, data[:source], data[:channel], data[:message])
onjoin(m)
}
- @client["PART"] = proc {|data|
- m = PartMessage.new(self, data["SOURCE"], data["CHANNEL"], data["MESSAGE"])
+ @client[:part] = proc {|data|
+ m = PartMessage.new(self, data[:source], data[:channel], data[:message])
onpart(m)
}
- @client["KICK"] = proc {|data|
- m = KickMessage.new(self, data["SOURCE"], data["TARGET"],data["CHANNEL"],data["MESSAGE"])
+ @client[:kick] = proc {|data|
+ m = KickMessage.new(self, data[:source], data[:target],data[:channel],data[:message])
onkick(m)
}
- @client["INVITE"] = proc {|data|
- if(data["TARGET"] =~ /^#{@nick}$/i)
- join data["CHANNEL"] if (@auth.allow?("join", data["SOURCE"], data["SOURCENICK"]))
+ @client[:invite] = proc {|data|
+ if(data[:target] =~ /^#{@nick}$/i)
+ join data[:channel] if (@auth.allow?("join", data[:source], data[:sourcenick]))
end
}
- @client["CHANGETOPIC"] = proc {|data|
- channel = data["CHANNEL"]
- sourcenick = data["SOURCENICK"]
- topic = data["TOPIC"]
- timestamp = data["UNIXTIME"] || Time.now.to_i
+ @client[:changetopic] = proc {|data|
+ channel = data[:channel]
+ sourcenick = data[:sourcenick]
+ topic = data[:topic]
+ timestamp = data[:unixtime] || Time.now.to_i
if(sourcenick == @nick)
log "@ I set topic \"#{topic}\"", channel
else
log "@ #{sourcenick} set topic \"#{topic}\"", channel
end
- m = TopicMessage.new(self, data["SOURCE"], data["CHANNEL"], timestamp, data["TOPIC"])
+ m = TopicMessage.new(self, data[:source], data[:channel], timestamp, data[:topic])
ontopic(m)
@plugins.delegate("listen", m)
@plugins.delegate("topic", m)
}
- @client["TOPIC"] = @client["TOPICINFO"] = proc {|data|
- channel = data["CHANNEL"]
- m = TopicMessage.new(self, data["SOURCE"], data["CHANNEL"], data["UNIXTIME"], data["TOPIC"])
+ @client[:topic] = @client[:topicinfo] = proc {|data|
+ channel = data[:channel]
+ m = TopicMessage.new(self, data[:source], data[:channel], data[:unixtime], data[:topic])
ontopic(m)
}
- @client["NAMES"] = proc {|data|
- channel = data["CHANNEL"]
- users = data["USERS"]
+ @client[:names] = proc {|data|
+ channel = data[:channel]
+ users = data[:users]
unless(@channels[channel])
puts "bug: got names for channel '#{channel}' I didn't think I was in\n"
exit 2
@channels[channel].users[u[0].sub(/^[@&~+]/, '')] = ["mode", u[1]]
}
}
- @client["UNKNOWN"] = proc {|data|
- debug "UNKNOWN: #{data['SERVERSTRING']}"
+ @client[:unknown] = proc {|data|
+ #debug "UNKNOWN: #{data[:serverstring]}"
+ log data[:serverstring], ":unknown"
}
end
end while(message.length > 0)
end
+ # queue an arbitraty message for the server
def sendq(message="")
# temporary
@socket.queue(message)
sendq "TOPIC #{where} :#{topic}"
end
+ # disconnect from the server and cleanup all plugins and modules
def shutdown(message = nil)
trap("SIGTERM", "DEFAULT")
trap("SIGHUP", "DEFAULT")
end
# totally shutdown and respawn the bot
- def restart
- shutdown("restarting, back in #{@config['server.reconnect_wait']}...")
+ def restart(message = false)
+ msg = message ? message : "restarting, back in #{@config['server.reconnect_wait']}..."
+ shutdown(msg)
sleep @config['server.reconnect_wait']
# now we re-exec
exec($0, *@argv)
return helpstr
end
+ # returns a string describing the current status of the bot (uptime etc)
def status
secs_up = Time.new - @startup_time
uptime = Utils.secs_to_string secs_up
part $1 if(@auth.allow?("join", m.source, m.replyto))
when (/^quit(?:\s+(.*))?$/i)
quit $1 if(@auth.allow?("quit", m.source, m.replyto))
- when (/^restart$/i)
- restart if(@auth.allow?("quit", m.source, m.replyto))
+ when (/^restart(?:\s+(.*))?$/i)
+ restart $1 if(@auth.allow?("quit", m.source, m.replyto))
when (/^hide$/i)
join 0 if(@auth.allow?("join", m.source, m.replyto))
when (/^save$/i)
else
# stuff to handle when not addressed
case m.message
- when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$))[\s,-.]+#{@nick}$/i)
+ when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{@nick}$/i)
say m.replyto, @lang.get("hello_X") % m.sourcenick
when (/^#{@nick}!*$/)
say m.replyto, @lang.get("hello_X") % m.sourcenick