Module: Net::IRC::CLI

Defined in:
lib/net/irc/cli.rb

Class Method Summary collapse

Class Method Details

.daemonize(foreground = false) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/net/irc/cli.rb', line 2

def self.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 "/"
    File.open("/dev/null") {|f|
      STDIN.reopen  f
      STDOUT.reopen f
      STDERR.reopen f
    }
    yield
  end
  exit! 0
end

.run(server_session_klass, argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/net/irc/cli.rb', line 20

def self.run(server_session_klass, argv)
  opts = {
    :port       => 16701,
    :host       => "localhost",
    :log        => nil,
    :debug      => false,
    :foreground => false,
  }

  OptionParser.new do |parser|
    parser.instance_eval do
      self.banner  = <<-EOB.gsub(/^\t+/, "")
      Usage: #{$0} [opts]

    EOB

      separator ""

      separator "Options:"
      on("-p", "--port [PORT=#{opts[:port]}]", "port number to listen") do |port|
        opts[:port] = port
      end

      on("-h", "--host [HOST=#{opts[:host]}]", "host name or IP address to listen") do |host|
        opts[:host] = host
      end

      on("-l", "--log LOG", "log file") do |log|
        opts[:log] = log
      end

      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

  daemonize(opts[:debug] || opts[:foreground]) do
    Net::IRC::Server.new(opts[:host], opts[:port], server_session_klass, opts).start
  end
end