implement handle selection

This commit is contained in:
JJTech0130 2023-07-31 16:30:06 -04:00
parent 8b6e1561d4
commit 5c592c9dee
No known key found for this signature in database
GPG key ID: 23C92EBCCF8F93D6
3 changed files with 29 additions and 4 deletions

22
demo.py
View file

@ -143,17 +143,18 @@ while True:
print('filter (f) [recipient]: set the current chat') print('filter (f) [recipient]: set the current chat')
print('effect (e): adds an iMessage effect to the next sent message') print('effect (e): adds an iMessage effect to the next sent message')
print('note: recipient must start with tel: or mailto: and include the country code') print('note: recipient must start with tel: or mailto: and include the country code')
print('handle <handle>: set the current handle (for sending messages)')
print('\\: escape commands (will be removed from message)') print('\\: escape commands (will be removed from message)')
elif msg == 'quit' or msg == 'q': elif msg == 'quit' or msg == 'q':
break break
elif msg.startswith("effect ") or msg.startswith("e "): elif msg == 'effect' or msg == 'e' or msg.startswith("effect ") or msg.startswith("e "):
msg = msg.split(" ") msg = msg.split(" ")
if len(msg) < 2 or msg[1] == "": if len(msg) < 2 or msg[1] == "":
print("effect [effect namespace]") print("effect [effect namespace]")
else: else:
print(f"next message will be sent with [{msg[1]}]") print(f"next message will be sent with [{msg[1]}]")
current_effect = msg[1] current_effect = msg[1]
elif msg.startswith('filter ') or msg.startswith('f '): elif msg == 'filter' or msg == 'f' or msg.startswith('filter ') or msg.startswith('f '):
# Set the curernt chat # Set the curernt chat
msg = msg.split(' ') msg = msg.split(' ')
if len(msg) < 2 or msg[1] == '': if len(msg) < 2 or msg[1] == '':
@ -161,13 +162,28 @@ while True:
else: else:
print(f'Filtering to {msg[1:]}') print(f'Filtering to {msg[1:]}')
current_participants = msg[1:] current_participants = msg[1:]
elif msg == 'handle' or msg.startswith('handle '):
msg = msg.split(' ')
if len(msg) < 2 or msg[1] == '':
print('handle [handle]')
print('Available handles:')
for h in user.handles:
print(f'\t{h}')
else:
h = msg[1]
if h in user.handles:
print(f'Using {h} as handle')
user.current_handle = h
else:
print(f'Handle {h} not found')
elif current_participants != []: elif current_participants != []:
if msg.startswith('\\'): if msg.startswith('\\'):
msg = msg[1:] msg = msg[1:]
im.send(imessage.iMessage( im.send(imessage.iMessage(
text=msg, text=msg,
participants=current_participants, participants=current_participants,
sender=user.handles[0], sender=user.current_handle,
effect=current_effect effect=current_effect
)) ))
current_effect = None current_effect = None

View file

@ -45,6 +45,8 @@ class IDSUser:
self._auth_keypair, self._auth_keypair,
self._push_keypair, self._push_keypair,
) )
self.current_handle = self.handles[0]
# Uses an existing authentication keypair # Uses an existing authentication keypair
def restore_authentication( def restore_authentication(
@ -79,5 +81,5 @@ class IDSUser:
self._id_keypair = id_keypair self._id_keypair = id_keypair
def lookup(self, uris: list[str], topic: str = "com.apple.madrid") -> any: def lookup(self, uris: list[str], topic: str = "com.apple.madrid") -> any:
return query.lookup(self.push_connection, self.handles[0], self._id_keypair, uris, topic) return query.lookup(self.push_connection, self.current_handle, self._id_keypair, uris, topic)

View file

@ -343,12 +343,19 @@ class iMessageUser:
return iMessage.from_raw(decrypted, body['sP']) return iMessage.from_raw(decrypted, body['sP'])
KEY_CACHE_HANDLE: str = ""
KEY_CACHE: dict[bytes, tuple[bytes, bytes]] = {} KEY_CACHE: dict[bytes, tuple[bytes, bytes]] = {}
"""Mapping of push token : (public key, session token)""" """Mapping of push token : (public key, session token)"""
USER_CACHE: dict[str, list[bytes]] = {} USER_CACHE: dict[str, list[bytes]] = {}
"""Mapping of handle : [push tokens]""" """Mapping of handle : [push tokens]"""
def _cache_keys(self, participants: list[str]): def _cache_keys(self, participants: list[str]):
# Clear the cache if the handle has changed
if self.KEY_CACHE_HANDLE != self.user.current_handle:
self.KEY_CACHE_HANDLE = self.user.current_handle
self.KEY_CACHE = {}
self.USER_CACHE = {}
# Check to see if we have cached the keys for all of the participants # Check to see if we have cached the keys for all of the participants
if all([p in self.USER_CACHE for p in participants]): if all([p in self.USER_CACHE for p in participants]):
return return