| 379 | | Net::IRC::Server.new("localhost", 16668, TwitterIrcGateway).start |
| | 379 | require "optparse" |
| | 380 | |
| | 381 | opts = { |
| | 382 | :port => 16670, |
| | 383 | :host => "localhost", |
| | 384 | :debug => false, |
| | 385 | :log => nil, |
| | 386 | :debug => false, |
| | 387 | } |
| | 388 | |
| | 389 | OptionParser.new do |parser| |
| | 390 | parser.instance_eval do |
| | 391 | self.banner = <<-EOB.gsub(/^\t+/, "") |
| | 392 | Usage: #{$0} [opts] |
| | 393 | |
| | 394 | EOB |
| | 395 | |
| | 396 | separator "" |
| | 397 | |
| | 398 | separator "Options:" |
| | 399 | on("-p", "--port [PORT=#{opts[:port]}]", "listen port number") do |port| |
| | 400 | opts[:port] = port |
| | 401 | end |
| | 402 | |
| | 403 | on("-h", "--host [HOST=#{opts[:host]}]", "listen host") do |host| |
| | 404 | opts[:host] = host |
| | 405 | end |
| | 406 | |
| | 407 | on("-l", "--log LOG", "log file") do |log| |
| | 408 | opts[:log] = log |
| | 409 | end |
| | 410 | |
| | 411 | on("--debug", "Enable debug mode") do |debug| |
| | 412 | opts[:log] = $stdout |
| | 413 | opts[:debug] = true |
| | 414 | end |
| | 415 | |
| | 416 | parse!(ARGV) |
| | 417 | end |
| | 418 | end |
| | 419 | |
| | 420 | opts[:logger] = Logger.new(opts[:log], "daily") |
| | 421 | opts[:logger].level = opts[:debug] ? Logger::DEBUG : Logger::INFO |
| | 422 | |
| | 423 | def daemonize(debug=false) |
| | 424 | return yield if $DEBUG || debug |
| | 425 | Process.fork do |
| | 426 | Process.setsid |
| | 427 | Dir.chdir "/" |
| | 428 | trap("SIGINT") { exit! 0 } |
| | 429 | trap("SIGTERM") { exit! 0 } |
| | 430 | trap("SIGHUP") { exit! 0 } |
| | 431 | File.open("/dev/null") {|f| |
| | 432 | STDIN.reopen f |
| | 433 | STDOUT.reopen f |
| | 434 | STDERR.reopen f |
| | 435 | } |
| | 436 | yield |
| | 437 | end |
| | 438 | exit! 0 |
| | 439 | end |
| | 440 | |
| | 441 | daemonize(opts[:debug]) do |
| | 442 | Net::IRC::Server.new(opts[:host], opts[:port], TwitterIrcGateway, opts).start |
| | 443 | end |