def wrap_arguments(prefix, args, length, separator=' ', max_args_per_line=0):
    """
    Takes a static prefix and a list of arguments, and returns a list of strings
    with the arguments wrapped across multiple lines. This is useful for breaking up
    long SJOIN or MODE strings so they aren't cut off by message length limits.
    """
    strings = []
    assert args, 'wrap_arguments: no arguments given'
    buf = prefix
    args = list(args)
    while args:
        assert len(prefix + args[0]
            ) <= length, 'wrap_arguments: Argument %r is too long for the given length %s' % (
            args[0], length)
        if len(buf + args[0]) + 1 <= length and (not max_args_per_line or 
            len(buf.split(' ')) < max_args_per_line):
            if buf != prefix:
                buf += separator
            buf += args.pop(0)
        else:
            strings.append(buf)
            buf = prefix
    else:
        strings.append(buf)
    return strings
