2018-10-30 14:58:48 +00:00
|
|
|
import typing, uuid
|
|
|
|
from src import IRCBot, IRCChannel, IRCBuffer, IRCObject, IRCServer, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-01 12:48:55 +00:00
|
|
|
class User(IRCObject.Object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, nickname: str, id: int, server: "IRCServer.Server",
|
|
|
|
bot: "IRCBot.Bot"):
|
2018-11-05 18:37:15 +00:00
|
|
|
self.name = ""
|
|
|
|
self.nickname = ""
|
|
|
|
self.nickname_lower = ""
|
|
|
|
|
2018-09-11 09:15:16 +00:00
|
|
|
self.server = server
|
2016-03-29 11:56:58 +00:00
|
|
|
self.set_nickname(nickname)
|
2018-09-05 12:00:04 +00:00
|
|
|
self._id = id
|
2019-11-21 15:49:08 +00:00
|
|
|
self._id_override: typing.Optional[int] = None
|
2019-10-25 22:32:00 +00:00
|
|
|
self.username: typing.Optional[str] = None
|
|
|
|
self.hostname: typing.Optional[str] = None
|
|
|
|
self.realname: typing.Optional[str] = None
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2019-10-25 22:32:00 +00:00
|
|
|
self.channels: typing.Set[IRCChannel.Channel] = set([])
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
self.account = None
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2018-09-04 09:15:20 +00:00
|
|
|
self.away = False
|
2019-10-25 22:32:00 +00:00
|
|
|
self.away_message: typing.Optional[str] = None
|
2019-02-22 22:26:50 +00:00
|
|
|
|
2018-09-11 09:15:16 +00:00
|
|
|
self.buffer = IRCBuffer.Buffer(bot, server)
|
2018-08-28 11:23:57 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def __repr__(self) -> str:
|
2018-08-28 11:23:57 +00:00
|
|
|
return "IRCUser.User(%s|%s)" % (self.server.name, self.name)
|
2018-10-30 14:58:48 +00:00
|
|
|
def __str__(self) -> str:
|
2018-10-01 12:48:55 +00:00
|
|
|
return self.nickname
|
2018-08-28 11:23:57 +00:00
|
|
|
|
2019-10-25 22:32:00 +00:00
|
|
|
def hostmask(self) -> typing.Optional[str]:
|
2019-09-27 15:21:59 +00:00
|
|
|
if self.nickname and self.username and self.hostname:
|
|
|
|
return "%s!%s@%s" % (self.nickname, self.username, self.hostname)
|
|
|
|
return None
|
2019-11-11 12:49:45 +00:00
|
|
|
def userhost(self) -> typing.Optional[str]:
|
|
|
|
if self.username and self.hostname:
|
|
|
|
return "%s@%s" % (self.username, self.hostname)
|
|
|
|
return None
|
2019-03-12 10:39:54 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_id(self)-> int:
|
2019-11-21 15:49:08 +00:00
|
|
|
return self._id_override or self._id
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_nickname(self, nickname: str):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.nickname = nickname
|
2019-01-24 17:12:50 +00:00
|
|
|
self.nickname_lower = self.server.irc_lower(nickname)
|
2018-11-23 13:55:40 +00:00
|
|
|
self.name = self.nickname_lower
|
2018-10-30 14:58:48 +00:00
|
|
|
def join_channel(self, channel: "IRCChannel.Channel"):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.channels.add(channel)
|
2018-10-30 14:58:48 +00:00
|
|
|
def part_channel(self, channel: "IRCChannel.Channel"):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.channels.remove(channel)
|
2018-10-30 14:58:48 +00:00
|
|
|
|
|
|
|
def set_setting(self, setting: str, value: typing.Any):
|
2018-09-05 11:58:12 +00:00
|
|
|
self.bot.database.user_settings.set(self.get_id(), setting, value)
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_setting(self, setting: str, default: typing.Any=None) -> typing.Any:
|
2018-09-05 11:58:12 +00:00
|
|
|
return self.bot.database.user_settings.get(self.get_id(), setting,
|
2018-08-18 23:19:53 +00:00
|
|
|
default)
|
2019-10-07 11:46:52 +00:00
|
|
|
|
|
|
|
def find_setting(self, pattern: str=None, prefix: str=None,
|
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
|
|
|
if not pattern == None:
|
|
|
|
return self.bot.database.user_settings.find(self.get_id(), pattern,
|
|
|
|
default)
|
|
|
|
elif not prefix == None:
|
|
|
|
return self.bot.database.user_settings.find_prefix(self.get_id(),
|
|
|
|
prefix, default)
|
|
|
|
else:
|
|
|
|
raise ValueError("Please provide 'pattern' or 'prefix'")
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def del_setting(self, setting):
|
2018-09-05 11:58:12 +00:00
|
|
|
self.bot.database.user_settings.delete(self.get_id(), setting)
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_channel_settings_per_setting(self, setting: str,
|
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.user_channel_settings.find_by_setting(
|
2018-09-05 11:58:12 +00:00
|
|
|
self.get_id(), setting, default)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-02-18 14:56:25 +00:00
|
|
|
def send_message(self, message: str, tags: dict={}):
|
|
|
|
self.server.send_message(self.nickname, message, tags=tags)
|
|
|
|
def send_notice(self, text: str, tags: dict={}):
|
|
|
|
self.server.send_notice(self.nickname, text, tags=tags)
|
2018-10-30 14:58:48 +00:00
|
|
|
def send_ctcp_response(self, command: str, args: str):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.send_notice("\x01%s %s\x01" % (command, args))
|
2019-02-22 22:34:54 +00:00
|
|
|
def send_tagmsg(self, tags: dict):
|
|
|
|
self.server.send_tagmsg(self.nickname, tags)
|