Tweaked and tidied
This commit is contained in:
parent
e6318fe725
commit
eb4620e1d6
3 changed files with 67 additions and 70 deletions
|
@ -8,7 +8,7 @@ import signal
|
|||
# set up enviroment
|
||||
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
|
||||
|
||||
print 'CloudBot REFRESH <http://git.io/cloudbotirc>'
|
||||
print 'CloudBot2 <http://git.io/cloudbotirc>'
|
||||
|
||||
def exit_gracefully(signum, frame):
|
||||
cloudbot.stop()
|
||||
|
@ -23,5 +23,3 @@ cloudbot.logger.debug("Bot initalized, starting main loop.")
|
|||
|
||||
while cloudbot.running:
|
||||
cloudbot.loop()
|
||||
|
||||
cloudbot.logger.debug("Stopped main loop.")
|
||||
|
|
211
core/bot.py
211
core/bot.py
|
@ -13,112 +13,7 @@ def clean_name(n):
|
|||
"""strip all spaces and capitalization"""
|
||||
return re.sub('[^A-Za-z0-9_]+', '', n.replace(" ", "_"))
|
||||
|
||||
|
||||
class Bot(object):
|
||||
def __init__(self):
|
||||
# basic variables
|
||||
self.start_time = time.time()
|
||||
self.running = True
|
||||
|
||||
# set up config and logging
|
||||
self.setup()
|
||||
self.logger.debug("Bot setup completed.")
|
||||
|
||||
# start IRC connections
|
||||
self.connections = {}
|
||||
self.connect()
|
||||
|
||||
# run plugin loader
|
||||
self.plugins = collections.defaultdict(list)
|
||||
self.threads = {}
|
||||
self.loader = loader.PluginLoader(self)
|
||||
|
||||
|
||||
def stop(self, reason=None):
|
||||
"""quits all networks and shuts the bot down"""
|
||||
self.logger.info("Stopping bot.")
|
||||
self.running = False
|
||||
|
||||
# wait for the bot loop to stop
|
||||
time.sleep(1)
|
||||
self.config.observer.stop()
|
||||
self.logger.debug("Stopping config reloader.")
|
||||
|
||||
self.loader.stop()
|
||||
self.logger.debug("Stopping plugin loader.")
|
||||
|
||||
for name, connection in self.connections.iteritems():
|
||||
# TODO: end connections properly
|
||||
self.logger.debug("({}) Closing connection.".format(name))
|
||||
|
||||
if reason:
|
||||
connection.cmd("QUIT", [reason])
|
||||
else:
|
||||
connection.cmd("QUIT")
|
||||
|
||||
connection.stop()
|
||||
|
||||
self.logger.debug("Logging engine stopped")
|
||||
logging.shutdown()
|
||||
sys.exit()
|
||||
|
||||
|
||||
def loop(self):
|
||||
"""recieves input from the IRC engine and processes it"""
|
||||
|
||||
for conn in self.connections.itervalues():
|
||||
try:
|
||||
incoming = conn.parsed_queue.get_nowait()
|
||||
main.main(self, conn, incoming)
|
||||
except Queue.Empty:
|
||||
pass
|
||||
|
||||
# if no messages are in the incoming queue, sleep
|
||||
while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()):
|
||||
time.sleep(.1)
|
||||
|
||||
|
||||
def connect(self):
|
||||
"""connect to all the networks defined in the bot config"""
|
||||
for conf in self.config['connections']:
|
||||
# strip all spaces and capitalization from the connection name
|
||||
name = clean_name(conf['name'])
|
||||
nick = conf['nick']
|
||||
server = conf['connection']['server']
|
||||
port = conf['connection'].get('port', 6667)
|
||||
|
||||
self.logger.debug("({}) Creating connection to {}.".format(name, server))
|
||||
|
||||
if conf['connection'].get('ssl'):
|
||||
self.connections[name] = irc.SSLIRC(name, server, nick, conf = conf,
|
||||
port = port, channels = conf['channels'],
|
||||
ignore_certificate_errors=conf['connection'].get('ignore_cert', True))
|
||||
self.logger.debug("({}) Created SSL connection.".format(name))
|
||||
else:
|
||||
self.connections[name] = irc.IRC(name, server, nick, conf = conf,
|
||||
port = port, channels = conf['channels'])
|
||||
self.logger.debug("({}) Created connection.".format(name))
|
||||
|
||||
|
||||
def setup(self):
|
||||
"""create the logger and config objects"""
|
||||
# logging
|
||||
self.logger = self.new_logger()
|
||||
self.logger.debug("Logging engine started.")
|
||||
|
||||
# data folder
|
||||
self.data_dir = os.path.abspath('data')
|
||||
if not os.path.exists(self.data_dir):
|
||||
self.logger.debug("Data folder not found, creating.")
|
||||
os.mkdir(self.data_dir)
|
||||
self.logger.debug("Created data folder.")
|
||||
|
||||
# config
|
||||
self.config = config.Config(self.logger)
|
||||
self.logger.debug("Config object created.")
|
||||
|
||||
|
||||
def new_logger(self):
|
||||
def get_logger(self):
|
||||
"""create and return a new logger object"""
|
||||
# create logger
|
||||
logger = logging.getLogger("cloudbot")
|
||||
|
@ -143,3 +38,107 @@ class Bot(object):
|
|||
logger.addHandler(fh)
|
||||
logger.addHandler(sh)
|
||||
return logger
|
||||
|
||||
|
||||
class Bot(object):
|
||||
def __init__(self):
|
||||
# basic variables
|
||||
self.start_time = time.time()
|
||||
self.running = True
|
||||
|
||||
# set up config and logging
|
||||
self.setup()
|
||||
self.logger.debug("Bot setup completed.")
|
||||
|
||||
# start IRC connections
|
||||
self.connections = {}
|
||||
self.connect()
|
||||
|
||||
# run plugin loader
|
||||
self.plugins = collections.defaultdict(list)
|
||||
self.threads = {}
|
||||
self.loader = loader.PluginLoader(self)
|
||||
|
||||
|
||||
def loop(self):
|
||||
"""recieves input from the IRC engine and processes it"""
|
||||
|
||||
for conn in self.connections.itervalues():
|
||||
try:
|
||||
incoming = conn.parsed_queue.get_nowait()
|
||||
main.main(self, conn, incoming)
|
||||
except Queue.Empty:
|
||||
pass
|
||||
|
||||
# if no messages are in the incoming queue, sleep
|
||||
while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()):
|
||||
time.sleep(.1)
|
||||
|
||||
|
||||
def setup(self):
|
||||
"""create the logger and config objects"""
|
||||
# logging
|
||||
self.logger = get_logger()
|
||||
self.logger.debug("Logging engine started.")
|
||||
|
||||
# data folder
|
||||
self.data_dir = os.path.abspath('data')
|
||||
if not os.path.exists(self.data_dir):
|
||||
self.logger.debug("Data folder not found, creating.")
|
||||
os.mkdir(self.data_dir)
|
||||
self.logger.debug("Created data folder.")
|
||||
|
||||
# config
|
||||
self.config = config.Config(self.logger)
|
||||
self.logger.debug("Config object created.")
|
||||
|
||||
|
||||
def connect(self):
|
||||
"""connect to all the networks defined in the bot config"""
|
||||
for conf in self.config['connections']:
|
||||
# strip all spaces and capitalization from the connection name
|
||||
name = clean_name(conf['name'])
|
||||
nick = conf['nick']
|
||||
server = conf['connection']['server']
|
||||
port = conf['connection'].get('port', 6667)
|
||||
|
||||
self.logger.debug("({}) Creating connection to {}.".format(name, server))
|
||||
|
||||
if conf['connection'].get('ssl'):
|
||||
self.connections[name] = irc.SSLIRC(name, server, nick, conf = conf,
|
||||
port = port, channels = conf['channels'],
|
||||
ignore_certificate_errors=conf['connection'].get('ignore_cert', True))
|
||||
self.logger.debug("({}) Created SSL connection.".format(name))
|
||||
else:
|
||||
self.connections[name] = irc.IRC(name, server, nick, conf = conf,
|
||||
port = port, channels = conf['channels'])
|
||||
self.logger.debug("({}) Created connection.".format(name))
|
||||
|
||||
|
||||
def stop(self, reason=None):
|
||||
"""quits all networks and shuts the bot down"""
|
||||
self.logger.info("Stopping bot.")
|
||||
self.running = False
|
||||
|
||||
# wait for the bot loop to stop
|
||||
time.sleep(1)
|
||||
self.config.observer.stop()
|
||||
self.logger.debug("Stopping config reloader.")
|
||||
|
||||
self.loader.stop()
|
||||
self.logger.debug("Stopping plugin loader.")
|
||||
|
||||
for name, connection in self.connections.iteritems():
|
||||
self.logger.debug("({}) Closing connection.".format(name))
|
||||
|
||||
if reason:
|
||||
connection.cmd("QUIT", [reason])
|
||||
else:
|
||||
connection.cmd("QUIT")
|
||||
|
||||
connection.stop()
|
||||
|
||||
self.logger.debug("Logging engine stopped")
|
||||
logging.shutdown()
|
||||
sys.exit()
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@ class SendThread(threading.Thread):
|
|||
def run(self):
|
||||
while not self.shutdown:
|
||||
line = self.output_queue.get().splitlines()[0][:500]
|
||||
print u"[{}]> {}".format(self.conn_name.upper(), line)
|
||||
print u"[{}]: {}".format(self.conn_name, line)
|
||||
self.output_buffer += line.encode('utf-8', 'replace') + '\r\n'
|
||||
while self.output_buffer:
|
||||
sent = self.socket.send(self.output_buffer)
|
||||
|
|
Reference in a new issue