def extract_girc_colours(msg, fill_last):
    """Extract the girc-formatted colours from the start of the string.

    Extracts the colours from the start, and returns the colour code in IRC
    format, and then the rest of the message.

    If `fill_last`, last number must be zero-padded.
    """
    if not len(msg):
        return '', ''

    if ']' not in msg or not msg.startswith('['):
        return '', msg

    original_msg = msg

    colours, msg = msg.split(']', 1)
    colours = colours.lstrip('[')

    if ',' in colours:
        fore, back = colours.split(',')
        if fore not in colour_name_to_code or back not in colour_name_to_code:
            return '', original_msg
        fore = colour_name_to_code[fore]
        back = colour_name_to_code[back]
        return '{},{}'.format(fore, back.zfill(2) if fill_last else back), msg
    else:
        if colours not in colour_name_to_code:
            return '', original_msg
        fore = colour_name_to_code[colours]
        return '{}'.format(fore.zfill(2) if fill_last else fore), msg
Extract the girc-formatted colours from the start of the string.

    Extracts the colours from the start, and returns the colour code in IRC
    format, and then the rest of the message.

    If `fill_last`, last number must be zero-padded.