bitbot-3.11-fork/src/IRCUser.py

81 lines
3.3 KiB
Python
Raw Normal View History

import typing, uuid
from src import IRCBot, IRCChannel, IRCBuffer, IRCObject, IRCServer, utils
2016-03-29 11:56:58 +00:00
class User(IRCObject.Object):
def __init__(self, nickname: str, id: int, server: "IRCServer.Server",
bot: "IRCBot.Bot"):
self.name = ""
self.nickname = ""
self.nickname_lower = ""
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
2016-03-29 11:56:58 +00:00
self.username = None
self.hostname = None
2017-07-16 20:18:58 +00:00
self.realname = None
2016-03-29 11:56:58 +00:00
self.bot = bot
self.channels = set([]) # type: typing.Set[IRCChannel.Channel]
self.identified_account = None
self.identified_account_override = None
self.identified_account_id = None
self.identified_account_id_override = None
2018-09-04 09:15:20 +00:00
self.away = False
self.away_message = None # type: typing.Optional[str]
self.buffer = IRCBuffer.Buffer(bot, server)
def __repr__(self) -> str:
return "IRCUser.User(%s|%s)" % (self.server.name, self.name)
def __str__(self) -> str:
return self.nickname
def hostmask(self) -> str:
return "%s!%s@%s" % (self.nickname, self.username, self.hostname)
def get_id(self)-> int:
return (self.identified_account_id_override or
self.identified_account_id or self._id)
def get_identified_account(self) -> typing.Optional[str]:
return (self.identified_account_override or self.identified_account)
def set_nickname(self, nickname: str):
2016-03-29 11:56:58 +00:00
self.nickname = nickname
self.nickname_lower = self.server.irc_lower(nickname)
self.name = self.nickname_lower
def join_channel(self, channel: "IRCChannel.Channel"):
2016-03-29 11:56:58 +00:00
self.channels.add(channel)
def part_channel(self, channel: "IRCChannel.Channel"):
2016-03-29 11:56:58 +00:00
self.channels.remove(channel)
def set_setting(self, setting: str, value: typing.Any):
self.bot.database.user_settings.set(self.get_id(), setting, value)
def get_setting(self, setting: str, default: typing.Any=None) -> typing.Any:
return self.bot.database.user_settings.get(self.get_id(), setting,
default)
def find_settings(self, pattern: str, default: typing.Any=[]
) -> typing.List[typing.Any]:
return self.bot.database.user_settings.find(self.get_id(), pattern,
default)
def find_settings_prefix(self, prefix: str, default: typing.Any=[]
) -> typing.List[typing.Any]:
return self.bot.database.user_settings.find_prefix(self.get_id(),
prefix, default)
2016-03-29 11:56:58 +00:00
def del_setting(self, setting):
self.bot.database.user_settings.delete(self.get_id(), setting)
def get_channel_settings_per_setting(self, setting: str,
default: typing.Any=[]) -> typing.List[typing.Any]:
return self.bot.database.user_channel_settings.find_by_setting(
self.get_id(), setting, default)
2016-03-29 11:56:58 +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)
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))
def send_tagmsg(self, tags: dict):
self.server.send_tagmsg(self.nickname, tags)