Add unfinished command handler system based on the improved bot support proposal
This commit is contained in:
parent
946dac989a
commit
307a32e0c0
9 changed files with 355 additions and 16 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
130
database/commands.go
Normal file
130
database/commands.go
Normal file
|
@ -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 <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 {
|
||||
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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue