forked from matrix-hacks/matrix-puppet-signal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
671 lines (620 loc) · 25 KB
/
index.js
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
const {
MatrixAppServiceBridge: {
Cli, AppServiceRegistration, EventBridgeStore, StoredEvent, RemoteUser, UserBridgeStore
},
Puppet,
MatrixPuppetBridgeBase,
utils: { download, sleep }
} = require("matrix-puppet-bridge");
const SignalClient = require('signal-client');
const config = require('./config.json');
const path = require('path');
const puppet = new Puppet(path.join(__dirname, './config.json' ));
const debug = require('debug')('matrix-puppet:signal');
let fs = require('fs');
let Promise = require('bluebird');
const {default: PQueue} = require('p-queue');
//We start the queue after client is ready
//It makes sure all events from signal are handled in the correct order
const signalQueue = new PQueue({concurrency: 1, autoStart: false});
class App extends MatrixPuppetBridgeBase {
getServicePrefix() {
return "signal";
}
getServiceName() {
return "Signal";
}
initThirdPartyClient() {
this.client = new SignalClient("matrix");
this.myNumber = config.phoneNumber.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
this.client.on('message', (ev) => {
const { source, message, timestamp } = ev.data;
let room = source;
//No need to know all members for received messages
let members = [source];
if ( message.group != null ) {
//Signal sends new groups as a message with a group name set
//Messages in groups have a group but without name attached
if(message.group.name != null) {
//We add it to the queue to make sure rooms get created before another message is being sent
signalQueue.add(() => {
return this.handleSignalGroup(message.group);
});
return;
}
room = window.btoa(message.group.id);
}
signalQueue.add(() => {
return this.handleSignalMessage({
roomId: room,
senderId: source,
}, message, timestamp, members, false);
});
});
this.client.on('call', (envelope, callingMessage) => {
//try and catch because we should only get a first call, so everything should be defined
try {
let room = envelope.source;
const members = [envelope.source];
const message = { body: "You missed a call (calls are not supported by this bridge)" };
signalQueue.add(() => {
return this.handleSignalMessage({
roomId: room,
senderId: envelope.source,
}, message, callingMessage.offer.callId, members, false);
});
} catch(err) {
console.error("We got a call without source or id", err);
}
});
this.client.on('sent', async (ev) => {
const { destination, message, timestamp } = ev.data;
let room = destination;
let members = [destination];
if ( message.group != null ) {
if(message.group.name != null) {
signalQueue.add(() => {
return this.handleSignalGroup(message.group);
});
return;
}
room = window.btoa(message.group.id);
//We add all members to be able to correctly use read receipts
const rRoom = await this.bridge.getUserStore().getRemoteUser(room);
if ( rRoom && rRoom.get('isGroup') == true) {
members = rRoom.members;
}
}
signalQueue.add(() => {
return this.handleSignalMessage({
roomId: room,
//Flag for base to know it is sent from us
senderId: undefined,
senderName: this.myNumber.substring(this.myNumber.lastIndexOf("\\") +1),
}, message, timestamp, members, true);
});
});
// triggered when we run syncGroups
this.client.on('group', (ev) => {
if(!ev.groupDetails.active) {
return;
}
signalQueue.add(() => {
return this.handleSignalGroup(ev.groupDetails);
});
});
this.client.on('contact', (ev) => {
//Makes startup slower but ensures all contacts are synced as we have no race in joining status room
signalQueue.add(async () => {
return this.handleSignalContact(ev.contactDetails);
});
});
this.client.on('read', (ev) => {
const { timestamp, source } = ev.read;
console.log("read event", timestamp, source);
signalQueue.add(() => {
return this.handleSignalReadReceipt(timestamp, source);
});
});
this.client.on('typing', (ev)=>{
let timestamp = ev.typing.timestamp;
let sender = ev.sender;
let status = ev.typing.started;
console.log('typing event', sender, timestamp, status);
let group = null;
if(ev.typing.groupId) {
group = window.btoa(ev.typing.groupId);
}
signalQueue.add(() => {
this.handleTypingEvent(sender,status,group);
});
});
this.client.on('client_ready', () => {
//Request sync and start handling of signal stuff
this.client.syncContacts();
this.client.syncGroups();
signalQueue.start();
});
return this.client.start();
}
async handleSignalContact(contactDetails) {
console.log('contact received', contactDetails);
let contact = {};
contact.userId = contactDetails.number;
if (contactDetails.name != null) {
contact.senderName = contactDetails.name;
contact.name = contactDetails.name;
}
else {
//If the unnamed sender allows us to use his profile name we will use this
contact.name = await this.client.getProfileNameForId(contact.userId);
contact.senderName = await this.client.getProfileNameForId(contact.userId);
}
if (!contact.name) {
contact.name = "Unnamed Person";
contact.senderName = "Unnamed Person";
}
const signalAvatarPath = await this.client.getPathForAvatar(contactDetails.number, false);
if (signalAvatarPath != null) {
contact.avatar = signalAvatarPath;
}
else if(contactDetails.avatar) {
let avatarBuffer = Buffer.from(contactDetails.avatar.data);
const fileName = contactDetails.number.replace(/[^a-zA-Z0-9]/g, '');
fs.writeFileSync(process.cwd() + '/data/' + fileName, avatarBuffer);
contact.avatar = process.cwd() + '/data/' + fileName;
}
const userStore = this.bridge.getUserStore();
let rUser = await userStore.getRemoteUser(contact.userId);
if ( rUser ) {
//If we saved it temporary so far we delete the old file
if (rUser.get('avatar') && !rUser.get('avatar').includes("attachments.noindex") && (rUser.get('avatar') != contact.avatar)) {
fs.unlinkSync(rUser.get('avatar'));
}
rUser.set('name', contact.name);
rUser.set('senderName', contact.senderName);
rUser.set('avatar', contact.avatar);
await userStore.setRemoteUser(rUser);
}
else {
//To differentiate between user and groups
contact.isGroup = false;
await userStore.setRemoteUser(new RemoteUser(contact.userId, contact));
}
let retry = 3;
let lastError;
let timeOut = 100;
while (retry--) {
try {
//Get avatar as data array instead of path
contact = await this.getThirdPartyUserDataById(contact.userId);
return await this.joinThirdPartyUsersToStatusRoom([contact]);
} catch(err) {
lastError = err;
}
if (lastError.errcode == 'M_LIMIT_EXCEEDED' && lastError.data.retry_after_ms) {
console.log("Matrix forces us to wait for ", lastError.data.retry_after_ms);
timeOut = lastError.data.retry_after_ms;
}
await sleep(timeOut);
}
console.log("Could not join user to status room, error:", lastError);
return;
}
async handleSignalGroup(groupDetails) {
console.log("Group received ", groupDetails);
let id = window.btoa(groupDetails.id);
if (groupDetails.name == "") {
groupDetails.name = "Unnamed Group";
}
let group = { name: groupDetails.name };
if(groupDetails.avatar) {
let avatarBuffer;
//If desktop knows the group it sends an array buffer
if (groupDetails.avatar.data) {
//We would prefer to use existing avatar
const signalAvatarPath = await this.client.getPathForAvatar(groupDetails.id, true);
if (signalAvatarPath != null) {
group.avatar = signalAvatarPath;
}
else {
const fileName = id.replace(/[^a-zA-Z0-9]/g, '');
fs.writeFileSync(process.cwd() + '/data/' + fileName, Buffer.from(groupDetails.avatar.data));
group.avatar = process.cwd() + '/data/' + fileName;
}
}
//New group, we have to download the avatar first
else {
const avData = await this.client.downloadAttachment(groupDetails.avatar);
const fileName = id.replace(/[^a-zA-Z0-9]/g, '');
fs.writeFileSync(process.cwd() + '/data/' + fileName, Buffer.from(avData.data));
group.avatar = process.cwd() + '/data/' + fileName;
}
}
const otherPeople = groupDetails.membersE164.filter(phoneNumber => !phoneNumber.match(this.myNumber));
group.members = otherPeople;
//We use userStore for groups as rooms need a linked matrix room
const userStore = this.bridge.getUserStore();
let rGroup = await userStore.getRemoteUser(id);
if ( rGroup ) {
if ( rGroup.get('isGroup') == true) {
if (rGroup.get('avatar') && !rGroup.get('avatar').includes("attachments.noindex") && (rGroup.get('avatar') != group.avatar)) {
fs.unlinkSync(rGroup.get('avatar'));
}
rGroup.set('name', group.name);
rGroup.set('avatar', group.avatar);
rGroup.set('members', group.members);
await userStore.setRemoteUser(rGroup);
}
else {
console.error("There seems to be a user with the same id as a new group");
return;
}
}
else {
group.isGroup = true;
await userStore.setRemoteUser(new RemoteUser(id, group));
}
const matrixRoomId = await this.getOrCreateMatrixRoomFromThirdPartyRoomId(id);
for (let i = 0; i < otherPeople.length; ++i) {
let ghost = await this.getIntentFromThirdPartySenderId(otherPeople[i]);
const roomsGhost = await ghost.getClient().getJoinedRooms();
const hasGhostJoined = roomsGhost.joined_rooms.includes(matrixRoomId);
if (!hasGhostJoined) {
console.log("Letting member join room", otherPeople[i]);
try {
await this.puppet.getClient().invite(matrixRoomId, ghost.client.credentials.userId);
await ghost._ensureJoined(matrixRoomId);
} catch(err) {
console.log("failed to join ghost: ", otherPeople[i], matrixRoomId, err);
}
}
}
return true;
}
async handleSignalMessage(payload, message, timeStamp, members = [], sentMessage = false) {
this.handleTypingEvent(payload.senderId, false, payload.roomId);
if ( message.body ) {
payload.text = message.body
}
//Undefined text means file will not get through, so we just set it to empty string
else {
payload.text = "";
}
//stickers seem to be just glorified pictures
if (message.sticker != null) {
message.attachments.push(message.sticker.data);
}
if (message.reaction != null) {
// We ignore remove events (redactions not implemented)
if (message.reaction.remove == true) {
return;
}
const reactionEventEntry = await this.bridge.getEventStore().getEntryByRemoteId(message.reaction.targetTimestamp.toNumber(), message.reaction.targetAuthorE164);
if (reactionEventEntry != null) {
payload.reaction = {
roomId: reactionEventEntry.getMatrixRoomId(),
eventId: reactionEventEntry.getMatrixEventId(),
emoji: message.reaction.emoji,
}
}
else {
debug("Did not find event for", message.reaction.targetTimestamp.toNumber(), message.reaction.targetAuthorE164);
return;
}
// reactions sent from us don't have a destination, therefore we need to set it to something (will not be used anyway)
if (payload.roomId == null) {
payload.roomId = message.reaction.targetAuthorE164;
}
}
//pictures as quotes cannot be handled in matrix so we ignore the quote
if (message.quote != null && message.attachments != null &&message.attachments.length === 0) {
//Get eventId from the eventstore to look for the quote, always same room so no need for that one
const quotedEventEntry = await this.bridge.getEventStore().getEntryByRemoteId(message.quote.id, message.quote.author);
if (quotedEventEntry != null) {
payload.quote = {
userId: message.quote.author,
eventId: quotedEventEntry.getMatrixEventId(),
text: message.quote.text,
};
if (message.quote.author == this.myNumber.substring(this.myNumber.lastIndexOf("\\") +1)) {
payload.quote.userId = undefined;
}
}
else {
debug("Did not find event for", message.quote.id, message.quote.author);
}
}
//TODO: Still needed? Checking when getting contacts anyway
if (!payload.senderName) { //Make sure senders have a name so they show up.
if (payload.senderId) {
const remoteUser = await this.getOrInitRemoteUserStoreDataFromThirdPartyUserId(payload.senderId);
payload.senderName = remoteUser.get('senderName');
if (!payload.senderName) {
//If the unnamed sender allows us to use his profile name we will use this after everything failed
const profileName = await this.client.getProfileNameForId(payload.senderId);
payload.senderName = profileName;
}
}
if (!payload.senderName) {
payload.senderName = "Unnamed Person";
}
}
const matrixRoomId = await this.getOrCreateMatrixRoomFromThirdPartyRoomId(payload.roomId);
let matrixEventId;
if ( message.attachments == null || message.attachments.length === 0 ) {
if(payload.text == null) {
return;
}
matrixEventId = await this.handleThirdPartyRoomMessage(payload);
} else {
let data;
for ( let i = 0; i < message.attachments.length; i++ ) {
let att = message.attachments[i];
data = await this.client.downloadAttachment(att);
payload.buffer = new Buffer.from(data.data);
payload.mimetype = data.contentType;
matrixEventId = await this.handleThirdPartyRoomMessageWithAttachment(payload);
}
}
this.saveMessageEvents(matrixRoomId, matrixEventId.event_id, timeStamp, members, sentMessage);
return true;
}
async handleTypingEvent(sender,status,group) {
//We don't need to handle typing events from ourselves
if (!sender) {
sender = this.myNumber.substring(this.myNumber.lastIndexOf("\\") +1);
}
try {
let id = sender;
if (group) {
id = group;
}
const ghostIntent = await this.getIntentFromThirdPartySenderId(sender);
const matrixRoomId = await this.getOrCreateMatrixRoomFromThirdPartyRoomId(id);
return ghostIntent.sendTyping(matrixRoomId, status, 60000);
} catch (err) {
debug('could not send typing event', err.message);
}
}
async handleSignalReadReceipt(timeStamp, reader) {
try {
//Get event and roomId from the eventstore
const eventEntry = await this.bridge.getEventStore().getEntryByRemoteId(timeStamp, reader);
if (eventEntry != null) {
const matrixRoomId = eventEntry.getMatrixRoomId();
const matrixEventId = eventEntry.getMatrixEventId();
const ghostIntent = await this.getIntentFromThirdPartySenderId(reader);
ghostIntent.sendReadReceipt (matrixRoomId, matrixEventId);
}
else {
debug('no event found for', timeStamp, reader);
}
} catch (err) {
debug('could not send read event', err.message);
}
}
//
async getThirdPartyRoomDataById(thirdPartyRoomId) {
let name = "";
let topic = "Signal Direct Message";
let avatar;
let direct = true;
const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( room ) {
name = room.get('name');
let avatarPath = room.get('avatar');
if (avatarPath) {
let file = fs.readFileSync(avatarPath);
avatar = {type: 'image/jpeg', buffer: new Uint8Array(file) };
}
if (room.get('isGroup') == true) {
topic = "Signal Group Message";
direct = false;
}
}
return Promise.resolve({name, topic, avatar, is_direct: direct});
}
async getThirdPartyUserDataById(thirdPartyRoomId) {
let contact = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( contact && contact.get('isGroup') == false ) {
let avatarPath = contact.get('avatar');
if (avatarPath) {
let file = fs.readFileSync(avatarPath);
contact.data.avatar = {type: 'image/jpeg', buffer: new Uint8Array(file) };
}
return contact.data;
} else {
return {senderName: thirdPartyRoomId};
}
}
async sendReadReceiptAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId) {
let timeStamp = await new Date().getTime();
let isGroup = false;
const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( room && room.get('isGroup') == true) {
thirdPartyRoomId = window.atob(thirdPartyRoomId);
isGroup = true;
}
console.log("sending read receipts for " + thirdPartyRoomId);
// mark messages as read in your signal clients
await this.client.syncReadReceipts(thirdPartyRoomId, isGroup, timeStamp, config.sendReadReceipts);
return true;
}
async sendTypingEventAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, status) {
let isGroup = false;
const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( room && room.get('isGroup') == true) {
thirdPartyRoomId = window.atob(thirdPartyRoomId);
isGroup = true;
}
await this.client.sendTypingMessage(thirdPartyRoomId, isGroup, status, config.sendTypingEvents);
}
async sendReactionAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, data) {
try {
let isGroup = false;
const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( room && room.get('isGroup') == true) {
thirdPartyRoomId = window.atob(thirdPartyRoomId);
isGroup = true;
}
//Get event and roomId from the eventstore
const eventEntry = await this.bridge.getEventStore().getEntryByMatrixId(data.room_id, data.content["m.relates_to"].event_id);
if (eventEntry != null) {
let target = {
targetTimestamp: eventEntry.getRemoteRoomId(),
targetAuthorE164: eventEntry.getRemoteEventId(),
}
let reaction = {
emoji: data.content["m.relates_to"].key,
}
return this.client.sendReactionMessage(thirdPartyRoomId, isGroup, reaction, target);
}
else {
debug('no event found for', data.room_id, data.content["m.relates_to"].event_id);
}
} catch (err) {
debug('could not send reaction', err.message);
}
}
sendImageMessageAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data) {
return this.sendFileMessageAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data);
}
sendAudioAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data) {
return this.sendFileMessageAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data);
}
sendVideoAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data) {
return this.sendFileMessageAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data);
}
async sendFileMessageAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, info, data) {
info.text = "";
let isGroup = false;
const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( room && room.get('isGroup') == true) {
thirdPartyRoomId = window.atob(thirdPartyRoomId);
isGroup = true;
}
return download.getTempfile(info.url, { tagFilename: true }).then(({path}) => {
let file = fs.readFileSync(path);
let bufferArray = new Uint8Array(file).buffer;
//We need to set a mimetype otherwise signal crashes
if (!info.mimetype) {
info.mimetype = "";
}
let finalizedAttachment = {
info: bufferArray,
size: file.byteLength,
contentType: info.mimetype,
fileName: info.filename,
path: path,
data: bufferArray,
};
return this.client.sendMessage(thirdPartyRoomId, isGroup, info.text, [finalizedAttachment]).then(result => {
let {timeStamp, members} = result;
this.saveMessageEvents(data.room_id, data.event_id, timeStamp, members, true);
});
});
}
//Gives unknown quote if quoted message was image sent from signal with text and we try to quote it
async sendMessageAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, text, data) {
let quote = null;
if (data.content["m.relates_to"] && data.content["m.relates_to"]["m.in_reply_to"]) {
const matrixRoomId = data.room_id;
const matrixEventId = data.content["m.relates_to"]["m.in_reply_to"].event_id;
const eventEntry = await this.bridge.getEventStore().getEntryByMatrixId(matrixRoomId, matrixEventId);
if (eventEntry != null) {
const quotedTimestamp = eventEntry.getRemoteRoomId();
let quotedSenderNumber = eventEntry.getRemoteEventId();
//We only save the "room", so we need to check if we are quoting ourselves
if (eventEntry.get('sentByMe') == true) {
quotedSenderNumber = this.myNumber.substring(this.myNumber.lastIndexOf("\\") +1);
}
const origFormated = data.content.formatted_body;
let endQuoteLinks = origFormated.lastIndexOf("</a><br>")+8;
let endQuote = origFormated.lastIndexOf("</blockquote></mx-reply>");
let quotedText = origFormated.substring(endQuoteLinks, endQuote);
//TODO: Check if this is needed
let startQuote = quotedText.lastIndexOf("</mx-reply>");
if (startQuote > 0) {
quotedText = quotedText.substring(startQuote+11, quotedText.lastIndexOf("</blockquote>"));
}
quote = {
id: quotedTimestamp,
author: quotedSenderNumber,
authorUuid: null,
text: quotedText,
attachments: []
};
text = origFormated.substring(endQuote+24);
}
else {
debug('no event found for', matrixRoomId, matrixEventId);
}
}
let isGroup = false;
const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
if ( room && room.get('isGroup') == true) {
thirdPartyRoomId = window.atob(thirdPartyRoomId);
isGroup = true;
}
return this.client.sendMessage(thirdPartyRoomId, isGroup, text, [], quote).then(result => {
let {timeStamp, members} = result;
this.saveMessageEvents(data.room_id, data.event_id, timeStamp, members, true);
});
}
//Signal uses timestamp as message id, and looks up recipients by timestamp before finding the one for the receipt.
//Therefore we add it to the event store with roomId timestamp and eventId userNumber, so we can find event later
saveMessageEvents(matrixRoomId, matrixEventId, timeStamp, members, sentMessage = false) {
let message;
//As we sent message we need to store ourselves as well
if (sentMessage == true) {
members.push(this.myNumber.substring(this.myNumber.lastIndexOf("\\") +1));
}
for ( let i = 0; i < members.length; i++ ) {
message = new StoredEvent(matrixRoomId, matrixEventId, timeStamp, members[i], {sentByMe: sentMessage});
this.bridge.getEventStore().upsertEvent(message);
}
}
//Signal-desktop does not seem to handlit leaving correctly, waiting for upstream fix or better idea
async sendLeavingEventAsPuppetToThirdPartyRoomWithId(thirdPartyRoomId, data) {
console.log("Ignoring leave event as it is buggy right now");
// const room = await this.bridge.getUserStore().getRemoteUser(thirdPartyRoomId);
// if ( room && room.get('isGroup') == true) {
// thirdPartyRoomId = window.atob(thirdPartyRoomId);
// this.client.leaveGroup(thirdPartyRoomId);
// }
}
}
new Cli({
port: config.port,
registrationPath: config.registrationPath,
generateRegistration: function(reg, callback) {
puppet.associate().then(()=>{
reg.setId(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setSenderLocalpart("signalbot");
reg.addRegexPattern("users", "@signal_.*", true);
reg.addRegexPattern("aliases", "#signal_.*", true);
callback(reg);
}).catch(err=>{
console.error(err.message);
process.exit(-1);
});
},
run: function(port) {
const app = new App(config, puppet);
console.log('starting matrix client');
return puppet.startClient().then(()=>{
console.log('starting signal client');
return app.initThirdPartyClient();
}).then(()=>{
return app.bridge.run(port, config);
}).then(()=>{
console.log('Matrix-side listening on port %s', port);
}).catch(err=>{
console.error(err.message);
process.exit(-1);
});
}
}).run();