summaryrefslogtreecommitdiff
path: root/lib/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-02-15 01:30:51 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-02-15 02:06:27 +0100
commit91a9024e21ec8b429605a036b5c9193442a580e3 (patch)
treef1524dd5595a80e32d0702c0d8939b1001c695fb /lib/rbot
parent38be7f6511447d98780a069bccefecd933238e30 (diff)
+ @bot.path and datafile methods
We provide two methods that make it more simple and elegant for botmodules to define paths relative to the bot's own directory (botclass) and to the BotModule's (assumed) non-registry directory. The first method is Irc::Bot#path(), which joins its arguments with the botclass. This method can be used to access datafiles in the bot directory with a much cleaner syntax; and since it uses File.join, the resulting paths are also properly formatted on each platform, which doesn't hurt. Each BotModule now also carries a dirname() method that should return the directory under botclass that holds the BotModule's datafiles. dirname() defaults to the BotModule's name(), but it can be overridden, e.g. for backwards compatibility (see the patch for the quotes plugin), or for BotModules that share their datafiles. Datafiles can be accessed using the BotModule#datafile() method that joins the botclass, the dirname() and whatever other argument is passed.
Diffstat (limited to 'lib/rbot')
-rw-r--r--lib/rbot/config.rb12
-rw-r--r--lib/rbot/core/auth.rb8
-rw-r--r--lib/rbot/core/irclog.rb6
-rw-r--r--lib/rbot/core/utils/utils.rb2
-rwxr-xr-xlib/rbot/core/utils/wordlist.rb2
-rw-r--r--lib/rbot/dbhash.rb25
-rw-r--r--lib/rbot/ircbot.rb6
-rw-r--r--lib/rbot/plugins.rb14
-rw-r--r--lib/rbot/registry.rb28
9 files changed, 66 insertions, 37 deletions
diff --git a/lib/rbot/config.rb b/lib/rbot/config.rb
index 4f81c695..e6145a82 100644
--- a/lib/rbot/config.rb
+++ b/lib/rbot/config.rb
@@ -262,9 +262,10 @@ module Config
return unless @bot
@changed = false
- if(File.exist?("#{@bot.botclass}/conf.yaml"))
+ conf = @bot.path 'conf.yaml'
+ if File.exist? conf
begin
- newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
+ newconfig = YAML::load_file conf
newconfig.each { |key, val|
@config[key.to_sym] = val
}
@@ -327,8 +328,10 @@ module Config
return
end
begin
+ conf = @bot.path 'conf.yaml'
+ fnew = conf + '.new'
debug "Writing new conf.yaml ..."
- File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
+ File.open(fnew, "w") do |file|
savehash = {}
@config.each { |key, val|
savehash[key.to_s] = val
@@ -336,8 +339,7 @@ module Config
file.puts savehash.to_yaml
end
debug "Officializing conf.yaml ..."
- File.rename("#{@bot.botclass}/conf.yaml.new",
- "#{@bot.botclass}/conf.yaml")
+ File.rename(fnew, conf)
@changed = false
rescue => e
error "failed to write configuration file conf.yaml! #{$!}"
diff --git a/lib/rbot/core/auth.rb b/lib/rbot/core/auth.rb
index 5e80d880..275f5d7e 100644
--- a/lib/rbot/core/auth.rb
+++ b/lib/rbot/core/auth.rb
@@ -817,13 +817,13 @@ class AuthModule < CoreBotModule
def auth_export(m, params)
- exportfile = "#{@bot.botclass}/new-auth.users"
+ exportfile = @bot.path "new-auth.users"
what = params[:things]
has_to = what[-2] == "to"
if has_to
- exportfile = "#{@bot.botclass}/#{what[-1]}"
+ exportfile = @bot.path what[-1]
what.slice!(-2,2)
end
@@ -889,13 +889,13 @@ class AuthModule < CoreBotModule
def auth_import(m, params)
- importfile = "#{@bot.botclass}/new-auth.users"
+ importfile = @bot.path "new-auth.users"
what = params[:things]
has_from = what[-2] == "from"
if has_from
- importfile = "#{@bot.botclass}/#{what[-1]}"
+ importfile = @bot.path what[-1]
what.slice!(-2,2)
end
diff --git a/lib/rbot/core/irclog.rb b/lib/rbot/core/irclog.rb
index 88d8b00f..b848947c 100644
--- a/lib/rbot/core/irclog.rb
+++ b/lib/rbot/core/irclog.rb
@@ -33,7 +33,9 @@ class IrcLogModule < CoreBotModule
@queue = Queue.new
@thread = Thread.new { loggers_thread }
@logs = Hash.new
- Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs")
+ logdir = @bot.path 'logs'
+ Dir.mkdir(logdir) unless File.exist?(logdir)
+ # TODO what shall we do if the logdir couldn't be created? (e.g. it existed as a file)
event_irclog_list_changed(@bot.config['irclog.no_log'], @bot.config['irclog.do_log'])
@fn_format = @bot.config['irclog.filename_format']
end
@@ -244,7 +246,7 @@ class IrcLogModule < CoreBotModule
end
def logfilepath(where_str, now)
- File.join(@bot.botclass, 'logs', now.strftime(@fn_format) % { :where => where_str })
+ @bot.path('logs', now.strftime(@fn_format) % { :where => where_str })
end
protected
diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb
index 5c44cf09..8df9626c 100644
--- a/lib/rbot/core/utils/utils.rb
+++ b/lib/rbot/core/utils/utils.rb
@@ -169,7 +169,7 @@ module ::Irc
def Utils.bot=(b)
debug "initializing utils"
@@bot = b
- @@safe_save_dir = "#{@@bot.botclass}/safe_save"
+ @@safe_save_dir = @@bot.path('safe_save')
end
diff --git a/lib/rbot/core/utils/wordlist.rb b/lib/rbot/core/utils/wordlist.rb
index fc341578..4e624f9b 100755
--- a/lib/rbot/core/utils/wordlist.rb
+++ b/lib/rbot/core/utils/wordlist.rb
@@ -11,7 +11,7 @@ module ::Irc
class Bot
class Wordlist
def self.wordlist_base
- @@wordlist_base ||= File.join(Utils.bot.botclass, 'wordlists')
+ @@wordlist_base ||= Utils.bot.path 'wordlists'
end
def self.get(path, options={})
diff --git a/lib/rbot/dbhash.rb b/lib/rbot/dbhash.rb
index dd61c720..704dafae 100644
--- a/lib/rbot/dbhash.rb
+++ b/lib/rbot/dbhash.rb
@@ -52,18 +52,20 @@ module Irc
def initialize(bot, key, absfilename=false)
@bot = bot
@key = key
+ relfilename = @bot.path key
+ relfilename << '.db'
if absfilename && File.exist?(key)
# db already exists, use it
@db = DBHash.open_db(key)
- elsif File.exist?(@bot.botclass + "/#{key}.db")
- # db already exists, use it
- @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
elsif absfilename
# create empty db
@db = DBHash.create_db(key)
+ elsif File.exist? relfilename
+ # db already exists, use it
+ @db = DBHash.open_db relfilename
else
# create empty db
- @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
+ @db = DBHash.create_db relfilename
end
end
@@ -98,27 +100,30 @@ module Irc
@key = key
if @@env.nil?
begin
- @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
+ @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
rescue => e
debug "DBTree: failed to open environment: #{e.pretty_inspect}. Retrying ..."
- @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
+ @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
end
- #@@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
+ #@@env = BDB::Env.open(@bot.botclass, BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
end
+ relfilename = @bot.path key
+ relfilename << '.db'
+
if absfilename && File.exist?(key)
# db already exists, use it
@db = DBTree.open_db(key)
elsif absfilename
# create empty db
@db = DBTree.create_db(key)
- elsif File.exist?(@bot.botclass + "/#{key}.db")
+ elsif File.exist? relfilename
# db already exists, use it
- @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
+ @db = DBTree.open_db relfilename
else
# create empty db
- @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
+ @db = DBTree.create_db relfilename
end
end
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb
index 6ae3e4ed..abf64618 100644
--- a/lib/rbot/ircbot.rb
+++ b/lib/rbot/ircbot.rb
@@ -791,6 +791,12 @@ class Bot
end
end
+ # Return a path under the current botclass by joining the mentioned
+ # components. The components are automatically converted to String
+ def path(*components)
+ File.join(@botclass, *(components.map {|c| c.to_s}))
+ end
+
def setup_plugins_path
plugdir_default = File.join(Config::datadir, 'plugins')
plugdir_local = File.join(@botclass, 'plugins')
diff --git a/lib/rbot/plugins.rb b/lib/rbot/plugins.rb
index 4d51cfc5..9d5523e4 100644
--- a/lib/rbot/plugins.rb
+++ b/lib/rbot/plugins.rb
@@ -349,6 +349,20 @@ module Plugins
@bot.plugins.mark_priorities_dirty
end
end
+
+ # Directory name to be joined to the botclass to access data files. By
+ # default this is the plugin name itself, but may be overridden, for
+ # example by plugins that share their datafiles or for backwards
+ # compatibilty
+ def dirname
+ name
+ end
+
+ # Filename for a datafile built joining the botclass, plugin dirname and
+ # actual file name
+ def datafile(*fname)
+ @bot.path dirname, *fname
+ end
end
# A CoreBotModule is a BotModule that provides core functionality.
diff --git a/lib/rbot/registry.rb b/lib/rbot/registry.rb
index 2ea2e578..05425341 100644
--- a/lib/rbot/registry.rb
+++ b/lib/rbot/registry.rb
@@ -16,30 +16,30 @@ class Bot
# NB this function is called _early_ in init(), pretty much all you have to
# work with is @bot.botclass.
def upgrade_data
- if File.exist?("#{@bot.botclass}/registry.db")
+ oldreg = @bot.path 'registry.db'
+ newreg = @bot.path 'plugin_registry.db'
+ if File.exist?(oldreg)
log _("upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format")
- old = BDB::Hash.open("#{@bot.botclass}/registry.db", nil,
- "r+", 0600)
- new = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
- BDB::CREATE | BDB::EXCL,
- 0600)
+ old = BDB::Hash.open(oldreg, nil, "r+", 0600)
+ new = BDB::CIBtree.open(newreg, nil, BDB::CREATE | BDB::EXCL, 0600)
old.each {|k,v|
new[k] = v
}
old.close
new.close
- File.rename("#{@bot.botclass}/registry.db", "#{@bot.botclass}/registry.db.old")
+ File.rename(oldreg, oldreg + ".old")
end
end
def upgrade_data2
- if File.exist?("#{@bot.botclass}/plugin_registry.db")
- Dir.mkdir("#{@bot.botclass}/registry") unless File.exist?("#{@bot.botclass}/registry")
- env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)# | BDB::TXN_NOSYNC)
+ oldreg = @bot.path 'plugin_registry.db'
+ newdir = @bot.path 'registry'
+ if File.exist?(oldreg)
+ Dir.mkdir(newdir) unless File.exist?(newdir)
+ env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)# | BDB::TXN_NOSYNC)
dbs = Hash.new
log _("upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format")
- old = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
- "r+", 0600, "env" => env)
+ old = BDB::CIBtree.open(oldreg, nil, "r+", 0600, "env" => env)
old.each {|k,v|
prefix,key = k.split("/", 2)
prefix.downcase!
@@ -64,7 +64,7 @@ class Bot
dbs[prefix][key] = v
}
old.close
- File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
+ File.rename(oldreg, oldreg + ".old")
dbs.each {|k,v|
log _("closing db #{k}")
v.close
@@ -126,7 +126,7 @@ class Bot
def initialize(bot, name)
@bot = bot
@name = name.downcase
- @filename = "#{@bot.botclass}/registry/#{@name}"
+ @filename = @bot.path 'registry', @name
dirs = File.dirname(@filename).split("/")
dirs.length.times { |i|
dir = dirs[0,i+1].join("/")+"/"