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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "strings"
  10. "syscall"
  11. "git.aiterp.net/rpdata/logbot3/internal/bot"
  12. "git.aiterp.net/rpdata/logbot3/internal/config"
  13. "git.aiterp.net/rpdata/logbot3/internal/models/users"
  14. )
  15. func main() {
  16. conf := config.Get()
  17. user, err := users.CheckToken(context.Background())
  18. if user.LoggedIn() {
  19. log.Printf("Logged in: %s (%s)", user.ID, strings.Join(user.Permissions, ", "))
  20. } else {
  21. log.Println("Warning: API key did not gain us access:", err)
  22. os.Exit(1)
  23. }
  24. bot := bot.New(context.Background(), conf.Bot.Nicks[0], conf.Bot.Nicks[1:], conf.Bot.User, conf.Bot.RealName)
  25. err = bot.Connect(conf.Server.Address, conf.Server.SSL, 3)
  26. if err != nil {
  27. log.Println("IRC server cannot be reached:", err)
  28. os.Exit(1)
  29. }
  30. if conf.Debug.Enabled {
  31. mux := http.NewServeMux()
  32. mux.HandleFunc("/state", func(w http.ResponseWriter, r *http.Request) {
  33. w.Header().Set("Content-Type", "application/json")
  34. w.WriteHeader(200)
  35. _ = json.NewEncoder(w).Encode(bot.ClientState())
  36. })
  37. go func() {
  38. err := http.ListenAndServe(conf.Debug.Listen, mux)
  39. if err != nil {
  40. log.Println("Failed to setup debug listener", err)
  41. }
  42. }()
  43. }
  44. // Listen for a quit signal.
  45. interrupt := make(chan os.Signal, 1)
  46. signal.Notify(interrupt, os.Interrupt)
  47. signal.Notify(interrupt, syscall.SIGTERM)
  48. <-interrupt
  49. log.Println("Interrupt received, stopping...")
  50. }