Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 99 lines (85 sloc) 2.703 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#!/usr/bin/env python
"""This is the main entry point to pyBawt.

The rest of the infrastructure ties together to make all the components of a
working IRC bot, this is just one sample implementation"""

##
# pyBawt is (Will be) released under the WTFPL
# You are hereby licensed to do WHATEVER THE FUCK YOU WANT with it's source
# code
##

# Rich Healey '08
from lib import *
import logging
try:
    import config
except InvalidConfig as e:
    logging.fatal("Couldn't load config file: %s" % (str(e)))

import ircSocket
import time
import sys
import os
import random
import bModules
import traceback


logging.info("pyBawt started")

# Have a crack at sweet argparsing

# TODO - hax involving stdout for debugging

try:
    import argparse
    parser = argparse.ArgumentParser(description='IRC bot written in python')
    parser.add_argument("-d", "--debug", dest='debug', action='store_true',
            default=False,
            help='include debug data, also crash violently on error')
    args = parser.parse_args()

    debug = args.debug
except ImportError:
# no argparse, probably py2.6
    debug = False
    # TODO - something clever here to make args still work

def restart_stub():
    net.quit("Going down for restart")
    os.execv(sys.executable, [sys.executable] + sys.argv)

net = ircSocket.chatnet(config.host, port=config.port, use_ssl=config.ssl)
# Ugly hax, port to argparse if we see any more nicks
nick = config.nick
net.identify(nick)
net.auth_self(config.nickserv_nick, config.nickserv_pass)

# Before we hit mainloop, write pidfile
if not debug:
    try:
        fh = open('/tmp/pyBawt.pid', 'w')
        fh.write(str(os.getpid()))
        fh.close()
    except IOError:
        logging.fatal("Couldn't write pidfile")

try:
    for i in config.channels:
        net.join(i)
    while True:
        try:
            net.recv_wait()
        except FlushQueue:
            pass
        net.dump_queue()
except KeyboardInterrupt:
    logging.error("Shutting down due to user intervention")
    net.quit("Killed from terminal")
except Restart:
    # TODO Include the user who did this
    logging.error("Restarting due to user intervention")
    restart_stub()
except IrcDisconnected:
    if ircSocket.should_reconnect():
        restart_stub()
except IrcTerminated:
    # Catch but don't handle, die gracefully
    pass
except Exception:
    # TODO - Checkout from stable git branch
    if debug: # Debug hook? Either way it's stupid.
        logging.error("Shutting down and bailing out")
        raise
    else:
        logging.error("Exception caught, restarting")
        traceback.print_exception(*sys.exc_info(), file=logging.Writer(logging.error))
        restart_stub()
Something went wrong with that request. Please try again.