The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.4 KiB

package main
import (
"context"
"encoding/json"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"git.aiterp.net/rpdata/logbot3/internal/bot"
"git.aiterp.net/rpdata/logbot3/internal/config"
"git.aiterp.net/rpdata/logbot3/internal/models/users"
)
func main() {
conf := config.Get()
user, err := users.CheckToken(context.Background())
if user.LoggedIn() {
log.Printf("Logged in: %s (%s)", user.ID, strings.Join(user.Permissions, ", "))
} else {
log.Println("Warning: API key did not gain us access:", err)
os.Exit(1)
}
bot := bot.New(context.Background(), conf.Bot.Nicks[0], conf.Bot.Nicks[1:], conf.Bot.User, conf.Bot.RealName)
err = bot.Connect(conf.Server.Address, conf.Server.SSL, 3)
if err != nil {
log.Println("IRC server cannot be reached:", err)
os.Exit(1)
}
if conf.Debug.Enabled {
mux := http.NewServeMux()
mux.HandleFunc("/state", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_ = json.NewEncoder(w).Encode(bot.ClientState())
})
go func() {
err := http.ListenAndServe(conf.Debug.Listen, mux)
if err != nil {
log.Println("Failed to setup debug listener", err)
}
}()
}
// Listen for a quit signal.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
signal.Notify(interrupt, syscall.SIGTERM)
<-interrupt
log.Println("Interrupt received, stopping...")
}