Compare commits

..

1 Commits

Author SHA1 Message Date
4dc94dc0cf
Prevent dice overflow
rand.Int() would panic when the max value is <= 0, which happens when
big.NewInt() was fed with a too large number. Avoid this by validating
the big.NewInt() return beforehand. Add error handling to all callers to
both gracefully return to IRC and to log an error message.
Rename the shadowed "max" variable whilst at it to avoid confusion.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
2024-10-10 02:46:47 +02:00
4 changed files with 3 additions and 7 deletions

View File

@ -1,5 +1,4 @@
watbot:
database: wat.db # wat.db (in the working directory) is the default
server:
host: irc.casa # mandatory, no default
port: 6697

View File

@ -20,7 +20,6 @@ type Config struct {
}
type watConfig struct {
Database string `default:"wat.db" yaml:"database"`
Nick string `yaml:"nick"`
Pass string `yaml:"pass"`
User string `yaml:"user"`
@ -100,7 +99,6 @@ func main() {
Name: config.Name,
}
watConfig := wat.WatConfig{
DatabasePath: config.Database,
AutoJoinChannels: config.Channels.Join,
PermittedChannels: config.Channels.Permitted,
IgnoredHosts: config.Ignores.Hosts,

View File

@ -20,7 +20,6 @@ type WatBot struct {
}
type WatConfig struct {
DatabasePath string
BotHosts []string
BotGames BotGameConfig
AdminHosts []string
@ -31,7 +30,7 @@ type WatConfig struct {
func NewWatBot(config *irc.ClientConfig, watConfig *WatConfig, serverConn *tls.Conn) *WatBot {
wat := WatBot{conn: serverConn, Nick: config.Nick, c: watConfig}
wat.Db = NewWatDb(watConfig.DatabasePath)
wat.Db = NewWatDb()
wat.game = NewWatGame(&wat, wat.Db)
wat.integration = NewWatIntegration(&wat, wat.Db, &WatIntegrationConfig{BotHosts: watConfig.BotHosts, BotGames: watConfig.BotGames})
config.Handler = irc.HandlerFunc(wat.HandleIrcMsg)

View File

@ -52,10 +52,10 @@ type WatDb struct {
db *gorm.DB
}
func NewWatDb(dbpath string) *WatDb {
func NewWatDb() *WatDb {
w := WatDb{}
var err error
w.db, err = gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
w.db, err = gorm.Open(sqlite.Open("wat.db"), &gorm.Config{})
if err != nil {
panic(err)
}