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
|
@ -88,7 +88,7 @@ func (bot *Bot) createPlugins() {
|
|||
|
||||
log.Debugf("Created plugin %s (type %s v%s)\n", plugin.ID, creator.Name, creator.Version)
|
||||
bot.Plugins[plugin.ID] = &PluginWrapper{
|
||||
Plugin: creator.Create(client.Proxy(plugin.ID)),
|
||||
Plugin: creator.Create(client.Proxy(plugin.ID), log.Sub(plugin.ID)),
|
||||
Creator: creator,
|
||||
DB: plugin,
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
cfg.Logging.Configure(log.DefaultLogger)
|
||||
log.OpenFile()
|
||||
log.Debugln("Logger configured")
|
||||
|
||||
bot := app.New(cfg)
|
||||
|
|
51
commands.go
51
commands.go
|
@ -106,16 +106,63 @@ const (
|
|||
MatchAgainstBody = "body"
|
||||
)
|
||||
|
||||
// JSONLeftEquals checks if the given JSON-parsed interfaces are equal.
|
||||
// Extra properties in the right interface are ignored.
|
||||
func JSONLeftEquals(left, right interface{}) bool {
|
||||
switch val := left.(type) {
|
||||
case nil:
|
||||
return right == nil
|
||||
case bool:
|
||||
rightVal, ok := right.(bool)
|
||||
return ok && rightVal
|
||||
case float64:
|
||||
rightVal, ok := right.(float64)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return val == rightVal
|
||||
case string:
|
||||
rightVal, ok := right.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return val == rightVal
|
||||
case []interface{}:
|
||||
rightVal, ok := right.([]interface{})
|
||||
if !ok || len(val) != len(rightVal) {
|
||||
return false
|
||||
}
|
||||
for index, leftChild := range val {
|
||||
rightChild := rightVal[index]
|
||||
if !JSONLeftEquals(leftChild, rightChild) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
case map[string]interface{}:
|
||||
rightVal, ok := right.(map[string]interface{})
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
for key, leftChild := range val {
|
||||
rightChild, ok := rightVal[key]
|
||||
if !ok || !JSONLeftEquals(leftChild, rightChild) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type PassiveCommand struct {
|
||||
Name string `json:"name"`
|
||||
Matches string `json:"matches"`
|
||||
MatchAgainst string `json:"match_against"`
|
||||
MatchEvent *Event `json:"match_event"`
|
||||
MatchEvent interface{} `json:"match_event"`
|
||||
}
|
||||
|
||||
func (cmd PassiveCommand) Equals(otherCmd PassiveCommand) bool {
|
||||
return cmd.Name == otherCmd.Name &&
|
||||
cmd.Matches == otherCmd.Matches &&
|
||||
cmd.MatchAgainst == otherCmd.MatchAgainst &&
|
||||
((cmd.MatchEvent != nil && cmd.MatchEvent.Equals(otherCmd.MatchEvent)) || otherCmd.MatchEvent == nil)
|
||||
(cmd.MatchEvent != nil && JSONLeftEquals(cmd.MatchEvent, otherCmd.MatchEvent) || otherCmd.MatchEvent == nil)
|
||||
}
|
||||
|
|
|
@ -17,9 +17,13 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"maunium.net/go/maulogger"
|
||||
"errors"
|
||||
"os"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"maunium.net/go/maulogger"
|
||||
)
|
||||
|
||||
// LogConfig contains configs for the logger.
|
||||
|
@ -29,7 +33,49 @@ type LogConfig struct {
|
|||
FileDateFormat string `yaml:"file_date_format"`
|
||||
FileMode uint32 `yaml:"file_mode"`
|
||||
TimestampFormat string `yaml:"timestamp_format"`
|
||||
Debug bool `yaml:"print_debug"`
|
||||
RawPrintLevel string `yaml:"print_level"`
|
||||
PrintLevel int `yaml:"-"`
|
||||
}
|
||||
|
||||
type umLogConfig LogConfig
|
||||
|
||||
func (lc *LogConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
err := unmarshal((*umLogConfig)(lc))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch strings.ToUpper(lc.RawPrintLevel) {
|
||||
case "DEBUG":
|
||||
lc.PrintLevel = maulogger.LevelDebug.Severity
|
||||
case "INFO":
|
||||
lc.PrintLevel = maulogger.LevelInfo.Severity
|
||||
case "WARN", "WARNING":
|
||||
lc.PrintLevel = maulogger.LevelWarn.Severity
|
||||
case "ERR", "ERROR":
|
||||
lc.PrintLevel = maulogger.LevelError.Severity
|
||||
case "FATAL":
|
||||
lc.PrintLevel = maulogger.LevelFatal.Severity
|
||||
default:
|
||||
return errors.New("invalid print level " + lc.RawPrintLevel)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (lc *LogConfig) MarshalYAML() (interface{}, error) {
|
||||
switch {
|
||||
case lc.PrintLevel >= maulogger.LevelFatal.Severity:
|
||||
lc.RawPrintLevel = maulogger.LevelFatal.Name
|
||||
case lc.PrintLevel >= maulogger.LevelError.Severity:
|
||||
lc.RawPrintLevel = maulogger.LevelError.Name
|
||||
case lc.PrintLevel >= maulogger.LevelWarn.Severity:
|
||||
lc.RawPrintLevel = maulogger.LevelWarn.Name
|
||||
case lc.PrintLevel >= maulogger.LevelInfo.Severity:
|
||||
lc.RawPrintLevel = maulogger.LevelInfo.Name
|
||||
default:
|
||||
lc.RawPrintLevel = maulogger.LevelDebug.Name
|
||||
}
|
||||
return lc, nil
|
||||
}
|
||||
|
||||
// CreateLogConfig creates a basic LogConfig.
|
||||
|
@ -40,29 +86,37 @@ func CreateLogConfig() LogConfig {
|
|||
TimestampFormat: "Jan _2, 2006 15:04:05",
|
||||
FileMode: 0600,
|
||||
FileDateFormat: "2006-01-02",
|
||||
Debug: false,
|
||||
PrintLevel: 10,
|
||||
}
|
||||
}
|
||||
|
||||
type FileFormatData struct {
|
||||
Date string
|
||||
Index int
|
||||
}
|
||||
|
||||
// GetFileFormat returns a mauLogger-compatible logger file format based on the data in the struct.
|
||||
func (lc LogConfig) GetFileFormat() maulogger.LoggerFileFormat {
|
||||
path := lc.FileNameFormat
|
||||
if len(lc.Directory) > 0 {
|
||||
path = lc.Directory + "/" + path
|
||||
}
|
||||
os.MkdirAll(lc.Directory, 0700)
|
||||
path := filepath.Join(lc.Directory, lc.FileNameFormat)
|
||||
tpl, _ := template.New("fileformat").Parse(path)
|
||||
|
||||
return func(now string, i int) string {
|
||||
return fmt.Sprintf(path, now, i)
|
||||
var buf strings.Builder
|
||||
tpl.Execute(&buf, FileFormatData{
|
||||
Date: now,
|
||||
Index: i,
|
||||
})
|
||||
return buf.String()
|
||||
}
|
||||
}
|
||||
|
||||
// Configure configures a mauLogger instance with the data in this struct.
|
||||
func (lc LogConfig) Configure(log *maulogger.Logger) {
|
||||
log.FileFormat = lc.GetFileFormat()
|
||||
log.FileMode = os.FileMode(lc.FileMode)
|
||||
log.FileTimeFormat = lc.FileDateFormat
|
||||
log.TimeFormat = lc.TimestampFormat
|
||||
if lc.Debug {
|
||||
log.PrintLevel = maulogger.LevelDebug.Severity
|
||||
}
|
||||
func (lc LogConfig) Configure(log maulogger.Logger) {
|
||||
basicLogger := log.(*maulogger.BasicLogger)
|
||||
basicLogger.FileFormat = lc.GetFileFormat()
|
||||
basicLogger.FileMode = os.FileMode(lc.FileMode)
|
||||
basicLogger.FileTimeFormat = lc.FileDateFormat
|
||||
basicLogger.TimeFormat = lc.TimestampFormat
|
||||
basicLogger.PrintLevel = lc.PrintLevel
|
||||
}
|
||||
|
|
41
logging.go
Normal file
41
logging.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
// 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 maubot
|
||||
|
||||
type Logger interface {
|
||||
Write(p []byte) (n int, err error)
|
||||
Debug(parts ...interface{})
|
||||
Debugln(parts ...interface{})
|
||||
Debugf(message string, args ...interface{})
|
||||
Debugfln(message string, args ...interface{})
|
||||
Info(parts ...interface{})
|
||||
Infoln(parts ...interface{})
|
||||
Infof(message string, args ...interface{})
|
||||
Infofln(message string, args ...interface{})
|
||||
Warn(parts ...interface{})
|
||||
Warnln(parts ...interface{})
|
||||
Warnf(message string, args ...interface{})
|
||||
Warnfln(message string, args ...interface{})
|
||||
Error(parts ...interface{})
|
||||
Errorln(parts ...interface{})
|
||||
Errorf(message string, args ...interface{})
|
||||
Errorfln(message string, args ...interface{})
|
||||
Fatal(parts ...interface{})
|
||||
Fatalln(parts ...interface{})
|
||||
Fatalf(message string, args ...interface{})
|
||||
Fatalfln(message string, args ...interface{})
|
||||
}
|
154
matrix.go
154
matrix.go
|
@ -16,43 +16,10 @@
|
|||
|
||||
package maubot
|
||||
|
||||
type EventType string
|
||||
type MessageType string
|
||||
|
||||
// State events
|
||||
const (
|
||||
StateAliases EventType = "m.room.aliases"
|
||||
StateCanonicalAlias = "m.room.canonical_alias"
|
||||
StateCreate = "m.room.create"
|
||||
StateJoinRules = "m.room.join_rules"
|
||||
StateMember = "m.room.member"
|
||||
StatePowerLevels = "m.room.power_levels"
|
||||
StateRoomName = "m.room.name"
|
||||
StateTopic = "m.room.topic"
|
||||
StateRoomAvatar = "m.room.avatar"
|
||||
StatePinnedEvents = "m.room.pinned_events"
|
||||
import (
|
||||
"maunium.net/go/gomatrix"
|
||||
)
|
||||
|
||||
// Message events
|
||||
const (
|
||||
EventRedaction EventType = "m.room.redaction"
|
||||
EventMessage = "m.room.message"
|
||||
EventSticker = "m.sticker"
|
||||
)
|
||||
|
||||
// Msgtypes
|
||||
const (
|
||||
MsgText MessageType = "m.text"
|
||||
MsgEmote = "m.emote"
|
||||
MsgNotice = "m.notice"
|
||||
MsgImage = "m.image"
|
||||
MsgLocation = "m.location"
|
||||
MsgVideo = "m.video"
|
||||
MsgAudio = "m.audio"
|
||||
)
|
||||
|
||||
const FormatHTML = "org.matrix.custom.html"
|
||||
|
||||
type EventHandler func(*Event) EventHandlerResult
|
||||
type EventHandlerResult int
|
||||
type CommandHandlerResult = EventHandlerResult
|
||||
|
@ -64,7 +31,7 @@ const (
|
|||
)
|
||||
|
||||
type MatrixClient interface {
|
||||
AddEventHandler(EventType, EventHandler)
|
||||
AddEventHandler(gomatrix.EventType, EventHandler)
|
||||
AddCommandHandler(string, CommandHandler)
|
||||
SetCommandSpec(*CommandSpec)
|
||||
GetEvent(string, string) *Event
|
||||
|
@ -73,120 +40,13 @@ type MatrixClient interface {
|
|||
type EventFuncs interface {
|
||||
MarkRead() error
|
||||
Reply(string) (string, error)
|
||||
ReplyContent(Content) (string, error)
|
||||
ReplyContent(gomatrix.Content) (string, error)
|
||||
SendMessage(string) (string, error)
|
||||
SendContent(Content) (string, error)
|
||||
SendRawEvent(EventType, interface{}) (string, error)
|
||||
SendContent(gomatrix.Content) (string, error)
|
||||
SendRawEvent(gomatrix.EventType, interface{}) (string, error)
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
EventFuncs
|
||||
|
||||
StateKey string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
|
||||
Sender string `json:"sender"` // The user ID of the sender of the event
|
||||
Type EventType `json:"type"` // The event type
|
||||
Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server
|
||||
ID string `json:"event_id"` // The unique ID of this event
|
||||
RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence)
|
||||
Content Content `json:"content"`
|
||||
Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event
|
||||
Unsigned Unsigned `json:"unsigned,omitempty"` // Unsigned content set by own homeserver.
|
||||
}
|
||||
|
||||
func (evt *Event) Equals(otherEvt *Event) bool {
|
||||
return evt.StateKey == otherEvt.StateKey &&
|
||||
evt.Sender == otherEvt.Sender &&
|
||||
evt.Type == otherEvt.Type &&
|
||||
evt.Timestamp == otherEvt.Timestamp &&
|
||||
evt.ID == otherEvt.ID &&
|
||||
evt.RoomID == otherEvt.RoomID &&
|
||||
evt.Content.Equals(&otherEvt.Content) &&
|
||||
evt.Redacts == otherEvt.Redacts &&
|
||||
evt.Unsigned.Equals(&otherEvt.Unsigned)
|
||||
}
|
||||
|
||||
type Unsigned struct {
|
||||
PrevContent *Content `json:"prev_content,omitempty"`
|
||||
PrevSender string `json:"prev_sender,omitempty"`
|
||||
ReplacesState string `json:"replaces_state,omitempty"`
|
||||
Age int64 `json:"age"`
|
||||
|
||||
PassiveCommand MatchedPassiveCommand `json:"m.passive_command,omitempty"`
|
||||
}
|
||||
|
||||
func (unsigned Unsigned) Equals(otherUnsigned *Unsigned) bool {
|
||||
return unsigned.PrevContent.Equals(otherUnsigned.PrevContent) &&
|
||||
unsigned.PrevSender == otherUnsigned.PrevSender &&
|
||||
unsigned.ReplacesState == otherUnsigned.ReplacesState &&
|
||||
unsigned.Age == otherUnsigned.Age
|
||||
}
|
||||
|
||||
type Content struct {
|
||||
Raw map[string]interface{} `json:"-"`
|
||||
|
||||
MsgType MessageType `json:"msgtype"`
|
||||
Body string `json:"body"`
|
||||
Format string `json:"format,omitempty"`
|
||||
FormattedBody string `json:"formatted_body,omitempty"`
|
||||
|
||||
Info *FileInfo `json:"info,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
|
||||
Membership string `json:"membership,omitempty"`
|
||||
|
||||
Command MatchedCommand `json:"m.command,omitempty"`
|
||||
RelatesTo RelatesTo `json:"m.relates_to,omitempty"`
|
||||
}
|
||||
|
||||
func (content Content) Equals(otherContent *Content) bool {
|
||||
return content.MsgType == otherContent.MsgType &&
|
||||
content.Body == otherContent.Body &&
|
||||
content.Format == otherContent.Format &&
|
||||
content.FormattedBody == otherContent.FormattedBody &&
|
||||
((content.Info != nil && content.Info.Equals(otherContent.Info)) || otherContent.Info == nil) &&
|
||||
content.URL == otherContent.URL &&
|
||||
content.Membership == otherContent.Membership &&
|
||||
content.RelatesTo == otherContent.RelatesTo
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
MimeType string `json:"mimetype,omitempty"`
|
||||
ThumbnailInfo *FileInfo `json:"thumbnail_info,omitempty"`
|
||||
ThumbnailURL string `json:"thumbnail_url,omitempty"`
|
||||
Height int `json:"h,omitempty"`
|
||||
Width int `json:"w,omitempty"`
|
||||
Size int `json:"size,omitempty"`
|
||||
}
|
||||
|
||||
func (fi *FileInfo) Equals(otherFI *FileInfo) bool {
|
||||
return fi.MimeType == otherFI.MimeType &&
|
||||
fi.ThumbnailURL == otherFI.ThumbnailURL &&
|
||||
fi.Height == otherFI.Height &&
|
||||
fi.Width == otherFI.Width &&
|
||||
fi.Size == otherFI.Size &&
|
||||
((fi.ThumbnailInfo != nil && fi.ThumbnailInfo.Equals(otherFI.ThumbnailInfo)) || otherFI.ThumbnailInfo == nil)
|
||||
}
|
||||
|
||||
type MatchedCommand struct {
|
||||
Target string `json:"target"`
|
||||
Matched string `json:"matched"`
|
||||
Arguments map[string]string `json:"arguments"`
|
||||
}
|
||||
|
||||
type MatchedPassiveCommand struct {
|
||||
Matched string `json:"matched"`
|
||||
Values []string `json:"captured"`
|
||||
|
||||
BackCompatCommand string `json:"command"`
|
||||
BackCompatArguments map[string]string `json:"arguments"`
|
||||
}
|
||||
|
||||
type RelatesTo struct {
|
||||
InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"`
|
||||
}
|
||||
|
||||
type InReplyTo struct {
|
||||
EventID string `json:"event_id"`
|
||||
// Not required, just for future-proofing
|
||||
RoomID string `json:"room_id,omitempty"`
|
||||
*gomatrix.Event
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
mxEvent.Content.RemoveReplyFallback()
|
||||
return &maubot.Event{
|
||||
EventFuncs: &EventFuncsImpl{
|
||||
Event: mxEvent,
|
||||
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,
|
||||
},
|
||||
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,10 +20,10 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ type Plugin interface {
|
|||
Stop()
|
||||
}
|
||||
|
||||
type PluginCreatorFunc func(client MatrixClient) Plugin
|
||||
type PluginCreatorFunc func(client MatrixClient, logger Logger) Plugin
|
||||
|
||||
type PluginCreator struct {
|
||||
Create PluginCreatorFunc
|
||||
|
|
Loading…
Reference in a new issue