-
Notifications
You must be signed in to change notification settings - Fork 5
/
matrix.py
268 lines (201 loc) · 7.25 KB
/
matrix.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import time
import logging
from markdown import Markdown
from errbot.errBot import ErrBot
from errbot.backends.base import Message, Person, Room, RoomOccupant
from errbot.backends.base import Identifier
log = logging.getLogger('errbot.backends.matrix')
try:
from matrix_client.client import MatrixClient
from matrix_client.api import MatrixRequestError
from matrix_client.api import MatrixHttpApi
except ImportError as _:
log.exception("Could not start the Matrix backend.")
log.fatal("""
If you intend to use the Matrix backend please install
matrix-client from PyPI.
""")
sys.exit(1)
class MatrixIdentifier(Identifier):
def __init__(self, id):
self._id = id
@property
def id(self):
return self._id
def __unicode__(self):
return str(self._id)
def __eq__(self, other):
return self._id == other.id
__str__ = __unicode__
aclattr = id
class MatrixPerson(Person):
def __init__(self, api, user_id=None, room_id=None):
self._userid = user_id
self._roomid = room_id
self._api = api
@property
def userid(self):
return self._userid
@property
def username(self):
return self._api.get_display_name(self._userid)
@property
def channelid(self):
self._roomid
@property
def channelname(self):
return self._api.get_room_name(self._roomid)
@property
def client(self):
# TODO: Check if this can be acquired.
return None
@property
def fullname(self):
return self.username
@property
def nick(self):
return self._nick
def __unicode__(self):
return "@%s" % self._username
def __str__(self):
return self.__unicode__()
@property
def person(self):
return "%s" % self._userid
# Override for ACLs
@property
def aclattr(self):
return "@%s" % self.username
# Compatibility with the generic API.
client = channelid
nick = username
class MatrixRoomOccupant(MatrixPerson, RoomOccupant):
def __init__(self, api, room, user_id=None, nick=None):
super().__init__(api, user_id, nick)
self._room = room
@property
def room(self):
return self._room
def __unicode__(self):
return "Room Occupant"
class MatrixRoom(MatrixIdentifier, Room):
def __init__(self, id):
super().__init__(id)
@property
def id(self):
return self._id
def join(self, username=None, password=None):
log.debug("Joining room %s" % self.id)
def __unicode__(self):
return "Room"
class MatrixBackend(ErrBot):
def __init__(self, config):
super().__init__(config)
if not hasattr(config, 'MATRIX_HOMESERVER'):
log.fatal("""
You need to specify a homeserver to connect to in
config.MATRIX_HOMESERVER.
For example:
MATRIX_HOMESERVER = "https://matrix.org"
""")
sys.exit(1)
self._homeserver = config.MATRIX_HOMESERVER
self._username = config.BOT_IDENTITY['username']
self._password = config.BOT_IDENTITY['password']
self._api = None
self._token = None
def serve_once(self):
def dispatch_event(event):
log.info("Received event: %s" % event)
if event['type'] == "m.room.member":
if event['membership'] == "invite" and event['state_key'] == self._client.user_id:
room_id = event['room_id']
self._client.join_room(room_id)
log.info("Auto-joined room: %s" % room_id)
if event['type'] == "m.room.message" and event['sender'] != self._client.user_id:
sender = event['sender']
room_id = event['room_id']
body = event['content']['body']
log.info("Received message from %s in room %s" % (sender, room_id))
# msg = Message(body)
# msg.frm = MatrixPerson(self._client, sender, room_id)
# msg.to = MatrixPerson(self._client, self._client.user_id, room_id)
# self.callback_message(msg)
msg = self.build_message(body)
room = MatrixRoom(room_id)
msg.frm = MatrixRoomOccupant(self._api, room, sender)
msg.to = room
self.callback_message(msg)
self.reset_reconnection_count()
self.connect_callback()
self._client = MatrixClient(self._homeserver)
try:
self._token = self._client.register_with_password(self._username,
self._password,)
except MatrixRequestError as e:
if e.code == 400:
try:
self._token = self._client.login_with_password(self._username,
self._password,)
except MatrixRequestError:
log.fatal("""
Incorrect username or password specified in
config.BOT_IDENTITY['username'] or config.BOT_IDENTITY['password'].
""")
sys.exit(1)
self._api = MatrixHttpApi(self._homeserver, self._token)
self.bot_identifier = MatrixPerson(self._api)
self._client.add_listener(dispatch_event)
try:
while True:
self._client.listen_for_events()
except KeyboardInterrupt:
log.info("Interrupt received, shutting down...")
return True
finally:
self.disconnect_callback()
def rooms(self):
rooms = []
raw_rooms = self._client.get_rooms()
for rid, robject in raw_rooms:
# TODO: Get the canonical alias rather than the first one from
# `Room.aliases`.
log.debug('Found room %s (aka %s)' % (rid, rid.aliases[0]))
def send_message(self, mess):
super().send_message(mess)
room_id = mess.to.room.id
text_content = item_url = mess.body
if item_url.startswith("http://") or item_url.startswith("https://"):
if item_url.endswith("gif"):
self._api.send_content(room_id, item_url, "image", "m.image")
return
# text_content = Markdown().convert(mess.body)
self._api.send_message(room_id, text_content)
def connect_callback(self):
super().connect_callback()
def build_identifier(self, txtrep):
raise Exception(
"XXX"
)
def build_reply(self, mess, text=None, private=False):
log.info("build_reply")
response = self.build_message(text)
response.frm = self.bot_identifier
response.to = mess.frm
return response
def change_presence(self, status: str = '', message: str = ''):
raise Exception(
"XXX"
)
@property
def mode(self):
return 'matrix'
def query_room(self, room):
raise Exception(
"XXX"
)