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"
|
2019-04-19 13:02:12 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
matrixbot "github.com/binaryplease/matrix-bot"
|
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
|
2019-04-19 13:02:12 +02:00
|
|
|
//map rooms to tokens
|
|
|
|
Tokens map[string]string
|
2019-04-14 22:38:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
func (gb *GiteaBot) Send(token, message string) {
|
|
|
|
for k, v := range gb.Tokens {
|
|
|
|
if v == token {
|
|
|
|
gb.SendToRoom(k, message)
|
2019-04-14 22:38:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
// func contains(s []string, e string) bool {
|
|
|
|
// for _, a := range s {
|
|
|
|
// if a == e {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false
|
|
|
|
// }
|
2019-04-14 22:51:52 +02:00
|
|
|
|
|
|
|
//NewGiteaBot creates a new bot form user credentials
|
2019-04-14 22:38:13 +02:00
|
|
|
func NewGiteaBot(user, pass string) *GiteaBot {
|
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
tokens := make(map[string]string)
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-14 22:51:52 +02:00
|
|
|
bot, err := matrixbot.NewMatrixBot(user, pass)
|
2019-04-14 22:38:13 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gbot := &GiteaBot{
|
|
|
|
bot,
|
2019-04-19 13:02:12 +02:00
|
|
|
tokens,
|
2019-04-14 22:38:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
bot.RegisterCommand("!gitea help", 0, gbot.handleCommandHelp)
|
|
|
|
bot.RegisterCommand("!gitea secret", 0, gbot.handleCommandSecret)
|
|
|
|
bot.RegisterCommand("!gitea set", 0, gbot.handleCommandSet)
|
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 {
|
2019-04-19 13:02:12 +02:00
|
|
|
//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 13:02:12 +02:00
|
|
|
func (gb *GiteaBot) handleCommandSet(message, room, sender string) {
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
// Get the parameter(s) given to the command
|
|
|
|
args := strings.Split(message, " ")
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
// Display help/error if more than one argument is given
|
|
|
|
if len(args) != 3 {
|
|
|
|
gb.SendToRoom(room, "set expects exactly one argument")
|
|
|
|
gb.SendToRoom(room, "!gitea set <token>")
|
2019-04-14 22:38:13 +02:00
|
|
|
} else {
|
2019-04-19 13:02:12 +02:00
|
|
|
// Display help/error if the token has the wrong length
|
|
|
|
if len(args[2]) != 20 {
|
|
|
|
gb.SendToRoom(room, "Tokens have a length of 20 characters")
|
|
|
|
} else {
|
|
|
|
// If the token seems ok, set it for the room
|
|
|
|
gb.SendToRoom(room, "Setting token for this room to:")
|
|
|
|
gb.SendToRoom(room, args[2])
|
|
|
|
gb.Tokens[room] = args[2]
|
|
|
|
}
|
2019-04-14 22:38:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
//Check if room already has a token
|
|
|
|
if gb.Tokens[room] != "" {
|
|
|
|
gb.SendToRoom(room, "This room already has a token. Your secert token is:")
|
|
|
|
gb.SendToRoom(room, gb.Tokens[room])
|
|
|
|
return
|
2019-04-14 22:38:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
token := tokenGenerator()
|
|
|
|
gb.Tokens[room] = token
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
gb.SendToRoom(room, "Your secert token is:")
|
|
|
|
gb.SendToRoom(room, token)
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
gb.SendToRoom(room, "Set up a weebhook in gitea with that token as secret")
|
2019-04-14 22:38:13 +02:00
|
|
|
|
2019-04-19 13:02:12 +02:00
|
|
|
//TODO change this to a real URL or make it configurable
|
|
|
|
gb.SendToRoom(room, "http://192.168.2.33:9000/post/")
|
2019-04-14 22:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gb *GiteaBot) handleCommandHelp(message, room, sender string) {
|
2019-04-19 13:02:12 +02:00
|
|
|
//TODO maybe make this help auto-generated for bots in general?
|
2019-04-14 22:38:13 +02:00
|
|
|
helpMsg := `
|
|
|
|
|
|
|
|
I'm your friendly Gitea Bot!
|
|
|
|
|
|
|
|
You can invite me to any matrix room to get updates on subscribed gitea repositorys.
|
|
|
|
The following commands are avaitible:
|
|
|
|
|
2019-04-18 20:45:16 +02:00
|
|
|
!sub user/repo Subscribe to a repository
|
|
|
|
!unsub user/repo Remove subscription to a repository
|
|
|
|
!listsubs List the room's subscriptions
|
|
|
|
!help Display this message
|
2019-04-14 22:38:13 +02:00
|
|
|
|
|
|
|
Some of the commands might require admin powers!
|
|
|
|
|
|
|
|
`
|
|
|
|
gb.SendToRoom(room, helpMsg)
|
|
|
|
}
|