@classmethod
def wrap_modes(cls, modes, limit, max_modes_per_msg=0):
    """
        IRC specific: Takes a list of modes and wraps it across multiple lines.
        """
    strings = []
    queued_modes = []
    total_length = 0
    last_prefix = '+'
    orig_modes = modes.copy()
    modes = list(modes)
    while modes:
        next_mode = modes.pop(0)
        modechar, arg = next_mode
        prefix = modechar[0]
        if prefix not in '+-':
            prefix = last_prefix
            modechar = prefix + modechar
            next_mode = modechar, arg
        next_length = 1
        if prefix != last_prefix:
            next_length += 1
        last_prefix = prefix
        if arg:
            next_length += 1
            next_length += len(arg)
        assert next_length <= limit, 'wrap_modes: Mode %s is too long for the given length %s' % (
            next_mode, limit)
        if next_length + total_length <= limit and (not max_modes_per_msg or
            len(queued_modes) < max_modes_per_msg):
            total_length += next_length
            cls._log_debug_modes('wrap_modes: Adding mode %s to queued modes',
                str(next_mode))
            queued_modes.append(next_mode)
            cls._log_debug_modes('wrap_modes: queued modes: %s', queued_modes)
        else:
            strings.append(cls.join_modes(queued_modes))
            queued_modes.clear()
            cls._log_debug_modes(
                'wrap_modes: cleared queue (length %s) and now adding %s',
                limit, str(next_mode))
            queued_modes.append(next_mode)
            total_length = next_length
    else:
        strings.append(cls.join_modes(queued_modes))
    cls._log_debug_modes('wrap_modes: returning %s for %s', strings, orig_modes
        )
    return strings
