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