Debian Codegraph @

ui/messages/parser.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package messages
18
19 import (
20 "fmt"
21 "strings"
22
•23 "github.com/gdamore/tcell/v2"
24
25 "maunium.net/go/mautrix/event"
26 "maunium.net/go/mautrix/id"
27
28 "maunium.net/go/gomuks/debug"
29 ifc "maunium.net/go/gomuks/interface"
30 "maunium.net/go/gomuks/matrix/muksevt"
31 "maunium.net/go/gomuks/matrix/rooms"
32 "maunium.net/go/gomuks/ui/messages/html"
33 "maunium.net/go/gomuks/ui/messages/tstring"
34 "maunium.net/go/gomuks/ui/widget"
35 )
36
37 func getCachedEvent(mainView ifc.MainView, roomID id.RoomID, eventID id.EventID) *UIMessage {
38 if roomView := mainView.GetRoom(roomID); roomView != nil {
39 if replyToIfcMsg := roomView.GetEvent(eventID); replyToIfcMsg != nil {
40 if replyToMsg, ok := replyToIfcMsg.(*UIMessage); ok && replyToMsg != nil {
41 return replyToMsg
42 }
43 }
44 }
45 return nil
46 }
47
48 func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *muksevt.Event) *UIMessage {
49 msg := directParseEvent(matrix, room, evt)
50 if msg == nil {
51 return nil
52 }
53 if content, ok := evt.Content.Parsed.(*event.MessageEventContent); ok && len(content.GetReplyTo()) > 0 {
54 if replyToMsg := getCachedEvent(mainView, room.ID, content.GetReplyTo()); replyToMsg != nil {
55 msg.ReplyTo = replyToMsg.Clone()
56 } else if replyToEvt, _ := matrix.GetEvent(room, content.GetReplyTo()); replyToEvt != nil {
57 if replyToMsg = directParseEvent(matrix, room, replyToEvt); replyToMsg != nil {
58 msg.ReplyTo = replyToMsg
59 msg.ReplyTo.Reactions = nil
60 } else {
61 // TODO add unrenderable reply header
62 }
63 } else {
64 // TODO add unknown reply header
65 }
66 }
67 return msg
68 }
69
70 func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Event) *UIMessage {
71 displayname := string(evt.Sender)
72 member := room.GetMember(evt.Sender)
73 if member != nil {
74 displayname = member.Displayname
75 }
76 if evt.Unsigned.RedactedBecause != nil || evt.Type == event.EventRedaction {
77 return NewRedactedMessage(evt, displayname)
78 }
79 switch content := evt.Content.Parsed.(type) {
80 case *event.MessageEventContent:
81 if evt.Type == event.EventSticker {
82 content.MsgType = event.MsgImage
83 }
84 return ParseMessage(matrix, room, evt, displayname)
85 case *muksevt.BadEncryptedContent:
86 return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString(content.Reason, tcell.StyleDefault.Italic(true)))
87 case *muksevt.EncryptionUnsupportedContent:
88 return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("gomuks not built with encryption support", tcell.StyleDefault.Italic(true)))
89 case *event.TopicEventContent, *event.RoomNameEventContent, *event.CanonicalAliasEventContent:
90 return ParseStateEvent(evt, displayname)
91 case *event.MemberEventContent:
92 return ParseMembershipEvent(room, evt)
93 default:
94 debug.Printf("Unknown event content type %T in directParseEvent", content)
95 return nil
96 }
97 }
98
99 func findAltAliasDifference(newList, oldList []id.RoomAlias) (addedStr, removedStr tstring.TString) {
100 var addedList, removedList []tstring.TString
101 OldLoop:
102 for _, oldAlias := range oldList {
103 for _, newAlias := range newList {
104 if oldAlias == newAlias {
105 continue OldLoop
106 }
107 }
108 removedList = append(removedList, tstring.NewStyleTString(string(oldAlias), tcell.StyleDefault.Foreground(widget.GetHashColor(oldAlias)).Underline(true)))
109 }
110 NewLoop:
111 for _, newAlias := range newList {
112 for _, oldAlias := range oldList {
113 if newAlias == oldAlias {
114 continue NewLoop
115 }
116 }
117 addedList = append(addedList, tstring.NewStyleTString(string(newAlias), tcell.StyleDefault.Foreground(widget.GetHashColor(newAlias)).Underline(true)))
118 }
119 if len(addedList) == 1 {
120 addedStr = tstring.NewColorTString("added alternative address ", tcell.ColorGreen).AppendTString(addedList[0])
121 } else if len(addedList) != 0 {
122 addedStr = tstring.
123 Join(addedList[:len(addedList)-1], ", ").
124 PrependColor("added alternative addresses ", tcell.ColorGreen).
125 AppendColor(" and ", tcell.ColorGreen).
126 AppendTString(addedList[len(addedList)-1])
127 }
128 if len(removedList) == 1 {
129 removedStr = tstring.NewColorTString("removed alternative address ", tcell.ColorGreen).AppendTString(removedList[0])
130 } else if len(removedList) != 0 {
131 removedStr = tstring.
132 Join(removedList[:len(removedList)-1], ", ").
133 PrependColor("removed alternative addresses ", tcell.ColorGreen).
134 AppendColor(" and ", tcell.ColorGreen).
135 AppendTString(removedList[len(removedList)-1])
136 }
137 return
138 }
139
140 func ParseStateEvent(evt *muksevt.Event, displayname string) *UIMessage {
141 text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender)).Append(" ")
142 switch content := evt.Content.Parsed.(type) {
143 case *event.TopicEventContent:
144 if len(content.Topic) == 0 {
145 text = text.AppendColor("removed the topic.", tcell.ColorGreen)
146 } else {
147 text = text.AppendColor("changed the topic to ", tcell.ColorGreen).
148 AppendStyle(content.Topic, tcell.StyleDefault.Underline(true)).
149 AppendColor(".", tcell.ColorGreen)
150 }
151 case *event.RoomNameEventContent:
152 if len(content.Name) == 0 {
153 text = text.AppendColor("removed the room name.", tcell.ColorGreen)
154 } else {
155 text = text.AppendColor("changed the room name to ", tcell.ColorGreen).
156 AppendStyle(content.Name, tcell.StyleDefault.Underline(true)).
157 AppendColor(".", tcell.ColorGreen)
158 }
159 case *event.CanonicalAliasEventContent:
160 prevContent := &event.CanonicalAliasEventContent{}
161 if evt.Unsigned.PrevContent != nil {
162 _ = evt.Unsigned.PrevContent.ParseRaw(evt.Type)
163 prevContent = evt.Unsigned.PrevContent.AsCanonicalAlias()
164 }
165 debug.Printf("%+v -> %+v", prevContent, content)
166 if len(content.Alias) == 0 && len(prevContent.Alias) != 0 {
167 text = text.AppendColor("removed the main address of the room", tcell.ColorGreen)
168 } else if content.Alias != prevContent.Alias {
169 text = text.
170 AppendColor("changed the main address of the room to ", tcell.ColorGreen).
171 AppendStyle(string(content.Alias), tcell.StyleDefault.Underline(true))
172 } else {
173 added, removed := findAltAliasDifference(content.AltAliases, prevContent.AltAliases)
174 if len(added) > 0 {
175 if len(removed) > 0 {
176 text = text.
177 AppendTString(added).
178 AppendColor(" and ", tcell.ColorGreen).
179 AppendTString(removed)
180 } else {
181 text = text.AppendTString(added)
182 }
183 } else if len(removed) > 0 {
184 text = text.AppendTString(removed)
185 } else {
186 text = text.AppendColor("changed nothing", tcell.ColorGreen)
187 }
188 text = text.AppendColor(" for this room", tcell.ColorGreen)
189 }
190 }
191 return NewExpandedTextMessage(evt, displayname, text)
192 }
193
194 func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Event, displayname string) *UIMessage {
195 content := evt.Content.AsMessage()
196 if len(content.GetReplyTo()) > 0 {
197 content.RemoveReplyFallback()
198 }
199 if len(evt.Gomuks.Edits) > 0 {
200 newContent := evt.Gomuks.Edits[len(evt.Gomuks.Edits)-1].Content.AsMessage().NewContent
201 if newContent != nil {
202 content = newContent
203 }
204 }
205 switch content.MsgType {
206 case event.MsgText, event.MsgNotice, event.MsgEmote:
207 var htmlEntity html.Entity
208 if content.Format == event.FormatHTML && len(content.FormattedBody) > 0 {
209 htmlEntity = html.Parse(matrix.Preferences(), room, content, evt, displayname)
210 if htmlEntity == nil {
211 htmlEntity = html.NewTextEntity("Malformed message")
212 htmlEntity.AdjustStyle(html.AdjustStyleTextColor(tcell.ColorRed), html.AdjustStyleReasonNormal)
213 }
214 } else if len(content.Body) > 0 {
215 content.Body = strings.Replace(content.Body, "\t", " ", -1)
216 htmlEntity = html.TextToEntity(content.Body, evt.ID, matrix.Preferences().EnableInlineURLs())
217 } else {
218 htmlEntity = html.NewTextEntity("Blank message")
219 htmlEntity.AdjustStyle(html.AdjustStyleTextColor(tcell.ColorRed), html.AdjustStyleReasonNormal)
220 }
221 return NewHTMLMessage(evt, displayname, htmlEntity)
222 case event.MsgImage, event.MsgVideo, event.MsgAudio, event.MsgFile:
223 msg := NewFileMessage(matrix, evt, displayname)
224 if !matrix.Preferences().DisableDownloads {
225 renderer := msg.Renderer.(*FileMessage)
226 renderer.DownloadPreview()
227 }
228 return msg
229 }
230 return nil
231 }
232
233 func getMembershipChangeMessage(evt *muksevt.Event, content *event.MemberEventContent, prevMembership event.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) {
234 switch content.Membership {
235 case "invite":
236 sender = "---"
237 text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", senderDisplayname, displayname), tcell.ColorGreen)
238 text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
239 text.Colorize(len(senderDisplayname)+len(" invited "), len(displayname), widget.GetHashColor(evt.StateKey))
240 case "join":
241 sender = "-->"
242 if prevMembership == event.MembershipInvite {
243 text = tstring.NewColorTString(fmt.Sprintf("%s accepted the invite.", displayname), tcell.ColorGreen)
244 } else {
245 text = tstring.NewColorTString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen)
246 }
247 text.Colorize(0, len(displayname), widget.GetHashColor(evt.StateKey))
248 case "leave":
249 sender = "<--"
250 if evt.Sender != id.UserID(*evt.StateKey) {
251 if prevMembership == event.MembershipBan {
252 text = tstring.NewColorTString(fmt.Sprintf("%s unbanned %s", senderDisplayname, displayname), tcell.ColorGreen)
253 text.Colorize(len(senderDisplayname)+len(" unbanned "), len(displayname), widget.GetHashColor(evt.StateKey))
254 } else {
255 text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, content.Reason), tcell.ColorRed)
256 text.Colorize(len(senderDisplayname)+len(" kicked "), len(displayname), widget.GetHashColor(evt.StateKey))
257 }
258 text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
259 } else {
260 if displayname == *evt.StateKey {
261 displayname = prevDisplayname
262 }
263 if prevMembership == event.MembershipInvite {
264 text = tstring.NewColorTString(fmt.Sprintf("%s rejected the invite.", displayname), tcell.ColorRed)
265 } else {
266 text = tstring.NewColorTString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed)
267 }
268 text.Colorize(0, len(displayname), widget.GetHashColor(evt.StateKey))
269 }
270 case "ban":
271 text = tstring.NewColorTString(fmt.Sprintf("%s banned %s: %s", senderDisplayname, displayname, content.Reason), tcell.ColorRed)
272 text.Colorize(len(senderDisplayname)+len(" banned "), len(displayname), widget.GetHashColor(evt.StateKey))
273 text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
274 }
275 return
276 }
277
278 func getMembershipEventContent(room *rooms.Room, evt *muksevt.Event) (sender string, text tstring.TString) {
279 member := room.GetMember(evt.Sender)
280 senderDisplayname := string(evt.Sender)
281 if member != nil {
282 senderDisplayname = member.Displayname
283 }
284
285 content := evt.Content.AsMember()
286 displayname := content.Displayname
287 if len(displayname) == 0 {
288 displayname = *evt.StateKey
289 }
290
291 prevMembership := event.MembershipLeave
292 prevDisplayname := *evt.StateKey
293 if evt.Unsigned.PrevContent != nil {
294 _ = evt.Unsigned.PrevContent.ParseRaw(evt.Type)
295 prevContent := evt.Unsigned.PrevContent.AsMember()
296 prevMembership = prevContent.Membership
297 prevDisplayname = prevContent.Displayname
298 if len(prevDisplayname) == 0 {
299 prevDisplayname = *evt.StateKey
300 }
301 }
302
303 if content.Membership != prevMembership {
304 sender, text = getMembershipChangeMessage(evt, content, prevMembership, senderDisplayname, displayname, prevDisplayname)
305 } else if displayname != prevDisplayname {
306 sender = "---"
307 color := widget.GetHashColor(evt.StateKey)
308 text = tstring.NewBlankTString().
309 AppendColor(prevDisplayname, color).
310 AppendColor(" changed their display name to ", tcell.ColorGreen).
311 AppendColor(displayname, color).
312 AppendColor(".", tcell.ColorGreen)
313 }
314 return
315 }
316
317 func ParseMembershipEvent(room *rooms.Room, evt *muksevt.Event) *UIMessage {
318 displayname, text := getMembershipEventContent(room, evt)
319 if len(text) == 0 {
320 return nil
321 }
322
323 return NewExpandedTextMessage(evt, displayname, text)
324 }