check the time format string when creating a new timer. Format should be like 30m, 10h, or 10h30m

This commit is contained in:
lyx0 2022-08-26 02:27:42 +02:00
parent 5abb28ec5d
commit 4ff29bcbaa

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"regexp"
"strings"
"github.com/gempir/go-twitch-irc/v3"
@ -23,6 +24,17 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
// parts: | prefix | |name | |repeat | <----------- text -------------> |
text := message.Message[prefixLength+len(name)+len(cmdParams[2]) : len(message.Message)]
// validateTimeFormat will be true if the repeat parameter is in
// the format of either 30m, 10h, or 10h30m.
validateTimeFormat, err := regexp.MatchString(`^(\d{1,2}[h])$|^(\d+[m])$|^((\d{1,2}[h])((([0]?|[1-5]{1})[0-9])[m]))$`, repeat)
if err != nil {
app.Logger.Errorw("Received malformed time format in timer",
"repeat", repeat,
"error", err,
)
return
}
timer := &data.Timer{
Name: name,
Text: text,
@ -30,27 +42,47 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
Repeat: repeat,
}
err := app.Models.Timers.Insert(timer)
if err != nil {
app.Logger.Errorw("Error inserting new timer into database",
// Check if the time string we got is valid, this is important
// because the Scheduler panics instead of erroring out if an invalid
// time format string is supplied.
if validateTimeFormat {
timer := &data.Timer{
Name: name,
Text: text,
Channel: message.Channel,
Repeat: repeat,
}
err = app.Models.Timers.Insert(timer)
if err != nil {
app.Logger.Errorw("Error inserting new timer into database",
"timer", timer,
"error", err,
)
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
common.Send(message.Channel, reply, app.TwitchClient)
return
} else {
// cronName is the internal, unique tag/name for the timer.
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
cronName := fmt.Sprintf("%s%s", message.Channel, name)
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, cronName)
app.Logger.Infow("Added new timer",
"timer", timer,
)
reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
} else {
app.Logger.Errorw("Received malformed time format in timer",
"timer", timer,
"error", err,
)
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
common.Send(message.Channel, reply, app.TwitchClient)
return
} else {
// cronName is the internal, unique tag/name for the timer.
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
cronName := fmt.Sprintf("%s%s", message.Channel, name)
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, cronName)
app.Logger.Infow("Added new timer",
"timer", timer,
)
reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat)
reply := fmt.Sprintf("Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30m, 10h, 10h30m")
common.Send(message.Channel, reply, app.TwitchClient)
return
}
@ -109,6 +141,11 @@ func (app *Application) newPrivateMessageTimer(channel, text string) {
func (app *Application) DeleteTimer(name string, message twitch.PrivateMessage) {
cronName := fmt.Sprintf("%s%s", message.Channel, name)
app.Scheduler.RemoveJob(cronName)
app.Logger.Infow("Deleting timer",
"name", name,
"message.Channel", message.Channel,
"cronName", cronName,
)
err := app.Models.Timers.Delete(name)
if err != nil {