2018-04-10 18:31:28 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2019-01-17 13:13:25 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-04-10 18:31:28 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-04-10 18:31:28 +02:00
|
|
|
// 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-04-10 18:31:28 +02:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// 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/>.
|
2018-04-10 18:31:28 +02:00
|
|
|
|
2018-04-14 10:50:18 +02:00
|
|
|
package parser
|
2018-04-10 18:31:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-13 23:00:35 +01:00
|
|
|
"html"
|
2018-04-24 21:08:57 +02:00
|
|
|
"strings"
|
2018-04-10 18:31:28 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/gomuks/debug"
|
|
|
|
"maunium.net/go/gomuks/interface"
|
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-04-14 10:50:18 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages"
|
2018-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages/tstring"
|
2018-04-10 18:31:28 +02:00
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-11-13 23:00:35 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2018-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/tcell"
|
2018-04-10 18:31:28 +02:00
|
|
|
)
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func ParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) messages.UIMessage {
|
2018-04-10 18:31:28 +02:00
|
|
|
switch evt.Type {
|
2018-11-13 23:00:35 +01:00
|
|
|
case mautrix.EventSticker:
|
|
|
|
evt.Content.MsgType = mautrix.MsgImage
|
|
|
|
fallthrough
|
|
|
|
case mautrix.EventMessage:
|
2018-04-18 17:35:24 +02:00
|
|
|
return ParseMessage(matrix, room, evt)
|
2019-01-11 22:28:47 +01:00
|
|
|
case mautrix.StateTopic, mautrix.StateRoomName, mautrix.StateAliases, mautrix.StateCanonicalAlias:
|
|
|
|
return ParseStateEvent(matrix, room, evt)
|
2018-11-13 23:00:35 +01:00
|
|
|
case mautrix.StateMember:
|
2018-04-18 12:38:33 +02:00
|
|
|
return ParseMembershipEvent(room, evt)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unixToTime(unix int64) time.Time {
|
|
|
|
timestamp := time.Now()
|
|
|
|
if unix != 0 {
|
|
|
|
timestamp = time.Unix(unix/1000, unix%1000*1000)
|
|
|
|
}
|
|
|
|
return timestamp
|
|
|
|
}
|
|
|
|
|
2019-01-11 22:28:47 +01:00
|
|
|
func ParseStateEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) messages.UIMessage {
|
|
|
|
displayname := evt.Sender
|
|
|
|
member := room.GetMember(evt.Sender)
|
|
|
|
if member != nil {
|
|
|
|
displayname = member.Displayname
|
|
|
|
}
|
|
|
|
text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender))
|
|
|
|
switch evt.Type {
|
|
|
|
case mautrix.StateTopic:
|
|
|
|
if len(evt.Content.Topic) == 0 {
|
|
|
|
text = text.AppendColor(" removed the topic.", tcell.ColorGreen)
|
|
|
|
} else {
|
|
|
|
text = text.AppendColor(" changed the topic to ", tcell.ColorGreen).
|
|
|
|
AppendStyle(evt.Content.Topic, tcell.StyleDefault.Underline(true)).
|
|
|
|
AppendColor(".", tcell.ColorGreen)
|
|
|
|
}
|
|
|
|
case mautrix.StateRoomName:
|
|
|
|
if len(evt.Content.Name) == 0 {
|
|
|
|
text = text.AppendColor(" removed the room name.", tcell.ColorGreen)
|
|
|
|
} else {
|
|
|
|
text = text.AppendColor(" changed the room name to ", tcell.ColorGreen).
|
|
|
|
AppendStyle(evt.Content.Name, tcell.StyleDefault.Underline(true)).
|
|
|
|
AppendColor(".", tcell.ColorGreen)
|
|
|
|
}
|
|
|
|
case mautrix.StateCanonicalAlias:
|
|
|
|
if len(evt.Content.Alias) == 0 {
|
|
|
|
text = text.AppendColor(" removed the main address of the room.", tcell.ColorGreen)
|
|
|
|
} else {
|
|
|
|
text = text.AppendColor(" changed the main address of the room to ", tcell.ColorGreen).
|
|
|
|
AppendStyle(evt.Content.Alias, tcell.StyleDefault.Underline(true)).
|
|
|
|
AppendColor(".", tcell.ColorGreen)
|
|
|
|
}
|
|
|
|
case mautrix.StateAliases:
|
|
|
|
text = ParseAliasEvent(evt, displayname)
|
|
|
|
}
|
|
|
|
ts := unixToTime(evt.Timestamp)
|
|
|
|
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, mautrix.MessageType(evt.Type.Type), text, ts)
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) messages.UIMessage {
|
2018-04-18 12:38:33 +02:00
|
|
|
displayname := evt.Sender
|
|
|
|
member := room.GetMember(evt.Sender)
|
|
|
|
if member != nil {
|
2018-09-05 09:55:48 +02:00
|
|
|
displayname = member.Displayname
|
2018-04-18 12:38:33 +02:00
|
|
|
}
|
2018-11-13 23:00:35 +01:00
|
|
|
if len(evt.Content.GetReplyTo()) > 0 {
|
|
|
|
evt.Content.RemoveReplyFallback()
|
2019-03-25 23:37:35 +01:00
|
|
|
roomID := evt.Content.RelatesTo.InReplyTo.RoomID
|
|
|
|
if len(roomID) == 0 {
|
|
|
|
roomID = room.ID
|
|
|
|
}
|
2019-04-05 22:44:17 +02:00
|
|
|
replyToEvt, _ := matrix.GetEvent(room, evt.Content.GetReplyTo())
|
2019-03-25 23:37:35 +01:00
|
|
|
if replyToEvt != nil {
|
|
|
|
replyToEvt.Content.RemoveReplyFallback()
|
|
|
|
if len(replyToEvt.Content.FormattedBody) == 0 {
|
|
|
|
replyToEvt.Content.FormattedBody = html.EscapeString(replyToEvt.Content.Body)
|
|
|
|
}
|
|
|
|
evt.Content.FormattedBody = fmt.Sprintf(
|
2019-04-07 21:54:55 +02:00
|
|
|
"In reply to <a href='https://matrix.to/#/%[1]s'>%[1]s</a><blockquote>%[2]s</blockquote><br/><br/>%[3]s",
|
2019-03-25 23:37:35 +01:00
|
|
|
replyToEvt.Sender, replyToEvt.Content.FormattedBody, evt.Content.FormattedBody)
|
|
|
|
} else {
|
|
|
|
evt.Content.FormattedBody = fmt.Sprintf(
|
|
|
|
"In reply to unknown event https://matrix.to/#/%[1]s/%[2]s<br/>%[3]s",
|
|
|
|
roomID, evt.Content.GetReplyTo(), evt.Content.FormattedBody)
|
2018-11-13 23:00:35 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 18:31:28 +02:00
|
|
|
ts := unixToTime(evt.Timestamp)
|
2018-09-05 09:55:48 +02:00
|
|
|
switch evt.Content.MsgType {
|
2018-04-13 20:25:45 +02:00
|
|
|
case "m.text", "m.notice", "m.emote":
|
2018-11-13 23:00:35 +01:00
|
|
|
if evt.Content.Format == mautrix.FormatHTML {
|
2019-04-07 02:22:51 +02:00
|
|
|
return messages.NewHTMLMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, ParseHTMLMessage(room, evt, displayname), ts)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
2018-09-05 09:55:48 +02:00
|
|
|
evt.Content.Body = strings.Replace(evt.Content.Body, "\t", " ", -1)
|
|
|
|
return messages.NewTextMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, evt.Content.Body, ts)
|
2018-04-10 18:31:28 +02:00
|
|
|
case "m.image":
|
2018-09-05 09:55:48 +02:00
|
|
|
data, hs, id, err := matrix.Download(evt.Content.URL)
|
2018-04-10 18:31:28 +02:00
|
|
|
if err != nil {
|
2018-09-05 09:55:48 +02:00
|
|
|
debug.Printf("Failed to download %s: %v", evt.Content.URL, err)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
2018-09-05 09:55:48 +02:00
|
|
|
return messages.NewImageMessage(matrix, evt.ID, evt.Sender, displayname, evt.Content.MsgType, evt.Content.Body, hs, id, data, ts)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func getMembershipChangeMessage(evt *mautrix.Event, membership, prevMembership mautrix.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) {
|
2018-05-22 16:36:41 +02:00
|
|
|
switch membership {
|
|
|
|
case "invite":
|
|
|
|
sender = "---"
|
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", senderDisplayname, displayname), tcell.ColorGreen)
|
|
|
|
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
|
|
|
|
text.Colorize(len(senderDisplayname)+len(" invited "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
|
|
|
case "join":
|
|
|
|
sender = "-->"
|
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen)
|
|
|
|
text.Colorize(0, len(displayname), widget.GetHashColor(*evt.StateKey))
|
|
|
|
case "leave":
|
|
|
|
sender = "<--"
|
|
|
|
if evt.Sender != *evt.StateKey {
|
2018-11-13 23:00:35 +01:00
|
|
|
if prevMembership == mautrix.MembershipBan {
|
2018-05-22 16:36:41 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s unbanned %s", senderDisplayname, displayname), tcell.ColorGreen)
|
|
|
|
text.Colorize(len(senderDisplayname)+len(" unbanned "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
|
|
|
} else {
|
2018-09-05 09:55:48 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, evt.Content.Reason), tcell.ColorRed)
|
2018-05-22 16:36:41 +02:00
|
|
|
text.Colorize(len(senderDisplayname)+len(" kicked "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
|
|
|
}
|
|
|
|
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
|
|
|
|
} else {
|
|
|
|
if displayname == *evt.StateKey {
|
|
|
|
displayname = prevDisplayname
|
|
|
|
}
|
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed)
|
|
|
|
text.Colorize(0, len(displayname), widget.GetHashColor(*evt.StateKey))
|
|
|
|
}
|
|
|
|
case "ban":
|
2018-09-05 09:55:48 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s banned %s: %s", senderDisplayname, displayname, evt.Content.Reason), tcell.ColorRed)
|
2018-05-22 16:36:41 +02:00
|
|
|
text.Colorize(len(senderDisplayname)+len(" banned "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
|
|
|
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender string, text tstring.TString) {
|
2018-04-18 12:38:33 +02:00
|
|
|
member := room.GetMember(evt.Sender)
|
2018-04-18 16:33:59 +02:00
|
|
|
senderDisplayname := evt.Sender
|
2018-04-18 12:38:33 +02:00
|
|
|
if member != nil {
|
2018-09-05 09:55:48 +02:00
|
|
|
senderDisplayname = member.Displayname
|
2018-04-18 12:38:33 +02:00
|
|
|
}
|
2018-04-18 16:33:59 +02:00
|
|
|
|
2018-09-05 09:55:48 +02:00
|
|
|
membership := evt.Content.Membership
|
|
|
|
displayname := evt.Content.Displayname
|
2018-04-10 18:31:28 +02:00
|
|
|
if len(displayname) == 0 {
|
|
|
|
displayname = *evt.StateKey
|
|
|
|
}
|
2018-04-18 16:33:59 +02:00
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
prevMembership := mautrix.MembershipLeave
|
2018-04-18 16:33:59 +02:00
|
|
|
prevDisplayname := *evt.StateKey
|
2018-04-10 18:31:28 +02:00
|
|
|
if evt.Unsigned.PrevContent != nil {
|
2018-09-05 09:55:48 +02:00
|
|
|
prevMembership = evt.Unsigned.PrevContent.Membership
|
|
|
|
prevDisplayname = evt.Unsigned.PrevContent.Displayname
|
2018-05-15 15:09:56 +02:00
|
|
|
if len(prevDisplayname) == 0 {
|
|
|
|
prevDisplayname = *evt.StateKey
|
|
|
|
}
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if membership != prevMembership {
|
2018-05-22 16:36:41 +02:00
|
|
|
sender, text = getMembershipChangeMessage(evt, membership, prevMembership, senderDisplayname, displayname, prevDisplayname)
|
2018-04-10 18:31:28 +02:00
|
|
|
} else if displayname != prevDisplayname {
|
|
|
|
sender = "---"
|
2018-05-22 20:18:46 +02:00
|
|
|
color := widget.GetHashColor(*evt.StateKey)
|
2019-01-11 22:28:47 +01:00
|
|
|
text = tstring.NewBlankTString().
|
|
|
|
AppendColor(prevDisplayname, color).
|
|
|
|
AppendColor(" changed their display name to ", tcell.ColorGreen).
|
|
|
|
AppendColor(displayname, color).
|
|
|
|
AppendColor(".", tcell.ColorGreen)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) messages.UIMessage {
|
2018-04-18 12:38:33 +02:00
|
|
|
displayname, text := getMembershipEventContent(room, evt)
|
2018-04-18 16:33:59 +02:00
|
|
|
if len(text) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 18:31:28 +02:00
|
|
|
ts := unixToTime(evt.Timestamp)
|
2018-04-30 22:09:14 +02:00
|
|
|
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.member", text, ts)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
2019-01-11 22:28:47 +01:00
|
|
|
|
|
|
|
func ParseAliasEvent(evt *mautrix.Event, displayname string) tstring.TString {
|
|
|
|
var prevAliases []string
|
|
|
|
if evt.Unsigned.PrevContent != nil {
|
|
|
|
prevAliases = evt.Unsigned.PrevContent.Aliases
|
|
|
|
}
|
|
|
|
aliases := evt.Content.Aliases
|
|
|
|
var added, removed []tstring.TString
|
|
|
|
Outer1:
|
|
|
|
for _, oldAlias := range prevAliases {
|
|
|
|
for _, newAlias := range aliases {
|
|
|
|
if oldAlias == newAlias {
|
|
|
|
continue Outer1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removed = append(removed, tstring.NewStyleTString(oldAlias, tcell.StyleDefault.Foreground(widget.GetHashColor(oldAlias)).Underline(true)))
|
|
|
|
}
|
|
|
|
Outer2:
|
|
|
|
for _, newAlias := range aliases {
|
|
|
|
for _, oldAlias := range prevAliases {
|
|
|
|
if oldAlias == newAlias {
|
|
|
|
continue Outer2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
added = append(added, tstring.NewStyleTString(newAlias, tcell.StyleDefault.Foreground(widget.GetHashColor(newAlias)).Underline(true)))
|
|
|
|
}
|
|
|
|
var addedStr, removedStr tstring.TString
|
|
|
|
if len(added) == 1 {
|
|
|
|
addedStr = added[0]
|
|
|
|
} else if len(added) > 1 {
|
|
|
|
addedStr = tstring.
|
|
|
|
Join(added[:len(added)-1], ", ").
|
|
|
|
Append(" and ").
|
|
|
|
AppendTString(added[len(added)-1])
|
|
|
|
}
|
|
|
|
if len(removed) == 1 {
|
|
|
|
removedStr = removed[0]
|
|
|
|
} else if len(removed) > 1 {
|
|
|
|
removedStr = tstring.
|
|
|
|
Join(removed[:len(removed)-1], ", ").
|
|
|
|
Append(" and ").
|
|
|
|
AppendTString(removed[len(removed)-1])
|
|
|
|
}
|
|
|
|
text := tstring.NewBlankTString()
|
|
|
|
if len(addedStr) > 0 && len(removedStr) > 0 {
|
|
|
|
text = text.AppendColor(fmt.Sprintf("%s added ", displayname), tcell.ColorGreen).
|
|
|
|
AppendTString(addedStr).
|
|
|
|
AppendColor(" and removed ", tcell.ColorGreen).
|
|
|
|
AppendTString(removedStr).
|
|
|
|
AppendColor(" as addresses for this room.", tcell.ColorGreen)
|
|
|
|
} else if len(addedStr) > 0 {
|
|
|
|
text = text.AppendColor(fmt.Sprintf("%s added ", displayname), tcell.ColorGreen).
|
|
|
|
AppendTString(addedStr).
|
|
|
|
AppendColor(" as addresses for this room.", tcell.ColorGreen)
|
|
|
|
} else if len(removedStr) > 0 {
|
|
|
|
text = text.AppendColor(fmt.Sprintf("%s removed ", displayname), tcell.ColorGreen).
|
|
|
|
AppendTString(removedStr).
|
|
|
|
AppendColor(" as addresses for this room.", tcell.ColorGreen)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|