def formatWhois(irc, replies, caller='', channel='', command='whois'):
    """Returns a string describing the target of a WHOIS command.

    Arguments are:
    * irc: the irclib.Irc object on which the replies was received

    * replies: a dict mapping the reply codes ('311', '312', etc.) to their
      corresponding ircmsg.IrcMsg

    * caller: an optional nick specifying who requested the whois information

    * channel: an optional channel specifying where the reply will be sent

    If provided, caller and channel will be used to avoid leaking information
    that the caller/channel shouldn't be privy to.
    """
    hostmask = '@'.join(replies['311'].args[2:4])
    nick = replies['318'].args[1]
    user = replies['311'].args[-1]
    START_CODE = '311' if command == 'whois' else '314'
    hostmask = '@'.join(replies[START_CODE].args[2:4])
    user = replies[START_CODE].args[-1]
    if _containsFormattingRe.search(user) and user[-1] != '\x0f':
        # For good measure, disable any formatting
        user = '%s\x0f' % user
    if '319' in replies:
        channels = []
        for msg in replies['319']:
            channels.extend(msg.args[-1].split())
        ops = []
        voices = []
        normal = []
        halfops = []
        for chan in channels:
            origchan = chan
            chan = chan.lstrip('@%+~!')
            # UnrealIRCd uses & for user modes and disallows it as a
            # channel-prefix, flying in the face of the RFC.  Have to
            # handle this specially when processing WHOIS response.
            testchan = chan.lstrip('&')
            if testchan != chan and irc.isChannel(testchan):
                chan = testchan
            diff = len(chan) - len(origchan)
            modes = origchan[:diff]
            chanState = irc.state.channels.get(chan)
            # The user is in a channel the bot is in, so the ircd may have
            # responded with otherwise private data.
            if chanState:
                # Skip channels the caller isn't in.  This prevents
                # us from leaking information when the channel is +s or the
                # target is +i.
                if caller not in chanState.users:
                    continue
                # Skip +s/+p channels the target is in only if the reply isn't
                # being sent to that channel.
                if set(('p', 's')) & set(chanState.modes.keys()) and \
                   not strEqual(channel or '', chan):
                    continue
            if not modes:
                normal.append(chan)
            elif utils.iter.any(lambda c: c in modes,('@', '&', '~', '!')):
                ops.append(chan)
            elif utils.iter.any(lambda c: c in modes, ('%',)):
                halfops.append(chan)
            elif utils.iter.any(lambda c: c in modes, ('+',)):
                voices.append(chan)
        L = []
        if ops:
            L.append(format(_('is an op on %L'), ops))
        if halfops:
            L.append(format(_('is a halfop on %L'), halfops))
        if voices:
            L.append(format(_('is voiced on %L'), voices))
        if normal:
            if L:
                L.append(format(_('is also on %L'), normal))
            else:
                L.append(format(_('is on %L'), normal))
    else:
        if command == 'whois':
            L = [_('isn\'t on any publicly visible channels')]
        else:
            L = []
    channels = format('%L', L)
    if '317' in replies:
        idle = utils.timeElapsed(replies['317'].args[2])
        signon = utils.str.timestamp(float(replies['317'].args[3]))
    else:
        idle = '<unknown>'
        signon = '<unknown>'
    if '312' in replies:
        server = replies['312'].args[2]
        if len(replies['312']) > 3:
            signoff = replies['312'].args[3]
    else:
        server = '<unknown>'
    if '301' in replies:
        away = ' %s is away: %s.' % (nick, replies['301'].args[2])
    else:
        away = ''
    if '320' in replies:
        if replies['320'].args[2]:
            identify = ' identified'
        else:
            identify = ''
    else:
        identify = ''
    if command == 'whois':
        s = _('%s (%s) has been%s on server %s since %s (idle for %s). %s '
              '%s.%s') % (user, hostmask, identify, server,
                          signon, idle, nick, channels, away)
    else:
        s = _('%s (%s) has been%s on server %s and disconnected on %s.') % \
                (user, hostmask, identify, server, signoff)
    return s
dataset/ETHPy150Open ProgVal/Limnoria/src/ircutils.py/formatWhois/original