Beispiel #1
0
def new_conn_with_handshake(username='******',
                            nick='Test',
                            gecos='Test User',
                            extensions=[BasicRFC],
                            **kwargs):
    ns = new_connection(username, nick, gecos, extensions, **kwargs)
    ns.draw_line()  # the USER
    ns.draw_line()  # and the NICK
    ns.inject_line(
        Line(command='001',
             params=(nick, 'Welcome to the Test network %s' % nick)))
    ns.inject_line(
        Line(command='002',
             params=(nick, 'Your host is nonexistent.test.server')))
    return ns
Beispiel #2
0
    def recv(self):
        # pylint: disable=arguments-differ
        timeout = self.kwargs.get('recv_timeout', None)

        if not self.scheduler.empty():
            timeout_s = self.scheduler.run(False)
            if timeout is None or timeout > timeout_s:
                timeout = timeout_s

        self.socket.settimeout(timeout)
        try:
            data = self.socket.recv(512)
            if not data:
                raise OSError("Connection reset by peer")

            data = self.data + data
        except socket.timeout:
            # XXX should try harder to meet user timeout deadlines and not
            # quit early.
            return

        lines = data.split(b'\r\n')
        self.data = lines.pop()

        for line in lines:
            line = Line.parse(line.decode('utf-8', 'ignore'))
            _logger.debug("IN: %s", str(line).rstrip())
            super().recv(line)
Beispiel #3
0
    def recv(self):
        timeout = self.kwargs.get('recv_timeout', None)

        if not self.scheduler.empty():
            timeout_s = self.scheduler.run(False)
            if timeout is None or timeout > timeout_s:
                timeout = timeout_s

        self.socket.settimeout(timeout)
        try:
            data = self.socket.recv(512)
            if not data:
                raise OSError("Connection reset by peer")

            data = self.data + data
        except socket.timeout:
            # XXX should try harder to meet user timeout deadlines and not
            # quit early.
            return

        lines = data.split(b'\r\n')
        self.data = lines.pop()

        for line in lines:
            line = Line.parse(line.decode('utf-8', 'ignore'))
            logger.debug("IN: %s", str(line).rstrip())
            super().recv(line)
Beispiel #4
0
 def line(self):
     """Return a :py:class:`~PyIRC.line.Line` instance representing this
     CTCP message."""
     message = '\x01{}'.format(self.command)
     if self.param is not None:
         message = message + ' ' + self.param
     message = message + '\x01'
     return Line(command=self.msgtype, params=[self.target, message])
Beispiel #5
0
def join_line(irc, name):
    """Helper: Construct a JOIN line.

    :param irc:
        The IRC ocnnection.

    :param name:
        The channel name.
    """

    return Line(hostmask=conn_mask(irc), command='JOIN', params=(name, ))
Beispiel #6
0
    def test_channel_no_topic(self):
        """Ensure channels with no topic are handled correctly."""
        name = '#test'

        irc = self.connection
        irc.inject_line(join_line(irc, name))
        self.assertIsNone(self.channel.topic)

        irc.inject_line(
            Line(hostmask='nonexistent.test.server',
                 command=Numerics.RPL_NOTOPIC,
                 params=(irc.nick, name)))
        self.assertIsNotNone(self.channel.topic)
Beispiel #7
0
    def test_channel_topic_user(self):
        """Ensure topic set by user is handled correctly."""
        name = '#Sporks'
        topic = "#Sporks - we'll bring yo dick"
        setter = Hostmask(nick='lstarnes',
                          username='******',
                          host='interlinked/netadmin/automaton/lstarnes')
        set_on = int(time())  # 1437288043

        irc = self.connection
        irc.inject_line(join_line(irc, name))
        irc.inject_line(
            Line(hostmask=setter, command='TOPIC', params=(name, topic)))

        self.assertTopicEqual(topic, setter, set_on)
