|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
import argparse
|
|
|
|
|
import socket
|
|
|
|
|
import yaml
|
|
|
|
|
from time import sleep
|
|
|
|
@ -9,25 +10,40 @@ class Util:
|
|
|
|
|
"""Utility functions."""
|
|
|
|
|
|
|
|
|
|
def yml(self, yml_file):
|
|
|
|
|
"Open a YAML file and return a dictionary of values."
|
|
|
|
|
fh = open(yml_file, "r")
|
|
|
|
|
data = yaml.safe_load(fh)
|
|
|
|
|
fh.close()
|
|
|
|
|
"""Open a YAML file and return a dictionary of values."""
|
|
|
|
|
try:
|
|
|
|
|
fh = open(yml_file, "r")
|
|
|
|
|
data = yaml.safe_load(fh)
|
|
|
|
|
fh.close()
|
|
|
|
|
except TypeError:
|
|
|
|
|
exit("[debug][err] Cannot load YML file. Please check it exists.")
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def rand(self, lst):
|
|
|
|
|
"""Return a random item from a given list."""
|
|
|
|
|
return lst[randint(0, len(lst)-1)]
|
|
|
|
|
|
|
|
|
|
def cli_flags(self):
|
|
|
|
|
"""Parse command line flags."""
|
|
|
|
|
self.argp = argparse.ArgumentParser()
|
|
|
|
|
self.argp.add_argument("-c", "--config", help="Config file")
|
|
|
|
|
return self.argp.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IRC:
|
|
|
|
|
"""Methods for basic IRC communication."""
|
|
|
|
|
|
|
|
|
|
def config(self, conf):
|
|
|
|
|
def config(self, def_conf):
|
|
|
|
|
"""Load runtime settings from a YAML config file, and returns a
|
|
|
|
|
dictionary of config values."""
|
|
|
|
|
dictionary of config values. Looks for the file in a runtime path or in
|
|
|
|
|
the default location."""
|
|
|
|
|
self.util = Util()
|
|
|
|
|
cfg = self.util.yml(conf)
|
|
|
|
|
# Check for runtime config locatiion
|
|
|
|
|
flags = self.util.cli_flags()
|
|
|
|
|
if flags.config != "":
|
|
|
|
|
cfg = self.util.yml(flags.config)
|
|
|
|
|
else:
|
|
|
|
|
cfg = self.util.yml(def_conf)
|
|
|
|
|
self.server = (cfg["server"]["host"], cfg["server"]["port"])
|
|
|
|
|
self.channels = cfg["channels"]
|
|
|
|
|
self.bot_nick = cfg["bot_nick"]
|
|
|
|
@ -41,6 +57,11 @@ class IRC:
|
|
|
|
|
"""A routine that connects to a server, joins channels, and attaches
|
|
|
|
|
the request listener hook to a loop."""
|
|
|
|
|
self.connect(self.server, self.bot_nick)
|
|
|
|
|
# Wait for server to reply before joining channels
|
|
|
|
|
svr_greet = self.receive()
|
|
|
|
|
while ("001 " + self.bot_nick) not in svr_greet:
|
|
|
|
|
sleep(1)
|
|
|
|
|
svr_greet = self.receive()
|
|
|
|
|
self.join_channels(self.channels)
|
|
|
|
|
while 1:
|
|
|
|
|
data = self.receive()
|
|
|
|
|