<commit_before>"""
Paging command and support functions.
"""
from src.objects.models import Object
from src import defines_global

def get_last_paged_objects(pobject):
    """
    Returns a list of objects of the user's last paged list, or None if invalid
    or non-existant.
    """
    last_paged_dbrefs = pobject.get_attribute_value("LASTPAGED")
    if last_paged_dbrefs is not False:
        last_paged_objects = list()
        try:
            last_paged_dbref_list = [
                    x.strip() for x in last_paged_dbrefs.split(',')
            ]
            for dbref in last_paged_dbref_list:
                if not Object.objects.is_dbref(dbref):
                    raise ValueError
                last_paged_object = Object.objects.dbref_search(dbref)
                if last_paged_object is not None:
                    last_paged_objects.append(last_paged_object)
            return last_paged_objects
        except ValueError:
            # Remove the invalid LASTPAGED attribute
            pobject.clear_attribute("LASTPAGED")
            return None   

def cmd_page(command):
    """
    Send a message to target user (if online).
    """
    session = command.session
    pobject = session.get_pobject()
    # Get the last paged person(s)
    last_paged_objects = get_last_paged_objects(pobject)
    
    # If they don't give a target, or any data to send to the target
    # then tell them who they last paged if they paged someone, if not
    # tell them they haven't paged anyone.
    if not command.command_argument:
        if last_paged_objects:
            session.msg("You last paged: %s." % (
                ', '.join([x.name for x in last_paged_objects])))
            return
        session.msg("You have not paged anyone.")
        return
    
    args = command.command_argument.split()
    targets = []

    # Build a list of targets
    # If there are no targets, then set the targets to the last person they
    # paged.
    cmd_targets = command.get_arg_targets()
    if cmd_targets is None:
        targets = last_paged_objects
    else:
        # For each of the targets listed, grab their objects and append
        # it to the targets list
        for target in cmd_targets:
            matched_object = Object.objects.local_and_global_search(pobject,
                    target,
                    limit_types=[defines_global.OTYPE_PLAYER])
            if matched_object:
                targets.append(matched_object[0])
            else:
                # search returned None
                session.msg("Player '%s' can not be found." % (
                        target))

    # Depending on the argument provided, either send the entire thing as
    # a message or break off the point after the equal sign.
    if command.arg_has_target():
        message = command.get_arg_target_value()
    else:
        session.msg("Page them what?")
        return
        
    sender_name = pobject.get_name(show_dbref=False)
    # Build our messages
    target_message = "%s pages: %s"
    sender_message = "You paged %s with '%s'."
    # Handle paged emotes
    if message.startswith(':'):
        message = message[1:]
        target_message = "From afar, %s %s"
        sender_message = "Long distance to %s: %s %s"
    # Handle paged emotes without spaces
    if message.startswith(';'):
        message = message[1:]
        target_message = "From afar, %s%s"
        sender_message = "Long distance to %s: %s%s"

    # We build a list of target_names for the sender_message later
    target_names = []
    for target in targets:
        # Check to make sure they're connected, or a player
        if target.is_connected_plr():
            target.emit_to(target_message % (sender_name, message))
            target_names.append(target.get_name(show_dbref=False))
        else:
            session.msg("Player %s does not exist or is not online." % (
                    target.get_name(show_dbref=False)))

    # Now send a confirmation to the person doing the paging.
    if len(target_names) > 0:
        target_names_string = ', '.join(target_names)
        try:
            session.msg(sender_message % (target_names_string, sender_name, message))
        except TypeError:<commit_msg>More work on the page command. Slightly closer to MUX2 behavior, although there's still a way to go.<commit_after>"""
Paging command and support functions.
"""
from src.objects.models import Object
from src import defines_global

def get_last_paged_objects(pobject):
    """
    Returns a list of objects of the user's last paged list, or None if invalid
    or non-existant.
    """
    last_paged_dbrefs = pobject.get_attribute_value("LASTPAGED")
    if last_paged_dbrefs is not False:
        last_paged_objects = list()
        try:
            last_paged_dbref_list = [
                    x.strip() for x in last_paged_dbrefs.split(',')
            ]
            for dbref in last_paged_dbref_list:
                if not Object.objects.is_dbref(dbref):
                    raise ValueError
                last_paged_object = Object.objects.dbref_search(dbref)
                if last_paged_object is not None:
                    last_paged_objects.append(last_paged_object)
            return last_paged_objects
        except ValueError:
            # Remove the invalid LASTPAGED attribute
            pobject.clear_attribute("LASTPAGED")
            return None   

def cmd_page(command):
    """
    Send a message to target user (if online).
    """
    session = command.session
    pobject = session.get_pobject()
    # Get the last paged person(s)
    last_paged_objects = get_last_paged_objects(pobject)
    
    # If they don't give a target, or any data to send to the target
    # then tell them who they last paged if they paged someone, if not
    # tell them they haven't paged anyone.
    if not command.command_argument:
        if last_paged_objects:
            session.msg("You last paged: %s." % (
                ', '.join([x.name for x in last_paged_objects])))
            return
        session.msg("You have not paged anyone.")
        return
    
    args = command.command_argument.split()
    targets = []

    # Build a list of targets
    # If there are no targets, then set the targets to the last person they
    # paged.
    cmd_targets = command.get_arg_targets()
    if cmd_targets is None:
        targets = last_paged_objects
        
        # No valid last paged players found, error out.
        if not targets:
            session.msg("Page who?")
            return
    else:
        # For each of the targets listed, grab their objects and append
        # it to the targets list
        for target in cmd_targets:
            matched_object = Object.objects.local_and_global_search(pobject,
                    target,
                    limit_types=[defines_global.OTYPE_PLAYER])
            if matched_object:
                targets.append(matched_object[0])
            else:
                # search returned None
                session.msg("Player '%s' can not be found." % (
                        target))

    # Depending on the argument provided, either send the entire thing as
    # a message or break off the point after the equal sign.
    if command.arg_has_target():
        message = command.get_arg_target_value()
    else:
        session.msg("Page them what?")
        return
        
    sender_name = pobject.get_name(show_dbref=False)
    # Build our messages
    target_message = "%s pages: %s"
    sender_message = "You paged %s with '%s'."
    # Handle paged emotes
    if message.startswith(':'):
        message = message[1:]
        target_message = "From afar, %s %s"
        sender_message = "Long distance to %s: %s %s"
        # Handle paged emotes without spaces
    elif message.startswith(';'):
        message = message[1:]
        target_message = "From afar, %s%s"
        sender_message = "Long distance to %s: %s%s"

    # We build a list of target_names for the sender_message later
    target_names = []
    for target in targets:
        # Check to make sure they're connected, or a player
        if target.is_connected_plr():
            target.emit_to(target_message % (sender_name, message))
            target_names.append(target.get_name(show_dbref=False))
        else:
            session.msg("Player %s does not exist or is not online." % (
                    target.get_name(show_dbref=False)))

    # Now send a confirmation to the person doing the paging.
    if len(target_names) > 0:
        target_names_string = ', '.join(target_names)
        try:
            session.msg(sender_message % (target_names_string, sender_name, message))
        except TypeError:
{'fraction_non_alphanumeric': 0.07937600367056664, 'fraction_numerical': 0.0010323468685478321, 'mean_word_length': 2.172852983988355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3588660', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2442, 'n_words': 770}