Change all instances of stdout.write+return to raise utils.EventError
in
modules
This commit is contained in:
parent
6c6d593e6d
commit
773d11f6cb
14 changed files with 45 additions and 75 deletions
|
@ -51,18 +51,16 @@ class Module(ModuleManager.BaseModule):
|
||||||
"""
|
"""
|
||||||
id = event["args_split"][0]
|
id = event["args_split"][0]
|
||||||
if not id.isdigit():
|
if not id.isdigit():
|
||||||
event["stderr"].write("Please provide a numeric server ID")
|
raise utils.EventError("Please provide a numeric server ID")
|
||||||
return
|
|
||||||
|
|
||||||
id = int(id)
|
id = int(id)
|
||||||
if not self.bot.database.servers.get(id):
|
if not self.bot.database.servers.get(id):
|
||||||
event["stderr"].write("Unknown server ID")
|
raise utils.EventError("Unknown server ID")
|
||||||
return
|
|
||||||
|
|
||||||
existing_server = self.bot.get_server(id)
|
existing_server = self.bot.get_server(id)
|
||||||
if existing_server:
|
if existing_server:
|
||||||
event["stderr"].write("Already connected to %s" % str(
|
raise utils.EventError("Already connected to %s" % str(
|
||||||
existing_server))
|
existing_server))
|
||||||
return
|
|
||||||
server = self.bot.add_server(id)
|
server = self.bot.add_server(id)
|
||||||
event["stdout"].write("Connecting to %s" % str(server))
|
event["stdout"].write("Connecting to %s" % str(server))
|
||||||
|
|
|
@ -58,9 +58,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
if len(definition):
|
if len(definition):
|
||||||
definition = definition[0]
|
definition = definition[0]
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Try again in a couple of "
|
raise utils.EventError("Try again in a couple of seconds")
|
||||||
"seconds")
|
|
||||||
return
|
|
||||||
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
||||||
page["word"], definition["text"]))
|
page["word"], definition["text"]))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -15,12 +15,10 @@ class Module(ModuleManager.BaseModule):
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
if len(roll) is not 2:
|
if len(roll) is not 2:
|
||||||
event["stderr"].write(ERROR_FORMAT)
|
raise utils.EventError(ERROR_FORMAT)
|
||||||
return
|
|
||||||
|
|
||||||
if roll[0].isdigit() is False or roll[1].isdigit() is False:
|
if roll[0].isdigit() is False or roll[1].isdigit() is False:
|
||||||
event["stderr"].write(ERROR_FORMAT)
|
raise utils.EventError(ERROR_FORMAT)
|
||||||
return
|
|
||||||
|
|
||||||
roll = [int(roll[0]), int(roll[1])]
|
roll = [int(roll[0]), int(roll[1])]
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
method="POST",
|
method="POST",
|
||||||
soup=True)
|
soup=True)
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
event["stderr"].write("%s: eval timed out" %
|
raise utils.EventError("%s: eval timed out" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
if page:
|
if page:
|
||||||
textareas = page.find_all("textarea")
|
textareas = page.find_all("textarea")
|
||||||
|
|
|
@ -33,9 +33,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
page = utils.http.get_url(EVAL_URL, json_data=args,
|
page = utils.http.get_url(EVAL_URL, json_data=args,
|
||||||
method="POST", json=True)
|
method="POST", json=True)
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
event["stderr"].write("%s: eval timed out" %
|
raise utils.EventError("%s: eval timed out" %
|
||||||
event["user"].nickname)
|
event["user"].nickname)
|
||||||
return
|
|
||||||
|
|
||||||
err_or_out = "stdout" if page["success"] else "stderr"
|
err_or_out = "stdout" if page["success"] else "stderr"
|
||||||
event[err_or_out].write("%s: %s" % (event["user"].nickname,
|
event[err_or_out].write("%s: %s" % (event["user"].nickname,
|
||||||
|
|
|
@ -20,8 +20,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
address_info = socket.getaddrinfo(hostname, 1, 0,
|
address_info = socket.getaddrinfo(hostname, 1, 0,
|
||||||
socket.SOCK_DGRAM)
|
socket.SOCK_DGRAM)
|
||||||
except socket.gaierror:
|
except socket.gaierror:
|
||||||
event["stderr"].write("Failed to find hostname")
|
raise utils.EventError("Failed to find hostname")
|
||||||
return
|
|
||||||
ips = []
|
ips = []
|
||||||
for _, _, _, _, address in address_info:
|
for _, _, _, _, address in address_info:
|
||||||
ips.append(address[0])
|
ips.append(address[0])
|
||||||
|
@ -68,13 +68,10 @@ class Module(ModuleManager.BaseModule):
|
||||||
match = REGEX_IP.search(line.message)
|
match = REGEX_IP.search(line.message)
|
||||||
ip = match.group(1) or match.group(2)
|
ip = match.group(1) or match.group(2)
|
||||||
if not ip:
|
if not ip:
|
||||||
event["stderr"].write("No IP provided")
|
raise utils.EventError("No IP provided")
|
||||||
return
|
|
||||||
|
|
||||||
print(ip)
|
|
||||||
try:
|
try:
|
||||||
hostname, alias, ips = socket.gethostbyaddr(ip)
|
hostname, alias, ips = socket.gethostbyaddr(ip)
|
||||||
except (socket.herror, socket.gaierror) as e:
|
except (socket.herror, socket.gaierror) as e:
|
||||||
event["stderr"].write(e.strerror)
|
raise utils.EventError(e.strerror)
|
||||||
return
|
|
||||||
event["stdout"].write("(%s) %s" % (ips[0], hostname))
|
event["stdout"].write("(%s) %s" % (ips[0], hostname))
|
||||||
|
|
|
@ -10,8 +10,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
"""
|
"""
|
||||||
name = event["args_split"][0].lower()
|
name = event["args_split"][0].lower()
|
||||||
if name in self.bot.modules.modules:
|
if name in self.bot.modules.modules:
|
||||||
event["stderr"].write("Module '%s' is already loaded" % name)
|
raise utils.EventError("Module '%s' is already loaded" % name)
|
||||||
return
|
|
||||||
self.bot.modules.load_module(self.bot, name)
|
self.bot.modules.load_module(self.bot, name)
|
||||||
event["stdout"].write("Loaded '%s'" % name)
|
event["stdout"].write("Loaded '%s'" % name)
|
||||||
|
|
||||||
|
@ -24,8 +23,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
"""
|
"""
|
||||||
name = event["args_split"][0].lower()
|
name = event["args_split"][0].lower()
|
||||||
if not name in self.bot.modules.modules:
|
if not name in self.bot.modules.modules:
|
||||||
event["stderr"].write("Module '%s' isn't loaded" % name)
|
raise utils.EventError("Module '%s' isn't loaded" % name)
|
||||||
return
|
|
||||||
self.bot.modules.unload_module(name)
|
self.bot.modules.unload_module(name)
|
||||||
event["stdout"].write("Unloaded '%s'" % name)
|
event["stdout"].write("Unloaded '%s'" % name)
|
||||||
|
|
||||||
|
@ -44,16 +42,13 @@ class Module(ModuleManager.BaseModule):
|
||||||
try:
|
try:
|
||||||
self._reload(name)
|
self._reload(name)
|
||||||
except ModuleManager.ModuleNotFoundException:
|
except ModuleManager.ModuleNotFoundException:
|
||||||
event["stderr"].write("Module '%s' isn't loaded" % name)
|
raise utils.EventError("Module '%s' isn't loaded" % name)
|
||||||
return
|
|
||||||
except ModuleManager.ModuleWarning as warning:
|
except ModuleManager.ModuleWarning as warning:
|
||||||
event["stderr"].write("Module '%s' not loaded: %s" % (
|
raise utils.EventError("Module '%s' not loaded: %s" % (
|
||||||
name, str(warning)))
|
name, str(warning)))
|
||||||
return
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
event["stderr"].write("Failed to reload module '%s': %s" % (
|
raise utils.EventError("Failed to reload module '%s': %s" % (
|
||||||
name, str(e)))
|
name, str(e)))
|
||||||
return
|
|
||||||
event["stdout"].write("Reloaded '%s'" % name)
|
event["stdout"].write("Reloaded '%s'" % name)
|
||||||
|
|
||||||
@utils.hook("received.command.reloadallmodules")
|
@utils.hook("received.command.reloadallmodules")
|
||||||
|
@ -92,8 +87,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
name = event["args_split"][0].lower()
|
name = event["args_split"][0].lower()
|
||||||
blacklist = self.bot.get_setting("module-blacklist", [])
|
blacklist = self.bot.get_setting("module-blacklist", [])
|
||||||
if not name in blacklist:
|
if not name in blacklist:
|
||||||
event["stderr"].write("Module '%s' isn't disabled" % name)
|
raise utils.EventError("Module '%s' isn't disabled" % name)
|
||||||
return
|
|
||||||
|
|
||||||
blacklist.remove(name)
|
blacklist.remove(name)
|
||||||
event["stdout"].write("Module '%s' has been enabled and can now "
|
event["stdout"].write("Module '%s' has been enabled and can now "
|
||||||
|
@ -114,8 +108,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
blacklist = self.bot.get_setting("module-blacklist", [])
|
blacklist = self.bot.get_setting("module-blacklist", [])
|
||||||
if name in blacklist:
|
if name in blacklist:
|
||||||
event["stderr"].write("Module '%s' is already disabled" % name)
|
raise utils.EventError("Module '%s' is already disabled" % name)
|
||||||
return
|
|
||||||
|
|
||||||
blacklist.append(name)
|
blacklist.append(name)
|
||||||
self.bot.set_setting("module-blacklist", blacklist)
|
self.bot.set_setting("module-blacklist", blacklist)
|
||||||
|
|
|
@ -149,10 +149,11 @@ class Module(ModuleManager.BaseModule):
|
||||||
})
|
})
|
||||||
|
|
||||||
if filter["errors"]:
|
if filter["errors"]:
|
||||||
return event["stderr"].write("Filter: " + filter["errors_summary"])
|
raise utils.EventError("Filter: " + filter["errors_summary"])
|
||||||
|
|
||||||
if filter["inter"] and filter["type"]!="departure":
|
if filter["inter"] and filter["type"]!="departure":
|
||||||
return event["stderr"].write("Filtering by intermediate stations is only supported for departures.")
|
raise utils.EventError("Filtering by intermediate stations is only "
|
||||||
|
"supported for departures.")
|
||||||
|
|
||||||
nr_filterlist = client.factory.create("filterList")
|
nr_filterlist = client.factory.create("filterList")
|
||||||
if filter["inter"]: nr_filterlist.crs.append(filter["inter"])
|
if filter["inter"]: nr_filterlist.crs.append(filter["inter"])
|
||||||
|
@ -171,9 +172,9 @@ class Module(ModuleManager.BaseModule):
|
||||||
nr_filterlist, "to", '', "PBS", filter["nonpassenger"])
|
nr_filterlist, "to", '', "PBS", filter["nonpassenger"])
|
||||||
except WebFault as detail:
|
except WebFault as detail:
|
||||||
if str(detail) == "Server raised fault: 'Invalid crs code supplied'":
|
if str(detail) == "Server raised fault: 'Invalid crs code supplied'":
|
||||||
return event["stderr"].write("Invalid CRS code.")
|
raise utils.EventError("Invalid CRS code.")
|
||||||
else:
|
else:
|
||||||
return event["stderr"].write("An error occurred.")
|
raise utils.EventError("An error occurred.")
|
||||||
|
|
||||||
nrcc_severe = len([a for a in query["nrccMessages"][0] if a["severity"] == "Major"]) if "nrccMessages" in query else 0
|
nrcc_severe = len([a for a in query["nrccMessages"][0] if a["severity"] == "Major"]) if "nrccMessages" in query else 0
|
||||||
if event.get("external"):
|
if event.get("external"):
|
||||||
|
@ -327,8 +328,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
})
|
})
|
||||||
|
|
||||||
if filter["errors"]:
|
if filter["errors"]:
|
||||||
event["stderr"].write("Filter: " + filter["errors_summary"])
|
raise utils.EventError("Filter: " + filter["errors_summary"])
|
||||||
return
|
|
||||||
|
|
||||||
rid = service_id
|
rid = service_id
|
||||||
if len(service_id) <= 8:
|
if len(service_id) <= 8:
|
||||||
|
@ -508,7 +508,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
datetime.utcnow().time().strftime("%H:%M:%S+0000"))
|
datetime.utcnow().time().strftime("%H:%M:%S+0000"))
|
||||||
|
|
||||||
if not query:
|
if not query:
|
||||||
return event["stderr"].write("No currently running services match this identifier")
|
raise utils.EventError("No currently running services match this "
|
||||||
|
"identifier")
|
||||||
|
|
||||||
services = query["serviceList"][0]
|
services = query["serviceList"][0]
|
||||||
if event.get("external"):
|
if event.get("external"):
|
||||||
|
@ -527,7 +528,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
client = self.client
|
client = self.client
|
||||||
|
|
||||||
if not event["args"].isnumeric():
|
if not event["args"].isnumeric():
|
||||||
return event["stderr"].write("The delay/cancellation code must be a number")
|
raise utils.EventError("The delay/cancellation code must be a "
|
||||||
|
"number")
|
||||||
reasons = {a["code"]:(a["lateReason"], a["cancReason"]) for a in client.service.GetReasonCodeList()[0]}
|
reasons = {a["code"]:(a["lateReason"], a["cancReason"]) for a in client.service.GetReasonCodeList()[0]}
|
||||||
if event["args"] in reasons:
|
if event["args"] in reasons:
|
||||||
event["stdout"].write("%s: %s" % (event["args"], " / ".join(reasons[event["args"]])))
|
event["stdout"].write("%s: %s" % (event["args"], " / ".join(reasons[event["args"]])))
|
||||||
|
|
|
@ -47,14 +47,12 @@ class Module(ModuleManager.BaseModule):
|
||||||
identity_mechanism = event["server"].get_setting("identity-mechanism",
|
identity_mechanism = event["server"].get_setting("identity-mechanism",
|
||||||
"internal")
|
"internal")
|
||||||
if not identity_mechanism == "internal":
|
if not identity_mechanism == "internal":
|
||||||
event["stderr"].write("The 'identify' command isn't available "
|
raise utils.EventError("The 'identify' command isn't available "
|
||||||
"on this network")
|
"on this network")
|
||||||
return
|
|
||||||
|
|
||||||
if not event["user"].channels:
|
if not event["user"].channels:
|
||||||
event["stderr"].write("You must share at least one channel "
|
raise utils.EventError("You must share at least one channel "
|
||||||
"with me before you can identify")
|
"with me before you can identify")
|
||||||
return
|
|
||||||
|
|
||||||
if not event["user"].identified_account_override:
|
if not event["user"].identified_account_override:
|
||||||
if len(event["args_split"]) > 1:
|
if len(event["args_split"]) > 1:
|
||||||
|
@ -91,9 +89,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
identity_mechanism = event["server"].get_setting("identity-mechanism",
|
identity_mechanism = event["server"].get_setting("identity-mechanism",
|
||||||
"internal")
|
"internal")
|
||||||
if not identity_mechanism == "internal":
|
if not identity_mechanism == "internal":
|
||||||
event["stderr"].write("The 'identify' command isn't available "
|
raise utils.EventError("The 'identify' command isn't available "
|
||||||
"on this network")
|
"on this network")
|
||||||
return
|
|
||||||
|
|
||||||
hash, salt = self._get_hash(event["server"], event["user"].nickname)
|
hash, salt = self._get_hash(event["server"], event["user"].nickname)
|
||||||
if not hash and not salt:
|
if not hash and not salt:
|
||||||
|
@ -203,8 +200,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
event["server"], event["args_split"][0])
|
event["server"], event["args_split"][0])
|
||||||
|
|
||||||
if target.get_identified_account() == None:
|
if target.get_identified_account() == None:
|
||||||
event["stderr"].write("%s isn't registered" % target.nickname)
|
raise utils.EventError("%s isn't registered" % target.nickname)
|
||||||
return
|
|
||||||
|
|
||||||
if permission in permissions:
|
if permission in permissions:
|
||||||
event["stderr"].write("%s already has permission '%s'" % (
|
event["stderr"].write("%s already has permission '%s'" % (
|
||||||
|
@ -226,8 +222,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
event["server"], event["args_split"][0])
|
event["server"], event["args_split"][0])
|
||||||
|
|
||||||
if target.identified_account == None:
|
if target.identified_account == None:
|
||||||
event["stderr"].write("%s isn't registered" % target.nickname)
|
raise utils.EventError("%s isn't registered" % target.nickname)
|
||||||
return
|
|
||||||
|
|
||||||
if permission not in permissions:
|
if permission not in permissions:
|
||||||
event["stderr"].write("%s doesn't have permission '%s'" % (
|
event["stderr"].write("%s doesn't have permission '%s'" % (
|
||||||
|
|
|
@ -33,8 +33,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
last_soundcloud.message).string
|
last_soundcloud.message).string
|
||||||
|
|
||||||
if not query and not url:
|
if not query and not url:
|
||||||
event["stderr"].write("no search phrase provided")
|
raise utils.EventError("no search phrase provided")
|
||||||
return
|
|
||||||
has_query = not query == None
|
has_query = not query == None
|
||||||
get_params = {"limit": 1,
|
get_params = {"limit": 1,
|
||||||
"client_id": self.bot.config["soundcloud-api-key"]}
|
"client_id": self.bot.config["soundcloud-api-key"]}
|
||||||
|
|
|
@ -19,12 +19,10 @@ class Module(ModuleManager.BaseModule):
|
||||||
if url:
|
if url:
|
||||||
url = re.search(REGEX_URL, url.message).group(0)
|
url = re.search(REGEX_URL, url.message).group(0)
|
||||||
if not url:
|
if not url:
|
||||||
event["stderr"].write("No URL provided/found.")
|
raise utils.EventError("No URL provided/found.")
|
||||||
return
|
|
||||||
soup = utils.http.get_url(url, soup=True)
|
soup = utils.http.get_url(url, soup=True)
|
||||||
if not soup:
|
if not soup:
|
||||||
event["stderr"].write("Failed to get URL.")
|
raise utils.EventError("Failed to get URL.")
|
||||||
return
|
|
||||||
title = soup.title
|
title = soup.title
|
||||||
if title:
|
if title:
|
||||||
title = title.text.replace("\n", " ").replace("\r", ""
|
title = title.text.replace("\n", " ").replace("\r", ""
|
||||||
|
|
|
@ -31,9 +31,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
todo = event["user"].get_setting("todo", [])
|
todo = event["user"].get_setting("todo", [])
|
||||||
for item in todo:
|
for item in todo:
|
||||||
if item.lower() == arg_lower:
|
if item.lower() == arg_lower:
|
||||||
event["stderr"].write(
|
raise utils.EventError("That is already in your todo")
|
||||||
"That is already in your todo")
|
|
||||||
return
|
|
||||||
todo.append(event["args"])
|
todo.append(event["args"])
|
||||||
event["user"].set_setting("todo", todo)
|
event["user"].set_setting("todo", todo)
|
||||||
event["stdout"].write("Saved")
|
event["stdout"].write("Saved")
|
||||||
|
@ -70,14 +68,12 @@ class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
_from, to = int(_from_str)-1, int(to_str)-1
|
_from, to = int(_from_str)-1, int(to_str)-1
|
||||||
if _from < 0 or to < 0:
|
if _from < 0 or to < 0:
|
||||||
event["stderr"].write("Both indexes must be above 0")
|
raise utils.EventError("Both indexes must be above 0")
|
||||||
return
|
|
||||||
|
|
||||||
todo = event["user"].get_setting("todo", [])
|
todo = event["user"].get_setting("todo", [])
|
||||||
if _from > len(todo) or to > len(todo):
|
if _from > len(todo) or to > len(todo):
|
||||||
event["stderr"].write("Both indexes must be less than the "
|
raise utils.EventError("Both indexes must be less than the "
|
||||||
"size of your todo list")
|
"size of your todo list")
|
||||||
return
|
|
||||||
|
|
||||||
todo.insert(to, todo.pop(_from))
|
todo.insert(to, todo.pop(_from))
|
||||||
event["user"].set_setting("todo", todo)
|
event["user"].set_setting("todo", todo)
|
||||||
|
|
|
@ -20,8 +20,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
if phrase:
|
if phrase:
|
||||||
phrase = phrase.message
|
phrase = phrase.message
|
||||||
if not phrase:
|
if not phrase:
|
||||||
event["stderr"].write("No phrase provided.")
|
raise utils.EventError("No phrase provided.")
|
||||||
return
|
|
||||||
source_language = "auto"
|
source_language = "auto"
|
||||||
target_language = "en"
|
target_language = "en"
|
||||||
|
|
||||||
|
|
|
@ -13,16 +13,14 @@ class Module(ModuleManager.BaseModule):
|
||||||
"""
|
"""
|
||||||
arg_len = len(event["args_split"][0])
|
arg_len = len(event["args_split"][0])
|
||||||
if not arg_len == 12 and not arg_len == 13:
|
if not arg_len == 12 and not arg_len == 13:
|
||||||
event["stderr"].write("Invalid UPC/EAN/GTIN provided")
|
raise utils.EventError("Invalid UPC/EAN/GTIN provided")
|
||||||
return
|
|
||||||
|
|
||||||
page = utils.http.get_url(UPCITEMDB_URL,
|
page = utils.http.get_url(UPCITEMDB_URL,
|
||||||
get_params={"upc": event["args_split"][0]},
|
get_params={"upc": event["args_split"][0]},
|
||||||
json=True)
|
json=True)
|
||||||
if page:
|
if page:
|
||||||
if not len(page["items"]):
|
if not len(page["items"]):
|
||||||
event["stderr"].write("UPC/EAN not found")
|
raise utils.EventError("UPC/EAN not found")
|
||||||
return
|
|
||||||
item = page["items"][0]
|
item = page["items"][0]
|
||||||
|
|
||||||
brand = item.get("brand", None)
|
brand = item.get("brand", None)
|
||||||
|
|
Loading…
Reference in a new issue