2018-08-03 13:29:37 +00:00
|
|
|
#--ignore
|
2016-04-13 13:55:38 +00:00
|
|
|
import types, json
|
2018-09-26 17:27:17 +00:00
|
|
|
from src import Utils
|
2016-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
def get_target(user):
|
|
|
|
return user.alias or user.nickname
|
|
|
|
def set_setting(user, setting, value):
|
|
|
|
target = get_target(user)
|
|
|
|
user.bot.database.set_user_setting(user.server.id, target,
|
|
|
|
setting, value)
|
|
|
|
def get_setting(user, setting, default=None):
|
|
|
|
target = get_target(user)
|
|
|
|
return user.bot.database.get_user_setting(user.server.id,
|
|
|
|
target, setting, default)
|
|
|
|
def find_settings(user, pattern, default=[]):
|
|
|
|
target = get_target(user)
|
|
|
|
return user.bot.databse.find_user_settings(user.server.id,
|
|
|
|
target, pattern, default)
|
|
|
|
def del_setting(user, setting):
|
|
|
|
target = get_target(user)
|
|
|
|
user.bot.database.del_user_setting(user.server.id, target,
|
|
|
|
setting)
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
_name = "Aliases"
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-04-13 13:55:38 +00:00
|
|
|
self.bot = bot
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("new.user")
|
2016-04-13 13:55:38 +00:00
|
|
|
def new_user(self, event):
|
|
|
|
method_type = types.MethodType
|
|
|
|
user = event["user"]
|
|
|
|
event["user"].alias = user.get_setting("alias")
|
2016-05-17 15:16:49 +00:00
|
|
|
if not event["user"].alias:
|
|
|
|
event["user"].set_setting("root-alias", True)
|
2016-04-13 13:55:38 +00:00
|
|
|
event["user"].set_setting = method_type(set_setting, user)
|
|
|
|
event["user"].get_setting = method_type(get_setting, user)
|
|
|
|
event["user"].find_settings = method_type(find_settings, user)
|
|
|
|
event["user"].del_setting = method_type(del_setting, user)
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.nick")
|
2016-04-13 13:55:38 +00:00
|
|
|
def nickname_change(self, event):
|
|
|
|
old_nickname = event["old_nickname"]
|
|
|
|
new_nickname = event["new_nickname"]
|
|
|
|
if not event["user"].alias:
|
2016-05-17 15:16:49 +00:00
|
|
|
root_alias = event["user"].get_setting("root-alias", False)
|
|
|
|
if not root_alias:
|
|
|
|
event["user"].set_setting("alias", old_nickname.lower())
|
|
|
|
event["user"].alias = old_nickname.lower()
|
|
|
|
else:
|
|
|
|
event["user"].alias = None
|
2016-04-13 13:55:38 +00:00
|
|
|
elif event["user"].nickname_lower == event["user"].alias:
|
|
|
|
event["user"].alias = None
|
|
|
|
|
|
|
|
def _get_aliases(self, target, server):
|
|
|
|
return self.bot.database.cursor().execute("""SELECT nickname
|
|
|
|
FROM user_settings WHERE setting='alias' AND value=?
|
|
|
|
AND server_id=?""", [json.dumps(target.lower()),
|
|
|
|
server.id]).fetchall()
|
2016-04-18 15:49:21 +00:00
|
|
|
def _change_nick(self, old_nickname, new_nickname):
|
|
|
|
self.bot.database.cursor().execute("""UPDATE user_settings
|
|
|
|
SET nickname=? WHERE nickname=?""", [new_nickname.lower(),
|
|
|
|
old_nickname.lower()])
|
2016-04-13 13:55:38 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.alias")
|
2016-04-13 13:55:38 +00:00
|
|
|
def alias(self, event):
|
|
|
|
if event["args"]:
|
|
|
|
target = event["args_split"][0]
|
|
|
|
else:
|
|
|
|
target = event["user"].nickname
|
|
|
|
temp_user = event["server"].get_user(target)
|
|
|
|
if temp_user.get_setting("alias"):
|
|
|
|
target = temp_user.get_setting("alias")
|
|
|
|
aliases = self._get_aliases(target, event["server"])
|
|
|
|
if any(aliases):
|
|
|
|
event["stdout"].write("Aliases for %s: %s" % (target,
|
|
|
|
", ".join([a[0] for a in aliases])))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("%s has no aliases" % target)
|
|
|
|
|
|
|
|
def main_alias(self, event):
|
|
|
|
if event["user"].alias:
|
|
|
|
aliases = self._get_aliases(event["user"].alias, event["server"])
|
|
|
|
new_alias = event["user"].nickname_lower()
|
|
|
|
event["user"].del_setting("alias")
|
|
|
|
if any(aliases):
|
|
|
|
for nickname in aliases:
|
|
|
|
event["server"].get_user(nickname).set_setting(
|
|
|
|
"alias", alias)
|
2016-04-18 15:49:21 +00:00
|
|
|
self._change_nick()
|
2016-04-13 13:55:38 +00:00
|
|
|
event["stdout"].write("This nickname has been set as the "
|
|
|
|
"main alias for it's group of aliases")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("This nickname is already a main alias")
|