forked from zulip/errbot-backend-zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zulip.py
380 lines (316 loc) · 13.1 KB
/
zulip.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import logging
import sys
from errbot.backends.base import RoomError, Identifier, Person, RoomOccupant, Stream, ONLINE, Room
from errbot.core import ErrBot
from errbot.rendering.ansiext import enable_format, TEXT_CHRS
# Can't use __name__ because of Yapsy.
log = logging.getLogger('errbot.backends.zulip')
ZULIP_MESSAGE_SIZE_LIMIT = 10000
try:
from zulip import Client
except ImportError:
log.exception("Could not start the Zulip back-end")
log.fatal(
"You need to install the Zulip support in order "
"to use the Zulip backend.\n"
"You should be able to install this package using:\n"
"pip install zulip"
)
sys.exit(1)
def parse_query_result(query_result):
if query_result['result'] == 'success':
return query_result
else:
raise ZulipRoomError("{}: {}".format(query_result['code'], query_result['msg']))
class ZulipRoomError(RoomError):
def __init__(self, message=None):
if message is None:
message = (
"I currently do not support this request :(\n"
"I still love you."
)
super().__init__(message)
class ZulipIdentifier(Identifier):
def __init__(self, id):
self._id = str(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__
# `ZulipPerson` is used for both 1-1 PMs and Group PMs.
class ZulipPerson(ZulipIdentifier, Person):
def __init__(self, id, full_name, emails, client):
super().__init__(id)
self._full_name = full_name
self._emails = emails
self._client = client
@property
def person(self):
return self._id
@property
def fullname(self):
return self._full_name
@property
def nick(self):
return None
@property
def client(self):
return self._client
@property
def emails(self):
return self._emails
@property
def aclattr(self):
return ','.join(sorted(self._emails))
# `ZulipRoom` is used for messages to streams.
class ZulipRoom(ZulipIdentifier, Room):
def __init__(self, title, id=None, subject=None, client=None):
super().__init__(id)
self._title = title
self._subject = subject
self._client = client
@property
def id(self):
return self._id
@property
def aclattr(self):
return self._id
# A stream in Zulip can have multiple subjects. However, since
# a `ZulipRoom` object is used to identify the message recipient,
# it also needs to include the subject where a message will be
# sent to.
@property
def subject(self):
return self._subject
@property
def title(self):
return self._title
def join(self, username: str=None, password: str=None):
parse_query_result(self._client.add_subscriptions([{"name": self._title}]))
def create(self):
raise ZulipRoomError()
def leave(self, reason: str=None):
parse_query_result(self._client.remove_subscriptions([self._title]))
def destroy(self):
raise ZulipRoomError()
@property
def joined(self):
result = parse_query_result(self._client.get_subscribers(stream=self._title))
return self._client.email in result['subscribers']
@property
def exists(self):
result = parse_query_result(self._client.get_streams(include_public=True, include_subscribed=False))
return any([stream['name'] == self._title for stream in result['streams']])
@property
def topic(self):
result = parse_query_result(self._client.get_streams(include_public=True, include_subscribed=False))
try:
return next(stream['description'] for stream in result['streams'] if stream['name'] == self._title)
except StopIteration():
raise ZulipRoomError("The stream {} could not be found.".format(self._title))
@property
def occupants(self):
result = parse_query_result(self._client.get_subscribers(stream=self._title))
return [ZulipRoomOccupant(id=email, full_name=None, emails=[email],
client=self._client, room=self) for email in result['subscribers']]
def invite(self, *args):
raise ZulipRoomError()
class ZulipRoomOccupant(ZulipPerson, RoomOccupant):
"""
This class represents a person subscribed to a stream.
"""
def __init__(self, id, full_name, emails, client, room):
super().__init__(id=id, full_name=full_name, emails=emails, client=client)
self._room = room
@property
def room(self):
return self._room
class ZulipBackend(ErrBot):
def __init__(self, config):
super().__init__(config)
config.MESSAGE_SIZE_LIMIT = ZULIP_MESSAGE_SIZE_LIMIT
self.identity = config.BOT_IDENTITY
for key in ('email', 'key', 'site'):
if key not in self.identity:
log.fatal(
"You need to supply the key `{}` for me to use. `{key}` and its value "
"can be found in your bot's `zuliprc` config file.".format(key)
)
sys.exit(1)
compact = config.COMPACT_OUTPUT if hasattr(config, 'COMPACT_OUTPUT') else False
enable_format('text', TEXT_CHRS, borders=not compact)
self.client = Client(email=self.identity['email'],
api_key=self.identity['key'],
site=self.identity['site'])
def serve_once(self):
self.bot_identifier = self.build_identifier(self.client.email)
log.info("Initializing connection")
self.client.ensure_session()
log.info("Connected")
self.reset_reconnection_count()
self.connect_callback()
try:
self.client.call_on_each_message(self._handle_message)
except KeyboardInterrupt:
log.info("Interrupt received, shutting down..")
return True # True means shutdown was requested.
except Exception:
log.exception("Error reading from Zulip updates stream.")
raise
finally:
log.debug("Triggering disconnect callback.")
self.disconnect_callback()
def _handle_message(self, message):
"""
Handles incoming messages.
In Zulip, there are three types of messages: Private messages, Private group messages,
and Stream messages. This plugin handles Group PMs as normal PMs between the bot and the
user. Stream messages are handled as messages to rooms.
"""
if not message['content']:
log.warning("Unhandled message type (not a text message) ignored")
return
message_instance = self.build_message(message['content'])
if message['type'] == 'private':
message_instance.frm = ZulipPerson(
id=message['sender_email'],
full_name=message['sender_full_name'],
emails=[message['sender_email']],
client=message['client']
)
message_instance.to = ZulipPerson(
id=message['sender_email'],
full_name=','.join([recipient['full_name'] for recipient in message['display_recipient']]),
emails=[recipient['email'] for recipient in message['display_recipient']],
client=None
)
elif message['type'] == 'stream':
room = ZulipRoom(
id=message['display_recipient'],
title=message['display_recipient'],
subject=message['subject']
)
message_instance.frm = ZulipRoomOccupant(
id=message['sender_email'],
full_name=message['sender_full_name'],
emails=[message['sender_email']],
client=message['client'],
room=room
)
message_instance.to = room
else:
raise ValueError("Invalid message type `{}`.".format(message['type']))
self.callback_message(message_instance)
def send_message(self, msg):
super().send_message(msg)
msg_data = {
'content': msg.body,
}
if isinstance(msg.to, ZulipRoom):
msg_data['type'] = 'stream'
msg_data['subject'] = msg.to.subject
msg_data['to'] = msg.to.title
elif isinstance(msg.to, ZulipPerson):
if isinstance(msg.to, ZulipRoomOccupant):
msg_data['type'] = 'stream'
msg_data['subject'] = msg.to.room.subject
msg_data['to'] = msg.to.room.title
else:
msg_data['type'] = 'private'
msg_data['to'] = msg.to.emails
else:
raise ValueError("Invalid message recipient of type {}".format(type(msg.to).__name__))
try:
self.client.send_message(msg_data)
except Exception:
log.exception(
"An exception occurred while trying to send the following message "
"to %s: %s" % (msg.to.id, msg.body)
)
raise
def is_from_self(self, msg):
return msg.frm.aclattr == self.client.email
def change_presence(self, status: str = ONLINE, message: str = '') -> None:
# At this time, Zulip doesn't support active presence change.
pass
def build_identifier(self, txtrep):
return ZulipPerson(id=txtrep,
full_name=txtrep,
emails=[txtrep],
client=self.client)
def build_reply(self, msg, text=None, private=False, threaded=False):
response = self.build_message(text)
response.to = msg.to
return response
@property
def mode(self):
return 'zulip'
def query_room(self, room):
return ZulipRoom(title=room, client=self.client)
def rooms(self):
result = parse_query_result(self.client.list_subscriptions())
return [ZulipRoom(title=subscription['name'], id=subscription['name']) for subscription in result['subscriptions']]
def prefix_groupchat_reply(self, message, identifier):
super().prefix_groupchat_reply(message, identifier)
message.body = '@**{0}** {1}'.format(identifier.full_name, message.body)
def _zulip_upload_stream(self, stream):
"""Perform upload defined in a stream."""
try:
stream.accept()
result = self.client.upload_file(stream.raw)
if result['result'] == 'success':
message_instance = self.build_message("[{}]({})".format(stream.name, result['uri']))
message_instance.to = stream.identifier
self.send_message(message_instance)
stream.success()
else:
stream.error()
except Exception:
log.exception("Upload of {0} to {1} failed.".format(stream.name,
stream.identifier))
def send_stream_request(self, identifier, fsource, name='file', size=None, stream_type=None):
"""Starts a file transfer.
:param identifier: ZulipPerson or ZulipRoom
Identifier of the Person or Room to send the stream to.
:param fsource: str, dict or binary data
File URL or binary content from a local file.
Optionally a dict with binary content plus metadata can be given.
See `stream_type` for more details.
:param name: str, optional
Name of the file. Not sure if this works always.
:param size: str, optional
Size of the file obtained with os.path.getsize.
This is only used for debug logging purposes.
:param stream_type: str, optional
Type of the stream. Choices: 'document', 'photo', 'audio', 'video', 'sticker', 'location'.
Right now used for debug logging purposes only.
:return stream: str or Stream
If `fsource` is str will return str, else return Stream.
"""
def _metadata(fsource):
if isinstance(fsource, dict):
return fsource.pop('content'), fsource
else:
return fsource, None
def _is_valid_url(url):
try:
from urlparse import urlparse
except Exception:
from urllib.parse import urlparse
return bool(urlparse(url).scheme)
content, meta = _metadata(fsource)
if isinstance(content, str):
if not _is_valid_url(content):
raise ValueError("Not valid URL: {}".format(content))
else:
raise NotImplementedError("The Zulip backend does not yet support URL stream requests.")
else:
stream = Stream(identifier, content, name, size, stream_type)
log.debug("Requesting upload of {0} to {1} (size hint: {2}, stream type: {3})".format(name,
identifier, size, stream_type))
self.thread_pool.apply_async(self._zulip_upload_stream, (stream,))
return stream