mirror of
https://github.com/maubot/maubot
synced 2025-08-29 19:00:39 +00:00
Update gomatrix, add logging and other things
This commit is contained in:
parent
d261997d84
commit
ff26598910
14 changed files with 242 additions and 424 deletions
|
@ -21,6 +21,7 @@ import (
|
|||
"regexp"
|
||||
"strings"
|
||||
|
||||
"maunium.net/go/gomatrix"
|
||||
log "maunium.net/go/maulogger"
|
||||
|
||||
"maubot.xyz"
|
||||
|
@ -33,7 +34,7 @@ type ParsedCommand struct {
|
|||
StartsWith string
|
||||
Matches *regexp.Regexp
|
||||
MatchAgainst string
|
||||
MatchesEvent *maubot.Event
|
||||
MatchesEvent interface{}
|
||||
}
|
||||
|
||||
func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error {
|
||||
|
@ -132,7 +133,7 @@ func deepGet(from map[string]interface{}, path string) interface{} {
|
|||
}
|
||||
}
|
||||
|
||||
func (pc *ParsedCommand) MatchActive(evt *maubot.Event) bool {
|
||||
func (pc *ParsedCommand) MatchActive(evt *gomatrix.Event) bool {
|
||||
if !strings.HasPrefix(evt.Content.Body, pc.StartsWith) {
|
||||
return false
|
||||
}
|
||||
|
@ -143,28 +144,30 @@ func (pc *ParsedCommand) MatchActive(evt *maubot.Event) bool {
|
|||
// First element is whole content
|
||||
match = match[1:]
|
||||
|
||||
evt.Content.Command.Arguments = make(map[string]string)
|
||||
command := &gomatrix.MatchedCommand{
|
||||
Arguments: make(map[string]string),
|
||||
}
|
||||
for i, value := range match {
|
||||
if i >= len(pc.Arguments) {
|
||||
break
|
||||
}
|
||||
key := pc.Arguments[i]
|
||||
evt.Content.Command.Arguments[key] = value
|
||||
command.Arguments[key] = value
|
||||
}
|
||||
|
||||
evt.Content.Command.Matched = pc.Name
|
||||
command.Matched = pc.Name
|
||||
// TODO add evt.Content.Command.Target?
|
||||
|
||||
evt.Content.Command = command
|
||||
return true
|
||||
}
|
||||
|
||||
func (pc *ParsedCommand) MatchPassive(evt *maubot.Event) bool {
|
||||
func (pc *ParsedCommand) MatchPassive(evt *gomatrix.Event) bool {
|
||||
matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string)
|
||||
if !ok {
|
||||
matchAgainst = evt.Content.Body
|
||||
}
|
||||
|
||||
if pc.MatchesEvent != nil && !pc.MatchesEvent.Equals(evt) {
|
||||
if pc.MatchesEvent != nil && !maubot.JSONLeftEquals(pc.MatchesEvent, evt) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -173,18 +176,19 @@ func (pc *ParsedCommand) MatchPassive(evt *maubot.Event) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
values := make([]string, len(matches))
|
||||
for i, match := range matches {
|
||||
values[i] = match[0]
|
||||
if evt.Unsigned.PassiveCommand == nil {
|
||||
evt.Unsigned.PassiveCommand = make(map[string]*gomatrix.MatchedPassiveCommand)
|
||||
}
|
||||
|
||||
evt.Unsigned.PassiveCommand.Matched = pc.Name
|
||||
evt.Unsigned.PassiveCommand.Values = values
|
||||
evt.Unsigned.PassiveCommand[pc.Name] = &gomatrix.MatchedPassiveCommand{
|
||||
Captured: matches,
|
||||
}
|
||||
//evt.Unsigned.PassiveCommand.Matched = pc.Name
|
||||
//evt.Unsigned.PassiveCommand.Captured = matches
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (pc *ParsedCommand) Match(evt *maubot.Event) bool {
|
||||
func (pc *ParsedCommand) Match(evt *gomatrix.Event) bool {
|
||||
if pc.IsPassive {
|
||||
return pc.MatchPassive(evt)
|
||||
} else {
|
||||
|
|
|
@ -17,81 +17,53 @@
|
|||
package matrix
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"maubot.xyz"
|
||||
"maunium.net/go/gomatrix"
|
||||
"maunium.net/go/gomatrix/format"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
*maubot.Event
|
||||
type EventFuncsImpl struct {
|
||||
*gomatrix.Event
|
||||
Client *Client
|
||||
}
|
||||
|
||||
func roundtripContent(rawContent map[string]interface{}) (content *maubot.Content) {
|
||||
content = &maubot.Content{}
|
||||
if len(rawContent) == 0 {
|
||||
content.Raw = rawContent
|
||||
return
|
||||
func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *maubot.Event {
|
||||
if mxEvent == nil {
|
||||
return nil
|
||||
}
|
||||
data, _ := json.Marshal(&rawContent)
|
||||
json.Unmarshal(data, &content)
|
||||
content.Raw = rawContent
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *Event {
|
||||
var stateKey string
|
||||
if mxEvent.StateKey != nil {
|
||||
stateKey = *mxEvent.StateKey
|
||||
}
|
||||
event := &Event{
|
||||
Client: client,
|
||||
}
|
||||
mbEvent := &maubot.Event{
|
||||
EventFuncs: event,
|
||||
StateKey: stateKey,
|
||||
Sender: mxEvent.Sender,
|
||||
Type: maubot.EventType(mxEvent.Type),
|
||||
Timestamp: mxEvent.Timestamp,
|
||||
ID: mxEvent.ID,
|
||||
RoomID: mxEvent.RoomID,
|
||||
Content: *roundtripContent(mxEvent.Content),
|
||||
Redacts: mxEvent.Redacts,
|
||||
Unsigned: maubot.Unsigned{
|
||||
PrevContent: roundtripContent(mxEvent.Unsigned.PrevContent),
|
||||
PrevSender: mxEvent.Unsigned.PrevSender,
|
||||
ReplacesState: mxEvent.Unsigned.ReplacesState,
|
||||
Age: mxEvent.Unsigned.Age,
|
||||
mxEvent.Content.RemoveReplyFallback()
|
||||
return &maubot.Event{
|
||||
EventFuncs: &EventFuncsImpl{
|
||||
Event: mxEvent,
|
||||
Client: client,
|
||||
},
|
||||
Event: mxEvent,
|
||||
}
|
||||
RemoveReplyFallback(mbEvent)
|
||||
event.Event = mbEvent
|
||||
return event
|
||||
}
|
||||
|
||||
func (evt *Event) MarkRead() error {
|
||||
func (evt *EventFuncsImpl) MarkRead() error {
|
||||
return evt.Client.MarkRead(evt.RoomID, evt.ID)
|
||||
}
|
||||
|
||||
func (evt *Event) Reply(text string) (string, error) {
|
||||
return evt.ReplyContent(RenderMarkdown(text))
|
||||
func (evt *EventFuncsImpl) Reply(text string) (string, error) {
|
||||
return evt.ReplyContent(format.RenderMarkdown(text))
|
||||
}
|
||||
|
||||
func (evt *Event) ReplyContent(content maubot.Content) (string, error) {
|
||||
return evt.SendContent(SetReply(content, evt))
|
||||
func (evt *EventFuncsImpl) ReplyContent(content gomatrix.Content) (string, error) {
|
||||
content.SetReply(evt.Event)
|
||||
return evt.SendContent(content)
|
||||
}
|
||||
|
||||
func (evt *Event) SendMessage(text string) (string, error) {
|
||||
return evt.SendContent(RenderMarkdown(text))
|
||||
func (evt *EventFuncsImpl) SendMessage(text string) (string, error) {
|
||||
return evt.SendContent(format.RenderMarkdown(text))
|
||||
}
|
||||
|
||||
func (evt *Event) SendContent(content maubot.Content) (string, error) {
|
||||
return evt.SendRawEvent(maubot.EventMessage, content)
|
||||
func (evt *EventFuncsImpl) SendContent(content gomatrix.Content) (string, error) {
|
||||
return evt.SendRawEvent(gomatrix.EventMessage, content)
|
||||
}
|
||||
|
||||
func (evt *Event) SendRawEvent(evtType maubot.EventType, content interface{}) (string, error) {
|
||||
resp, err := evt.Client.SendMessageEvent(evt.RoomID, string(evtType), content)
|
||||
func (evt *EventFuncsImpl) SendRawEvent(evtType gomatrix.EventType, content interface{}) (string, error) {
|
||||
resp, err := evt.Client.SendMessageEvent(evt.RoomID, evtType, content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -20,15 +20,15 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
|
||||
|
||||
type htmlParser struct {}
|
||||
type htmlParser struct{}
|
||||
|
||||
type taggedString struct {
|
||||
string
|
||||
|
@ -124,13 +124,13 @@ func (parser *htmlParser) linkToString(node *html.Node, stripLinebreak bool) str
|
|||
}
|
||||
match := matrixToURL.FindStringSubmatch(href)
|
||||
if len(match) == 2 {
|
||||
// pillTarget := match[1]
|
||||
// if pillTarget[0] == '@' {
|
||||
// if member := parser.room.GetMember(pillTarget); member != nil {
|
||||
// return member.DisplayName
|
||||
// }
|
||||
// }
|
||||
// return pillTarget
|
||||
// pillTarget := match[1]
|
||||
// if pillTarget[0] == '@' {
|
||||
// if member := parser.room.GetMember(pillTarget); member != nil {
|
||||
// return member.DisplayName
|
||||
// }
|
||||
// }
|
||||
// return pillTarget
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("%s (%s)", str, href)
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
// maubot - A plugin-based Matrix bot system 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 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 matrix
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"gopkg.in/russross/blackfriday.v2"
|
||||
"maubot.xyz"
|
||||
)
|
||||
|
||||
func RenderMarkdown(text string) maubot.Content {
|
||||
parser := blackfriday.New(
|
||||
blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
|
||||
blackfriday.Tables |
|
||||
blackfriday.FencedCode |
|
||||
blackfriday.Strikethrough |
|
||||
blackfriday.SpaceHeadings |
|
||||
blackfriday.DefinitionLists))
|
||||
ast := parser.Parse([]byte(text))
|
||||
|
||||
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
||||
Flags: blackfriday.UseXHTML,
|
||||
})
|
||||
|
||||
var buf strings.Builder
|
||||
renderer.RenderHeader(&buf, ast)
|
||||
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
|
||||
return renderer.RenderNode(&buf, node, entering)
|
||||
})
|
||||
renderer.RenderFooter(&buf, ast)
|
||||
htmlBody := buf.String()
|
||||
|
||||
return maubot.Content{
|
||||
FormattedBody: htmlBody,
|
||||
Format: maubot.FormatHTML,
|
||||
MsgType: maubot.MsgText,
|
||||
Body: HTMLToText(htmlBody),
|
||||
}
|
||||
}
|
|
@ -47,8 +47,8 @@ func NewClient(db *database.MatrixClient) (*Client, error) {
|
|||
client.syncer = NewMaubotSyncer(client, client.Store)
|
||||
client.Client.Syncer = client.syncer
|
||||
|
||||
client.AddEventHandler(maubot.StateMember, client.onJoin)
|
||||
client.AddEventHandler(maubot.EventMessage, client.onMessage)
|
||||
client.AddEventHandler(gomatrix.StateMember, client.onJoin)
|
||||
client.AddEventHandler(gomatrix.EventMessage, client.onMessage)
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ func (client *Client) Proxy(owner string) *ClientProxy {
|
|||
}
|
||||
}
|
||||
|
||||
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
|
||||
func (client *Client) AddEventHandler(evt gomatrix.EventType, handler maubot.EventHandler) {
|
||||
client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
|
||||
if evt.Sender == client.UserID {
|
||||
return maubot.StopEventPropagation
|
||||
|
@ -95,13 +95,13 @@ func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
|
|||
log.Warnf("Failed to get event %s @ %s: %v\n", eventID, roomID, err)
|
||||
return nil
|
||||
}
|
||||
return client.ParseEvent(evt).Event
|
||||
return client.ParseEvent(evt)
|
||||
}
|
||||
|
||||
func (client *Client) TriggerCommand(command *ParsedCommand, evt *maubot.Event) maubot.CommandHandlerResult {
|
||||
handlers, ok := client.handlers[command.Name]
|
||||
if !ok {
|
||||
log.Warnf("Command %s triggered by %s doesn't have any handlers.", command.Name, evt.Sender)
|
||||
log.Warnf("Command %s triggered by %s doesn't have any handlers.\n", command.Name, evt.Sender)
|
||||
return maubot.Continue
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,7 @@ func (client *Client) TriggerCommand(command *ParsedCommand, evt *maubot.Event)
|
|||
|
||||
func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult {
|
||||
for _, command := range client.commands {
|
||||
if command.Match(evt) {
|
||||
if command.Match(evt.Event) {
|
||||
return client.TriggerCommand(command, evt)
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult {
|
|||
}
|
||||
|
||||
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
|
||||
if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" {
|
||||
if client.DB.AutoJoinRooms && evt.GetStateKey() == client.DB.UserID && evt.Content.Membership == "invite" {
|
||||
client.JoinRoom(evt.RoomID)
|
||||
return maubot.StopEventPropagation
|
||||
}
|
||||
|
|
|
@ -1,103 +0,0 @@
|
|||
// maubot - A plugin-based Matrix bot system 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 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 matrix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"maubot.xyz"
|
||||
)
|
||||
|
||||
var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
|
||||
|
||||
func TrimReplyFallbackHTML(html string) string {
|
||||
return HTMLReplyFallbackRegex.ReplaceAllString(html, "")
|
||||
}
|
||||
|
||||
func TrimReplyFallbackText(text string) string {
|
||||
if !strings.HasPrefix(text, "> ") || !strings.Contains(text, "\n") {
|
||||
return text
|
||||
}
|
||||
|
||||
lines := strings.Split(text, "\n")
|
||||
for len(lines) > 0 && strings.HasPrefix(lines[0], "> ") {
|
||||
lines = lines[1:]
|
||||
}
|
||||
return strings.TrimSpace(strings.Join(lines, "\n"))
|
||||
}
|
||||
|
||||
func RemoveReplyFallback(evt *maubot.Event) {
|
||||
if len(evt.Content.RelatesTo.InReplyTo.EventID) > 0 {
|
||||
if evt.Content.Format == maubot.FormatHTML {
|
||||
evt.Content.FormattedBody = TrimReplyFallbackHTML(evt.Content.FormattedBody)
|
||||
}
|
||||
evt.Content.Body = TrimReplyFallbackText(evt.Content.Body)
|
||||
}
|
||||
}
|
||||
|
||||
const ReplyFormat = `<mx-reply><blockquote>
|
||||
<a href="https://matrix.to/#/%s/%s">In reply to</a>
|
||||
<a href="https://matrix.to/#/%s">%s</a>
|
||||
%s
|
||||
</blockquote></mx-reply>
|
||||
`
|
||||
|
||||
func ReplyFallbackHTML(evt *Event) string {
|
||||
body := evt.Content.FormattedBody
|
||||
if len(body) == 0 {
|
||||
body = html.EscapeString(evt.Content.Body)
|
||||
}
|
||||
|
||||
senderDisplayName := evt.Sender
|
||||
|
||||
return fmt.Sprintf(ReplyFormat, evt.RoomID, evt.ID, evt.Sender, senderDisplayName, body)
|
||||
}
|
||||
|
||||
func ReplyFallbackText(evt *Event) string {
|
||||
body := evt.Content.Body
|
||||
lines := strings.Split(strings.TrimSpace(body), "\n")
|
||||
firstLine, lines := lines[0], lines[1:]
|
||||
|
||||
senderDisplayName := evt.Sender
|
||||
|
||||
var fallbackText strings.Builder
|
||||
fmt.Fprintf(&fallbackText, "> <%s> %s", senderDisplayName, firstLine)
|
||||
for _, line := range lines {
|
||||
fmt.Fprintf(&fallbackText, "\n> %s", line)
|
||||
}
|
||||
fallbackText.WriteString("\n\n")
|
||||
return fallbackText.String()
|
||||
}
|
||||
|
||||
func SetReply(content maubot.Content, inReplyTo *Event) maubot.Content {
|
||||
content.RelatesTo.InReplyTo.EventID = inReplyTo.ID
|
||||
content.RelatesTo.InReplyTo.RoomID = inReplyTo.RoomID
|
||||
|
||||
if content.MsgType == maubot.MsgText || content.MsgType == maubot.MsgNotice {
|
||||
if len(content.FormattedBody) == 0 || content.Format != maubot.FormatHTML {
|
||||
content.FormattedBody = html.EscapeString(content.Body)
|
||||
content.Format = maubot.FormatHTML
|
||||
}
|
||||
content.FormattedBody = ReplyFallbackHTML(inReplyTo) + content.FormattedBody
|
||||
content.Body = ReplyFallbackText(inReplyTo) + content.Body
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
|
@ -13,7 +13,7 @@ import (
|
|||
type MaubotSyncer struct {
|
||||
Client *Client
|
||||
Store gomatrix.Storer
|
||||
listeners map[maubot.EventType][]maubot.EventHandler
|
||||
listeners map[gomatrix.EventType][]maubot.EventHandler
|
||||
}
|
||||
|
||||
// NewDefaultSyncer returns an instantiated DefaultSyncer
|
||||
|
@ -21,7 +21,7 @@ func NewMaubotSyncer(client *Client, store gomatrix.Storer) *MaubotSyncer {
|
|||
return &MaubotSyncer{
|
||||
Client: client,
|
||||
Store: store,
|
||||
listeners: make(map[maubot.EventType][]maubot.EventHandler),
|
||||
listeners: make(map[gomatrix.EventType][]maubot.EventHandler),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ func (s *MaubotSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
|
|||
|
||||
// OnEventType allows callers to be notified when there are new events for the given event type.
|
||||
// There are no duplicate checks.
|
||||
func (s *MaubotSyncer) OnEventType(eventType maubot.EventType, callback maubot.EventHandler) {
|
||||
func (s *MaubotSyncer) OnEventType(eventType gomatrix.EventType, callback maubot.EventHandler) {
|
||||
_, exists := s.listeners[eventType]
|
||||
if !exists {
|
||||
s.listeners[eventType] = []maubot.EventHandler{}
|
||||
|
@ -96,14 +96,9 @@ func (s *MaubotSyncer) shouldProcessResponse(resp *gomatrix.RespSync, since stri
|
|||
// TODO: We probably want to process messages from after the last join event in the timeline.
|
||||
for roomID, roomData := range resp.Rooms.Join {
|
||||
for i := len(roomData.Timeline.Events) - 1; i >= 0; i-- {
|
||||
e := roomData.Timeline.Events[i]
|
||||
if e.Type == "m.room.member" && e.StateKey != nil && *e.StateKey == s.Client.UserID {
|
||||
m := e.Content["membership"]
|
||||
mship, ok := m.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if mship == "join" {
|
||||
evt := roomData.Timeline.Events[i]
|
||||
if evt.Type == gomatrix.StateMember && evt.GetStateKey() == s.Client.UserID {
|
||||
if evt.Content.Membership == gomatrix.MembershipJoin {
|
||||
_, ok := resp.Rooms.Join[roomID]
|
||||
if !ok {
|
||||
continue
|
||||
|
@ -130,12 +125,12 @@ func (s *MaubotSyncer) getOrCreateRoom(roomID string) *gomatrix.Room {
|
|||
|
||||
func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) {
|
||||
event := s.Client.ParseEvent(mxEvent)
|
||||
listeners, exists := s.listeners[maubot.EventType(event.Type)]
|
||||
listeners, exists := s.listeners[event.Type]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
for _, fn := range listeners {
|
||||
if fn(event.Event) == maubot.StopEventPropagation {
|
||||
if fn(event) == maubot.StopEventPropagation {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue