From 62dae5a0a2ad9cead08aa13810561e9436d5a40f Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 5 Mar 2020 16:59:37 +0100 Subject: `client_pickle` messenger configuration param so that a serialized client object can be passed, bypassing the need to re-login at each restart --- external/messenger.py | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) (limited to 'external') diff --git a/external/messenger.py b/external/messenger.py index b5dd2d3..cd175e1 100755 --- a/external/messenger.py +++ b/external/messenger.py @@ -10,6 +10,9 @@ import time import traceback from urllib.parse import unquote as UrlUnquote +import base64 +import getpass +import zlib import hashlib import fbchat @@ -210,27 +213,17 @@ class MessengerBridge: self.init_backlog_length = int(cmd["data"]["initial_backlog"]) client_file = "/tmp/fbclient_" + hashlib.sha224(cmd["data"]["email"].encode("utf-8")).hexdigest() - try: - f = open(client_file, "rb") - self.client = pickle.load(f) - f.close() - sys.stderr.write("(python messenger) using previous client: {}\n".format(client_file)) - except: - self.client = None - - if self.client is None: + if "client_pickle" in cmd["data"]: + data = base64.b64decode(cmd["data"]["client_pickle"]) + data = zlib.decompress(data) + self.client = pickle.loads(data) + else: email, password = cmd["data"]["email"], cmd["data"]["password"] self.client = MessengerBridgeClient(email=email, password=password, max_tries=1) + ## TODO: save client in new client_pickle config value if not self.client.isLoggedIn(): - return {"_type": "rep_error", "error": "Unable to login (?)"} - - try: - f = open(client_file, "wb") - pickle.dump(self.client, f) - f.close() - except: - pass + return {"_type": "rep_error", "error": "Unable to login (invalid pickle?)"} self.client.setBridge(self) @@ -524,7 +517,25 @@ class MessengerBridge: "data": {"name": new_title}, }) +# ---- CLI ---- + +def createClientPickle(): + email = input("Email address of Facebook account: ") + password = getpass.getpass() + client = MessengerBridgeClient(email, password, max_tries=1) + if not client.isLoggedIn(): + print("Could not log in (why???)") + return + print("") + data = pickle.dumps(client) + data = zlib.compress(data) + data = base64.b64encode(data).decode('ascii') + print(data) + if __name__ == "__main__": - bridge = MessengerBridge() - bridge.run() + if "create_client_pickle" in sys.argv: + createClientPickle() + else: + bridge = MessengerBridge() + bridge.run() -- cgit v1.2.3