client_pickle messenger configuration param

so that a serialized client object can be passed, bypassing the need to re-login at each restart
This commit is contained in:
Alex 2020-03-05 16:59:37 +01:00
parent b7090546a9
commit 62dae5a0a2
2 changed files with 35 additions and 21 deletions

View file

@ -45,7 +45,10 @@ func init() {
Name: "password", Name: "password",
Description: "Password", Description: "Password",
IsPassword: true, IsPassword: true,
Required: true, },
&ConfigEntry{
Name: "client_pickle",
Description: "Client pickle (alternative login method)",
}, },
&ConfigEntry{ &ConfigEntry{
Name: "initial_backlog", Name: "initial_backlog",

45
external/messenger.py vendored
View file

@ -10,6 +10,9 @@ import time
import traceback import traceback
from urllib.parse import unquote as UrlUnquote from urllib.parse import unquote as UrlUnquote
import base64
import getpass
import zlib
import hashlib import hashlib
import fbchat import fbchat
@ -210,27 +213,17 @@ class MessengerBridge:
self.init_backlog_length = int(cmd["data"]["initial_backlog"]) self.init_backlog_length = int(cmd["data"]["initial_backlog"])
client_file = "/tmp/fbclient_" + hashlib.sha224(cmd["data"]["email"].encode("utf-8")).hexdigest() client_file = "/tmp/fbclient_" + hashlib.sha224(cmd["data"]["email"].encode("utf-8")).hexdigest()
try: if "client_pickle" in cmd["data"]:
f = open(client_file, "rb") data = base64.b64decode(cmd["data"]["client_pickle"])
self.client = pickle.load(f) data = zlib.decompress(data)
f.close() self.client = pickle.loads(data)
sys.stderr.write("(python messenger) using previous client: {}\n".format(client_file)) else:
except:
self.client = None
if self.client is None:
email, password = cmd["data"]["email"], cmd["data"]["password"] email, password = cmd["data"]["email"], cmd["data"]["password"]
self.client = MessengerBridgeClient(email=email, password=password, max_tries=1) self.client = MessengerBridgeClient(email=email, password=password, max_tries=1)
## TODO: save client in new client_pickle config value
if not self.client.isLoggedIn(): if not self.client.isLoggedIn():
return {"_type": "rep_error", "error": "Unable to login (?)"} return {"_type": "rep_error", "error": "Unable to login (invalid pickle?)"}
try:
f = open(client_file, "wb")
pickle.dump(self.client, f)
f.close()
except:
pass
self.client.setBridge(self) self.client.setBridge(self)
@ -524,7 +517,25 @@ class MessengerBridge:
"data": {"name": new_title}, "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__": if __name__ == "__main__":
if "create_client_pickle" in sys.argv:
createClientPickle()
else:
bridge = MessengerBridge() bridge = MessengerBridge()
bridge.run() bridge.run()