Beispiel #8
0
def rpl_topic_line(irc, name, topic):
    """Helper: Construct an RPL_TOPIC line.

    :param irc:
        The IRC connection.

    :param name:
        The channel name.

    :param topic:
        The topic.
    """

    return Line(hostmask=':nonexistent.test.server',
                command=Numerics.RPL_TOPIC.value,
                params=(irc.nick, name, topic))
Beispiel #9
0
    def data_received(self, data):
        data = self.data + data

        lines = data.split(b'\r\n')
        self.data = lines.pop()

        for line in lines:
            line = Line.parse(line.decode('utf-8', 'ignore'))
            logger.debug("IN: %s", str(line).rstrip())
            try:
                super().recv(line)
            except Exception as e:
                # We should never get here!
                self.send("QUIT", ["Exception received!"])
                self.transport.close()
                raise
Beispiel #10
0
    def send(self, command, params):
        """Send a line out onto the wire.

        :param command:
            IRC command to send.

        :param params:
            A Sequence of parameters to send with the command. Only the last
            parameter may contain spaces due to IRC framing format
            limitations.

        """
        line = Line(command=command, params=params)
        event, results = self.call_event("commands_out", command, line)
        if event.cancelled:
            return None

        return line
Beispiel #11
0
def rpl_topiwhotime_line(irc, name, setter, set_on):
    """Helper: Construct an RPL_TOPICWHOTIME line.

    :param irc:
        The IRC connection.

    :param name:
        The channel name.

    :param setter:
        The setter of the topic (str or Hostmask).

    :param set_on:
        The timestamp (str or int).
    """

    return Line(hostmask=':nonexistent.test.server',
                command=Numerics.RPL_TOPICWHOTIME.value,
                params=(irc.nick, name, str(setter), str(set_on)))
Beispiel #12
0
    def recv(self):
        # pylint: disable=arguments-differ,no-member
        timeout = self.kwargs.get('recv_timeout', None)
        self.socket.settimeout(timeout)
        try:
            data = self.socket.recv(512)
            if not data:
                raise OSError("Connection reset by peer")

            data = self.data + data
        except socket.timeout:
            return

        lines = data.split(b'\r\n')
        self.data = lines.pop()

        for line in lines:
            line = Line.parse(line.decode('utf-8', 'ignore'))
            _logger.debug("IN: %s", str(line).rstrip())
            spawn_n(super().recv(line))
Beispiel #13
0
    def data_received(self, data):
        data = self.data + data

        lines = data.split(b'\r\n')
        self.data = lines.pop()

        for line in lines:
            line = Line.parse(line.decode('utf-8', 'ignore'))
            _logger.debug("IN: %s", str(line).rstrip())
            try:
                super().recv(line)
            except Exception:
                # We should never get here!
                _logger.exception("Exception received in recv loop")
                self.send("QUIT", ["Exception received!"])
                self.transport.close()

                # This is fatal and needs to be reported so stop the event
                # loop.
                loop = asyncio.get_event_loop()
                loop.stop()

                raise
Beispiel #14
0
    def data_received(self, data):
        data = self.data + data

        lines = data.split(b'\r\n')
        self.data = lines.pop()

        for line in lines:
            line = Line.parse(line.decode('utf-8', 'ignore'))
            _logger.debug("IN: %s", str(line).rstrip())
            try:
                super().recv(line)
            except Exception:
                # We should never get here!
                _logger.exception("Exception received in recv loop")
                self.send("QUIT", ["Exception received!"])
                self.transport.close()

                # This is fatal and needs to be reported so stop the event
                # loop.
                loop = asyncio.get_event_loop()
                loop.stop()

                raise
Beispiel #15
0
 def line(self):
     """Return a :py:class:`~PyIRC.line.Line` instance representing this
     CTCP message"""
     message = '\x01{} {}\x01'.format(self.command, self.param)
     return Line(command=self.msgtype, params=[self.target, message])