1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-22 19:31:58 +01:00
gitea-matrix-bot/giteadb.go

167 lines
3.1 KiB
Go
Raw Normal View History

2019-04-19 21:35:01 +02:00
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
2019-04-19 21:35:01 +02:00
"os"
)
//GiteaDB holds a database and the neeeded functions
type GiteaDB struct {
path string
}
//NewGiteaDB returns a new GiteaDB
func NewGiteaDB(path string) *GiteaDB {
2019-04-20 15:34:03 +02:00
2019-04-19 21:35:01 +02:00
db := &GiteaDB{path}
2019-04-20 15:34:03 +02:00
if *initDBFlag {
log.Debugf("Using Database: %s (will be created/overwritten)", db.path)
db.Init()
} else {
log.Debugf("Using database: %s will be used (already existing)", db.path)
}
2019-04-19 21:35:01 +02:00
return db
}
//Init initializes the db, if it exists in the path it will be overwritten
func (dbg *GiteaDB) Init() {
2019-04-22 16:30:52 +02:00
log.Debugf("Initializing DB in %s", dbg.path)
2019-04-19 21:35:01 +02:00
os.Remove(dbg.path)
dbtmp, err := sql.Open("sqlite3", dbg.path)
if err != nil {
log.Fatal(err)
}
defer dbtmp.Close()
sqlStmt := `
create table tokens (room text not null primary key, token text);
delete from tokens;
`
_, err = dbtmp.Exec(sqlStmt)
if err != nil {
log.Fatalf("%q: %s\n", err, sqlStmt)
2019-04-19 21:35:01 +02:00
return
}
}
//GetToken returns the token for a room, if found
2019-04-19 21:35:01 +02:00
func (dbg *GiteaDB) GetToken(room string) string {
2019-04-22 16:30:52 +02:00
log.Debugf("Getting token for room %s", room)
2019-04-19 21:35:01 +02:00
db, err := sql.Open("sqlite3", dbg.path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
2019-04-19 23:54:13 +02:00
stmt, err := db.Prepare("select token from tokens where room = ?")
2019-04-19 21:35:01 +02:00
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var token string
err = stmt.QueryRow(room).Scan(&token)
if err != nil {
2019-04-22 16:30:52 +02:00
log.Debug("No token found!")
return ""
2019-04-19 21:35:01 +02:00
}
2019-04-22 16:30:52 +02:00
log.Debugf("Found token: %s", token)
2019-04-19 21:35:01 +02:00
return token
}
//GetAll returns all existing rooms with token
func (dbg *GiteaDB) GetAll() map[string]string {
tokens := make(map[string]string)
2019-04-22 16:30:52 +02:00
log.Debugf("Retrieving all tokens from %s", dbg.path)
2019-04-19 21:35:01 +02:00
db, err := sql.Open("sqlite3", dbg.path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("select room, token from tokens")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var room string
var token string
err = rows.Scan(&room, &token)
if err != nil {
log.Fatal(err)
}
tokens[room] = token
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
2019-04-22 16:30:52 +02:00
log.Debugf("Found %v tokens", len(tokens))
2019-04-19 21:35:01 +02:00
return tokens
}
//Unset deletes a room and it's token from the database, if it exists
func (dbg *GiteaDB) Unset(room, token string) {
db, err := sql.Open("sqlite3", dbg.path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
stmt, err := db.Prepare("delete from tokens where room = ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(room)
if err != nil {
log.Fatal(err)
}
}
//Set sets a token for a room and saves it to the db
func (dbg *GiteaDB) Set(room, token string) {
dbg.Unset(room, token)
db, err := sql.Open("sqlite3", dbg.path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
2019-04-19 23:54:13 +02:00
2019-04-19 21:35:01 +02:00
stmt, err := tx.Prepare("insert into tokens(room, token) values(?, ?)")
if err != nil {
log.Fatal(err)
}
2019-04-19 23:54:13 +02:00
2019-04-19 21:35:01 +02:00
defer stmt.Close()
2019-04-19 23:54:13 +02:00
_, err = stmt.Exec(room, token)
if err != nil {
log.Fatal(err)
2019-04-19 21:35:01 +02:00
}
tx.Commit()
}