aboutsummaryrefslogtreecommitdiff
path: root/external/messenger.py
blob: 6ec6e9cf059f5962d0fb8ef684ef12e1cd4b01d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3

import sys
import json
import signal
import threading
import queue
import pickle

import hashlib

import fbchat
from fbchat.models import *

# ---- 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"

# Event types
EVENT_JOIN = "join"
EVENT_LEAVE = "leave"
EVENT_MESSAGE = "message"
EVENT_ACTION = "action"


# ---- MESSENGER CLIENT CLASS THAT HANDLES EVENTS ----


def mediaObjectOfURL(url):
    return {
            "filename": url.split("?")[0].split("/")[-1],
            "url": url,
            }


# class MessengerBridgeClient(fbchat.Client):
#     def __init__(self, bridge, *args, **kwargs):
#         super(MessengerBridgeClient, self).__init__(*args, **kwargs)
# 
#     # TODO: handle events


class InitialSyncThread(threading.Thread):
    def __init__(self, client, bridge, *args, **kwargs):
        super(InitialSyncThread, self).__init__(*args, **kwargs)

        self.client = client
        self.bridge = bridge

    def run(self):
        threads = self.client.fetchThreadList()
        sys.stderr.write("fb thread list: {}\n".format(threads))
        for thread in threads:
            sys.stderr.write("fb thread: {}\n".format(thread))
            if thread.type == ThreadType.GROUP:
                members = self.client.fetchAllUsersFromThreads([thread])

                self.bridge.write({
                    "_type": JOINED,
                    "room": thread.uid,
                })
                
                self.send_room_info(thread, members)
                self.send_room_members(thread, members)
            elif thread.type == ThreadType.USER:
                self.bridge.getUserId(thread)

            self.backlog_room(thread)


    def send_room_info(self, thread, members):
        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.bridge.getUserShortName(m) for m in who[:3]] + ["..."])
            else:
                room_info["name"] = ", ".join([self.bridge.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.bridge.write({
            "_type": ROOM_INFO_UPDATED,
            "room": thread.uid,
            "data": room_info,
        })

    def send_room_members(self, thread, members):
        for member in members:
            sys.stderr.write("fb thread member: {}\n".format(member))
            self.bridge.write({
                "_type": EVENT,
                "data": {
                    "type": EVENT_JOIN,
                    "author": self.bridge.getUserId(member),
                    "room": thread.uid,
                }
            })

            user_info = {
                "display_name": member.name,
            }
            if member.photo is not None:
                user_info["avatar"] = mediaObjectOfURL(member.photo)
            self.bridge.write({
                "_type": USER_INFO_UPDATED,
                "user": self.bridge.getUserId(member),
                "data": user_info,
            })

    def backlog_room(self, thread):
        pass # TODO



# ---- MAIN LOOP THAT HANDLES REQUESTS FROM BRIDGE ----

class MessengerBridge:
    def __init__(self):
        self.rev_uid = {}

    def getUserId(self, user):
        if user.url is not None and not "?" in user.url:
            user_id = user.url.split("/")[-1]
            self.rev_uid[user_id] = user.uid
            return user_id
        else:
            return user.uid

    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

    def run(self):
        self.client = None
        self.keep_running = True
        self.cache_gets = {}
        self.num = 0

        while self.keep_running:
            line = sys.stdin.readline()
            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:
                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:
            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:
                email, password = cmd["data"]["email"], cmd["data"]["password"]
                self.client = fbchat.Client(email=email, password=password, max_tries=1)

            if self.client.isLoggedIn():
                try:
                    f = open(client_file, "wb")
                    pickle.dump(self.client, f)
                    f.close()
                except:
                    pass

                InitialSyncThread(self.client, self).start()

        elif ty == CLOSE:
            self.keep_running = False

        elif ty == GET_USER:
            return {"_type": REP_OK, "user": self.client.uid}

        elif ty == INVITE and cmd["room"] == "":
            return {"_type": REP_OK}

        elif ty == SEND:
            event = cmd["data"]
            if event["type"] in [EVENT_MESSAGE, EVENT_ACTION]:
                # TODO: attachments
                msg = Message(event["text"])
                if event["type"] == EVENT_ACTION:
                    msg.text = "* " + event["text"]

                if event["room"] != "":
                    msg_id = self.client.send(msg, thread_id=event["room"], thread_type=ThreadType.GROUP)
                elif event["recipient"] != "":
                    uid = self.revUserId(event["recipient"])
                    msg_id = self.client.send(msg, thread_id=uid, thread_type=ThreadType.USER)
                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"])

        else:
            return {"_type": REP_ERROR, "error": "Not implemented"}

    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})
        rep = q.get(block=True, timeout=30)
        del self.cache_gets[num]
        return rep


if __name__ == "__main__":
    bridge = MessengerBridge()
    bridge.run()