/
pyBawt.py
executable file
·89 lines (76 loc) · 2.17 KB
/
pyBawt.py
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
#!/usr/bin/env python
##
# Insert License
##
# Rich Healey '08
import ircSocket
import time
import sys
import os
import random
import config
import bModules
import logging
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('nickserv', 'b@dpass')
net._debug = True
# Before we hit mainloop, write pidfile
try:
fh = open('/tmp/pyBawt.pid', 'w')
fh.write(str(os.getpid()))
fh.close()
except IOError:
print "Couldn't write pidfile"
exit()
try:
for i in config.channels:
net.join(i)
while True:
try:
net.recv_wait()
except ircSocket.FlushQueue:
pass
net.dump_queue()
except KeyboardInterrupt:
logging.error("Shutting down due to user intervention")
net.quit("Killed from terminal")
except bModules.Restart:
# TODO Include the user who did this
logging.error("Restarting due to user intervention")
restart_stub()
except ircSocket.IrcDisconnected:
if ircSocket.should_reconnect():
restart_stub()
except ircSocket.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")
logging.error(sys.exc_info())
restart_stub()