Initial commit
This commit is contained in:
commit
d572522a96
20 changed files with 2075 additions and 0 deletions
110
database/clients.go
Normal file
110
database/clients.go
Normal file
|
@ -0,0 +1,110 @@
|
|||
// jesaribot - A simple maubot plugin.
|
||||
// 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 (
|
||||
log "maunium.net/go/maulogger"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type MatrixClient struct {
|
||||
db *Database
|
||||
sql *sql.DB
|
||||
|
||||
UserID string `json:"user_id"`
|
||||
Homeserver string `json:"homeserver"`
|
||||
AccessToken string `json:"access_token"`
|
||||
NextBatch string `json:"next_batch"`
|
||||
FilterID string `json:"filter_id"`
|
||||
|
||||
Sync bool `json:"sync"`
|
||||
AutoJoinRooms bool `json:"auto_join_rooms"`
|
||||
DisplayName string `json:"display_name"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
|
||||
type MatrixClientStatic struct {
|
||||
db *Database
|
||||
sql *sql.DB
|
||||
}
|
||||
|
||||
func (mcs *MatrixClientStatic) CreateTable() error {
|
||||
_, err := mcs.sql.Exec(`CREATE TABLE IF NOT EXISTS matrix_client (
|
||||
user_id VARCHAR(255) PRIMARY KEY,
|
||||
homeserver VARCHAR(255) NOT NULL,
|
||||
access_token VARCHAR(255) NOT NULL,
|
||||
next_batch VARCHAR(255) NOT NULL,
|
||||
filter_id VARCHAR(255) NOT NULL,
|
||||
|
||||
sync BOOLEAN NOT NULL,
|
||||
autojoin BOOLEAN NOT NULL,
|
||||
display_name VARCHAR(255) NOT NULL,
|
||||
avatar_url VARCHAR(255) NOT NULL
|
||||
)`)
|
||||
return err
|
||||
}
|
||||
|
||||
func (mcs *MatrixClientStatic) Get(userID string) *MatrixClient {
|
||||
row := mcs.sql.QueryRow("SELECT user_id, homeserver, access_token, next_batch, filter_id, sync, autojoin, display_name, avatar_url FROM matrix_client WHERE user_id=?", userID)
|
||||
if row != nil {
|
||||
return mcs.New().Scan(row)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mcs *MatrixClientStatic) GetAll() (clients []*MatrixClient) {
|
||||
rows, err := mcs.sql.Query("SELECT user_id, homeserver, access_token, next_batch, filter_id, sync, autojoin, display_name, avatar_url FROM matrix_client")
|
||||
if err != nil || rows == nil {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
clients = append(clients, mcs.New().Scan(rows))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (mcs *MatrixClientStatic) New() *MatrixClient {
|
||||
return &MatrixClient{
|
||||
db: mcs.db,
|
||||
sql: mcs.sql,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
return mxc
|
||||
}
|
||||
|
||||
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)
|
||||
return err
|
||||
}
|
||||
|
||||
func (mxc *MatrixClient) Update() error {
|
||||
_, err := mxc.sql.Exec("UPDATE matrix_client SET access_token=?, next_batch=?, filter_id=?, sync=?, autojoin=?, display_name=?, avatar_url=? WHERE user_id=?",
|
||||
mxc.AccessToken, mxc.NextBatch, mxc.FilterID, mxc.Sync, mxc.AutoJoinRooms, mxc.DisplayName, mxc.AvatarURL, mxc.UserID)
|
||||
return err
|
||||
}
|
64
database/database.go
Normal file
64
database/database.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
// jesaribot - A simple maubot plugin.
|
||||
// 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"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
log "maunium.net/go/maulogger"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
Type string `yaml:"type"`
|
||||
Name string `yaml:"name"`
|
||||
|
||||
MatrixClient *MatrixClientStatic `yaml:"-"`
|
||||
Plugin *PluginStatic `yaml:"-"`
|
||||
|
||||
sql *sql.DB
|
||||
}
|
||||
|
||||
func (db *Database) Connect() (err error) {
|
||||
db.sql, err = sql.Open(db.Type, db.Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
db.MatrixClient = &MatrixClientStatic{db: db, sql: db.sql}
|
||||
db.Plugin = &PluginStatic{db: db, sql: db.sql}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Database) CreateTables() {
|
||||
log.Debugln("Creating database tables")
|
||||
|
||||
err := db.MatrixClient.CreateTable()
|
||||
if err != nil {
|
||||
log.Errorln("Failed to create matrix_client table:", err)
|
||||
}
|
||||
|
||||
err = db.Plugin.CreateTable()
|
||||
if err != nil {
|
||||
log.Errorln("Failed to create plugin table:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (db *Database) SQL() *sql.DB {
|
||||
return db.sql
|
||||
}
|
105
database/plugins.go
Normal file
105
database/plugins.go
Normal file
|
@ -0,0 +1,105 @@
|
|||
// jesaribot - A simple maubot plugin.
|
||||
// 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"
|
||||
|
||||
log "maunium.net/go/maulogger"
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
db *Database
|
||||
sql *sql.DB
|
||||
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
UserID string `json:"user_id"`
|
||||
//User *MatrixClient `json:"-"`
|
||||
}
|
||||
|
||||
type PluginStatic struct {
|
||||
db *Database
|
||||
sql *sql.DB
|
||||
}
|
||||
|
||||
func (ps *PluginStatic) CreateTable() error {
|
||||
_, err := ps.sql.Exec(`CREATE TABLE IF NOT EXISTS plugin (
|
||||
id VARCHAR(255) PRIMARY KEY,
|
||||
type VARCHAR(255) NOT NULL,
|
||||
enabled BOOLEAN NOT NULL,
|
||||
|
||||
user_id VARCHAR(255) NOT NULL,
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES matrix_client(user_id)
|
||||
ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
)`)
|
||||
return err
|
||||
}
|
||||
|
||||
func (ps *PluginStatic) Get(id string) *Plugin {
|
||||
row := ps.sql.QueryRow("SELECT * FROM plugin WHERE id=?", id)
|
||||
if row != nil {
|
||||
return ps.New().Scan(row)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ps *PluginStatic) GetAll() (plugins []*Plugin) {
|
||||
rows, err := ps.sql.Query("SELECT * FROM plugin")
|
||||
if err != nil || rows == nil {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
plugins = append(plugins, ps.New().Scan(rows))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ps *PluginStatic) New() *Plugin {
|
||||
return &Plugin{
|
||||
db: ps.db,
|
||||
sql: ps.sql,
|
||||
}
|
||||
}
|
||||
|
||||
/*func (p *Plugin) LoadUser() *Plugin {
|
||||
p.User = p.db.MatrixClient.Get(p.UserID)
|
||||
return p
|
||||
}*/
|
||||
|
||||
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)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Plugin) Insert() error {
|
||||
_, err := p.sql.Exec("INSERT INTO plugin (id, type, enabled, user_id) VALUES (?, ?, ?, ?)",
|
||||
p.ID, p.Type, p.Enabled, p.UserID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Plugin) Update() error {
|
||||
_, err := p.sql.Exec("UPDATE plugin SET enabled=? WHERE id=?",
|
||||
p.Enabled, p.ID)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue