[webservice] response as json if asked to
[rbot] / bin / rbot
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 require 'etc'
45 require 'getoptlong'
46 require 'fileutils'
47
48 $version ||= '0.9.15'
49 $version_timestamp ||= 0
50 $opts = Hash.new
51
52 orig_opts = ARGV.dup
53
54 opts = GetoptLong.new(
55   ["--background", "-b", GetoptLong::NO_ARGUMENT],
56   ["--debug", "-d", GetoptLong::NO_ARGUMENT],
57   ["--help",  "-h", GetoptLong::NO_ARGUMENT],
58   ["--loglevel",  "-l", GetoptLong::REQUIRED_ARGUMENT],
59   ["--trace",  "-t", GetoptLong::REQUIRED_ARGUMENT],
60   ["--pidfile", "-p", GetoptLong::REQUIRED_ARGUMENT],
61   ["--version", "-v", GetoptLong::NO_ARGUMENT]
62 )
63
64 $debug = $DEBUG
65 $daemonize = false
66
67 opts.each {|opt, arg|
68   $debug = true if(opt == "--debug")
69   $daemonize = true if(opt == "--background")
70   $opts[opt.sub(/^-+/, "")] = arg
71 }
72
73 $cl_loglevel = $opts["loglevel"].to_i if $opts["loglevel"]
74
75 if ($opts["trace"])
76   set_trace_func proc { |event, file, line, id, binding, classname|
77     if classname.to_s == $opts["trace"]
78       printf "TRACE: %8s %s:%-2d %10s %8s\n", event, File.basename(file), line, id, classname
79     end
80   }
81 end
82
83 defaultlib = File.expand_path(File.dirname($0) + '/../lib')
84
85 if File.directory? "#{defaultlib}/rbot"
86   unless $:.include? defaultlib
87     $:.unshift defaultlib
88   end
89 end
90
91 begin
92   require 'rbot/ircbot'
93 rescue LoadError => e
94   puts "Error: couldn't find the rbot/ircbot module (or one of its dependencies)\n"
95   puts e
96   exit 2
97 end
98
99 if ($opts["version"])
100   puts "rbot #{$version}"
101   exit 0
102 end
103
104 if ($opts["help"])
105   puts "usage: rbot [options] [config directory]"
106   puts "  -h, --help         this message"
107   puts "  -v, --version      version information"
108   puts "  -d, --debug        enable debug messages"
109   puts "  -l, --loglevel     sets the log level verbosity"
110   puts "  -b, --background   background (daemonize) the bot"
111   puts "  -p, --pidfile      write the bot pid to file"
112   puts "config directory defaults to ~/.rbot"
113   exit 0
114 end
115
116 if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts))
117   # just run the bot
118   bot.mainloop
119 end
120