def _log_write(self, command: IrcMessage, *, channel: 'Optional[data.Channel]' = None, whisper: 'Optional[WhisperMessage]' = None, timestamp: Optional[datetime] = None) -> None: timestamp = timestamp or utils.now() if command.command == 'PASS': command = IrcMessage(command='PASS') files: List[str] = [] logs: List[str] = [] files.append(f'{bot.config.botnick}-{self.name}.log') logs.append('> ' + str(command)) file: str log: str if whisper and channel: for file, log in zip(files, logs): utils.logIrcMessage(file, log, timestamp) raise ValueError() if whisper: files.append(f'@{whisper.nick}@whisper.log') logs.append(f'{bot.config.botnick}: {whisper.message}') files.append(f'{bot.config.botnick}-All Whisper.log') logs.append( f'{bot.config.botnick} -> {whisper.nick}: {whisper.message}') files.append(f'{bot.config.botnick}-Raw Whisper.log') logs.append(f'> {command}') if channel: files.append(f'{channel.ircChannel}#full.log') logs.append(f'> {command}') if command.command == 'PRIVMSG': files.append(f'{channel.ircChannel}#msg.log') logs.append(f'{bot.config.botnick}: {command.params.trailing}') for file, log in zip(files, logs): utils.logIrcMessage(file, log, timestamp)
def irc_userstate(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: channels: Mapping[str, data.Channel] = connection.channels where: str = message.params.middle if where[0] == '#': utils.logIrcMessage(where + '#userstate.log', '< ' + str(message), timestamp) if where[0] == '#' and where[1:] in channels: userstate.parse(channels[where[1:]], message.tags)
def irc_353(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: channels: Mapping[str, data.Channel] = connection.channels where: str = message.params.middle.split()[-1] nicks: List[str] = message.params.trailing.split(' ') if where[0] == '#': utils.logIrcMessage(where + '#full.log', '< ' + str(message), timestamp) if where[1:] in channels: channels[where[1:]].ircUsers.update(nicks)
def irc_privmsg(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: channels: Mapping[str, data.Channel] = connection.channels nick: Optional[str] = message.prefix.nick where: str = message.params.middle msg: Optional[str] = message.params.trailing if where[0] == '#' and where[1:] in channels: channel.parse(channels[where[1:]], message.tags, nick, msg, timestamp) if where[0] == '#': utils.logIrcMessage(where + '#msg.log', nick + ': ' + msg, timestamp) if bot.config.botnick in msg.lower().split(): utils.logIrcMessage(bot.config.botnick + '-Mentions.log', nick + ' -> ' + where + ': ' + msg, timestamp)
def irc_notice(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: channels: Mapping[str, data.Channel] = connection.channels nick: Optional[str] = None chan: Optional[data.Channel] = None msg: Optional[str] = message.params.trailing if message.prefix.nick is not None: nick = message.prefix.nick where: Optional[str] = message.params.middle if where[0] == '#' and where[1:] in channels: chan = channels[where[1:]] if where[0] == '#': utils.logIrcMessage(where + '#notice.log', msg, timestamp) notice.parse(message.tags, chan, nick, msg)
def irc_clearchat(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: channels: Mapping[str, data.Channel] = connection.channels nick: Optional[str] = None chan: Optional[data.Channel] = None if message.params.trailing is not None: nick = message.params.trailing where: Optional[str] = message.params.middle if where[0] == '#' and where[1:] in channels: chan = channels[where[1:]] if where[0] == '#': utils.logIrcMessage(where + '#clearchat.log', nick if nick else '#chat', timestamp) clearchat.parse(chan, nick)
def compare_domains(originalUrl: str, responseUrl: str, *, chat: 'data.Channel', nick: str, timestamp: datetime) -> bool: parsedOriginal: ParseResult = urlparse(originalUrl) parsedResponse: ParseResult = urlparse(responseUrl) original: str = parsedOriginal.netloc response: str = parsedResponse.netloc if original.startswith('www.'): original = original[len('www.'):] if response.startswith('www.'): response = response[len('www.'):] if original != response: utils.logIrcMessage(f'{chat.ircChannel}#blockurl-match.log', f'{nick}: {originalUrl} -> {responseUrl}', timestamp) return True return False
async def check_domain_redirect(chat: 'data.Channel', nick: str, message: Message, timestamp: datetime) -> None: dataCache: cache.CacheStore async with cache.get_cache() as dataCache: if await dataCache.twitch_num_followers(nick): return # Record all urls with users of no follows utils.logIrcMessage(f'{chat.ircChannel}#blockurl.log', f'{nick}: {message}', timestamp) session: aiohttp.ClientSession async with aiohttp.ClientSession() as session: match: Match[str] for match in re.finditer(parser.twitchUrlRegex, str(message)): originalUrl: str = match.group(0) url: str = originalUrl if (not url.startswith('http://') and not url.startswith('https://')): url = 'http://' + url headers = {'User-Agent': 'BotGotsThis/' + bot.config.botnick} try: response: aiohttp.ClientSession async with session.get(url, headers=headers) as response: isBadRedirect: bool = compare_domains(url, str(response.url), chat=chat, nick=nick, timestamp=timestamp) if isBadRedirect: await handle_different_domains(chat, nick, message) return except aiohttp.ClientConnectorError: pass except Exception: utils.logException(str(message), timestamp)
def irc_whisper(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: tags: IrcMessageTagsReadOnly = message.tags nick: Optional[str] = message.prefix.nick msg: Optional[str] = message.params.trailing utils.logIrcMessage('@' + nick + '@whisper.log', nick + ': ' + msg, timestamp) utils.logIrcMessage(bot.config.botnick + '-All Whisper.log', nick + ' -> ' + bot.config.botnick + ': ' + msg, timestamp) utils.logIrcMessage(bot.config.botnick + '-Raw Whisper.log', '< ' + str(message), timestamp) whisper.parse(tags, nick, msg, timestamp)
def test_logIrcMessage_config_None(self, mock_now, mock_config, mock_logging): mock_now.return_value = datetime(2000, 1, 1) mock_config.ircLogFolder = None utils.logIrcMessage('botgotsthis', 'Kappa') self.assertFalse(mock_logging.log.called)
def test_logIrcMessage_config(self, mock_now, mock_config, mock_logging): mock_now.return_value = datetime(2000, 1, 1) mock_config.ircLogFolder = 'log' utils.logIrcMessage('botgotsthis', 'Kappa') mock_logging.log.assert_called_once_with(StrContains('botgotsthis'), StrContains('Kappa'))
def _log_read(self, message: str) -> None: file: str = f'{bot.config.botnick}-{self.name}.log' utils.logIrcMessage(file, '< ' + message)
def log_channel_message(message: IrcMessage, timestamp: datetime) -> None: if message.command in _logCommandPerChannel: where = message.params.middle.split(None, 1)[0] if where[0] == '#': utils.logIrcMessage(where + '#full.log', '< ' + str(message), timestamp)
def irc_366(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: where: str = message.params.middle.split()[-1] if where[0] == '#': utils.logIrcMessage(where + '#full.log', '< ' + str(message), timestamp)
def irc_hosttarget(connection: 'connectionM.ConnectionHandler', message: IrcMessage, timestamp: datetime) -> None: where: Optional[str] = message.params.middle if where[0] == '#': utils.logIrcMessage(where + '#hosttarget.log', str(message), timestamp)