![](http://www.gnu-darwin.org/hexley-gd-sm.png)
|
# File rbot/ircbot.rb, line 69
def initialize(botclass)
@botclass = botclass.gsub(/\/$/, "")
Dir.mkdir("#{botclass}") if(!File.exist?("#{botclass}"))
Dir.mkdir("#{botclass}/logs") if(!File.exist?("#{botclass}/logs"))
@config = Irc::BotConfig.new(self)
@timer = Timer::Timer.new
@registry = BotRegistry.new self
@timer.add(@config["SAVE_EVERY"].to_i) { save }
@channels = Hash.new
@logs = Hash.new
@lang = Irc::Language.new(@config["LANGUAGE"])
@keywords = Irc::Keywords.new(self)
@auth = Irc::IrcAuth.new(self)
@plugins = Irc::Plugins.new(self, ["#{botclass}/plugins"])
@socket = Irc::IrcSocket.new(@config["SERVER"], @config["PORT"], @config["HOST"], @config["SENDQ_DELAY"], @config["SENDQ_BURST"])
@nick = @config["NICK"]
@server_password = @config["SERVER_PASSWORD"]
if @config["ADDRESS_PREFIX"]
@addressing_prefixes = @config["ADDRESS_PREFIX"].split(" ")
else
@addressing_prefixes = Array.new
end
@client = Irc::IrcClient.new
@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"])
# pass it off to plugins that want to hear everything
@plugins.delegate "listen", message
}
@client["MOTD"] = proc { |data|
data['MOTD'].each_line { |line|
log "MOTD: #{line}", "server"
}
}
@client["NICKTAKEN"] = proc { |data|
nickchg "#{@nick}_"
}
@client["BADNICK"] = proc {|data|
puts "WARNING, bad nick (#{data['NICK']})"
}
@client["PING"] = proc {|data|
# (jump the queue for pongs)
@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"])
if(sourcenick == @nick)
@nick = nick
end
@channels.each {|k,v|
if(v.users.has_key?(sourcenick))
log "@ #{sourcenick} is now known as #{nick}", k
v.users[nick] = v.users[sourcenick]
v.users.delete(sourcenick)
end
}
@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}/)
else
@channels.each {|k,v|
if(v.users.has_key?(sourcenick))
log "@ Quit: #{sourcenick}: #{message}", k
v.users.delete(sourcenick)
end
}
end
@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"]
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']}"
@nick = data['NICK']
if(@config["JOIN_CHANNELS"])
@config["JOIN_CHANNELS"].split(", ").each {|c|
puts "autojoining channel #{c}"
if(c =~ /^(\S+)\s+(\S+)$/)
join $1, $2
else
join c if(c)
end
}
end
}
@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"])
onpart(m)
}
@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}$/)
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"]
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"], data["TOPIC"])
@plugins.delegate("topic", m)
@plugins.delegate("listen", m)
}
@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
end
@channels[channel].users.clear
users.each {|u|
@channels[channel].users[u[0]] = ["mode", u[1]]
}
}
@client["UNKNOWN"] = proc {|data|
debug "UNKNOWN: #{data['SERVERSTRING']}"
}
end
|