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

Add flags and comments

This commit is contained in:
Pablo Ovelleiro Corral 2019-04-20 15:34:03 +02:00
parent fb284b7fd0
commit e0539b050b
No known key found for this signature in database
GPG Key ID: F7C1D57C8464E825
3 changed files with 36 additions and 11 deletions

@ -52,6 +52,7 @@ func (gb *GiteaBot) checkToken(room, token string) bool {
func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
//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")
@ -60,30 +61,31 @@ func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
reqRoom := msgParts[2]
//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
}
//Check if room already has a token
//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
}
// If everything is fine, generate a token and add it to the db
token := tokenGenerator()
gb.Tokens[reqRoom] = token
gb.db.Set(reqRoom, token)
//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)
}

@ -14,8 +14,16 @@ type GiteaDB struct {
//NewGiteaDB returns a new GiteaDB
func NewGiteaDB(path string) *GiteaDB {
db := &GiteaDB{path}
db.Init()
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)
}
return db
}

29
main.go

@ -2,8 +2,9 @@ package main
import (
// "fmt"
"flag"
"github.com/go-ini/ini"
"gopkg.in/alecthomas/kingpin.v2"
"time"
// "time"
log "github.com/sirupsen/logrus"
)
@ -12,11 +13,18 @@ var cfg *ini.File
var err error
var mygiteabot *GiteaBot
var verboseFlag = flag.Bool("v", false, "Display additional information")
var (
verboseFlag = kingpin.Flag("verbose", "Verbose mode, displays additional information.").Short('v').Default("false").Bool()
configFlag = kingpin.Flag("config", "Configuration file to use").Short('c').Default("config.ini").String()
initDBFlag = kingpin.Flag("initdb", "Initialize the database. If it exists, it will be overwritten!").Default("false").Bool()
syncSecsFlag = kingpin.Flag("sync", "Matrix synchronizing interval").Default("1").Int()
)
func init() {
flag.Parse()
//Parse flags and set log-level
kingpin.Version("1.0.0")
kingpin.Parse()
if !*verboseFlag {
log.SetLevel(log.InfoLevel)
@ -28,29 +36,36 @@ func init() {
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
//Load config
cfg, err = ini.Load("config.ini")
log.Debugf("Using configuration file %s", *configFlag)
cfg, err = ini.Load(*configFlag)
if err != nil {
log.Fatalf("Fail to read file: %v", err)
}
matrixUser := cfg.Section("matrix").Key("matrix_user").String()
matrixPass := cfg.Section("matrix").Key("matrix_pass").String()
dbPath := cfg.Section("bot").Key("db_path").String()
mygiteabot = NewGiteaBot(matrixUser, matrixPass, "./tokens.db")
//Set up the bot
mygiteabot = NewGiteaBot(matrixUser, matrixPass, dbPath)
if err != nil {
log.Fatal(err)
}
log.Debug("Bot created")
}
func main() {
log.Info("Starting POST-listener")
log.Info("Setting up POST-listener")
go func() {
for {
mygiteabot.Sync()
// Optional: Wait a period of time before trying to sync again.
time.Sleep(time.Duration(*syncSecsFlag) * time.Second)
}
}()
setupListener()