2020-02-29 17:30:43 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
2020-02-29 19:47:44 +00:00
|
|
|
import signal
|
|
|
|
import threading
|
2020-03-01 12:08:32 +00:00
|
|
|
import queue
|
|
|
|
import pickle
|
2020-03-01 13:14:18 +00:00
|
|
|
import time
|
2020-03-01 14:19:09 +00:00
|
|
|
import traceback
|
|
|
|
from urllib.parse import unquote as UrlUnquote
|
2020-02-29 17:30:43 +00:00
|
|
|
|
|
|
|
import hashlib
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
import fbchat
|
2020-02-29 19:47:44 +00:00
|
|
|
from fbchat.models import *
|
2020-02-29 17:30:43 +00:00
|
|
|
|
|
|
|
# ---- MESSAGE TYPES ----
|
|
|
|
|
|
|
|
# ezbr -> external
|
|
|
|
CONFIGURE = "configure"
|
|
|
|
GET_USER = "get_user"
|
|
|
|
SET_USER_INFO = "set_user_info"
|
|
|
|
SET_ROOM_INFO = "set_room_info"
|
|
|
|
JOIN = "join"
|
|
|
|
INVITE = "invite"
|
|
|
|
LEAVE = "leave"
|
|
|
|
SEND = "send"
|
|
|
|
CLOSE = "close"
|
|
|
|
|
|
|
|
# external -> ezbr
|
|
|
|
JOINED = "joined"
|
|
|
|
LEFT = "left"
|
|
|
|
USER_INFO_UPDATED = "user_info_updated"
|
|
|
|
ROOM_INFO_UPDATED = "room_info_updated"
|
|
|
|
EVENT = "event"
|
|
|
|
CACHE_PUT = "cache_put"
|
|
|
|
CACHE_GET = "cache_get"
|
|
|
|
|
|
|
|
# reply messages
|
|
|
|
# ezbr -> external: all must wait for a reply!
|
|
|
|
# external -> ezbr: only CACHE_GET produces a reply
|
|
|
|
REP_OK = "rep_ok"
|
|
|
|
REP_ERROR = "rep_error"
|
|
|
|
|
2020-02-29 19:47:44 +00:00
|
|
|
# Event types
|
|
|
|
EVENT_JOIN = "join"
|
|
|
|
EVENT_LEAVE = "leave"
|
|
|
|
EVENT_MESSAGE = "message"
|
|
|
|
EVENT_ACTION = "action"
|
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-02-29 19:47:44 +00:00
|
|
|
|
|
|
|
def mediaObjectOfURL(url):
|
|
|
|
return {
|
|
|
|
"filename": url.split("?")[0].split("/")[-1],
|
|
|
|
"url": url,
|
|
|
|
}
|
|
|
|
|
2020-03-01 14:19:09 +00:00
|
|
|
def stripFbLinkPrefix(url):
|
|
|
|
PREFIX = "https://l.facebook.com/l.php?u="
|
|
|
|
if url[:len(PREFIX)] == PREFIX:
|
|
|
|
return UrlUnquote(url[len(PREFIX):].split('&')[0])
|
|
|
|
else:
|
|
|
|
return url
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
# ---- MESSENGER CLIENT CLASS THAT HANDLES EVENTS ----
|
|
|
|
|
|
|
|
class MessengerBridgeClient(fbchat.Client):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(MessengerBridgeClient, self).__init__(*args, **kwargs)
|
|
|
|
self.bridge = None
|
|
|
|
|
|
|
|
def setBridge(self, bridge):
|
|
|
|
self.bridge = bridge
|
|
|
|
|
|
|
|
## Redirect all interesting events to Bridge
|
|
|
|
def onMessage(self, *args, **kwargs):
|
|
|
|
self.bridge.onMessage(*args, **kwargs)
|
|
|
|
def onPeopleAdded(self, *args, **kwargs):
|
|
|
|
self.bridge.onPeopleAdded(*args, **kwargs)
|
|
|
|
def onPersonRemoved(self, *args, **kwargs):
|
|
|
|
self.bridge.onPersonRemoved(*args, **kwargs)
|
|
|
|
def onTitleChange(self, *args, **kwargs):
|
|
|
|
self.bridge.onTitleChange(*args, **kwargs)
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
# ---- SEPARATE THREADS FOR INITIAL SYNC & CLIENT LISTEN ----
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-02-29 19:47:44 +00:00
|
|
|
class InitialSyncThread(threading.Thread):
|
2020-03-01 13:30:51 +00:00
|
|
|
def __init__(self, client, bridge, threads, *args, **kwargs):
|
2020-02-29 19:47:44 +00:00
|
|
|
super(InitialSyncThread, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.client = client
|
|
|
|
self.bridge = bridge
|
2020-03-01 13:30:51 +00:00
|
|
|
self.threads = threads
|
2020-02-29 19:47:44 +00:00
|
|
|
|
|
|
|
def run(self):
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python) fb thread list: {}\n".format(self.threads))
|
2020-03-01 13:30:51 +00:00
|
|
|
|
|
|
|
for thread in self.threads:
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python) fb thread: {}\n".format(thread))
|
|
|
|
self.bridge.setup_joined_thread(thread)
|
2020-03-01 12:08:32 +00:00
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
|
|
|
|
class ClientListenThread(threading.Thread):
|
|
|
|
def __init__(self, client, *args, **kwargs):
|
|
|
|
super(ClientListenThread, self).__init__(*args, **kwargs)
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
self.client = client
|
|
|
|
|
|
|
|
def run(self):
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python messenger) Start client.listen()\n")
|
2020-03-01 13:14:18 +00:00
|
|
|
self.client.listen()
|
2020-02-29 17:30:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ---- MAIN LOOP THAT HANDLES REQUESTS FROM BRIDGE ----
|
|
|
|
|
|
|
|
class MessengerBridge:
|
2020-02-29 19:47:44 +00:00
|
|
|
def __init__(self):
|
2020-03-01 12:08:32 +00:00
|
|
|
self.rev_uid = {}
|
2020-03-01 12:27:29 +00:00
|
|
|
self.uid_map = {}
|
2020-03-01 19:38:00 +00:00
|
|
|
self.others_joined_map = {}
|
|
|
|
self.my_joined_rooms = {}
|
2020-03-01 14:48:58 +00:00
|
|
|
self.init_backlog_length = 100
|
2020-03-01 12:08:32 +00:00
|
|
|
|
|
|
|
def getUserId(self, user):
|
2020-03-01 13:30:51 +00:00
|
|
|
retval = None
|
2020-03-01 12:08:32 +00:00
|
|
|
if user.url is not None and not "?" in user.url:
|
2020-03-01 14:19:09 +00:00
|
|
|
retval = user.url.split("/")[-1]
|
2020-03-01 12:08:32 +00:00
|
|
|
else:
|
2020-03-01 13:30:51 +00:00
|
|
|
retval = user.uid
|
2020-03-01 13:14:18 +00:00
|
|
|
|
2020-03-01 13:30:51 +00:00
|
|
|
if user.uid not in self.uid_map:
|
|
|
|
self.uid_map[user.uid] = retval
|
2020-03-01 14:19:09 +00:00
|
|
|
self.rev_uid[retval] = user.uid
|
2020-03-01 13:30:51 +00:00
|
|
|
|
|
|
|
user_info = {
|
|
|
|
"display_name": user.name,
|
|
|
|
}
|
|
|
|
if user.photo is not None:
|
|
|
|
user_info["avatar"] = mediaObjectOfURL(user.photo)
|
2020-03-01 13:48:42 +00:00
|
|
|
self.write({
|
2020-03-01 13:30:51 +00:00
|
|
|
"_type": USER_INFO_UPDATED,
|
2020-03-01 13:48:42 +00:00
|
|
|
"user": self.getUserId(user),
|
2020-03-01 13:30:51 +00:00
|
|
|
"data": user_info,
|
|
|
|
})
|
|
|
|
|
|
|
|
return retval
|
2020-03-01 13:14:18 +00:00
|
|
|
|
|
|
|
def getUserIdFromUid(self, uid):
|
|
|
|
if uid in self.uid_map:
|
|
|
|
return self.uid_map[uid]
|
|
|
|
else:
|
|
|
|
user = self.client.fetchUserInfo(uid)[uid]
|
|
|
|
return self.getUserId(user)
|
2020-03-01 12:08:32 +00:00
|
|
|
|
|
|
|
def revUserId(self, user_id):
|
|
|
|
if user_id in self.rev_uid:
|
|
|
|
return self.rev_uid[user_id]
|
|
|
|
else:
|
|
|
|
return user_id
|
|
|
|
|
|
|
|
def getUserShortName(self, user):
|
|
|
|
if user.first_name != None:
|
|
|
|
return user.first_name
|
|
|
|
else:
|
|
|
|
return user.name
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
def run(self):
|
|
|
|
self.client = None
|
|
|
|
self.keep_running = True
|
2020-03-01 12:08:32 +00:00
|
|
|
self.cache_gets = {}
|
|
|
|
self.num = 0
|
2020-02-29 17:30:43 +00:00
|
|
|
|
|
|
|
while self.keep_running:
|
2020-03-01 13:14:18 +00:00
|
|
|
try:
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
except KeyboardInterrupt:
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python messenger) shutting down")
|
2020-03-01 13:14:18 +00:00
|
|
|
self.close()
|
|
|
|
time.sleep(5)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
sys.stderr.write("(python) reading {}\n".format(line.strip()))
|
|
|
|
cmd = json.loads(line)
|
|
|
|
|
|
|
|
try:
|
|
|
|
rep = self.handle_cmd(cmd)
|
|
|
|
if rep is None:
|
|
|
|
rep = {}
|
|
|
|
if "_type" not in rep:
|
|
|
|
rep["_type"] = REP_OK
|
|
|
|
except Exception as e:
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python) {}\n".format(traceback.format_exc()))
|
2020-02-29 17:30:43 +00:00
|
|
|
rep = {
|
|
|
|
"_type": REP_ERROR,
|
|
|
|
"error": "{}".format(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
rep["_id"] = cmd["_id"]
|
|
|
|
self.write(rep)
|
|
|
|
|
|
|
|
def write(self, msg):
|
|
|
|
msgstr = json.dumps(msg)
|
|
|
|
sys.stderr.write("(python) writing {}\n".format(msgstr))
|
|
|
|
sys.stdout.write(msgstr + "\n")
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
def handle_cmd(self, cmd):
|
|
|
|
ty = cmd["_type"]
|
|
|
|
if ty == CONFIGURE:
|
2020-03-01 14:48:58 +00:00
|
|
|
self.init_backlog_length = int(cmd["data"]["initial_backlog"])
|
2020-03-01 12:08:32 +00:00
|
|
|
client_file = "/tmp/fbclient_" + hashlib.sha224(cmd["data"]["email"].encode("utf-8")).hexdigest()
|
2020-02-29 17:30:43 +00:00
|
|
|
|
|
|
|
try:
|
2020-03-01 12:08:32 +00:00
|
|
|
f = open(client_file, "rb")
|
|
|
|
self.client = pickle.load(f)
|
2020-02-29 17:30:43 +00:00
|
|
|
f.close()
|
2020-03-01 12:08:32 +00:00
|
|
|
sys.stderr.write("(python messenger) using previous client: {}\n".format(client_file))
|
2020-02-29 17:30:43 +00:00
|
|
|
except:
|
2020-03-01 12:08:32 +00:00
|
|
|
self.client = None
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-03-01 12:08:32 +00:00
|
|
|
if self.client is None:
|
|
|
|
email, password = cmd["data"]["email"], cmd["data"]["password"]
|
2020-03-01 13:14:18 +00:00
|
|
|
self.client = MessengerBridgeClient(email=email, password=password, max_tries=1)
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-03-01 13:30:51 +00:00
|
|
|
if not self.client.isLoggedIn():
|
|
|
|
return {"_type": "ret_error", "error": "Unable to login (?)"}
|
|
|
|
|
|
|
|
try:
|
|
|
|
f = open(client_file, "wb")
|
|
|
|
pickle.dump(self.client, f)
|
|
|
|
f.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.client.setBridge(self)
|
|
|
|
|
|
|
|
threads = self.client.fetchThreadList()
|
|
|
|
# ensure we have a correct mapping for bridged user IDs to fb uids
|
|
|
|
# (this should be fast)
|
|
|
|
for thread in threads:
|
2020-03-01 14:19:09 +00:00
|
|
|
if thread.type == ThreadType.USER:
|
2020-03-01 13:30:51 +00:00
|
|
|
self.getUserId(thread)
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-03-01 13:30:51 +00:00
|
|
|
InitialSyncThread(self.client, self, threads).start()
|
|
|
|
ClientListenThread(self.client).start()
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
elif ty == CLOSE:
|
2020-03-01 13:14:18 +00:00
|
|
|
self.close()
|
2020-02-29 17:30:43 +00:00
|
|
|
|
2020-02-29 19:47:44 +00:00
|
|
|
elif ty == GET_USER:
|
2020-03-01 13:14:18 +00:00
|
|
|
userId = self.getUserIdFromUid(self.client.uid)
|
|
|
|
return {"_type": REP_OK, "user": userId}
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-03-01 19:38:00 +00:00
|
|
|
elif ty == JOIN:
|
|
|
|
self.ensure_i_joined(cmd["room"])
|
|
|
|
|
|
|
|
elif ty == LEAVE:
|
|
|
|
thread_id = cmd["room"]
|
|
|
|
self.client.removeUserFromGroup(self.client.uid, thread_id)
|
|
|
|
if thread_id in self.my_joined_rooms:
|
|
|
|
del self.my_joined_rooms[thread_id]
|
|
|
|
|
|
|
|
elif ty == INVITE:
|
|
|
|
if cmd["room"] != "":
|
|
|
|
uid = self.revUserId(cmd["user"])
|
|
|
|
self.client.addUsersToGroup([uid], cmd["room"])
|
2020-03-01 12:08:32 +00:00
|
|
|
|
|
|
|
elif ty == SEND:
|
|
|
|
event = cmd["data"]
|
|
|
|
if event["type"] in [EVENT_MESSAGE, EVENT_ACTION]:
|
2020-03-01 14:19:09 +00:00
|
|
|
attachments = []
|
|
|
|
if "attachments" in event and isinstance(event["attachments"], list):
|
|
|
|
for at in event["attachments"]:
|
|
|
|
if "url" in at:
|
|
|
|
attachments.append(at["url"])
|
|
|
|
else:
|
|
|
|
# TODO
|
|
|
|
sys.stdout.write("Unhandled: attachment without URL")
|
|
|
|
|
2020-03-01 12:08:32 +00:00
|
|
|
msg = Message(event["text"])
|
|
|
|
if event["type"] == EVENT_ACTION:
|
|
|
|
msg.text = "* " + event["text"]
|
|
|
|
|
|
|
|
if event["room"] != "":
|
2020-03-01 14:19:09 +00:00
|
|
|
if len(attachments) > 0:
|
|
|
|
msg_id = self.client.sendRemoteFiles(attachments, message=msg, thread_id=event["room"], thread_type=ThreadType.GROUP)
|
|
|
|
else:
|
|
|
|
msg_id = self.client.send(msg, thread_id=event["room"], thread_type=ThreadType.GROUP)
|
2020-03-01 12:08:32 +00:00
|
|
|
elif event["recipient"] != "":
|
|
|
|
uid = self.revUserId(event["recipient"])
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python) Sending to {}\n".format(uid))
|
2020-03-01 14:19:09 +00:00
|
|
|
if len(attachments) > 0:
|
|
|
|
msg_id = self.client.sendRemoteFiles(attachments, message=msg, thread_id=uid, thread_type=ThreadType.USER)
|
|
|
|
else:
|
|
|
|
msg_id = self.client.send(msg, thread_id=uid, thread_type=ThreadType.USER)
|
2020-03-01 12:08:32 +00:00
|
|
|
else:
|
|
|
|
return {"_type": REP_ERROR, "error": "Invalid message"}
|
|
|
|
|
|
|
|
return {"_type": REP_OK, "event_id": msg_id}
|
|
|
|
|
|
|
|
elif ty == REP_OK and cmd["_id"] in self.cache_gets:
|
|
|
|
self.cache_gets[cmd["_id"]].put(cmd["value"])
|
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
else:
|
|
|
|
return {"_type": REP_ERROR, "error": "Not implemented"}
|
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
def close(self):
|
|
|
|
self.keep_running = False
|
|
|
|
self.client.stopListening()
|
|
|
|
|
2020-03-01 12:08:32 +00:00
|
|
|
def cache_get(self, key):
|
|
|
|
self.num += 1
|
|
|
|
num = self.num
|
|
|
|
q = queue.Queue(1)
|
|
|
|
self.cache_gets[num] = q
|
|
|
|
self.write({"_type": CACHE_GET, "_id": num, "key": key})
|
2020-03-01 19:38:00 +00:00
|
|
|
try:
|
|
|
|
rep = q.get(block=True, timeout=30)
|
|
|
|
except queue.Empty:
|
|
|
|
rep = ""
|
2020-03-01 12:08:32 +00:00
|
|
|
del self.cache_gets[num]
|
|
|
|
return rep
|
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
def cache_put(self, key, value):
|
|
|
|
self.write({"_type": CACHE_PUT, "key": key, "value": value})
|
2020-03-01 12:27:29 +00:00
|
|
|
|
2020-03-01 19:38:00 +00:00
|
|
|
# ---- Info sync ----
|
|
|
|
|
|
|
|
def ensure_i_joined(self, thread_id):
|
|
|
|
if thread_id not in self.my_joined_rooms:
|
2020-03-01 21:12:43 +00:00
|
|
|
self.my_joined_rooms[thread_id] = True
|
|
|
|
|
2020-03-01 19:38:00 +00:00
|
|
|
thread = self.client.fetchThreadInfo(thread_id)[thread_id]
|
|
|
|
self.setup_joined_thread(thread)
|
|
|
|
|
|
|
|
def setup_joined_thread(self, thread):
|
|
|
|
if thread.type == ThreadType.GROUP:
|
|
|
|
members = self.client.fetchAllUsersFromThreads([thread])
|
|
|
|
|
|
|
|
self.write({
|
|
|
|
"_type": JOINED,
|
|
|
|
"room": thread.uid,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.send_room_info(thread, members)
|
|
|
|
self.send_room_members(thread, members)
|
|
|
|
|
|
|
|
self.backlog_room(thread)
|
|
|
|
|
|
|
|
|
2020-03-01 14:48:58 +00:00
|
|
|
def send_room_info(self, thread, members):
|
2020-03-01 21:12:43 +00:00
|
|
|
members.sort(key=lambda m: m.uid)
|
|
|
|
|
2020-03-01 14:48:58 +00:00
|
|
|
room_info = {}
|
|
|
|
if thread.name is not None:
|
|
|
|
room_info["name"] = thread.name
|
|
|
|
else:
|
|
|
|
who = [m for m in members if m.uid != self.client.uid]
|
|
|
|
if len(who) > 3:
|
|
|
|
room_info["name"] = ", ".join([self.getUserShortName(m) for m in who[:3]] + ["..."])
|
|
|
|
else:
|
|
|
|
room_info["name"] = ", ".join([self.getUserShortName(m) for m in who])
|
|
|
|
|
|
|
|
if thread.photo is not None:
|
|
|
|
room_info["picture"] = mediaObjectOfURL(thread.photo)
|
|
|
|
else:
|
|
|
|
for m in members:
|
|
|
|
if m.uid != self.client.uid and m.photo is not None:
|
|
|
|
room_info["picture"] = mediaObjectOfURL(m.photo)
|
|
|
|
break
|
|
|
|
|
|
|
|
self.write({
|
|
|
|
"_type": ROOM_INFO_UPDATED,
|
|
|
|
"room": thread.uid,
|
|
|
|
"data": room_info,
|
|
|
|
})
|
|
|
|
|
|
|
|
def send_room_members(self, thread, members):
|
|
|
|
for member in members:
|
2020-03-01 19:38:00 +00:00
|
|
|
sys.stderr.write("(python) fb thread member: {}\n".format(member))
|
2020-03-01 14:48:58 +00:00
|
|
|
self.ensureJoined(self.getUserId(member), thread.uid)
|
|
|
|
|
2020-03-01 19:38:00 +00:00
|
|
|
def backlog_room(self, thread):
|
|
|
|
prev_last_seen = self.cache_get("last_seen_%s"%thread.uid)
|
|
|
|
if prev_last_seen == "":
|
|
|
|
prev_last_seen = None
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
found = False
|
|
|
|
while not found:
|
|
|
|
before = None
|
|
|
|
if len(messages) > 0:
|
|
|
|
before = messages[-1].timestamp
|
|
|
|
page = self.client.fetchThreadMessages(thread.uid, before=before, limit=20)
|
|
|
|
for m in page:
|
|
|
|
if m.uid == prev_last_seen or len(messages) > self.init_backlog_length:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
messages.append(m)
|
|
|
|
|
|
|
|
for m in reversed(messages):
|
|
|
|
if m.text is None:
|
|
|
|
m.text = ""
|
|
|
|
m.text = "[{}] {}".format(
|
|
|
|
time.strftime("%Y-%m-%d %H:%M %Z", time.localtime(float(m.timestamp)/1000)).strip(),
|
|
|
|
m.text)
|
|
|
|
self.onMessage(thread_id=thread.uid,
|
|
|
|
thread_type=thread.type,
|
|
|
|
message_object=m)
|
|
|
|
|
2020-03-01 13:30:51 +00:00
|
|
|
def ensureJoined(self, userId, room):
|
|
|
|
key = "{}--{}".format(userId, room)
|
2020-03-01 19:38:00 +00:00
|
|
|
if not key in self.others_joined_map:
|
2020-03-01 13:48:42 +00:00
|
|
|
self.write({
|
2020-03-01 13:30:51 +00:00
|
|
|
"_type": EVENT,
|
|
|
|
"data": {
|
|
|
|
"type": EVENT_JOIN,
|
|
|
|
"author": userId,
|
|
|
|
"room": room,
|
|
|
|
}
|
|
|
|
})
|
2020-03-01 19:38:00 +00:00
|
|
|
self.others_joined_map[key] = True
|
|
|
|
|
|
|
|
# ---- Event handlers ----
|
2020-03-01 13:30:51 +00:00
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
def onMessage(self, thread_id, thread_type, message_object, **kwargs):
|
2020-03-01 19:38:00 +00:00
|
|
|
self.ensure_i_joined(thread_id)
|
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
if message_object.author == self.client.uid:
|
|
|
|
# Ignore our own messages
|
|
|
|
return
|
|
|
|
|
2020-03-01 13:48:42 +00:00
|
|
|
sys.stderr.write("(python messenger) Got message: {}\n".format(message_object))
|
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
author = self.getUserIdFromUid(message_object.author)
|
|
|
|
|
|
|
|
event = {
|
2020-03-01 21:12:43 +00:00
|
|
|
"id": message_object.uid,
|
2020-03-01 13:14:18 +00:00
|
|
|
"type": EVENT_MESSAGE,
|
|
|
|
"author": author,
|
|
|
|
"text": message_object.text,
|
2020-03-01 13:48:42 +00:00
|
|
|
"attachments": []
|
2020-03-01 13:14:18 +00:00
|
|
|
}
|
2020-03-01 14:19:09 +00:00
|
|
|
if event["text"] is None:
|
|
|
|
event["text"] = ""
|
|
|
|
|
2020-03-01 13:48:42 +00:00
|
|
|
for at in message_object.attachments:
|
|
|
|
if isinstance(at, ImageAttachment):
|
|
|
|
full_url = self.client.fetchImageUrl(at.uid)
|
|
|
|
event["attachments"].append({
|
|
|
|
"filename": full_url.split("?")[0].split("/")[-1],
|
|
|
|
"url": full_url,
|
|
|
|
"image_size": {
|
|
|
|
"width": at.width,
|
|
|
|
"height": at.height,
|
|
|
|
},
|
|
|
|
})
|
2020-03-01 14:19:09 +00:00
|
|
|
elif isinstance(at, FileAttachment):
|
|
|
|
url = stripFbLinkPrefix(at.url)
|
|
|
|
event["attachments"].append({
|
|
|
|
"filename": at.name,
|
|
|
|
"url": url,
|
|
|
|
})
|
|
|
|
elif isinstance(at, AudioAttachment):
|
|
|
|
url = stripFbLinkPrefix(at.url)
|
2020-03-01 13:48:42 +00:00
|
|
|
event["attachments"].append({
|
2020-03-01 14:19:09 +00:00
|
|
|
"filename": at.filename,
|
|
|
|
"url": url,
|
2020-03-01 13:48:42 +00:00
|
|
|
})
|
|
|
|
else:
|
|
|
|
event["text"] += "\nUnhandled attachment: {}".format(at)
|
|
|
|
|
2020-03-01 13:14:18 +00:00
|
|
|
if thread_type == ThreadType.GROUP:
|
|
|
|
event["room"] = thread_id
|
2020-03-01 13:30:51 +00:00
|
|
|
self.ensureJoined(author, thread_id)
|
|
|
|
|
2020-03-01 19:38:00 +00:00
|
|
|
if event["text"] != "" or len(event["attachments"]) > 0:
|
|
|
|
self.write({"_type": EVENT, "data": event})
|
2020-03-01 13:14:18 +00:00
|
|
|
|
|
|
|
self.cache_put("last_seen_%s"%thread_id, message_object.uid)
|
|
|
|
|
2020-03-01 14:38:54 +00:00
|
|
|
def onPeopleAdded(self, added_ids, thread_id, *args, **kwargs):
|
|
|
|
for user_id in added_ids:
|
2020-03-01 19:38:00 +00:00
|
|
|
if user_id == self.client.uid:
|
|
|
|
self.ensure_i_joined(thread_id)
|
|
|
|
else:
|
|
|
|
self.ensureJoined(self.getUserIdFromUid(user_id), thread_id)
|
2020-03-01 14:38:54 +00:00
|
|
|
|
|
|
|
def onPersonRemoved(self, removed_id, thread_id, *args, **kwargs):
|
2020-03-01 19:38:00 +00:00
|
|
|
if removed_id == self.client.uid:
|
|
|
|
self.write({
|
|
|
|
"_type": LEFT,
|
2020-03-01 14:38:54 +00:00
|
|
|
"room": thread_id,
|
2020-03-01 19:38:00 +00:00
|
|
|
})
|
|
|
|
if thread_id in self.my_joined_rooms:
|
|
|
|
del self.my_joined_rooms[thread_id]
|
|
|
|
else:
|
|
|
|
userId = self.getUserIdFromUid(removed_id),
|
|
|
|
self.write({
|
|
|
|
"_type": EVENT,
|
|
|
|
"data": {
|
|
|
|
"type": EVENT_JOIN,
|
|
|
|
"author": userId,
|
|
|
|
"room": thread_id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
map_key = "{}--{}".format(userId, thread_id)
|
|
|
|
if map_key in self.others_joined_map:
|
|
|
|
del self.others_joined_map[map_key]
|
2020-03-01 14:38:54 +00:00
|
|
|
|
|
|
|
def onTitleChange(self, author_id, new_title, thread_id, thread_type, *args, **kwargs):
|
2020-03-01 19:38:00 +00:00
|
|
|
self.ensure_i_joined(thread_id)
|
2020-03-01 14:38:54 +00:00
|
|
|
if thread_type == ThreadType.GROUP:
|
2020-03-01 19:38:00 +00:00
|
|
|
self.write({
|
2020-03-01 14:38:54 +00:00
|
|
|
"_type": ROOM_INFO_UPDATED,
|
|
|
|
"room": thread_id,
|
|
|
|
"data": {"name": new_title},
|
|
|
|
})
|
2020-03-01 12:27:29 +00:00
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
bridge = MessengerBridge()
|
|
|
|
bridge.run()
|
|
|
|
|