]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - bin/rbot
* we're now "git version", not "svn version" by default
[user/henk/code/ruby/rbot.git] / 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 Tom Gilbert.
40 #
41 # Permission is hereby granted, free of charge, to any person obtaining a copy
42 # of this software and associated documentation files (the "Software"), to
43 # deal in the Software without restriction, including without limitation the
44 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
45 # sell copies of the Software, and to permit persons to whom the Software is
46 # furnished to do so, subject to the following conditions:
47 #
48 # The above copyright notice and this permission notice shall be included in
49 # all copies of the Software and its documentation and acknowledgment shall be
50 # given in the documentation and software packages that this Software was
51 # used.
52 #
53 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
56 # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
57 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
58 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59
60 # Most of the string processing across rbot is done against IRC messages, which
61 # do not have a well-defined encoding. Although many clients are now using
62 # UTF-8, there is no guarantee that an arbitrary string received from IRC will
63 # be UTF-8 encoded. We have to force ASCII (byte-wise/charset agnostic)
64 # matching because otherwise some strings can give problems: in particular, for
65 # example, the bytesequence "\340\350\354\362\371" (that is the aeiou vowels,
66 # each with a grave accent) will cause the string to be considered up to the
67 # "\354" (i with grave accent) only: so either the rest of the message is
68 # ignored, or the matching fails.
69 $KCODE = 'a'
70
71 $VERBOSE=true
72
73 require 'etc'
74 require 'getoptlong'
75 require 'fileutils'
76
77 $version="0.9.11-git"
78 $opts = Hash.new
79
80 if $version =~ /git/
81   if defined?(SCM_DIR) and File.exists?(File.join(SCM_DIR, '.git'))
82     git_out = `git status`
83
84     git_out.match(/^# On branch (.*)\n/)
85     if $1 # git 1.5.x
86       branch = $1.dup || "unknown"
87       changed = git_out.match(/^# Change(.*)\n/)
88       rev = "revision "
89       git_out = `git log -1 --pretty=format:"%h%n%b"`.split("\n")
90       rev << git_out.first
91       if git_out.last.match(/^git-svn-id: \S+@(\d+)/)
92         rev << "(svn #{$1})"
93       end
94       rev << ", local changes" if changed
95     else # older gits
96       git_out = `git branch`
97       git_out.match(/^\* (.*)\n/)
98       branch = $1.dup rescue "unknown"
99       rev = "revision " + `git rev-parse HEAD`[0,6]
100     end
101
102     $version << " (#{branch} branch, #{rev})"
103   else
104     up = File.dirname(__FILE__) + "/.."
105     rev = " (unknown revision)"
106     begin
107       svn_out = `svn info #{up}`
108       if svn_out =~ /Last Changed Rev: (\d+)/
109         rev = " (revision #{$1}"
110       end
111       svn_st = `svn st #{up}`
112       if svn_st =~ /^[MDA] /
113         rev << ", local changes"
114       end
115       rev << ")"
116     rescue => e
117       puts e.inspect
118     end
119     $version += rev
120   end
121 end
122
123 orig_opts = ARGV.dup
124
125 opts = GetoptLong.new(
126   ["--background", "-b", GetoptLong::NO_ARGUMENT],
127   ["--debug", "-d", GetoptLong::NO_ARGUMENT],
128   ["--help",  "-h", GetoptLong::NO_ARGUMENT],
129   ["--loglevel",  "-l", GetoptLong::REQUIRED_ARGUMENT],
130   ["--trace",  "-t", GetoptLong::REQUIRED_ARGUMENT],
131   ["--pidfile", "-p", GetoptLong::REQUIRED_ARGUMENT],
132   ["--version", "-v", GetoptLong::NO_ARGUMENT]
133 )
134
135 $debug = $DEBUG
136 $daemonize = false
137
138 opts.each {|opt, arg|
139   $debug = true if(opt == "--debug")
140   $daemonize = true if(opt == "--background")
141   $opts[opt.sub(/^-+/, "")] = arg
142 }
143
144 $cl_loglevel = $opts["loglevel"].to_i if $opts["loglevel"]
145
146 if ($opts["trace"])
147   set_trace_func proc { |event, file, line, id, binding, classname|
148     if classname.to_s == $opts["trace"]
149       printf "TRACE: %8s %s:%-2d %10s %8s\n", event, File.basename(file), line, id, classname
150     end
151   }
152 end
153
154 defaultlib = File.expand_path(File.dirname($0) + '/../lib')
155
156 if File.directory? "#{defaultlib}/rbot"
157   unless $:.include? defaultlib
158     $:.unshift defaultlib
159   end
160 end
161   
162 begin
163   require 'rbot/ircbot'
164 rescue LoadError => e
165   puts "Error: couldn't find the rbot/ircbot module (or one of its dependencies)\n"
166   puts e
167   exit 2
168 end
169
170 if ($opts["version"])
171   puts "rbot #{$version}"
172   exit 0
173 end
174
175 if ($opts["help"])
176   puts "usage: rbot [options] [config directory]"
177   puts "  -h, --help         this message"
178   puts "  -v, --version      version information"
179   puts "  -d, --debug        enable debug messages"
180   puts "  -l, --loglevel     sets the log level verbosity"
181   puts "  -b, --background   background (daemonize) the bot"
182   puts "  -p, --pidfile      write the bot pid to file"
183   puts "config directory defaults to ~/.rbot"
184   exit 0
185 end
186
187 if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts))
188   # just run the bot
189   bot.mainloop
190 end
191