Beispiel #1
0
 def flush_writes(self) -> None:
     self.messaging.cleanOldTimestamps()
     whisperMessage: WhisperMessage
     for whisperMessage in iter(self.messaging.popWhisper, None):
         ircMsg: str = f'.w {whisperMessage.nick} {whisperMessage.message}'
         ircMsg = ircMsg[:bot.config.messageLimit]
         try:
             self.queue_write(IrcMessage(
                 None, None, 'PRIVMSG',
                 IrcMessageParams(bot.globals.groupChannel.ircChannel,
                                  ircMsg)),
                              whisper=whisperMessage)
         except ValueError:
             utils.logException()
     nessage: ChatMessage
     for message in iter(self.messaging.popChat, None):
         try:
             self.queue_write(IrcMessage(
                 None, None, 'PRIVMSG',
                 IrcMessageParams(
                     message.channel.ircChannel,
                     message.message[:bot.config.messageLimit])),
                              channel=message.channel)
         except ValueError:
             utils.logException()
Beispiel #2
0
 def test_logException_config_None(self, mock_now, mock_config,
                                   mock_logging):
     mock_now.return_value = datetime(2000, 1, 1)
     mock_config.exceptionLog = None
     try:
         raise Exception()
     except Exception:
         utils.logException()
     self.assertFalse(mock_logging.log.called)
Beispiel #3
0
async def join_manager() -> None:
    name = 'Channel Join Manager'
    print(f'{utils.now()} Starting {name}')
    while bot.globals.running:
        try:
            join_a_channel()
            await asyncio.sleep(1 / bot.config.joinPerSecond)
        except Exception:
            utils.logException()
    print(f'{utils.now()} Ending {name}')
Beispiel #4
0
 def test_logException_no_development(self, mock_now, mock_config,
                                      mock_logging, mock_stderr):
     mock_now.return_value = datetime(2000, 1, 1)
     mock_config.development = False
     mock_config.exceptionLog = 'exception'
     try:
         raise Exception()
     except Exception:
         utils.logException()
     mock_logging.log.assert_called_once_with(
         StrContains('exception'),
         StrContains('2000', '1', 'Exception', __file__,
                     'test_logException', 'raise Exception'))
     self.assertEqual(mock_stderr.getvalue(), '')
Beispiel #5
0
async def whisperCommand(tags: IrcMessageTagsReadOnly, nick: str,
                         message: Message, timestamp: datetime) -> None:
    manager: bool
    permissions: WhisperPermissionSet
    arguments: data.WhisperCommandArgs
    command: data.WhisperCommand
    cacheStore: cache.CacheStore
    try:
        async with cache.get_cache() as cacheStore:
            manager = await cacheStore.isBotManager(nick)
            permissions = WhisperPermissionSet(tags, nick, manager)

            arguments = data.WhisperCommandArgs(cacheStore, nick, message,
                                                permissions, timestamp)
            for command in commandsToProcess(message.command):
                if await command(arguments):
                    return
    except Exception:
        utils.logException(f'From: {nick}\nMessage: {message}', timestamp)
Beispiel #6
0
    async def read(self) -> None:
        if self._transport is None:
            raise ConnectionError()
        try:
            ircmsg: bytes = await self._reader.readuntil(b'\r\n')
        except ConnectionError:
            utils.logException()
            return

        try:
            ircmsg = ircmsg[:-2]
            if not ircmsg:
                return
            message: str = ircmsg.decode('utf-8')
            self._log_read(message)
            lib.ircmessage.parseMessage(self, message, utils.now())
        except data.ConnectionReset:
            raise
        except data.LoginUnsuccessful:
            bot.globals.running = False
            raise
Beispiel #7
0
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)
Beispiel #8
0
    async def write(self,
                    command: IrcMessage,
                    *,
                    channel: 'Optional[data.Channel]' = None,
                    whisper: Optional[WhisperMessage] = None) -> None:
        if not isinstance(command, IrcMessage):
            raise TypeError()
        if self._transport is None:
            raise ConnectionError()

        try:
            messageBytes: bytes = str(command).encode('utf-8')
            timestamp: datetime = utils.now()
            self._writer.write(messageBytes)
            self._writer.write(b'\r\n')
            await self._writer.drain()
            self._on_write(command, timestamp, channel=channel)
            self._log_write(command,
                            channel=channel,
                            whisper=whisper,
                            timestamp=timestamp)
        except Exception:
            utils.logException()
            raise
Beispiel #9
0
async def _run_task(task: Callable[[datetime], Awaitable[None]],
                    timestamp: datetime) -> None:
    try:
        await task(timestamp)
    except Exception:
        utils.logException()