| 1 | #!/usr/bin/env ruby |
|---|
| 2 | |
|---|
| 3 | =begin rdoc |
|---|
| 4 | |
|---|
| 5 | = rbot main executable |
|---|
| 6 | |
|---|
| 7 | Usage: |
|---|
| 8 | |
|---|
| 9 | % rbot [options] [config directory] |
|---|
| 10 | |
|---|
| 11 | == Options |
|---|
| 12 | |
|---|
| 13 | [-h, --help] |
|---|
| 14 | display a help message and exit |
|---|
| 15 | [-v, --version] |
|---|
| 16 | display version information and exit |
|---|
| 17 | [-d, --debug] |
|---|
| 18 | enable debug messages |
|---|
| 19 | [-l, --loglevel _level_] |
|---|
| 20 | sets the minimum log level verbosity |
|---|
| 21 | [-b, --background] |
|---|
| 22 | background (daemonize) the bot |
|---|
| 23 | [-p, --pidfile _filename_] |
|---|
| 24 | write the bot pid to _filename_ |
|---|
| 25 | |
|---|
| 26 | The default config directory is <tt>~/.rbot</tt>. |
|---|
| 27 | |
|---|
| 28 | The default pidfile is <tt><i>botdir</i>/rbot.pid</tt>. |
|---|
| 29 | |
|---|
| 30 | The logfile is located at <tt><i>botdir</i>/<i>botname</i>.log</tt>, and |
|---|
| 31 | the default loglevel is 1 (INFO messages). Possible values for the loglevel |
|---|
| 32 | are 0 (DEBUG), 1 (INFO), 2 (WARN), 3 (ERROR), 4 (FATAL). |
|---|
| 33 | |
|---|
| 34 | Please note that the logfile doesn't contain IRC logs (which are located at |
|---|
| 35 | <tt><i>botdir</i>/logs/*</tt>, but only rbot diagnostic messages. |
|---|
| 36 | |
|---|
| 37 | =end |
|---|
| 38 | |
|---|
| 39 | # Copyright (C) 2002-2006 Tom Gilbert. |
|---|
| 40 | # Copyright (C) 2007-2008 Giuseppe Bilotta and the rbot development team |
|---|
| 41 | # |
|---|
| 42 | # This is free software, see COPYING for licensing details |
|---|
| 43 | |
|---|
| 44 | $VERBOSE=true |
|---|
| 45 | |
|---|
| 46 | require 'etc' |
|---|
| 47 | require 'getoptlong' |
|---|
| 48 | require 'fileutils' |
|---|
| 49 | |
|---|
| 50 | $version ||= '0.9.15' |
|---|
| 51 | $version_timestamp ||= 0 |
|---|
| 52 | $opts = Hash.new |
|---|
| 53 | |
|---|
| 54 | orig_opts = ARGV.dup |
|---|
| 55 | |
|---|
| 56 | opts = GetoptLong.new( |
|---|
| 57 | ["--background", "-b", GetoptLong::NO_ARGUMENT], |
|---|
| 58 | ["--debug", "-d", GetoptLong::NO_ARGUMENT], |
|---|
| 59 | ["--help", "-h", GetoptLong::NO_ARGUMENT], |
|---|
| 60 | ["--loglevel", "-l", GetoptLong::REQUIRED_ARGUMENT], |
|---|
| 61 | ["--trace", "-t", GetoptLong::REQUIRED_ARGUMENT], |
|---|
| 62 | ["--pidfile", "-p", GetoptLong::REQUIRED_ARGUMENT], |
|---|
| 63 | ["--version", "-v", GetoptLong::NO_ARGUMENT] |
|---|
| 64 | ) |
|---|
| 65 | |
|---|
| 66 | $debug = $DEBUG |
|---|
| 67 | $daemonize = false |
|---|
| 68 | |
|---|
| 69 | opts.each {|opt, arg| |
|---|
| 70 | $debug = true if(opt == "--debug") |
|---|
| 71 | $daemonize = true if(opt == "--background") |
|---|
| 72 | $opts[opt.sub(/^-+/, "")] = arg |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | $cl_loglevel = $opts["loglevel"].to_i if $opts["loglevel"] |
|---|
| 76 | |
|---|
| 77 | if ($opts["trace"]) |
|---|
| 78 | set_trace_func proc { |event, file, line, id, binding, classname| |
|---|
| 79 | if classname.to_s == $opts["trace"] |
|---|
| 80 | printf "TRACE: %8s %s:%-2d %10s %8s\n", event, File.basename(file), line, id, classname |
|---|
| 81 | end |
|---|
| 82 | } |
|---|
| 83 | end |
|---|
| 84 | |
|---|
| 85 | defaultlib = File.expand_path(File.dirname($0) + '/../lib') |
|---|
| 86 | |
|---|
| 87 | if File.directory? "#{defaultlib}/rbot" |
|---|
| 88 | unless $:.include? defaultlib |
|---|
| 89 | $:.unshift defaultlib |
|---|
| 90 | end |
|---|
| 91 | end |
|---|
| 92 | |
|---|
| 93 | begin |
|---|
| 94 | require 'rbot/ircbot' |
|---|
| 95 | rescue LoadError => e |
|---|
| 96 | puts "Error: couldn't find the rbot/ircbot module (or one of its dependencies)\n" |
|---|
| 97 | puts e |
|---|
| 98 | exit 2 |
|---|
| 99 | end |
|---|
| 100 | |
|---|
| 101 | # ruby 1.9 specific fixes |
|---|
| 102 | unless RUBY_VERSION < '1.9' |
|---|
| 103 | require 'rbot/compat19' |
|---|
| 104 | end |
|---|
| 105 | |
|---|
| 106 | if ($opts["version"]) |
|---|
| 107 | puts "rbot #{$version}" |
|---|
| 108 | exit 0 |
|---|
| 109 | end |
|---|
| 110 | |
|---|
| 111 | if ($opts["help"]) |
|---|
| 112 | puts "usage: rbot [options] [config directory]" |
|---|
| 113 | puts " -h, --help this message" |
|---|
| 114 | puts " -v, --version version information" |
|---|
| 115 | puts " -d, --debug enable debug messages" |
|---|
| 116 | puts " -l, --loglevel sets the log level verbosity" |
|---|
| 117 | puts " -b, --background background (daemonize) the bot" |
|---|
| 118 | puts " -p, --pidfile write the bot pid to file" |
|---|
| 119 | puts "config directory defaults to ~/.rbot" |
|---|
| 120 | exit 0 |
|---|
| 121 | end |
|---|
| 122 | |
|---|
| 123 | if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts)) |
|---|
| 124 | # just run the bot |
|---|
| 125 | bot.mainloop |
|---|
| 126 | end |
|---|
| 127 | |
|---|