Пример #1
0
def rename_group(
    new_name: str,
    window: 'TxWindow',
    contact_list: 'ContactList',
    group_list: 'GroupList',
    settings: 'Settings',
    queues: 'QueueDict',
) -> None:
    """Rename the active group."""
    if window.type == WIN_TYPE_CONTACT or window.group is None:
        raise FunctionReturn("Error: Selected window is not a group window.",
                             head_clear=True)

    error_msg = validate_group_name(new_name, contact_list, group_list)
    if error_msg:
        raise FunctionReturn(error_msg, head_clear=True)

    command = GROUP_RENAME + window.uid + new_name.encode()
    queue_command(command, settings, queues)

    old_name = window.group.name
    window.group.name = new_name
    group_list.store_groups()

    raise FunctionReturn(f"Renamed group '{old_name}' to '{new_name}'.",
                         delay=1,
                         tail_clear=True)
Пример #2
0
def group_rename(cmd_data: bytes, ts: 'datetime', window_list: 'WindowList',
                 contact_list: 'ContactList', group_list: 'GroupList') -> None:
    """Rename the group."""
    group_id, new_name_bytes = separate_header(cmd_data, GROUP_ID_LENGTH)

    try:
        group = group_list.get_group_by_id(group_id)
    except StopIteration:
        raise SoftError(
            f"Error: No group with ID '{b58encode(group_id)}' found.")

    try:
        new_name = new_name_bytes.decode()
    except UnicodeError:
        raise SoftError(
            f"Error: New name for group '{group.name}' was invalid.")

    error_msg = validate_group_name(new_name, contact_list, group_list)
    if error_msg:
        raise SoftError(error_msg)

    old_name = group.name
    group.name = new_name
    group_list.store_groups()

    window = window_list.get_window(group.group_id)
    window.name = new_name

    message = f"Renamed group '{old_name}' to '{new_name}'."
    cmd_win = window_list.get_window(WIN_UID_COMMAND)
    cmd_win.add_new(ts, message, output=True)
Пример #3
0
 def test_validate_group_name(self):
     self.assertEqual(
         validate_group_name('test_group\x1f', self.contact_list,
                             self.group_list),
         "Error: Group name must be printable.")
     self.assertEqual(
         validate_group_name(PADDING_LENGTH * 'a', self.contact_list,
                             self.group_list),
         "Error: Group name must be less than 255 chars long.")
     self.assertEqual(
         validate_group_name(DUMMY_GROUP, self.contact_list,
                             self.group_list),
         "Error: Group name cannot use the name reserved for database padding."
     )
     self.assertEqual(
         validate_group_name(nick_to_onion_address("Alice"),
                             self.contact_list, self.group_list),
         "Error: Group name cannot have the format of an account.")
     self.assertEqual(
         validate_group_name('Alice', self.contact_list, self.group_list),
         "Error: Group name cannot be a nick of contact.")
     self.assertEqual(
         validate_group_name('test_group', self.contact_list,
                             self.group_list),
         "Error: Group with name 'test_group' already exists.")
     self.assertEqual(
         validate_group_name('test_group2', self.contact_list,
                             self.group_list), '')
Пример #4
0
def group_create(group_name:   str,
                 purp_members: List[bytes],
                 contact_list: 'ContactList',
                 group_list:   'GroupList',
                 settings:     'Settings',
                 queues:       'QueueDict',
                 _:            'MasterKey',
                 group_id:     Optional[bytes] = None
                 ) -> None:
    """Create a new group.
    
    Validate the group name and determine what members can be added.
    """
    error_msg = validate_group_name(group_name, contact_list, group_list)
    if error_msg:
        raise FunctionReturn(error_msg, head_clear=True)

    public_keys   = set(contact_list.get_list_of_pub_keys())
    purp_pub_keys = set(purp_members)
    accepted      = list(purp_pub_keys & public_keys)
    rejected      = list(purp_pub_keys - public_keys)

    if len(accepted) > settings.max_number_of_group_members:
        raise FunctionReturn(f"Error: TFC settings only allow {settings.max_number_of_group_members} "
                             f"members per group.", head_clear=True)

    if len(group_list) == settings.max_number_of_groups:
        raise FunctionReturn(f"Error: TFC settings only allow {settings.max_number_of_groups} groups.", head_clear=True)

    header = GROUP_MSG_INVITE_HEADER if group_id is None else GROUP_MSG_JOIN_HEADER

    if group_id is None:
        while True:
            group_id = os.urandom(GROUP_ID_LENGTH)
            if group_id not in group_list.get_list_of_group_ids():
                break

    group_list.add_group(group_name,
                         group_id,
                         settings.log_messages_by_default,
                         settings.show_notifications_by_default,
                         members=[contact_list.get_contact_by_pub_key(k) for k in accepted])

    command = GROUP_CREATE + group_id + group_name.encode() + US_BYTE + b''.join(accepted)
    queue_command(command, settings, queues)

    group_management_print(NEW_GROUP,        accepted, contact_list, group_name)
    group_management_print(UNKNOWN_ACCOUNTS, rejected, contact_list, group_name)

    if accepted:
        if yes("Publish the list of group members to participants?", abort=False):
            create_packet = header + group_id + b''.join(accepted)
            queue_to_nc(create_packet, queues[RELAY_PACKET_QUEUE])

    else:
        m_print(f"Created an empty group '{group_name}'.", bold=True, head=1)