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()
def test_from_twitch_privmsg_bits(self): IrcMessage.fromMessage('''\ @badges=staff/1,bits/1000;bits=100;color=;display-name=TWITCH_UserNaME;\ emotes=;id=b34ccfc7-4977-403a-8a94-33c6bac34fb8;mod=0;room-id=1337;\ subscriber=0;turbo=1;user-id=1337;user-type=staff \ :twitch_username!twitch_username@twitch_username.tmi.twitch.tv \ PRIVMSG #channel :cheer100''')
def test_from_twitch_privmsg(self): self.assertEqual( IrcMessage.fromMessage( '@badges=broadcaster/1;color=#DAA520;display-name=BotGotsThis;' 'emotes=25:6-10;mod=1;room-id=42553092;subscriber=0;turbo=0;' 'user-id=55612319;user-type=mod ' ':[email protected] ' 'PRIVMSG #botgotsthis :Hello Kappa'), IrcMessage( tags=IrcMessageTagsReadOnly({ 'badges': 'broadcaster/1', 'color': '#DAA520', 'display-name': 'BotGotsThis', 'emotes': '25:6-10', 'mod': '1', 'room-id': '42553092', 'subscriber': '0', 'turbo': '0', 'user-id': '55612319', 'user-type': 'mod' }), prefix=IrcMessagePrefix(nick='botgotsthis', user='******', host='botgotsthis.tmi.twitch.tv'), command='PRIVMSG', params=IrcMessageParams(middle='#botgotsthis', trailing='Hello Kappa')))
def test_from_twitch_001(self): self.assertEqual( IrcMessage.fromMessage( ':tmi.twitch.tv 001 botgotsthis :Welcome, GLHF!'), IrcMessage(prefix=IrcMessagePrefix(servername='tmi.twitch.tv'), command=1, params=IrcMessageParams(middle='botgotsthis', trailing='Welcome, GLHF!')))
def test_command(self): message = IrcMessage(command='RECONNECT') self.assertIsNone(message.tags) self.assertIsNone(message.prefix) self.assertEqual(message.command, 'RECONNECT') self.assertIsNotNone(message.params) self.assertIs(message.params.isEmpty, True) self.assertEqual(message, IrcMessage(command='RECONNECT'))
def test_queueWrite_multi_prepend(self): message1 = IrcMessage(None, None, 1) message2 = IrcMessage(None, None, 2) self.connection.queue_write(message1) self.connection.queue_write(message2, prepend=True) self.assertEqual(len(self.connection.writeQueue), 2) self.assertEqual(self.connection.writeQueue[0][0], (message2,)) self.assertEqual(self.connection.writeQueue[1][0], (message1,))
def test_from_prefix_command_param_middle(self): self.assertEqual( IrcMessage.fromMessage('''\ :[email protected] JOIN #botgotsthis'''), IrcMessage( prefix=IrcMessagePrefix(nick='botgotsthis', user='******', host='botgotsthis.tmi.twitch.tv'), command='JOIN', params=IrcMessageParams(middle='#botgotsthis')))
def test_command_params_middle(self): message = IrcMessage(command='JOIN', params=IrcMessageParams(middle='#botgotsthis')) self.assertIsNone(message.tags) self.assertIsNone(message.prefix) self.assertEqual(message.command, 'JOIN') self.assertIs(message.params.middle, '#botgotsthis') self.assertIs(message.params.trailing, None) self.assertEqual(message, IrcMessage(command='JOIN', params=IrcMessageParams('#botgotsthis')))
def test_command_params_middle_trailing(self): message = IrcMessage(command='PRIVMSG', params=IrcMessageParams(middle='#botgotsthis', trailing='Hello World')) self.assertIsNone(message.tags) self.assertIsNone(message.prefix) self.assertEqual(message.command, 'PRIVMSG') self.assertIs(message.params.middle, '#botgotsthis') self.assertIs(message.params.trailing, 'Hello World') self.assertEqual( message, IrcMessage(command='PRIVMSG', params=IrcMessageParams('#botgotsthis', 'Hello World')))
def test_parse_prefix_servername_command_params_middle_trailing(self): self.assertEqual( IrcMessage.parse(':tmi.twitch.tv PONG tmi.twitch.tv :botgotsthis'), ParsedMessage(None, IrcMessagePrefix(servername='tmi.twitch.tv'), 'PONG', IrcMessageParams('tmi.twitch.tv', 'botgotsthis')))
def test_log_irc_353(self): message = IrcMessage.fromMessage( ':botgotsthis.tmi.twitch.tv 353 botgotsthis = #botgotsthis ' ':botgotsthis') ircmessage.irc_353(self.connection, message, self.now) self.assertEqual(self.channel.ircUsers, {'botgotsthis'}) self.assertTrue(self.mock_log.called)
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 test_log_irc_notice(self, mock_parse): message = IrcMessage.fromMessage( '@msg-id=bad_timeout_broadcaster :tmi.twitch.tv NOTICE ' '#botgotsthis :You cannot timeout the broadcaster.') ircmessage.irc_notice(self.connection, message, self.now) self.assertTrue(mock_parse.called) self.assertTrue(self.mock_log.called)
def test_on_write_join(self, mock_onJoin, mock_record_join, mock_stdout): now = datetime(2000, 1, 1) message = IrcMessage(None, None, 'JOIN') self.connection._on_write(message, now, channel=self.channel) mock_onJoin.assert_called_once_with(self.channel) mock_record_join.assert_called_once_with() self.assertNotEqual(mock_stdout.getvalue(), '')
def test_queueWrite_channel(self): message = IrcMessage(None, None, 1) self.connection.queue_write(message, channel=self.channel) self.assertEqual(len(self.connection.writeQueue), 1) self.assertEqual(self.connection.writeQueue[0][0], (message,)) self.assertEqual(self.connection.writeQueue[0][1], {'channel': self.channel})
async def test_drain_whisper(self, mock_write): message = IrcMessage(None, None, 1) self.connection.queue_write(message, whisper=self.whisper) await self.connection.drain() mock_write.assert_called_once_with(message, whisper=self.whisper) self.assertIs(mock_write.called, True) self.assertFalse(self.connection.writeQueue)
def test_queueWrite_whisper(self): message = IrcMessage(None, None, 1) self.connection.queue_write(message, whisper=self.whisper) self.assertEqual(len(self.connection.writeQueue), 1) self.assertEqual(self.connection.writeQueue[0][0], (message,)) self.assertEqual(self.connection.writeQueue[0][1], {'whisper': self.whisper})
def test_log_irc_part(self): self.channel.ircUsers.add('botgotsthis') message = IrcMessage.fromMessage( ':[email protected] PART ' '#botgotsthis') ircmessage.irc_part(self.connection, message, self.now) self.assertEqual(self.channel.ircUsers, set())
def test_log_irc_whisper(self, mock_parse, mock_config): mock_config.botnick = 'botgotsthis' message = IrcMessage.fromMessage( ':[email protected] WHISPER ' 'botgotsthis :Kappa') ircmessage.irc_whisper(self.connection, message, self.now) self.assertTrue(mock_parse.called) self.assertGreaterEqual(self.mock_log.call_count, 3)
def test_log_irc_privmsg_mention(self, mock_parse, mock_config): mock_config.botnick = 'botgotsthis' message = IrcMessage.fromMessage( ':[email protected] PRIVMSG ' '#botgotsthis :botgotsthis') ircmessage.irc_privmsg(self.connection, message, self.now) self.assertTrue(mock_parse.called) self.assertGreaterEqual(self.mock_log.call_count, 2)
def test_log_irc_privmsg_no_channel(self, mock_parse, mock_config): mock_config.botnick = 'botgotsthis' message = IrcMessage.fromMessage( ':[email protected] PRIVMSG ' '#megotsthis :Hello Kappa') ircmessage.irc_privmsg(self.connection, message, self.now) self.assertFalse(mock_parse.called) self.assertTrue(self.mock_log.called)
def test_parse_multiple_spaces(self): self.assertEqual( IrcMessage.parse('''\ @multiple=spaces :will!be@used HERE to test :if this passes'''), ParsedMessage( IrcMessageTagsReadOnly({'multiple': 'spaces'}), IrcMessagePrefix(nick='will', user='******', host='used'), 'HERE', IrcMessageParams('to test', 'if this passes')))
def part_channel(self, channel: 'data.Channel') -> None: if channel.channel not in self._channels: return self.queue_write( IrcMessage(None, None, 'PART', IrcMessageParams(channel.ircChannel))) del self._channels[channel.channel] join.on_part(channel.channel) print(f'{utils.now()} Parted {channel.channel}')
def test_str_magic_prefix_command_params(self): self.assertEqual( str(IrcMessage( prefix=IrcMessagePrefix(nick='botgotsthis', user='******', host='botgotsthis.tmi.twitch.tv'), command='PART', params=IrcMessageParams('#botgotsthis'))), '''\ :[email protected] PART #botgotsthis''')
def test_parse_prefix_command_params_middle(self): self.assertEqual( IrcMessage.parse('''\ :[email protected] JOIN #botgotsthis'''), ParsedMessage(None, IrcMessagePrefix(nick='bot_gots_this', user='******', host='botgotsthis.tmi.twitch.tv'), 'JOIN', IrcMessageParams('#botgotsthis')))
def send_ping(self) -> None: now = utils.now() sinceLastSend: timedelta = now - self.lastSentPing sinceLast: timedelta = now - self.lastPing if sinceLastSend >= timedelta(minutes=1): self.queue_write(IrcMessage(None, None, 'PING', IrcMessageParams(bot.config.botnick)), prepend=True) self.lastSentPing = now elif sinceLast >= timedelta(minutes=1, seconds=15): raise ConnectionError()
async def test_write_ConnectionError(self, mock_log_write, mock_logException): self.mock_writer.drain.side_effect = ConnectionError message = IrcMessage(None, None, 1) with self.assertRaises(ConnectionError): await self.connection.write(message) self.assertFalse(mock_log_write.called) self.mock_writer.write.assert_any_call(b'001') self.mock_writer.write.assert_any_call(b'\r\n') self.mock_writer.drain.assert_called_once_with() mock_logException.assert_called_once_with()
async def test_write_channel_whisper(self, mock_log_write, mock_logException): message = IrcMessage(None, None, 1) await self.connection.write(message, channel=self.channel, whisper=self.whisper) mock_log_write.assert_called_once_with( self.connection, message, channel=self.channel, whisper=self.whisper, timestamp=self.now) self.mock_writer.write.assert_any_call(b'001') self.mock_writer.write.assert_any_call(b'\r\n') self.mock_writer.drain.assert_called_once_with() self.assertFalse(mock_logException.called)
async def login(self, writer: asyncio.StreamWriter) -> None: if not isinstance(writer, asyncio.StreamWriter): raise TypeError() commands: List[IrcMessage] = [ IrcMessage(None, None, 'PASS', IrcMessageParams(bot.config.password or None)), IrcMessage(None, None, 'NICK', IrcMessageParams(bot.config.botnick)), IrcMessage( None, None, 'USER', IrcMessageParams(bot.config.botnick + ' 0 *', bot.config.botnick)), IrcMessage(None, None, 'CAP', IrcMessageParams('REQ', 'twitch.tv/membership')), IrcMessage(None, None, 'CAP', IrcMessageParams('REQ', 'twitch.tv/commands')), IrcMessage(None, None, 'CAP', IrcMessageParams('REQ', 'twitch.tv/tags')), ] command: IrcMessage for command in commands: message: bytes = (str(command) + '\r\n').encode('utf-8') writer.write(message) self._log_write(command) await writer.drain()
def parseMessage(connection: 'connectionM.ConnectionHandler', ircmsg: str, timestamp: datetime) -> None: message: IrcMessage = IrcMessage.fromMessage(ircmsg) if message.command in ircHandlers: ircHandlers[message.command](connection, message, timestamp) log_channel_message(message, timestamp) pkg: str for pkg in bot.globals.pkgs: ircmessage: Any ircmessage = importlib.import_module('pkg.' + pkg + '.ircmessage') ircmessage.parseMessage(connection, ircmsg, timestamp)