def change_nick(user_input: 'UserInput', window: 'Window', contact_list: 'ContactList', group_list: 'GroupList', settings: 'Settings', c_queue: 'Queue') -> None: """Change nick of contact.""" if window.type == 'group': raise FunctionReturn("Error: Group is selected.") try: nick = user_input.plaintext.split()[1] except IndexError: raise FunctionReturn("Error: No nick specified.") rx_acco = window.contact.rx_account success, error_msg = validate_nick(nick, (contact_list, group_list, rx_acco)) if not success: raise FunctionReturn(error_msg) window.contact.nick = nick window.name = nick contact_list.store_contacts() packet = CHANGE_NICK_HEADER + rx_acco.encode() + US_BYTE + nick.encode() queue_command(packet, settings, c_queue) box_print(f"Changed {rx_acco} nick to {nick}.")
def change_nick(user_input: 'UserInput', window: 'TxWindow', contact_list: 'ContactList', group_list: 'GroupList', settings: 'Settings', queues: 'QueueDict') -> None: """Change nick of contact.""" try: nick = user_input.plaintext.split()[1] except IndexError: raise SoftError("Error: No nick specified.", head_clear=True) if window.type == WIN_TYPE_GROUP: group_rename(nick, window, contact_list, group_list, settings, queues) if window.contact is None: raise SoftError("Error: Window does not have contact.") onion_pub_key = window.contact.onion_pub_key error_msg = validate_nick(nick, (contact_list, group_list, onion_pub_key)) if error_msg: raise SoftError(error_msg, head_clear=True) window.contact.nick = nick window.name = nick contact_list.store_contacts() command = CH_NICKNAME + onion_pub_key + nick.encode() queue_command(command, settings, queues)
def change_nick(cmd_data: bytes, ts: 'datetime', window_list: 'WindowList', contact_list: 'ContactList', group_list: 'GroupList') -> None: """Change contact nick.""" account, nick = [f.decode() for f in cmd_data.split(US_BYTE)] success, error_msg = validate_nick(nick, (contact_list, group_list, account)) if not success: raise FunctionReturn(error_msg) c_window = window_list.get_window(account) c_window.name = nick contact = contact_list.get_contact(account) contact.nick = nick contact_list.store_contacts() cmd_win = window_list.get_local_window() cmd_win.print_new(ts, f"Changed {account} nick to {nick}.")
def test_validate_nick(self): self.assertEqual( validate_nick("Alice_", (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), '') self.assertEqual( validate_nick(254 * "a", (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), '') self.assertEqual( validate_nick(255 * "a", (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), 'Error: Nick must be shorter than 255 chars.') self.assertEqual( validate_nick("\x01Alice", (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), 'Error: Nick must be printable.') self.assertEqual( validate_nick('', (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), "Error: Nick cannot be empty.") self.assertEqual( validate_nick('Me', (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), "Error: 'Me' is a reserved nick.") self.assertEqual( validate_nick('-!-', (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), "Error: '-!-' is a reserved nick.") self.assertEqual( validate_nick(LOCAL_ID, (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), "Error: Nick cannot have the format of an account.") self.assertEqual( validate_nick(nick_to_onion_address('A'), (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), "Error: Nick cannot have the format of an account.") self.assertEqual( validate_nick('Bob', (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), 'Error: Nick already in use.') self.assertEqual( validate_nick("Alice", (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), '') self.assertEqual( validate_nick("test_group", (self.contact_list, self.group_list, nick_to_pub_key("Alice"))), "Error: Nick cannot be a group name.")
def test_validate_nick(self): # Setup contact_list = ContactList(nicks=['Alice', 'Bob']) group_list = GroupList(groups=['testgroup']) # Test self.assertEqual(validate_nick("Alice_", (contact_list, group_list, '*****@*****.**')), (True, '')) self.assertEqual(validate_nick(254*"a", (contact_list, group_list, '*****@*****.**')), (True, '')) self.assertEqual(validate_nick(255*"a", (contact_list, group_list, '*****@*****.**')), (False, 'Nick must be shorter than 255 chars.')) self.assertEqual(validate_nick("\x01Alice", (contact_list, group_list, '*****@*****.**')), (False, 'Nick must be printable.')) self.assertEqual(validate_nick('', (contact_list, group_list, '*****@*****.**')), (False, "Nick can't be empty.")) self.assertEqual(validate_nick('Me', (contact_list, group_list, '*****@*****.**')), (False, "'Me' is a reserved nick.")) self.assertEqual(validate_nick('-!-', (contact_list, group_list, '*****@*****.**')), (False, "'-!-' is a reserved nick.")) self.assertEqual(validate_nick('local', (contact_list, group_list, '*****@*****.**')), (False, "Nick can't refer to local keyfile.")) self.assertEqual(validate_nick('*****@*****.**', (contact_list, group_list, '*****@*****.**')), (False, "Nick can't have format of an account.")) self.assertEqual(validate_nick('Bob', (contact_list, group_list, '*****@*****.**')), (False, 'Nick already in use.')) self.assertEqual(validate_nick("Alice", (contact_list, group_list, '*****@*****.**')), (True, '')) self.assertEqual(validate_nick("testgroup", (contact_list, group_list, '*****@*****.**')), (False, "Nick can't be a group name."))
def test_validate_nick(self): self.assertEqual( validate_nick( "Alice_", (self.contact_list, self.group_list, '*****@*****.**')), '') self.assertEqual( validate_nick( 254 * "a", (self.contact_list, self.group_list, '*****@*****.**')), '') self.assertEqual( validate_nick( 255 * "a", (self.contact_list, self.group_list, '*****@*****.**')), 'Nick must be shorter than 255 chars.') self.assertEqual( validate_nick( "\x01Alice", (self.contact_list, self.group_list, '*****@*****.**')), 'Nick must be printable.') self.assertEqual( validate_nick( '', (self.contact_list, self.group_list, '*****@*****.**')), "Nick can't be empty.") self.assertEqual( validate_nick( 'Me', (self.contact_list, self.group_list, '*****@*****.**')), "'Me' is a reserved nick.") self.assertEqual( validate_nick( '-!-', (self.contact_list, self.group_list, '*****@*****.**')), "'-!-' is a reserved nick.") self.assertEqual( validate_nick( 'local', (self.contact_list, self.group_list, '*****@*****.**')), "Nick can't refer to local keyfile.") self.assertEqual( validate_nick( '*****@*****.**', (self.contact_list, self.group_list, '*****@*****.**')), "Nick can't have format of an account.") self.assertEqual( validate_nick( 'Bob', (self.contact_list, self.group_list, '*****@*****.**')), 'Nick already in use.') self.assertEqual( validate_nick( "Alice", (self.contact_list, self.group_list, '*****@*****.**')), '') self.assertEqual( validate_nick( "testgroup", (self.contact_list, self.group_list, '*****@*****.**')), "Nick can't be a group name.")