diff --git a/giteabot.go b/giteabot.go index 70059b7..d695a0f 100644 --- a/giteabot.go +++ b/giteabot.go @@ -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 \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) } diff --git a/giteadb.go b/giteadb.go index e889876..1d36846 100644 --- a/giteadb.go +++ b/giteadb.go @@ -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 } diff --git a/main.go b/main.go index 273666d..1c7520b 100644 --- a/main.go +++ b/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()