Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 181 lines (135 sloc) 4.45 KB
#!/usr/bin/env ruby
#
# Terminus-Bot: An IRC bot to solve all of the problems with IRC bots.
#
# Copyright (C) 2010-2015 Kyle Johnson <kyle@vacantminded.com>, Alex Iadicicco
# (http://terminus-bot.net/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Don't comment this out. No, really.
unless RUBY_VERSION.start_with? '2'
abort 'Ruby version 2.0 or better is required.'
end
VERSION = 'Terminus-Bot v0.9'
PID_FILE = 'var/terminus-bot.pid'
LICENSE = 'MIT'
# Load the rest later. This will make --help much lighter.
require 'optparse'
$opts = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
opts.separator ''
opts.separator 'Options:'
opts.separator ''
$opts[:verbose] = false
opts.on '-v', '--[no-]verbose', 'Output a lot of info while starting' do |v|
$opts[:verbose] = v
end
$opts[:config_file] = 'terminus-bot.conf'
opts.on '-c', '--config FILE', 'Use another config file (default: terminus-bot.conf)' do |v|
$opts[:config_file] = v
end
$opts[:database_file] = 'var/terminus-bot/data.db'
opts.on '-d', '--database FILE', 'Use another database file (default: var/terminus-bot/data.db)' do |v|
$opts[:database_file] = v
end
$opts[:fork] = true
opts.on '-f', '--no-fork', 'Don\'t run in the background' do
$opts[:fork] = false
end
opts.on_tail '-h', '--help', 'Show this message' do
puts opts
exit
end
end.parse!
unless FileTest.exists? $opts[:config_file]
abort "Config file #{$opts[:config_file]} not found. Exiting."
end
require 'thread'
require 'logger'
require 'eventmachine'
require 'fileutils'
$log = Logger.new(STDOUT)
$log.level = $opts[:verbose] ? Logger::DEBUG : Logger::INFO
$log.info('preload') { VERSION }
$log.info('preload') { '<http://terminus-bot.net/>' }
$log.info('preload') { "Released under #{LICENSE}." }
Dir.chdir File.dirname __FILE__
Dir.mkdir 'var' unless Dir.exist? 'var'
if File.exist? PID_FILE
begin
pid = File.read(PID_FILE).to_i
$log.debug('preload') { "Found PID file with: #{pid}" }
if Process.kill(0, pid)
$log.fatal('preload') { "Already running as #{pid}" }
exit
end
rescue
# We're good to go. I think.
end
$log.warn('preload') { 'PID file appears to be stale. Deleting.' }
File.delete PID_FILE
end
$log.info('preload') { 'Loading core files.' }
def load_lib
# These have to be first.
load 'lib/canonizedhash.rb'
load 'lib/flags.rb'
#load 'lib/database.rb'
load 'lib/ircmacros.rb'
load 'lib/config.rb'
load 'lib/command.rb'
load 'lib/events.rb'
load 'lib/hash.rb'
load 'lib/string.rb'
load 'lib/time.rb'
load 'lib/numeric.rb'
load 'lib/users.rb'
load 'lib/ircconnection.rb'
load 'lib/message.rb'
load 'lib/channels.rb'
load 'lib/commands.rb'
# These have to be last (in this order).
load 'lib/modules.rb'
load 'lib/scripts.rb'
load 'lib/core.rb'
end
def write_pid_file pid
# Bot.run changes $log to something else, so just puts this.
puts "Terminus-Bot started as #{pid}. Writing to #{PID_FILE}"
File.open(PID_FILE, 'w') {|f| f.puts pid}
end
load_lib
# TODO: Verify configuration and check for database corruption.
# Launch!
unless $opts[:fork]
write_pid_file Process.pid
EM.run { Bot.run }
# Go ahead and try to delete the PID file one more time, just in case EM.run
# failed for some reason.
if File.exist? PID_FILE
File.delete PID_FILE
end
exit
end
pid = EM.fork_reactor { Bot.run }
write_pid_file pid
Process.detach pid
# vim: set tabstop=2 expandtab: