2018-04-10 18:31:28 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-14 10:50:18 +02:00
|
|
|
package parser
|
2018-04-10 18:31:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/gomatrix"
|
|
|
|
"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-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/tcell"
|
2018-04-10 18:31:28 +02:00
|
|
|
)
|
|
|
|
|
2018-04-14 10:50:18 +02:00
|
|
|
func ParseEvent(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
|
2018-04-10 18:31:28 +02:00
|
|
|
member := room.GetMember(evt.Sender)
|
|
|
|
if member != nil {
|
|
|
|
evt.Sender = member.DisplayName
|
|
|
|
}
|
|
|
|
switch evt.Type {
|
|
|
|
case "m.room.message":
|
2018-04-14 10:44:07 +02:00
|
|
|
return ParseMessage(gmx, room, evt)
|
2018-04-10 18:31:28 +02:00
|
|
|
case "m.room.member":
|
|
|
|
return ParseMembershipEvent(evt)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unixToTime(unix int64) time.Time {
|
|
|
|
timestamp := time.Now()
|
|
|
|
if unix != 0 {
|
|
|
|
timestamp = time.Unix(unix/1000, unix%1000*1000)
|
|
|
|
}
|
|
|
|
return timestamp
|
|
|
|
}
|
|
|
|
|
2018-04-14 10:50:18 +02:00
|
|
|
func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
|
2018-04-10 18:31:28 +02:00
|
|
|
msgtype, _ := evt.Content["msgtype"].(string)
|
|
|
|
ts := unixToTime(evt.Timestamp)
|
|
|
|
switch msgtype {
|
2018-04-13 20:25:45 +02:00
|
|
|
case "m.text", "m.notice", "m.emote":
|
2018-04-13 23:34:25 +02:00
|
|
|
format, hasFormat := evt.Content["format"].(string)
|
|
|
|
if hasFormat && format == "org.matrix.custom.html" {
|
2018-04-14 10:44:07 +02:00
|
|
|
text := ParseHTMLMessage(room, evt)
|
2018-04-14 10:50:18 +02:00
|
|
|
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, msgtype, text, ts)
|
2018-04-13 23:34:25 +02:00
|
|
|
} else {
|
|
|
|
text, _ := evt.Content["body"].(string)
|
2018-04-14 10:50:18 +02:00
|
|
|
return messages.NewTextMessage(evt.ID, evt.Sender, msgtype, text, ts)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
2018-04-10 18:31:28 +02:00
|
|
|
case "m.image":
|
|
|
|
url, _ := evt.Content["url"].(string)
|
2018-04-11 18:20:40 +02:00
|
|
|
data, hs, id, err := gmx.Matrix().Download(url)
|
2018-04-10 18:31:28 +02:00
|
|
|
if err != nil {
|
|
|
|
debug.Printf("Failed to download %s: %v", url, err)
|
|
|
|
}
|
2018-04-14 10:50:18 +02:00
|
|
|
return messages.NewImageMessage(gmx, evt.ID, evt.Sender, msgtype, hs, id, data, ts)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:15 +02:00
|
|
|
func getMembershipEventContent(evt *gomatrix.Event) (sender string, text tstring.TString) {
|
2018-04-10 18:31:28 +02:00
|
|
|
membership, _ := evt.Content["membership"].(string)
|
|
|
|
displayname, _ := evt.Content["displayname"].(string)
|
|
|
|
if len(displayname) == 0 {
|
|
|
|
displayname = *evt.StateKey
|
|
|
|
}
|
|
|
|
prevMembership := "leave"
|
|
|
|
prevDisplayname := ""
|
|
|
|
if evt.Unsigned.PrevContent != nil {
|
|
|
|
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
|
|
|
|
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if membership != prevMembership {
|
|
|
|
switch membership {
|
|
|
|
case "invite":
|
|
|
|
sender = "---"
|
2018-04-13 20:25:45 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", evt.Sender, displayname), tcell.ColorGreen)
|
2018-04-10 18:31:28 +02:00
|
|
|
text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender))
|
|
|
|
text.Colorize(len(evt.Sender)+len(" invited "), len(displayname), widget.GetHashColor(displayname))
|
|
|
|
case "join":
|
|
|
|
sender = "-->"
|
2018-04-11 16:57:15 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen)
|
2018-04-10 18:31:28 +02:00
|
|
|
text.Colorize(0, len(displayname), widget.GetHashColor(displayname))
|
|
|
|
case "leave":
|
|
|
|
sender = "<--"
|
|
|
|
if evt.Sender != *evt.StateKey {
|
|
|
|
reason, _ := evt.Content["reason"].(string)
|
2018-04-11 16:57:15 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", evt.Sender, displayname, reason), tcell.ColorRed)
|
2018-04-10 18:31:28 +02:00
|
|
|
text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender))
|
|
|
|
text.Colorize(len(evt.Sender)+len(" kicked "), len(displayname), widget.GetHashColor(displayname))
|
|
|
|
} else {
|
2018-04-11 16:57:15 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed)
|
2018-04-10 18:31:28 +02:00
|
|
|
text.Colorize(0, len(displayname), widget.GetHashColor(displayname))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if displayname != prevDisplayname {
|
|
|
|
sender = "---"
|
2018-04-11 16:57:15 +02:00
|
|
|
text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorYellow)
|
2018-04-10 18:31:28 +02:00
|
|
|
text.Colorize(0, len(prevDisplayname), widget.GetHashColor(prevDisplayname))
|
|
|
|
text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), widget.GetHashColor(displayname))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 10:50:18 +02:00
|
|
|
func ParseMembershipEvent(evt *gomatrix.Event) messages.UIMessage {
|
2018-04-10 18:31:28 +02:00
|
|
|
sender, text := getMembershipEventContent(evt)
|
|
|
|
ts := unixToTime(evt.Timestamp)
|
2018-04-14 10:50:18 +02:00
|
|
|
return messages.NewExpandedTextMessage(evt.ID, sender, "m.room.membership", text, ts)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|