2019-04-19 21:35:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-04-20 14:59:08 +02:00
|
|
|
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() {
|
|
|
|
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 {
|
2019-04-20 14:59:08 +02:00
|
|
|
log.Fatalf("%q: %s\n", err, sqlStmt)
|
2019-04-19 21:35:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 14:59:08 +02:00
|
|
|
//GetToken returns the token for a room, if found
|
2019-04-19 21:35:01 +02:00
|
|
|
func (dbg *GiteaDB) GetToken(room string) string {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetAll returns all existing rooms with token
|
|
|
|
func (dbg *GiteaDB) GetAll() map[string]string {
|
|
|
|
tokens := make(map[string]string)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|