all listed channels. Note that the channel portion of the URL need
*not* have a leading '#' unless the channel name itself does.
-Options: -d sets the debug-message level (probably only of interest to
-developers). -l sets a logfile to capture message traffic from
-channels. -n sets the nick and -p the nickserv password. The -V
-option prints the program version and exits.
-
Design and code by Eric S. Raymond <esr@thyrsus.com>. See the project
resource page at <http://www.catb.org/~esr/irker/>.
-Requires Python 2.6 or 2.5 with the simplejson library installed.
+Requires Python 2.7, or:
+* 2.6 with the argparse package installed.
+* 2.5 with the argparse and simplejson packages installed.
"""
from __future__ import with_statement
import Queue
import SocketServer
-import getopt
+import argparse
import logging
try:
import simplejson as json # Faster, also makes us Python-2.4-compatible
#socket = self.request[1]
irker.handle(data)
-def usage():
- sys.stdout.write("""
-Usage:
- irkerd [-d debuglevel] [-l logfile] [-n nick] [-p password] [-i channel message] [-V] [-h]
-
-Options
- -d set debug level
- -l set logfile
- -n set nick-style
- -p set nickserv password
- -i immediate mode
- -V return irkerd version
- -h print this help dialog
-""")
if __name__ == '__main__':
- log_level = None
- immediate = None
- nick_template = "irker%03d"
- password = None
- logfile = None
- try:
- (options, arguments) = getopt.getopt(sys.argv[1:], "d:i:l:n:p:Vh")
- except getopt.GetoptError as e:
- sys.stderr.write("%s" % e)
- usage()
- sys.exit(1)
- for (opt, val) in options:
- if opt == '-d': # Enable debug/progress messages
- if val.lower() not in LOG_LEVELS:
- sys.stderr.write('invalid log level %r (choices: %s)\n' % (
- val, ', '.join(LOG_LEVELS)))
- sys.exit(1)
- log_level = getattr(logging, val.upper())
- elif opt == '-i': # Immediate mode - send one message, then exit.
- immediate = val
- elif opt == '-l': # Logfile mode - report traffic read in
- logfile = val
- elif opt == '-n': # Force the nick
- nick_template = val
- elif opt == '-p': # Set a nickserv password
- password = val
- elif opt == '-V': # Emit version and exit
- sys.stdout.write("irkerd version %s\n" % version)
- sys.exit(0)
- elif opt == '-h':
- usage()
- sys.exit(0)
+ parser = argparse.ArgumentParser(
+ description=__doc__.strip().splitlines()[0])
+ parser.add_argument(
+ '-d', '--log-level', metavar='LEVEL', choices=LOG_LEVELS,
+ help='file of trusted certificates for SSL/TLS')
+ parser.add_argument(
+ '-l', '--log-file', metavar='PATH',
+ help='file for saving captured message traffic')
+ parser.add_argument(
+ '-n', '--nick', metavar='NAME', default='irker%03d',
+ help="nickname (optionally with a '%%.*d' server connection marker)")
+ parser.add_argument(
+ '-p', '--password', metavar='PASSWORD',
+ help='NickServ password')
+ parser.add_argument(
+ '-i', '--immediate', action='store_const', const=True,
+ help='disconnect after sending each message')
+ parser.add_argument(
+ '-V', '--version', action='version',
+ version='%(prog)s {0}'.format(version))
+ args = parser.parse_args()
handler = logging.StreamHandler()
LOG.addHandler(handler)
- if log_level:
+ if args.log_level:
+ log_level = getattr(logging, args.log_level.upper())
LOG.setLevel(log_level)
irker = Irker(
- logfile=logfile,
- nick_template=nick_template,
- nick_needs_number=re.search("%.*d", nick_template),
- password=password,
+ logfile=args.log_file,
+ nick_template=args.nick,
+ nick_needs_number=re.search('%.*d', args.nick),
+ password=args.password,
)
LOG.info("irkerd version %s" % version)
- if immediate:
+ if args.immediate:
irker.irc.add_event_handler("quit", lambda _c, _e: sys.exit(0))
irker.handle('{"to":"%s","privmsg":"%s"}' % (immediate, arguments[0]), quit_after=True)
irker.irc.spin()