diff --git a/apns.py b/apns.py index 53d3180..755abf6 100644 --- a/apns.py +++ b/apns.py @@ -50,7 +50,7 @@ class IncomingQueue: with self.lock: self.queue.append(item) - def pop(self, index): + def pop(self, index = -1): with self.lock: return self.queue.pop(index) diff --git a/demo.py b/demo.py index 578a961..1ab696a 100644 --- a/demo.py +++ b/demo.py @@ -108,7 +108,44 @@ with open("config.json", "w") as f: im = imessage.iMessageUser(conn, user) +# Create a thread to take user input +INPUT_QUEUE = apns.IncomingQueue() + +def input_thread(): + while True: + from prompt_toolkit import prompt + + try: + msg = prompt('>> ') + except: + msg = 'quit' + INPUT_QUEUE.append(msg) + +threading.Thread(target=input_thread, daemon=True).start() + + while True: msg = im.receive() if msg is not None: print(f"Got message {msg}") + + if len(INPUT_QUEUE) > 0: + msg = INPUT_QUEUE.pop() + if msg == 'help' or msg == 'h': + print('help (h): show this message') + print('quit (q): quit') + print('send (s) [recipiant] [message]: send a message') + elif msg == 'quit' or msg == 'q': + break + elif msg.startswith('send') or msg.startswith('s'): + msg = msg.split(' ') + if len(msg) < 3: + print('send [recipiant] [message]') + else: + imsg = imessage.iMessage() + imsg.text = ' '.join(msg[2:]) + imsg.participants = [msg[1], user.handles[0]] + imsg.sender = user.handles[0] + + im.send(imsg) +