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",
Description: "Password",
IsPassword: true,
Required: true,
},
&ConfigEntry{
Name: "client_pickle",
Description: "Client pickle (alternative login method)",
},
&ConfigEntry{
Name: "initial_backlog",

51
external/messenger.py vendored
View file

@ -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},
})
if __name__ == "__main__":
bridge = MessengerBridge()
bridge.run()
# ---- 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 "create_client_pickle" in sys.argv:
createClientPickle()
else:
bridge = MessengerBridge()
bridge.run()