forked from mautrix/imessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imessage.go
307 lines (288 loc) · 10.5 KB
/
imessage.go
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
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"time"
log "maunium.net/go/maulogger/v2"
"go.mau.fi/mautrix-imessage/imessage"
)
type iMessageHandler struct {
bridge *IMBridge
log log.Logger
stop chan struct{}
}
func NewiMessageHandler(bridge *IMBridge) *iMessageHandler {
return &iMessageHandler{
bridge: bridge,
log: bridge.Log.Sub("iMessage"),
stop: make(chan struct{}),
}
}
func (imh *iMessageHandler) Start() {
messages := imh.bridge.IM.MessageChan()
readReceipts := imh.bridge.IM.ReadReceiptChan()
typingNotifications := imh.bridge.IM.TypingNotificationChan()
chats := imh.bridge.IM.ChatChan()
contacts := imh.bridge.IM.ContactChan()
messageStatuses := imh.bridge.IM.MessageStatusChan()
backfillTasks := imh.bridge.IM.BackfillTaskChan()
for {
var start time.Time
var thing string
select {
case msg := <-messages:
thing = "message"
start = time.Now()
imh.HandleMessage(msg)
case rr := <-readReceipts:
thing = "read receipt"
start = time.Now()
imh.HandleReadReceipt(rr)
case notif := <-typingNotifications:
thing = "typing notification"
start = time.Now()
imh.HandleTypingNotification(notif)
case chat := <-chats:
thing = "chat"
start = time.Now()
imh.HandleChat(chat)
case contact := <-contacts:
thing = "contact"
start = time.Now()
imh.HandleContact(contact)
case status := <-messageStatuses:
thing = "message status"
start = time.Now()
imh.HandleMessageStatus(status)
case backfillTask := <-backfillTasks:
thing = "backfill task"
start = time.Now()
imh.HandleBackfillTask(backfillTask)
case <-imh.stop:
return
}
imh.log.Debugfln(
"Handled %s in %s (queued: %dm/%dr/%dt/%dch/%dct/%ds/%db)",
thing, time.Since(start),
len(messages), len(readReceipts), len(typingNotifications), len(chats), len(contacts), len(messageStatuses), len(backfillTasks),
)
}
}
const PortalBufferTimeout = 10 * time.Second
func (imh *iMessageHandler) rerouteGroupMMS(portal *Portal, msg *imessage.Message) *Portal {
if !imh.bridge.Config.Bridge.RerouteSMSGroupReplies ||
!portal.Identifier.IsGroup ||
portal.Identifier.Service != "SMS" ||
msg.ReplyToGUID == "" ||
portal.MXID != "" {
return portal
}
mergedChatGUID := imh.bridge.DB.MergedChat.Get(portal.GUID)
if mergedChatGUID != "" && mergedChatGUID != portal.GUID {
newPortal := imh.bridge.GetPortalByGUID(mergedChatGUID)
if newPortal.MXID != "" {
imh.log.Debugfln("Rerouted %s from %s to %s based on merged_chat table", msg.GUID, msg.ChatGUID, portal.GUID)
return newPortal
}
}
checkedChatGUIDs := []string{msg.ChatGUID}
isCheckedGUID := func(guid string) bool {
for _, checkedGUID := range checkedChatGUIDs {
if guid == checkedGUID {
return true
}
}
return false
}
replyMsg := msg
for i := 0; i < 20; i++ {
chatGUID := imh.bridge.DB.Message.FindChatByGUID(replyMsg.ReplyToGUID)
if chatGUID != "" && !isCheckedGUID(chatGUID) {
newPortal := imh.bridge.GetPortalByGUID(chatGUID)
if newPortal.MXID != "" {
imh.log.Debugfln("Rerouted %s from %s to %s based on reply metadata (found reply in local db)", msg.GUID, msg.ChatGUID, portal.GUID)
imh.log.Debugfln("Merging %+v -> %s", checkedChatGUIDs, newPortal.GUID)
imh.bridge.DB.MergedChat.Set(nil, newPortal.GUID, checkedChatGUIDs...)
return newPortal
}
checkedChatGUIDs = append(checkedChatGUIDs, chatGUID)
}
var err error
replyMsg, err = imh.bridge.IM.GetMessage(replyMsg.ReplyToGUID)
if err != nil {
imh.log.Warnfln("Failed to get reply target %s for rerouting %s: %v", replyMsg.ReplyToGUID, msg.GUID, err)
break
}
if !isCheckedGUID(replyMsg.ChatGUID) {
newPortal := imh.bridge.GetPortalByGUID(chatGUID)
if newPortal.MXID != "" {
imh.log.Debugfln("Rerouted %s from %s to %s based on reply metadata (got reply msg from connector)", msg.GUID, msg.ChatGUID, portal.GUID)
imh.log.Debugfln("Merging %+v -> %s", checkedChatGUIDs, newPortal.GUID)
imh.bridge.DB.MergedChat.Set(nil, newPortal.GUID, checkedChatGUIDs...)
return newPortal
}
checkedChatGUIDs = append(checkedChatGUIDs, chatGUID)
}
}
imh.log.Debugfln("Didn't find any existing room to reroute %s into (checked portals %+v)", msg.GUID, checkedChatGUIDs)
return portal
}
func (imh *iMessageHandler) updateChatGUIDByThreadID(portal *Portal, threadID string) *Portal {
if len(portal.MXID) > 0 || !portal.Identifier.IsGroup || threadID == "" || portal.bridge.Config.IMessage.Platform == "android" {
return portal
}
existingByThreadID := imh.bridge.FindPortalsByThreadID(threadID)
if len(existingByThreadID) > 1 {
imh.log.Warnfln("Found multiple portals with thread ID %s (message chat guid: %s)", threadID, portal.GUID)
} else if len(existingByThreadID) == 0 {
// no need to log, this is just an ordinary new group
} else if existingByThreadID[0].MXID != "" {
imh.log.Infofln("Found existing portal %s for thread ID %s, merging %s into it", existingByThreadID[0].GUID, threadID, portal.GUID)
existingByThreadID[0].reIDInto(portal.GUID, portal, true, false)
return existingByThreadID[0]
} else {
imh.log.Infofln("Found existing portal %s for thread ID %s, but it doesn't have a room", existingByThreadID[0].GUID, threadID, portal.GUID)
}
return portal
}
func (imh *iMessageHandler) getPortalFromMessage(msg *imessage.Message) *Portal {
portal := imh.rerouteGroupMMS(imh.bridge.GetPortalByGUID(msg.ChatGUID), msg)
return portal
}
func (imh *iMessageHandler) HandleMessage(msg *imessage.Message) {
imh.log.Debugfln("Received incoming message %s in %s (%s)", msg.GUID, msg.ChatGUID, msg.ThreadID)
// TODO trace log
//imh.log.Debugfln("Received incoming message: %+v", msg)
portal := imh.updateChatGUIDByThreadID(
imh.rerouteGroupMMS(
imh.bridge.GetPortalByGUID(msg.ChatGUID),
msg,
),
msg.ThreadID,
)
if msg.ThreadID != "" && msg.ThreadID != portal.ThreadID {
portal.log.Infoln("Found portal thread ID in message: %s (prev: %s)", msg.ThreadID, portal.ThreadID)
portal.ThreadID = msg.ThreadID
if len(portal.MXID) > 0 {
portal.Update(nil)
portal.UpdateBridgeInfo()
}
}
if len(portal.MXID) == 0 {
portal.log.Infoln("Creating Matrix room to handle message")
err := portal.CreateMatrixRoom(nil, nil)
if err != nil {
imh.log.Warnfln("Failed to create Matrix room to handle message: %v", err)
return
}
}
select {
case portal.Messages <- msg:
case <-time.After(PortalBufferTimeout):
imh.log.Errorln("Portal message buffer is still full after 10 seconds, dropping message %s", msg.GUID)
}
}
func (imh *iMessageHandler) HandleMessageStatus(status *imessage.SendMessageStatus) {
portal := imh.bridge.GetPortalByGUID(status.ChatGUID)
if len(portal.MXID) == 0 {
imh.log.Debugfln("Ignoring message status for message from unknown portal %s/%s", status.GUID, status.ChatGUID)
return
}
select {
case portal.MessageStatuses <- status:
case <-time.After(PortalBufferTimeout):
imh.log.Errorln("Portal message status buffer is still full after 10 seconds, dropping %+v", *status)
}
}
func (imh *iMessageHandler) HandleReadReceipt(rr *imessage.ReadReceipt) {
portal := imh.bridge.GetPortalByGUID(rr.ChatGUID)
if len(portal.MXID) == 0 {
imh.log.Debugfln("Ignoring read receipt in unknown portal %s", rr.ChatGUID)
return
}
select {
case portal.ReadReceipts <- rr:
case <-time.After(PortalBufferTimeout):
imh.log.Errorln("Portal read receipt buffer is still full after 10 seconds, dropping %+v", *rr)
}
}
func (imh *iMessageHandler) HandleTypingNotification(notif *imessage.TypingNotification) {
portal := imh.bridge.GetPortalByGUID(notif.ChatGUID)
if len(portal.MXID) == 0 {
return
}
_, err := portal.MainIntent().UserTyping(portal.MXID, notif.Typing, 60*time.Second)
if err != nil {
action := "typing"
if !notif.Typing {
action = "not typing"
}
portal.log.Warnln("Failed to mark %s as %s in %s: %v", portal.MainIntent().UserID, action, portal.MXID, err)
}
}
func (imh *iMessageHandler) HandleChat(chat *imessage.ChatInfo) {
chat.Identifier = imessage.ParseIdentifier(chat.JSONChatGUID)
if chat.Delete {
portal := imh.bridge.GetPortalByGUIDIfExists(chat.Identifier.String())
if portal != nil {
portal.zlog.Info().Msg("Received delete command, deleting and cleaning up portal...")
portal.Delete()
portal.Cleanup(false)
portal.zlog.Info().Msg("Portal cleanup completed")
} else {
imh.log.Warnfln("Received delete command for unknown portal %s", chat.Identifier.String())
}
return
}
portal := imh.bridge.GetPortalByGUID(chat.Identifier.String())
portal = imh.updateChatGUIDByThreadID(portal, chat.ThreadID)
if len(portal.MXID) > 0 {
portal.log.Infoln("Syncing Matrix room to handle chat command")
portal.SyncWithInfo(chat)
} else if !chat.NoCreateRoom {
portal.log.Infoln("Creating Matrix room to handle chat command")
err := portal.CreateMatrixRoom(chat, nil)
if err != nil {
imh.log.Warnfln("Failed to create Matrix room to handle chat command: %v", err)
return
}
}
}
func (imh *iMessageHandler) HandleBackfillTask(task *imessage.BackfillTask) {
if !imh.bridge.Config.Bridge.Backfill.Enable {
imh.log.Warnfln("Connector sent backfill task, but backfill is disabled in bridge config")
imh.bridge.IM.SendBackfillResult(task.ChatGUID, task.BackfillID, false, nil)
return
}
portal := imh.bridge.GetPortalByGUID(task.ChatGUID)
if len(portal.MXID) == 0 {
portal.log.Errorfln("Tried to backfill chat %s with no portal", portal.GUID)
imh.bridge.IM.SendBackfillResult(portal.GUID, task.BackfillID, false, nil)
return
}
portal.log.Debugfln("Running backfill %s in background", task.BackfillID)
go portal.sendBackfill(task.BackfillID, task.Messages, false, false, false)
}
func (imh *iMessageHandler) HandleContact(contact *imessage.Contact) {
puppet := imh.bridge.GetPuppetByGUID(contact.UserGUID)
if len(puppet.MXID) > 0 {
puppet.log.Infoln("Syncing Puppet to handle contact command")
puppet.SyncWithContact(contact)
}
}
func (imh *iMessageHandler) Stop() {
close(imh.stop)
}