1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-26 08:43:47 +01:00
gitea-matrix-bot/giteabot.go

92 lines
2.5 KiB
Go
Raw Normal View History

2019-04-14 22:38:13 +02:00
package main
import (
2019-04-19 11:28:21 +02:00
"crypto/rand"
2019-04-14 22:38:13 +02:00
"fmt"
"strings"
matrixbot "github.com/binaryplease/matrix-bot"
log "github.com/sirupsen/logrus"
2019-04-14 22:38:13 +02:00
)
2019-04-14 22:51:52 +02:00
//GiteaBot is the main struct to hold the bot
2019-04-14 22:38:13 +02:00
type GiteaBot struct {
2019-04-14 22:51:52 +02:00
*matrixbot.MatrixBot
//map rooms to tokens
Tokens map[string]string
2019-04-19 21:35:01 +02:00
db *GiteaDB
2019-04-14 22:38:13 +02:00
}
2019-04-14 22:51:52 +02:00
//NewGiteaBot creates a new bot form user credentials
2019-04-19 21:35:01 +02:00
func NewGiteaBot(user, pass string, DBPath string) *GiteaBot {
2019-04-14 22:38:13 +02:00
2019-04-19 21:35:01 +02:00
bot, err := matrixbot.NewMatrixBot(user, pass, "gitea")
2019-04-14 22:38:13 +02:00
if err != nil {
log.Fatal(err)
2019-04-14 22:38:13 +02:00
}
2019-04-19 21:35:01 +02:00
db := NewGiteaDB(DBPath)
2019-04-14 22:38:13 +02:00
gbot := &GiteaBot{
bot,
2019-04-19 21:35:01 +02:00
db.GetAll(),
db,
2019-04-14 22:38:13 +02:00
}
2019-04-19 21:35:01 +02:00
bot.RegisterCommand("secret", 0, "Request token for a webhook", gbot.handleCommandSecret)
2019-04-14 22:51:52 +02:00
2019-04-14 22:38:13 +02:00
return gbot
}
2019-04-19 11:28:21 +02:00
func tokenGenerator() string {
//TODO make token length configurable
b := make([]byte, 20)
2019-04-19 11:28:21 +02:00
rand.Read(b)
return fmt.Sprintf("%x", b)
}
2019-04-19 21:35:01 +02:00
func (gb *GiteaBot) checkToken(room, token string) bool {
return token == gb.db.GetToken(room)
}
func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
2019-04-19 21:35:01 +02:00
2019-04-20 15:34:03 +02:00
//Check if a parameter was supplied (room for which to generate the token)
msgParts := strings.Split(message, " ")
if len(msgParts) != 3 {
gb.SendToRoom(room, "!gitea secert expects exactly on parameter, a room for which to request a token.\n Usage: !gitea secret <room id> \n\n e.g. !gitea secert !FoJFjcBoIJyKuPnDFf:matrix.org")
return
2019-04-14 22:38:13 +02:00
}
reqRoom := msgParts[2]
2019-04-14 22:38:13 +02:00
2019-04-20 15:34:03 +02:00
//Basic check if the roomID is properly formatted. The user might mistake it for the alias or address
if !strings.HasPrefix(reqRoom, "!") {
gb.SendToRoom(room, "Room IDs start with an exclamation mark\n\n e.g. !gitea secert !FoJFjcBoIJyKuPnDFf:matrix.org \n\n This is *not* the same as the rooms name or alias!")
return
}
2019-04-20 00:00:09 +02:00
2019-04-20 15:34:03 +02:00
//Check if room already has a token, if so display it.
if gb.Tokens[reqRoom] != "" {
gb.SendToRoom(room, "This room already has a token. Your secert token is:")
gb.SendToRoom(room, gb.Tokens[reqRoom])
return
2019-04-14 22:38:13 +02:00
}
2019-04-20 15:34:03 +02:00
// If everything is fine, generate a token and add it to the db
token := tokenGenerator()
gb.Tokens[reqRoom] = token
gb.db.Set(reqRoom, token)
2019-04-14 22:38:13 +02:00
2019-04-20 15:34:03 +02:00
//Print the token and help to the room it was requested from
gb.SendToRoom(room, "Your secert token is:")
gb.SendToRoom(room, token)
gb.SendToRoom(room, "Now, set up a weebhook in gitea with that token as secret")
httpHost := cfg.Section("http").Key("http_host").String()
httpPort := cfg.Section("http").Key("http_port").String()
httpURI := cfg.Section("http").Key("http_uri").String()
gb.SendToRoom(room, httpHost+":"+httpPort+httpURI+room)
2019-04-14 22:38:13 +02:00
}