From 307a32e0c0272d7d8a9f13cb6509365774faec84 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 19 Jun 2018 01:25:47 +0300 Subject: [PATCH 1/5] Add unfinished command handler system based on the improved bot support proposal --- app/plugins.go | 2 +- commands.go | 105 ++++++++++++++++++++++++++++++++++ database/clients.go | 30 ++++++++-- database/commands.go | 130 +++++++++++++++++++++++++++++++++++++++++++ database/database.go | 8 ++- database/plugins.go | 2 +- matrix.go | 53 ++++++++++++++++-- matrix/event.go | 5 +- matrix/matrix.go | 36 ++++++++++++ 9 files changed, 355 insertions(+), 16 deletions(-) create mode 100644 commands.go create mode 100644 database/commands.go diff --git a/app/plugins.go b/app/plugins.go index d53e09b..e4fe50c 100644 --- a/app/plugins.go +++ b/app/plugins.go @@ -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), + Plugin: creator.Create(client.Proxy(plugin.ID)), Creator: creator, DB: plugin, } diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..4c8e683 --- /dev/null +++ b/commands.go @@ -0,0 +1,105 @@ +// 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 . + +package maubot + +type CommandHandler func(*Event) + +type CommandSpec struct { + Commands []Command `json:"commands"` + PassiveCommands []PassiveCommand `json:"passive_commands"` +} + +func (spec *CommandSpec) Equals(otherSpec *CommandSpec) bool { + if len(spec.Commands) != len(otherSpec.Commands) || len(spec.PassiveCommands) != len(otherSpec.PassiveCommands) { + return false + } + + for index, cmd := range spec.Commands { + otherCmd := otherSpec.Commands[index] + if !cmd.Equals(otherCmd) { + return false + } + } + + for index, cmd := range spec.PassiveCommands { + otherCmd := otherSpec.PassiveCommands[index] + if !cmd.Equals(otherCmd) { + return false + } + } + + return true +} + +type Command struct { + Syntax string `json:"syntax"` + Description string `json:"description,omitempty"` + Arguments ArgumentMap `json:"arguments"` +} + +func (cmd Command) Equals(otherCmd Command) bool { + return cmd.Syntax == otherCmd.Syntax && + cmd.Description == otherCmd.Description && + cmd.Arguments.Equals(otherCmd.Arguments) +} + +type ArgumentMap map[string]Argument + +func (argMap ArgumentMap) Equals(otherMap ArgumentMap) bool { + if len(argMap) != len(otherMap) { + return false + } + + for name, argument := range argMap { + otherArgument, ok := otherMap[name] + if !ok || !argument.Equals(otherArgument) { + return false + } + } + return true +} + +type Argument struct { + Matches string `json:"matches"` + Required bool `json:"required"` + Description string `json:"description,omitempty"` +} + +func (arg Argument) Equals(otherArg Argument) bool { + return arg.Matches == otherArg.Matches && + arg.Required == otherArg.Required && + arg.Description == otherArg.Description +} + +// Common PassiveCommand MatchAgainst targets. +const ( + MatchAgainstBody = "body" +) + +type PassiveCommand struct { + Name string `json:"name"` + Matches string `json:"matches"` + MatchAgainst string `json:"match_against"` + MatchEvent *Event `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) +} diff --git a/database/clients.go b/database/clients.go index 3ee5c50..660bd10 100644 --- a/database/clients.go +++ b/database/clients.go @@ -1,4 +1,4 @@ -// jesaribot - A simple maubot plugin. +// 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 @@ -17,6 +17,7 @@ package database import ( + "maubot.xyz" log "maunium.net/go/maulogger" "database/sql" ) @@ -35,6 +36,8 @@ type MatrixClient struct { AutoJoinRooms bool `json:"auto_join_rooms"` DisplayName string `json:"display_name"` AvatarURL string `json:"avatar_url"` + + Commands map[string]*CommandSpec `json:"commandspecs"` } type MatrixClientStatic struct { @@ -85,15 +88,32 @@ func (mcs *MatrixClientStatic) New() *MatrixClient { } } -type Scannable interface { - Scan(...interface{}) error -} - func (mxc *MatrixClient) Scan(row Scannable) *MatrixClient { err := row.Scan(&mxc.UserID, &mxc.Homeserver, &mxc.AccessToken, &mxc.NextBatch, &mxc.FilterID, &mxc.Sync, &mxc.AutoJoinRooms, &mxc.DisplayName, &mxc.AvatarURL) if err != nil { log.Fatalln("Database scan failed:", err) } + mxc.LoadCommandSpecs() + return mxc +} + +func (mxc *MatrixClient) SetCommandSpec(owner string, newSpec *maubot.CommandSpec) bool { + spec := mxc.db.CommandSpec.GetOrCreate(owner, mxc.UserID) + if newSpec.Equals(spec.CommandSpec) { + return false + } + spec.CommandSpec = newSpec + spec.Update() + mxc.Commands[owner] = spec + return true +} + +func (mxc *MatrixClient) LoadCommandSpecs() *MatrixClient { + specs := mxc.db.CommandSpec.GetAllByClient(mxc.UserID) + mxc.Commands = make(map[string]*CommandSpec) + for _, spec := range specs { + mxc.Commands[spec.Owner] = spec + } return mxc } diff --git a/database/commands.go b/database/commands.go new file mode 100644 index 0000000..ccf5e37 --- /dev/null +++ b/database/commands.go @@ -0,0 +1,130 @@ +// 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 . + +package database + +import ( + "database/sql" + "encoding/json" + + "maubot.xyz" + log "maunium.net/go/maulogger" +) + +type CommandSpec struct { + db *Database + sql *sql.DB + + *maubot.CommandSpec + Owner string `json:"owner"` + Client string `json:"client"` +} + +type CommandSpecStatic struct { + db *Database + sql *sql.DB +} + +func (css *CommandSpecStatic) CreateTable() error { + _, err := css.sql.Exec(`CREATE TABLE IF NOT EXISTS command_spec ( + owner VARCHAR(255), + client VARCHAR(255), + spec TEXT, + + PRIMARY KEY (owner, client), + FOREIGN KEY (owner) REFERENCES plugin(id) + ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (client) REFERENCES matrix_client(user_id) + ON DELETE CASCADE ON UPDATE CASCADE + )`) + return err +} + +func (css *CommandSpecStatic) Get(owner, client string) *CommandSpec { + row := css.sql.QueryRow("SELECT * FROM command_spec WHERE owner=? AND client=?", owner, client) + if row != nil { + return css.New().Scan(row) + } + return nil +} + +func (css *CommandSpecStatic) GetOrCreate(owner, client string) (spec *CommandSpec) { + spec = css.Get(owner, client) + if spec == nil { + spec = css.New() + spec.Owner = owner + spec.Client = client + spec.Insert() + } + return +} + +func (css *CommandSpecStatic) getAllByQuery(query string, args ...interface{}) (specs []*CommandSpec) { + rows, err := css.sql.Query(query, args...) + if err != nil || rows == nil { + return nil + } + defer rows.Close() + for rows.Next() { + specs = append(specs, css.New().Scan(rows)) + } + return +} + +func (css *CommandSpecStatic) GetAllByOwner(owner string) []*CommandSpec { + return css.getAllByQuery("SELECT * FROM command_spec WHERE owner=?", owner) +} + +func (css *CommandSpecStatic) GetAllByClient(client string) []*CommandSpec { + return css.getAllByQuery("SELECT * FROM command_spec WHERE client=?", client) +} + +func (css *CommandSpecStatic) New() *CommandSpec { + return &CommandSpec{ + db: css.db, + sql: css.sql, + } +} + +func (cs *CommandSpec) Scan(row Scannable) *CommandSpec { + var spec string + err := row.Scan(&cs.Owner, &cs.Client, &spec) + if err != nil { + log.Fatalln("Database scan failed:", err) + } + json.Unmarshal([]byte(spec), &cs.CommandSpec) + return cs +} + +func (cs *CommandSpec) Insert() error { + data, err := json.Marshal(cs.CommandSpec) + if err != nil { + return err + } + _, err = cs.sql.Exec("INSERT INTO command_spec (owner, client, spec) VALUES (?, ?, ?)", + cs.Owner, cs.Client, string(data)) + return err +} + +func (cs *CommandSpec) Update() error { + data, err := json.Marshal(cs.CommandSpec) + if err != nil { + return err + } + _, err = cs.sql.Exec("UPDATE command_spec SET spec=? WHERE owner=? AND client=?", + string(data), cs.Owner, cs.Client) + return err +} diff --git a/database/database.go b/database/database.go index 26121ae..b49b063 100644 --- a/database/database.go +++ b/database/database.go @@ -1,4 +1,4 @@ -// jesaribot - A simple maubot plugin. +// 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 @@ -22,12 +22,17 @@ import ( log "maunium.net/go/maulogger" ) +type Scannable interface { + Scan(...interface{}) error +} + type Database struct { Type string `yaml:"type"` Name string `yaml:"name"` MatrixClient *MatrixClientStatic `yaml:"-"` Plugin *PluginStatic `yaml:"-"` + CommandSpec *CommandSpecStatic `yaml:"-"` sql *sql.DB } @@ -40,6 +45,7 @@ func (db *Database) Connect() (err error) { db.MatrixClient = &MatrixClientStatic{db: db, sql: db.sql} db.Plugin = &PluginStatic{db: db, sql: db.sql} + db.CommandSpec = &CommandSpecStatic{db: db, sql: db.sql} return nil } diff --git a/database/plugins.go b/database/plugins.go index 4224e67..3b4ef91 100644 --- a/database/plugins.go +++ b/database/plugins.go @@ -1,4 +1,4 @@ -// jesaribot - A simple maubot plugin. +// 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 diff --git a/matrix.go b/matrix.go index 5d68e7d..89b16a3 100644 --- a/matrix.go +++ b/matrix.go @@ -63,6 +63,8 @@ const ( type MatrixClient interface { AddEventHandler(EventType, EventHandler) + AddCommandHandler(string, CommandHandler) + SetCommandSpec(*CommandSpec) GetEvent(string, string) *Event } @@ -89,11 +91,30 @@ type Event struct { 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"` + PrevContent *Content `json:"prev_content,omitempty"` + PrevSender string `json:"prev_sender,omitempty"` + ReplacesState string `json:"replaces_state,omitempty"` + Age int64 `json:"age"` +} + +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 { @@ -104,14 +125,25 @@ type Content struct { Format string `json:"format,omitempty"` FormattedBody string `json:"formatted_body,omitempty"` - Info FileInfo `json:"info,omitempty"` - URL string `json:"url,omitempty"` + Info *FileInfo `json:"info,omitempty"` + URL string `json:"url,omitempty"` Membership string `json:"membership,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"` @@ -121,6 +153,15 @@ type FileInfo struct { 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 RelatesTo struct { InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"` } diff --git a/matrix/event.go b/matrix/event.go index 3abb801..9e808d9 100644 --- a/matrix/event.go +++ b/matrix/event.go @@ -28,7 +28,8 @@ type Event struct { Client *Client } -func roundtripContent(rawContent map[string]interface{}) (content maubot.Content) { +func roundtripContent(rawContent map[string]interface{}) (content *maubot.Content) { + content = &maubot.Content{} if len(rawContent) == 0 { content.Raw = rawContent return @@ -55,7 +56,7 @@ func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *Event { Timestamp: mxEvent.Timestamp, ID: mxEvent.ID, RoomID: mxEvent.RoomID, - Content: roundtripContent(mxEvent.Content), + Content: *roundtripContent(mxEvent.Content), Redacts: mxEvent.Redacts, Unsigned: maubot.Unsigned{ PrevContent: roundtripContent(mxEvent.Unsigned.PrevContent), diff --git a/matrix/matrix.go b/matrix/matrix.go index df1e9e4..55762a4 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -45,10 +45,18 @@ func NewClient(db *database.MatrixClient) (*Client, error) { client.Client.Syncer = client.syncer client.AddEventHandler(maubot.StateMember, client.onJoin) + client.AddEventHandler(maubot.EventMessage, client.onMessage) return client, nil } +func (client *Client) Proxy(owner string) *ClientProxy { + return &ClientProxy{ + hiddenClient: client, + owner: owner, + } +} + func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) { client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult { if evt.Sender == client.UserID { @@ -58,6 +66,18 @@ func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.Event }) } +func (client *Client) AddCommandHandler(evt string, handler maubot.CommandHandler) { + // TODO add command handler +} + +func (client *Client) SetCommandSpec(owner string, spec *maubot.CommandSpec) { + changed := client.DB.SetCommandSpec(owner, spec) + if changed { + log.Debugln("Command spec of", owner, "on", client.UserID, "updated.") + // TODO + } +} + func (client *Client) GetEvent(roomID, eventID string) *maubot.Event { evt, err := client.Client.GetEvent(roomID, eventID) if err != nil { @@ -67,6 +87,11 @@ func (client *Client) GetEvent(roomID, eventID string) *maubot.Event { return client.ParseEvent(evt).Event } +func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult { + // TODO call command handlers + return maubot.Continue +} + func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult { if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" { client.JoinRoom(evt.RoomID) @@ -87,3 +112,14 @@ func (client *Client) Sync() { } }() } + +type hiddenClient = Client + +type ClientProxy struct { + *hiddenClient + owner string +} + +func (cp *ClientProxy) SetCommandSpec(spec *maubot.CommandSpec) { + cp.hiddenClient.SetCommandSpec(cp.owner, spec) +} From 4536fcfe62a239352091ad1b01ba047c0d320a8b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 20 Jun 2018 22:25:33 +0300 Subject: [PATCH 2/5] Add command spec parsing/handler execution --- commands.go | 24 ++++++-- database/clients.go | 50 +++++++++++++--- database/commands.go | 21 ++++--- database/database.go | 5 ++ database/plugins.go | 2 +- matrix.go | 17 ++++-- matrix/commands.go | 138 +++++++++++++++++++++++++++++++++++++++++++ matrix/matrix.go | 60 +++++++++++++++---- matrix/sync.go | 2 +- 9 files changed, 282 insertions(+), 37 deletions(-) create mode 100644 matrix/commands.go diff --git a/commands.go b/commands.go index 4c8e683..3718634 100644 --- a/commands.go +++ b/commands.go @@ -16,15 +16,31 @@ package maubot -type CommandHandler func(*Event) +type CommandHandler func(*Event) CommandHandlerResult type CommandSpec struct { - Commands []Command `json:"commands"` - PassiveCommands []PassiveCommand `json:"passive_commands"` + Commands []Command `json:"commands,omitempty"` + PassiveCommands []PassiveCommand `json:"passive_commands,omitempty"` +} + +func (spec *CommandSpec) Clone() *CommandSpec { + return &CommandSpec{ + Commands: append([]Command(nil), spec.Commands...), + PassiveCommands: append([]PassiveCommand(nil), spec.PassiveCommands...), + } +} + +func (spec *CommandSpec) Merge(otherSpecs ...*CommandSpec) { + for _, otherSpec := range otherSpecs { + spec.Commands = append(spec.Commands, otherSpec.Commands...) + spec.PassiveCommands = append(spec.PassiveCommands, otherSpec.PassiveCommands...) + } } func (spec *CommandSpec) Equals(otherSpec *CommandSpec) bool { - if len(spec.Commands) != len(otherSpec.Commands) || len(spec.PassiveCommands) != len(otherSpec.PassiveCommands) { + if otherSpec == nil || + len(spec.Commands) != len(otherSpec.Commands) || + len(spec.PassiveCommands) != len(otherSpec.PassiveCommands) { return false } diff --git a/database/clients.go b/database/clients.go index 660bd10..1717ba2 100644 --- a/database/clients.go +++ b/database/clients.go @@ -20,6 +20,7 @@ import ( "maubot.xyz" log "maunium.net/go/maulogger" "database/sql" + "sort" ) type MatrixClient struct { @@ -37,7 +38,7 @@ type MatrixClient struct { DisplayName string `json:"display_name"` AvatarURL string `json:"avatar_url"` - Commands map[string]*CommandSpec `json:"commandspecs"` + CommandSpecs map[string]*CommandSpec `json:"command_specs"` } type MatrixClientStatic struct { @@ -91,32 +92,63 @@ func (mcs *MatrixClientStatic) New() *MatrixClient { func (mxc *MatrixClient) Scan(row Scannable) *MatrixClient { err := row.Scan(&mxc.UserID, &mxc.Homeserver, &mxc.AccessToken, &mxc.NextBatch, &mxc.FilterID, &mxc.Sync, &mxc.AutoJoinRooms, &mxc.DisplayName, &mxc.AvatarURL) if err != nil { - log.Fatalln("Database scan failed:", err) + log.Errorln("MatrixClient scan failed:", err) + return mxc } mxc.LoadCommandSpecs() return mxc } func (mxc *MatrixClient) SetCommandSpec(owner string, newSpec *maubot.CommandSpec) bool { - spec := mxc.db.CommandSpec.GetOrCreate(owner, mxc.UserID) - if newSpec.Equals(spec.CommandSpec) { + spec, ok := mxc.CommandSpecs[owner] + if ok && newSpec.Equals(spec.CommandSpec) { return false } - spec.CommandSpec = newSpec - spec.Update() - mxc.Commands[owner] = spec + if spec == nil { + spec = mxc.db.CommandSpec.New() + spec.CommandSpec = newSpec + spec.Insert() + } else { + spec.CommandSpec = newSpec + spec.Update() + } + mxc.CommandSpecs[owner] = spec return true } func (mxc *MatrixClient) LoadCommandSpecs() *MatrixClient { specs := mxc.db.CommandSpec.GetAllByClient(mxc.UserID) - mxc.Commands = make(map[string]*CommandSpec) + mxc.CommandSpecs = make(map[string]*CommandSpec) for _, spec := range specs { - mxc.Commands[spec.Owner] = spec + mxc.CommandSpecs[spec.Owner] = spec } + log.Debugln("Loaded command specs:", mxc.CommandSpecs) return mxc } +func (mxc *MatrixClient) CommandSpecIDs() []string { + keys := make([]string, len(mxc.CommandSpecs)) + i := 0 + for key := range mxc.CommandSpecs { + keys[i] = key + i++ + } + sort.Strings(keys) + return keys +} + +func (mxc *MatrixClient) Commands() *maubot.CommandSpec { + if len(mxc.CommandSpecs) == 0 { + return &maubot.CommandSpec{} + } + specIDs := mxc.CommandSpecIDs() + spec := mxc.CommandSpecs[specIDs[0]].Clone() + for _, specID := range specIDs[1:] { + spec.Merge(mxc.CommandSpecs[specID].CommandSpec) + } + return spec +} + func (mxc *MatrixClient) Insert() error { _, err := mxc.sql.Exec("INSERT INTO matrix_client (user_id, homeserver, access_token, next_batch, filter_id, sync, autojoin, display_name, avatar_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", mxc.UserID, mxc.Homeserver, mxc.AccessToken, mxc.NextBatch, mxc.FilterID, mxc.Sync, mxc.AutoJoinRooms, mxc.DisplayName, mxc.AvatarURL) diff --git a/database/commands.go b/database/commands.go index ccf5e37..0425a47 100644 --- a/database/commands.go +++ b/database/commands.go @@ -54,11 +54,11 @@ func (css *CommandSpecStatic) CreateTable() error { } func (css *CommandSpecStatic) Get(owner, client string) *CommandSpec { - row := css.sql.QueryRow("SELECT * FROM command_spec WHERE owner=? AND client=?", owner, client) - if row != nil { - return css.New().Scan(row) + rows, err := css.sql.Query("SELECT * FROM command_spec WHERE owner=? AND client=?", owner, client) + if err != nil { + log.Errorf("Failed to Get(%s, %s): %v\n", owner, client, err) } - return nil + return css.New().Scan(rows) } func (css *CommandSpecStatic) GetOrCreate(owner, client string) (spec *CommandSpec) { @@ -74,13 +74,15 @@ func (css *CommandSpecStatic) GetOrCreate(owner, client string) (spec *CommandSp func (css *CommandSpecStatic) getAllByQuery(query string, args ...interface{}) (specs []*CommandSpec) { rows, err := css.sql.Query(query, args...) - if err != nil || rows == nil { + if err != nil { + log.Errorf("Failed to getAllByQuery(%s): %v\n", query, err) return nil } defer rows.Close() for rows.Next() { specs = append(specs, css.New().Scan(rows)) } + log.Debugln("getAllByQuery() =", specs) return } @@ -103,9 +105,14 @@ func (cs *CommandSpec) Scan(row Scannable) *CommandSpec { var spec string err := row.Scan(&cs.Owner, &cs.Client, &spec) if err != nil { - log.Fatalln("Database scan failed:", err) + log.Errorln("CommandSpec scan failed:", err) + return cs + } + cs.CommandSpec = &maubot.CommandSpec{} + err = json.Unmarshal([]byte(spec), cs.CommandSpec) + if err != nil { + log.Errorln("CommandSpec parse failed:", err) } - json.Unmarshal([]byte(spec), &cs.CommandSpec) return cs } diff --git a/database/database.go b/database/database.go index b49b063..09fc87d 100644 --- a/database/database.go +++ b/database/database.go @@ -62,6 +62,11 @@ func (db *Database) CreateTables() { if err != nil { log.Errorln("Failed to create plugin table:", err) } + + err = db.CommandSpec.CreateTable() + if err != nil { + log.Errorln("Failed to create command_spec table:", err) + } } func (db *Database) SQL() *sql.DB { diff --git a/database/plugins.go b/database/plugins.go index 3b4ef91..0d43f43 100644 --- a/database/plugins.go +++ b/database/plugins.go @@ -87,7 +87,7 @@ func (ps *PluginStatic) New() *Plugin { func (p *Plugin) Scan(row Scannable) *Plugin { err := row.Scan(&p.ID, &p.Type, &p.Enabled, &p.UserID) if err != nil { - log.Fatalln("Database scan failed:", err) + log.Errorln("Plugin scan failed:", err) } return p } diff --git a/matrix.go b/matrix.go index 89b16a3..04a0ae7 100644 --- a/matrix.go +++ b/matrix.go @@ -54,11 +54,13 @@ const ( const FormatHTML = "org.matrix.custom.html" type EventHandler func(*Event) EventHandlerResult -type EventHandlerResult bool +type EventHandlerResult int +type CommandHandlerResult = EventHandlerResult const ( - Continue EventHandlerResult = false - StopPropagation EventHandlerResult = true + Continue EventHandlerResult = iota + StopEventPropagation + StopCommandPropagation CommandHandlerResult = iota ) type MatrixClient interface { @@ -130,7 +132,8 @@ type Content struct { Membership string `json:"membership,omitempty"` - RelatesTo RelatesTo `json:"m.relates_to,omitempty"` + Command MatchedCommand `json:"m.command,omitempty"` + RelatesTo RelatesTo `json:"m.relates_to,omitempty"` } func (content Content) Equals(otherContent *Content) bool { @@ -162,6 +165,12 @@ func (fi *FileInfo) Equals(otherFI *FileInfo) bool { ((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 RelatesTo struct { InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"` } diff --git a/matrix/commands.go b/matrix/commands.go new file mode 100644 index 0000000..62bee76 --- /dev/null +++ b/matrix/commands.go @@ -0,0 +1,138 @@ +// 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 . + +package matrix + +import ( + "fmt" + "regexp" + "strings" + + log "maunium.net/go/maulogger" + + "maubot.xyz" +) + +type ParsedCommand struct { + Name string + StartsWith string + Matches *regexp.Regexp + MatchAgainst string + MatchesEvent *maubot.Event +} + +func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error { + regexBuilder := &strings.Builder{} + swBuilder := &strings.Builder{} + argumentEncountered := false + + regexBuilder.WriteRune('^') + words := strings.Split(command.Syntax, " ") + for i, word := range words { + argument, ok := command.Arguments[word] + if ok { + argumentEncountered = true + regex := argument.Matches + if argument.Required { + regex = fmt.Sprintf("(?:%s)?", regex) + } + regexBuilder.WriteString(regex) + } else { + if !argumentEncountered { + swBuilder.WriteString(word) + } + regexBuilder.WriteString(regexp.QuoteMeta(word)) + } + + if i < len(words) - 1 { + if !argumentEncountered { + swBuilder.WriteRune(' ') + } + regexBuilder.WriteRune(' ') + } + } + regexBuilder.WriteRune('$') + + var err error + pc.StartsWith = swBuilder.String() + // Trim the extra space at the end added in the parse loop + pc.StartsWith = pc.StartsWith[:len(pc.StartsWith)-1] + pc.Matches, err = regexp.Compile(regexBuilder.String()) + pc.MatchAgainst = "body" + return err +} + +func (pc *ParsedCommand) parsePassiveCommandSyntax(command maubot.PassiveCommand) error { + pc.MatchAgainst = command.MatchAgainst + var err error + pc.Matches, err = regexp.Compile(command.Matches) + pc.MatchesEvent = command.MatchEvent + return err +} + +func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) { + for _, command := range spec.Commands { + parsing := &ParsedCommand{ + Name: command.Syntax, + } + err := parsing.parseCommandSyntax(command) + if err != nil { + log.Warnf("Failed to parse regex of command %s: %v\n", command.Syntax, err) + continue + } + commands = append(commands, parsing) + } + for _, command := range spec.PassiveCommands { + parsing := &ParsedCommand{ + Name: command.Name, + } + err := parsing.parsePassiveCommandSyntax(command) + if err != nil { + log.Warnf("Failed to parse regex of passive command %s: %v\n", command.Name, err) + continue + } + commands = append(commands, parsing) + } + return commands +} + +func deepGet(from map[string]interface{}, path string) interface{} { + for { + dotIndex := strings.IndexRune(path, '.') + if dotIndex == -1 { + return from[path] + } + + var key string + key, path = path[:dotIndex], path[dotIndex+1:] + var ok bool + from, ok = from[key].(map[string]interface{}) + if !ok { + return nil + } + } +} + +func (pc *ParsedCommand) Match(evt *maubot.Event) bool { + matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string) + if !ok { + matchAgainst = evt.Content.Body + } + + return strings.HasPrefix(matchAgainst, pc.StartsWith) && + pc.Matches.MatchString(matchAgainst) && + (pc.MatchesEvent == nil || pc.MatchesEvent.Equals(evt)) +} diff --git a/matrix/matrix.go b/matrix/matrix.go index 55762a4..bb4bdbe 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -25,9 +25,10 @@ import ( type Client struct { *gomatrix.Client - syncer *MaubotSyncer - - DB *database.MatrixClient + syncer *MaubotSyncer + handlers map[string][]maubot.CommandHandler + commands []*ParsedCommand + DB *database.MatrixClient } func NewClient(db *database.MatrixClient) (*Client, error) { @@ -37,8 +38,10 @@ func NewClient(db *database.MatrixClient) (*Client, error) { } client := &Client{ - Client: mxClient, - DB: db, + Client: mxClient, + handlers: make(map[string][]maubot.CommandHandler), + commands: ParseSpec(db.Commands()), + DB: db, } client.syncer = NewMaubotSyncer(client, client.Store) @@ -60,21 +63,29 @@ func (client *Client) Proxy(owner string) *ClientProxy { func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) { client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult { if evt.Sender == client.UserID { - return maubot.StopPropagation + return maubot.StopEventPropagation } return handler(evt) }) } -func (client *Client) AddCommandHandler(evt string, handler maubot.CommandHandler) { - // TODO add command handler +func (client *Client) AddCommandHandler(owner, evt string, handler maubot.CommandHandler) { + log.Debugln("Registering command handler for event", evt, "by", owner) + list, ok := client.handlers[evt] + if !ok { + list = []maubot.CommandHandler{handler} + } else { + list = append(list, handler) + } + client.handlers[evt] = list } func (client *Client) SetCommandSpec(owner string, spec *maubot.CommandSpec) { + log.Debugln("Registering command spec for", owner, "on", client.UserID) changed := client.DB.SetCommandSpec(owner, spec) if changed { + client.commands = ParseSpec(client.DB.Commands()) log.Debugln("Command spec of", owner, "on", client.UserID, "updated.") - // TODO } } @@ -87,15 +98,38 @@ func (client *Client) GetEvent(roomID, eventID string) *maubot.Event { return client.ParseEvent(evt).Event } +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) + return maubot.Continue + } + log.Debugf("Command %s on client %s triggered by %s\n", command.Name, client.UserID, evt.Sender) + for _, handler := range handlers { + result := handler(evt) + if result == maubot.StopCommandPropagation { + break + } else if result != maubot.Continue { + return result + } + } + + return maubot.Continue +} + func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult { - // TODO call command handlers + for _, command := range client.commands { + if command.Match(evt) { + return client.TriggerCommand(command, evt) + } + } return maubot.Continue } func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult { if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" { client.JoinRoom(evt.RoomID) - return maubot.StopPropagation + return maubot.StopEventPropagation } return maubot.Continue } @@ -120,6 +154,10 @@ type ClientProxy struct { owner string } +func (cp *ClientProxy) AddCommandHandler(evt string, handler maubot.CommandHandler) { + cp.hiddenClient.AddCommandHandler(cp.owner, evt, handler) +} + func (cp *ClientProxy) SetCommandSpec(spec *maubot.CommandSpec) { cp.hiddenClient.SetCommandSpec(cp.owner, spec) } diff --git a/matrix/sync.go b/matrix/sync.go index dbdaa68..39bc588 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -135,7 +135,7 @@ func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) { return } for _, fn := range listeners { - if fn(event.Event) { + if fn(event.Event) == maubot.StopEventPropagation { break } } From ad5e2ef72fb9ad2ca76404ae71bb4effced26c07 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 20 Jun 2018 23:28:01 +0300 Subject: [PATCH 3/5] Fill event values with matched command stuff --- matrix.go | 10 +++++++ matrix/commands.go | 73 ++++++++++++++++++++++++++++++++++++++++------ matrix/matrix.go | 1 + 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/matrix.go b/matrix.go index 04a0ae7..01219b3 100644 --- a/matrix.go +++ b/matrix.go @@ -110,6 +110,8 @@ type Unsigned struct { 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 { @@ -171,6 +173,14 @@ type MatchedCommand struct { 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"` } diff --git a/matrix/commands.go b/matrix/commands.go index 62bee76..b4ec037 100644 --- a/matrix/commands.go +++ b/matrix/commands.go @@ -28,6 +28,8 @@ import ( type ParsedCommand struct { Name string + IsPassive bool + Arguments []string StartsWith string Matches *regexp.Regexp MatchAgainst string @@ -43,12 +45,14 @@ func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error { words := strings.Split(command.Syntax, " ") for i, word := range words { argument, ok := command.Arguments[word] - if ok { + // TODO enable $ check? + if ok && len(word) > 0 /*&& word[0] == '$'*/ { argumentEncountered = true regex := argument.Matches if argument.Required { regex = fmt.Sprintf("(?:%s)?", regex) } + pc.Arguments = append(pc.Arguments, word) regexBuilder.WriteString(regex) } else { if !argumentEncountered { @@ -57,7 +61,7 @@ func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error { regexBuilder.WriteString(regexp.QuoteMeta(word)) } - if i < len(words) - 1 { + if i < len(words)-1 { if !argumentEncountered { swBuilder.WriteRune(' ') } @@ -78,7 +82,7 @@ func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error { func (pc *ParsedCommand) parsePassiveCommandSyntax(command maubot.PassiveCommand) error { pc.MatchAgainst = command.MatchAgainst var err error - pc.Matches, err = regexp.Compile(command.Matches) + pc.Matches, err = regexp.Compile(fmt.Sprintf("(%s)", command.Matches)) pc.MatchesEvent = command.MatchEvent return err } @@ -86,7 +90,8 @@ func (pc *ParsedCommand) parsePassiveCommandSyntax(command maubot.PassiveCommand func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) { for _, command := range spec.Commands { parsing := &ParsedCommand{ - Name: command.Syntax, + Name: command.Syntax, + IsPassive: false, } err := parsing.parseCommandSyntax(command) if err != nil { @@ -97,7 +102,8 @@ func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) { } for _, command := range spec.PassiveCommands { parsing := &ParsedCommand{ - Name: command.Name, + Name: command.Name, + IsPassive: true, } err := parsing.parsePassiveCommandSyntax(command) if err != nil { @@ -126,13 +132,62 @@ func deepGet(from map[string]interface{}, path string) interface{} { } } -func (pc *ParsedCommand) Match(evt *maubot.Event) bool { +func (pc *ParsedCommand) MatchActive(evt *maubot.Event) bool { + if !strings.HasPrefix(evt.Content.Body, pc.StartsWith) { + return false + } + match := pc.Matches.FindStringSubmatch(evt.Content.Body) + if match == nil { + return false + } + // First element is whole content + match = match[1:] + + evt.Content.Command.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 + } + + evt.Content.Command.Matched = pc.Name + // TODO add evt.Content.Command.Target? + + return true +} + +func (pc *ParsedCommand) MatchPassive(evt *maubot.Event) bool { matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string) if !ok { matchAgainst = evt.Content.Body } - return strings.HasPrefix(matchAgainst, pc.StartsWith) && - pc.Matches.MatchString(matchAgainst) && - (pc.MatchesEvent == nil || pc.MatchesEvent.Equals(evt)) + if pc.MatchesEvent != nil && !pc.MatchesEvent.Equals(evt) { + return false + } + + matches := pc.Matches.FindAllStringSubmatch(matchAgainst, -1) + if matches == nil { + return false + } + + values := make([]string, len(matches)) + for i, match := range matches { + values[i] = match[0] + } + + evt.Unsigned.PassiveCommand.Matched = pc.Name + evt.Unsigned.PassiveCommand.Values = values + + return true +} + +func (pc *ParsedCommand) Match(evt *maubot.Event) bool { + if pc.IsPassive { + return pc.MatchPassive(evt) + } else { + return pc.MatchActive(evt) + } } diff --git a/matrix/matrix.go b/matrix/matrix.go index bb4bdbe..fd1f925 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -104,6 +104,7 @@ func (client *Client) TriggerCommand(command *ParsedCommand, evt *maubot.Event) log.Warnf("Command %s triggered by %s doesn't have any handlers.", command.Name, evt.Sender) return maubot.Continue } + log.Debugf("Command %s on client %s triggered by %s\n", command.Name, client.UserID, evt.Sender) for _, handler := range handlers { result := handler(evt) From d261997d8433c70157a06bc2eb953cd0909366c2 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 21 Jun 2018 22:08:33 +0300 Subject: [PATCH 4/5] Require ! at start of active commands --- matrix/commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/commands.go b/matrix/commands.go index b4ec037..df4d683 100644 --- a/matrix/commands.go +++ b/matrix/commands.go @@ -41,7 +41,7 @@ func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error { swBuilder := &strings.Builder{} argumentEncountered := false - regexBuilder.WriteRune('^') + regexBuilder.WriteString("^!") words := strings.Split(command.Syntax, " ") for i, word := range words { argument, ok := command.Arguments[word] From ff265989106eeaf305b554f04351a62cca9923d0 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 20 Sep 2018 01:16:13 +0300 Subject: [PATCH 5/5] Update gomatrix, add logging and other things --- app/plugins.go | 2 +- cmd/maubot/main.go | 1 + commands.go | 57 ++++++++++++++-- config/logging.go | 88 +++++++++++++++++++----- logging.go | 41 ++++++++++++ matrix.go | 156 +++---------------------------------------- matrix/commands.go | 34 +++++----- matrix/event.go | 76 +++++++-------------- matrix/htmltotext.go | 18 ++--- matrix/htmlutil.go | 53 --------------- matrix/matrix.go | 14 ++-- matrix/replyutil.go | 103 ---------------------------- matrix/sync.go | 21 +++--- plugin.go | 2 +- 14 files changed, 242 insertions(+), 424 deletions(-) create mode 100644 logging.go delete mode 100644 matrix/htmlutil.go delete mode 100644 matrix/replyutil.go diff --git a/app/plugins.go b/app/plugins.go index e4fe50c..4caddf1 100644 --- a/app/plugins.go +++ b/app/plugins.go @@ -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, } diff --git a/cmd/maubot/main.go b/cmd/maubot/main.go index fa76fb4..4c6edd1 100644 --- a/cmd/maubot/main.go +++ b/cmd/maubot/main.go @@ -53,6 +53,7 @@ func main() { return } cfg.Logging.Configure(log.DefaultLogger) + log.OpenFile() log.Debugln("Logger configured") bot := app.New(cfg) diff --git a/commands.go b/commands.go index 3718634..39aa80e 100644 --- a/commands.go +++ b/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"` + Name string `json:"name"` + Matches string `json:"matches"` + MatchAgainst string `json:"match_against"` + 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) } diff --git a/config/logging.go b/config/logging.go index 0c170d5..8a8da36 100644 --- a/config/logging.go +++ b/config/logging.go @@ -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 } diff --git a/logging.go b/logging.go new file mode 100644 index 0000000..e8abe7c --- /dev/null +++ b/logging.go @@ -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 . + +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{}) +} diff --git a/matrix.go b/matrix.go index 01219b3..c8d3389 100644 --- a/matrix.go +++ b/matrix.go @@ -16,55 +16,22 @@ 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 const ( - Continue EventHandlerResult = iota + Continue EventHandlerResult = iota StopEventPropagation StopCommandPropagation CommandHandlerResult = iota ) 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 } diff --git a/matrix/commands.go b/matrix/commands.go index df4d683..9a23687 100644 --- a/matrix/commands.go +++ b/matrix/commands.go @@ -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 { diff --git a/matrix/event.go b/matrix/event.go index 9e808d9..448fa2b 100644 --- a/matrix/event.go +++ b/matrix/event.go @@ -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 } diff --git a/matrix/htmltotext.go b/matrix/htmltotext.go index d1aa5af..4d30248 100644 --- a/matrix/htmltotext.go +++ b/matrix/htmltotext.go @@ -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) diff --git a/matrix/htmlutil.go b/matrix/htmlutil.go deleted file mode 100644 index 9c235fd..0000000 --- a/matrix/htmlutil.go +++ /dev/null @@ -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 . - -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), - } -} diff --git a/matrix/matrix.go b/matrix/matrix.go index fd1f925..4d9a8be 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -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 } diff --git a/matrix/replyutil.go b/matrix/replyutil.go deleted file mode 100644 index 29e7773..0000000 --- a/matrix/replyutil.go +++ /dev/null @@ -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 . - -package matrix - -import ( - "fmt" - "regexp" - "strings" - - "golang.org/x/net/html" - "maubot.xyz" -) - -var HTMLReplyFallbackRegex = regexp.MustCompile(`^[\s\S]+?`) - -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 = `
-In reply to -%s -%s -
-` - -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 -} diff --git a/matrix/sync.go b/matrix/sync.go index 39bc588..3e5bea8 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -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 } } diff --git a/plugin.go b/plugin.go index 1fe0cd3..45554ba 100644 --- a/plugin.go +++ b/plugin.go @@ -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