examples/nig.rb in net-irc-0.0.2 vs examples/nig.rb in net-irc-0.0.3

- old
+ new

@@ -61,11 +61,11 @@ def main_channel "#nowa" end def api_base - URI("http://api.nowa.jp/") + URI("https://api.nowa.jp/") end def api_source "nig.rb" end @@ -81,10 +81,11 @@ opts = { :port => 16671, :host => "localhost", :log => nil, :debug => false, + :foreground => false, } OptionParser.new do |parser| parser.instance_eval do self.banner = <<-EOB.gsub(/^\t+/, "") @@ -110,35 +111,40 @@ on("--debug", "Enable debug mode") do |debug| opts[:log] = $stdout opts[:debug] = true end + on("-f", "--foreground", "run foreground") do |foreground| + opts[:log] = $stdout + opts[:foreground] = true + end + parse!(ARGV) end end opts[:logger] = Logger.new(opts[:log], "daily") opts[:logger].level = opts[:debug] ? Logger::DEBUG : Logger::INFO - def daemonize(debug=false) - return yield if $DEBUG || debug + def daemonize(foreground=false) + trap("SIGINT") { exit! 0 } + trap("SIGTERM") { exit! 0 } + trap("SIGHUP") { exit! 0 } + return yield if $DEBUG || foreground Process.fork do Process.setsid Dir.chdir "/" - trap("SIGINT") { exit! 0 } - trap("SIGTERM") { exit! 0 } - trap("SIGHUP") { exit! 0 } File.open("/dev/null") {|f| STDIN.reopen f STDOUT.reopen f STDERR.reopen f } yield end exit! 0 end - daemonize(opts[:debug]) do + daemonize(opts[:debug] || opts[:foreground]) do Net::IRC::Server.new(opts[:host], opts[:port], NowaIrcGateway, opts).start end end