func onCommand(bot *telebot.Bot, command string, args []string) { if command == "msg" && len(args) > 1 { user := config.GetUser(args[0]) if user.UID == config.NilUser.UID { log.Errorf("[Syscmds] Couldn't get an user with the name or UID %s", args[0]) } msg := connect(args[1:]) bot.SendMessage(user, "*[Sysadmin]* "+msg, util.Markdown) log.Infof("[Syscmds] Sent message %[1]s to %[2]s", msg, user.Name) } else if command == "broadcast" && len(args) > 0 { msg := connect(args) for _, user := range config.GetAllUsers() { bot.SendMessage(user, "*[Sysadmin Broadcast]* "+msg, util.Markdown) } log.Infof("[Syscmds] Broadcasted message %[1]s", msg) } else if command == "config" && len(args) > 0 { if strings.EqualFold(args[0], "save") { if !config.IndentConfig && len(args) > 1 && strings.EqualFold(args[0], "pretty") { config.IndentConfig = true config.Save() config.IndentConfig = false } else { config.Save() } } else if strings.EqualFold(args[0], "load") { config.Load() } } else if command == "stop" { Shutdown("Sysadmin") } }
// Loop is an infinite loop that checks for new Ranssi posts func Loop(bot *telebot.Bot, noNotifAtInit bool) { for { readNow := config.GetConfig().LastReadPost + 1 node := getPost(readNow) if node != nil { topic := strings.TrimSpace(node.FirstChild.FirstChild.Data) log.Infof("[Posts] New post detected: %s (ID %d)", topic, readNow) if !noNotifAtInit { config.GetUsersWithSettingAndRun(func(u config.User) { bot.SendMessage(u, lang.Translatef(u, "posts.new", topic, readNow), util.Markdown) }, subSetting) } config.GetConfig().LastReadPost = readNow config.ASave() updateNews() time.Sleep(5 * time.Second) continue } noNotifAtInit = false time.Sleep(1 * time.Minute) } }
// Shutdown shuts down the Ranssibot. func Shutdown(by string) { log.Infof("Ranssibot cleaning up and exiting...") config.Save() log.Shutdown() var shutdown = "Ranssibot shut down" if *debug { shutdown = fmt.Sprintf("Ranssibot shut down by %[2]s @ %[1]s", time.Now().Format("15:04:05 02.01.2006"), by) } log.Infof(shutdown) onoffspam(shutdown) os.Exit(0) }
func loadTemplates() { log.Infof("Loading HTML templates...") err := data.LoadTemplates(config.ImageTemplate) if err != nil { log.Fatalf("Failed to load image page: %s", err) os.Exit(3) } log.Debugln("Successfully loaded HTML templates") }
func main() { flag.Parse() if !*disableSafeShutdown { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c os.Stdout.Write([]byte("\n")) log.Infof("Shutting down mauImageServer...") database.Unload() os.Exit(0) }() } // Configure the logger if *debug { log.PrintLevel = 0 } os.MkdirAll(*logPath, 0755) log.Fileformat = func(date string, i int) string { return filepath.Join(*logPath, fmt.Sprintf("%[1]s-%02[2]d.log", date, i)) } // Initialize the logger log.Init() log.Infof("Initializing mauImageServer " + version) loadConfig() loadDatabase() loadTemplates() handlers.Init(config, database, auth) log.Infof("Registering handlers") http.HandleFunc("/auth/login", handlers.Login) http.HandleFunc("/auth/register", handlers.Register) http.HandleFunc("/insert", handlers.Insert) http.HandleFunc("/delete", handlers.Delete) http.HandleFunc("/hide", handlers.Hide) http.HandleFunc("/search", handlers.Search) http.HandleFunc("/", handlers.Get) log.Infof("Listening on %s:%d", config.IP, config.Port) http.ListenAndServe(config.IP+":"+strconv.Itoa(config.Port), nil) }
func loadConfig() { log.Infof("Loading config...") var err error config, err = data.LoadConfig(*confPath) if err != nil { log.Fatalf("Failed to load config: %[1]s", err) os.Exit(1) } log.Debugln("Successfully loaded config.") }
func onCommand(command string, args []string) { if command == "remove" && len(args) > 1 { var err error if args[0] == "short" { err = data.DeleteShort(args[1]) } else if args[0] == "url" { err = data.DeleteURL(args[1]) } if err != nil { log.Errorf("Failed to delete: %s", err) } else { log.Infof("Successfully deleted all entries where %s=%s", args[0], args[1]) } } else if command == "set" && len(args) > 2 { err := data.InsertDirect(args[0], args[1], args[2]) if err != nil { log.Errorf("Failed to insert: %s", err) } else { log.Infof("Successfully inserted entry: (%s, %s, %s)", args[0], args[1], args[2]) } } }
func main() { // Configure the logger log.PrintDebug = *debug log.Fileformat = func(date string, i int) string { return fmt.Sprintf("%[3]s/%[1]s-%02[2]d.log", date, i, *logPath) } // Initialize the logger log.Init() log.Infof("Initializing mau\\Lu") loadConfig() loadTemplates() loadDatabase() if *stdin { go stdinListen() } log.Infof("Listening on %s:%d", config.IP, config.Port) http.HandleFunc("/query/", query) http.HandleFunc("/", get) http.ListenAndServe(config.IP+":"+strconv.Itoa(config.Port), nil) }
func main() { start := util.TimestampMS() // Connect to Telegram var err error bot, err = telebot.NewBot(*token) if err != nil { log.Fatalf("Error connecting to Telegram: %[1]s", err) return } messages := make(chan telebot.Message) // Enable message listener bot.Listen(messages, 1*time.Second) // Print "connected" message log.Infof("Successfully connected to Telegram!") // Update timetables timetables.Update() go posts.Loop(bot, *debug) go laundry.Loop(bot) go listen(bot) startedAt = time.Now() var startup = "Ranssibot started up" if *debug { startup = fmt.Sprintf("Ranssibot started up in %[1]dms @ %[2]s (Debug mode)", util.TimestampMS()-start, startedAt.Format("15:04:05 02.01.2006")) } log.Infof(startup) onoffspam(startup) // Listen to messages for message := range messages { go handleCommand(bot, message) } }
func loadDatabase() { log.Infof("Loading database...") database = data.CreateDatabase(config.SQL) err := database.Load() if err != nil { log.Fatalf("Failed to load database: %[1]s", err) os.Exit(2) } auth, err = mauth.Create(database.GetInternalDB()) if err != nil { log.Fatalf("Failed to load Mauth: %[1]s", err) } log.Debugln("Successfully loaded database.") }