List information about the sessions.

    .. versionadded:: 2016.11.0

    :param logged_in_users_only: If True, only return sessions with users logged in.
    :return: A list containing dictionaries of session information.

    CLI Example:

    .. code-block:: bash

        salt '*' rdp.list_sessions

def list_sessions(logged_in_users_only=False):
    '''
    List information about the sessions.

    .. versionadded:: 2016.11.0

    :param logged_in_users_only: If True, only return sessions with users logged in.
    :return: A list containing dictionaries of session information.

    CLI Example:

    .. code-block:: bash

        salt '*' rdp.list_sessions
    '''
    ret = list()
    server = win32ts.WTS_CURRENT_SERVER_HANDLE
    protocols = {win32ts.WTS_PROTOCOL_TYPE_CONSOLE: 'console',
                 win32ts.WTS_PROTOCOL_TYPE_ICA: 'citrix',
                 win32ts.WTS_PROTOCOL_TYPE_RDP: 'rdp'}
    statuses = {win32ts.WTSActive: 'active', win32ts.WTSConnected: 'connected',
                win32ts.WTSConnectQuery: 'connect_query', win32ts.WTSShadow: 'shadow',
                win32ts.WTSDisconnected: 'disconnected', win32ts.WTSIdle: 'idle',
                win32ts.WTSListen: 'listen', win32ts.WTSReset: 'reset',
                win32ts.WTSDown: 'down', win32ts.WTSInit: 'init'}

    for session in win32ts.WTSEnumerateSessions(server):
        user = win32ts.WTSQuerySessionInformation(server, session['SessionId'],
                                                  win32ts.WTSUserName) or None
        protocol_id = win32ts.WTSQuerySessionInformation(server, session['SessionId'],
                                                         win32ts.WTSClientProtocolType)
        status_id = win32ts.WTSQuerySessionInformation(server, session['SessionId'],
                                                       win32ts.WTSConnectState)
        protocol = protocols.get(protocol_id, 'unknown')
        connection_status = statuses.get(status_id, 'unknown')
        station = session['WinStationName'] or 'Disconnected'
        connection_info = {'connection_status': connection_status, 'protocol': protocol,
                           'session_id': session['SessionId'], 'station': station,
                           'user': user}
        if logged_in_users_only:
            if user:
                ret.append(connection_info)
        else:
            ret.append(connection_info)

    if not ret:
        _LOG.warning('No sessions found.')
    return sorted(ret, key=lambda k: k['session_id'])