#!/usr/bin/python
"""%(name)s, an IRC<->dAmn proxy
Version %(version)s
Usage:
%(progname)s\t [--help] [-i 0.0.0.0] [-p 6667]
\t\t [-d [start|stop|restart]]
\t\t [--logfile /tmp/frabjous.log]
\t\t [--pidfile frabjous.pid]
Arguments:
--help: Show this message and quit
-i or --ip: IP to bind to when listening for incoming connections.
Use 127.0.0.1 to only listen for connections from localhost.
Defaults to 0.0.0.0 (listen on all interfaces)
-p or --port: Port to bind to when listening for incoming connections.
Defaults to 6667
-d or --daemon: Start, stop, or restart as a daemon, depending on argument.
If omitted, will not run as a daemon.
--logfile: Specifies where to log to in daemon mode.
Defaults to /tmp/frabjous.log
--pidfile: Specifies where to store the pid file in daemon mode.
Defaults to frabjous.pid in the current directory.
%(gplinfo)s
%(name)s is (c) 2005 Kevin Wallace
"""
import sys
import errno
import ircd
import getopt
import SocketServer, socket
import daemonize
name = ircd.NAME
version = ircd.VERSION
progname = sys.argv[0]
gplinfo = """%(name)s comes with ABSOLUTELY NO WARRANTY.
This is free software, you are welcome to redistribute it under certain
contitions. See LICENSE.txt for details.""" % vars()
__doc__ = __doc__ % vars()
def usage(progname):
print __doc__
def main(argv):
progname = argv[0]
# set defaults
ip = ""
port = 6667
daemon_action = False
stdoutfile = "/tmp/frabjous.log"
pidfile = "frabjous.pid"
# grab arguments
try:
(list, args) = getopt.getopt(argv[1:], "i:p:d:", ["help", "ip=", "port=", "daemon=", "logfile=", "pidfile="])
if list == [] and len(args) == 1 and args[0] in ("start", "stop", "restart"):
daemon_action = args[0]
except getopt.GetoptError:
usage(progname)
sys.exit(2)
# parse arguments
for (opt, value) in list:
if opt == "--help":
usage(progname)
sys.exit()
elif opt in ("-i", "--ip"):
ip = value
elif opt in ("-p", "--port"):
port = int(value)
elif opt in ("-d", "--daemon"):
daemon_action = value
elif opt == "--logfile":
stdoutfile = value
elif opt == "--pidfile":
pidfile = value
print "%s\n" % gplinfo % vars()
if daemon_action:
daemonize.startstop(daemon_action, stdout=stdoutfile, pidfile=pidfile)
try:
server = SocketServer.ThreadingTCPServer((ip, port), ircd.dAmnIRCDHandler)
except socket.error, (err, description):
if err == errno.EADDRINUSE:
print "Unable to start server: %s\n(is another instance of %s running?)" % (description, name)
sys.exit(2)
else:
raise e
if ip == "":
print "Listening for incoming connections on *:%s" % (port)
else:
print "Listening for incoming connections on %s:%s" % (ip, port)
try:
server.serve_forever()
except KeyboardInterrupt:
print "Caught ctrl-c, quitting."
if __name__ == "__main__":
main(sys.argv)