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-20 14:59:08 +02:00
"strings"
2019-04-19 13:02:12 +02:00
2023-07-13 18:29:50 +02:00
matrixbot "github.com/pinpox/matrix-bot"
2019-04-20 14:59:08 +02:00
log "github.com/sirupsen/logrus"
2019-04-14 22:38:13 +02:00
)
2023-07-13 15:42:23 +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-19 21:35:01 +02:00
db * GiteaDB
2019-04-14 22:38:13 +02:00
}
2023-07-13 15:42:23 +02:00
// NewGiteaBot creates a new bot form user credentials
2019-04-23 17:57:32 +02:00
func NewGiteaBot ( user , pass , host string , DBPath string ) * GiteaBot {
2019-04-14 22:38:13 +02:00
2023-07-13 18:29:50 +02:00
log . Info ( "Creating bot" )
2023-07-13 15:42:23 +02:00
bot , err := matrixbot . NewMatrixBot ( user , pass , host , "gitea" )
2019-04-14 22:38:13 +02:00
if err != nil {
2023-07-13 18:29:50 +02:00
log . Fatal ( "Failed to create bot" , 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 {
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 21:35:01 +02:00
func ( gb * GiteaBot ) checkToken ( room , token string ) bool {
return token == gb . db . GetToken ( room )
}
2019-04-20 14:59:08 +02:00
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)
2019-04-20 14:59:08 +02:00
msgParts := strings . Split ( message , " " )
if len ( msgParts ) != 3 {
2019-04-22 16:30:52 +02:00
gb . SendTextToRoom ( 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" )
2019-04-20 14:59:08 +02:00
return
2019-04-14 22:38:13 +02:00
}
2019-04-20 14:59:08 +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
2019-04-20 14:59:08 +02:00
if ! strings . HasPrefix ( reqRoom , "!" ) {
2019-04-22 16:30:52 +02:00
gb . SendTextToRoom ( 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!" )
2019-04-20 14:59:08 +02:00
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.
2019-04-20 14:59:08 +02:00
if gb . Tokens [ reqRoom ] != "" {
2019-04-22 16:30:52 +02:00
gb . SendTextToRoom ( room , "This room already has a token. Your secert token is:" )
gb . SendTextToRoom ( room , gb . Tokens [ reqRoom ] )
2019-04-19 13:02:12 +02:00
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
2019-04-19 13:02:12 +02:00
token := tokenGenerator ( )
2019-04-20 14:59:08 +02:00
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
2019-04-22 16:30:52 +02:00
gb . SendTextToRoom ( room , "Your secert token is:" )
gb . SendTextToRoom ( room , token )
gb . SendTextToRoom ( room , "Now, set up a weebhook in gitea with that token as secret" )
2019-04-20 14:59:08 +02:00
httpHost := cfg . Section ( "http" ) . Key ( "http_host" ) . String ( )
httpPort := cfg . Section ( "http" ) . Key ( "http_port" ) . String ( )
httpURI := cfg . Section ( "http" ) . Key ( "http_uri" ) . String ( )
2019-04-22 16:30:52 +02:00
gb . SendTextToRoom ( room , httpHost + ":" + httpPort + httpURI + room )
2019-04-14 22:38:13 +02:00
}