Merge pull request #3 from maubot/command-handlers

Add command handler system
This commit is contained in:
Tulir Asokan 2018-09-20 01:16:36 +03:00 committed by GitHub
commit 22ef8195dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 830 additions and 362 deletions

View File

@ -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), log.Sub(plugin.ID)),
Creator: creator,
DB: plugin,
}

View File

@ -53,6 +53,7 @@ func main() {
return
}
cfg.Logging.Configure(log.DefaultLogger)
log.OpenFile()
log.Debugln("Logger configured")
bot := app.New(cfg)

168
commands.go Normal file
View File

@ -0,0 +1,168 @@
// 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 CommandHandler func(*Event) CommandHandlerResult
type CommandSpec struct {
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 otherSpec == nil ||
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"
)
// 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 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 && JSONLeftEquals(cmd.MatchEvent, otherCmd.MatchEvent) || otherCmd.MatchEvent == nil)
}

View File

@ -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
}

View File

@ -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,8 +17,10 @@
package database
import (
"maubot.xyz"
log "maunium.net/go/maulogger"
"database/sql"
"sort"
)
type MatrixClient struct {
@ -35,6 +37,8 @@ type MatrixClient struct {
AutoJoinRooms bool `json:"auto_join_rooms"`
DisplayName string `json:"display_name"`
AvatarURL string `json:"avatar_url"`
CommandSpecs map[string]*CommandSpec `json:"command_specs"`
}
type MatrixClientStatic struct {
@ -85,18 +89,66 @@ 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)
log.Errorln("MatrixClient scan failed:", err)
return mxc
}
mxc.LoadCommandSpecs()
return mxc
}
func (mxc *MatrixClient) SetCommandSpec(owner string, newSpec *maubot.CommandSpec) bool {
spec, ok := mxc.CommandSpecs[owner]
if ok && newSpec.Equals(spec.CommandSpec) {
return false
}
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.CommandSpecs = make(map[string]*CommandSpec)
for _, spec := range specs {
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)

137
database/commands.go Normal file
View File

@ -0,0 +1,137 @@
// 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 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 {
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 css.New().Scan(rows)
}
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 {
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
}
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.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)
}
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
}

View File

@ -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
}
@ -56,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 {

View File

@ -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
@ -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
}

41
logging.go Normal file
View 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{})
}

108
matrix.go
View File

@ -16,117 +16,37 @@
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 bool
type EventHandlerResult int
type CommandHandlerResult = EventHandlerResult
const (
Continue EventHandlerResult = false
StopPropagation EventHandlerResult = true
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
}
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.
}
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"`
}
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"`
RelatesTo RelatesTo `json:"m.relates_to,omitempty"`
}
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"`
}
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
}

197
matrix/commands.go Normal file
View File

@ -0,0 +1,197 @@
// 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"
"maunium.net/go/gomatrix"
log "maunium.net/go/maulogger"
"maubot.xyz"
)
type ParsedCommand struct {
Name string
IsPassive bool
Arguments []string
StartsWith string
Matches *regexp.Regexp
MatchAgainst string
MatchesEvent interface{}
}
func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error {
regexBuilder := &strings.Builder{}
swBuilder := &strings.Builder{}
argumentEncountered := false
regexBuilder.WriteString("^!")
words := strings.Split(command.Syntax, " ")
for i, word := range words {
argument, ok := command.Arguments[word]
// 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 {
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(fmt.Sprintf("(%s)", 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,
IsPassive: false,
}
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,
IsPassive: true,
}
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) MatchActive(evt *gomatrix.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:]
command := &gomatrix.MatchedCommand{
Arguments: make(map[string]string),
}
for i, value := range match {
if i >= len(pc.Arguments) {
break
}
key := pc.Arguments[i]
command.Arguments[key] = value
}
command.Matched = pc.Name
// TODO add evt.Content.Command.Target?
evt.Content.Command = command
return true
}
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 && !maubot.JSONLeftEquals(pc.MatchesEvent, evt) {
return false
}
matches := pc.Matches.FindAllStringSubmatch(matchAgainst, -1)
if matches == nil {
return false
}
if evt.Unsigned.PassiveCommand == nil {
evt.Unsigned.PassiveCommand = make(map[string]*gomatrix.MatchedPassiveCommand)
}
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 *gomatrix.Event) bool {
if pc.IsPassive {
return pc.MatchPassive(evt)
} else {
return pc.MatchActive(evt)
}
}

View File

@ -17,80 +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) {
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
}

View File

@ -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)

View File

@ -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),
}
}

View File

@ -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,40 +38,99 @@ 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)
client.Client.Syncer = client.syncer
client.AddEventHandler(maubot.StateMember, client.onJoin)
client.AddEventHandler(gomatrix.StateMember, client.onJoin)
client.AddEventHandler(gomatrix.EventMessage, client.onMessage)
return client, nil
}
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
func (client *Client) Proxy(owner string) *ClientProxy {
return &ClientProxy{
hiddenClient: client,
owner: owner,
}
}
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.StopPropagation
return maubot.StopEventPropagation
}
return handler(evt)
})
}
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.")
}
}
func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
evt, err := client.Client.GetEvent(roomID, eventID)
if err != nil {
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.\n", 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 {
for _, command := range client.commands {
if command.Match(evt.Event) {
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" {
if client.DB.AutoJoinRooms && evt.GetStateKey() == client.DB.UserID && evt.Content.Membership == "invite" {
client.JoinRoom(evt.RoomID)
return maubot.StopPropagation
return maubot.StopEventPropagation
}
return maubot.Continue
}
@ -87,3 +147,18 @@ func (client *Client) Sync() {
}
}()
}
type hiddenClient = Client
type ClientProxy struct {
*hiddenClient
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)
}

View File

@ -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
}

View File

@ -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) {
if fn(event) == maubot.StopEventPropagation {
break
}
}

View File

@ -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