diff --git a/bot/bot.go b/bot/bot.go index 16689e7..a78fb9b 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -148,39 +148,39 @@ func NewBot(config *config.Config, c *irc.Client) *Bot { func (b *Bot) migrateDB() { _, err := b.DB.Exec(`create table if not exists version (version integer);`) if err != nil { - log.Fatal(err) + log.Fatal("Initial DB migration create version table: ", err) } - var version int64 + var version sql.NullInt64 err = b.DB.QueryRow("select max(version) from version").Scan(&version) - switch { - case err == sql.ErrNoRows: + if err != nil { + log.Fatal("Initial DB migration get version: ", err) + } + if version.Valid { + b.DBVersion = version.Int64 + log.Printf("Database version: %v\n", b.DBVersion) + } else { log.Printf("No versions, we're the first!.") _, err := b.DB.Exec(`insert into version (version) values (1)`) if err != nil { - log.Fatal(err) + log.Fatal("Initial DB migration insert: ", err) } - case err != nil: - log.Fatal(err) - default: - b.DBVersion = version - log.Printf("Database version: %d\n", version) } - if version == 1 { + if b.DBVersion == 1 { if _, err := b.DB.Exec(`create table if not exists variables ( id integer primary key, name string, perms string, type string );`); err != nil { - log.Fatal(err) + log.Fatal("Initial DB migration create variables table: ", err) } - if _, err := b.DB.Exec(`create table if not exists values ( + if _, err := b.DB.Exec(`create table if not exists 'values' ( id integer primary key, varId integer, value string );`); err != nil { - log.Fatal(err) + log.Fatal("Initial DB migration create values table: ", err) } } } @@ -233,45 +233,6 @@ func (b *Bot) SendAction(channel, message string) { b.SendMessage(channel, message) } -// Handles incomming PRIVMSG requests -func (b *Bot) MsgRecieved(client *irc.Client, inMsg irc.Msg) { - if inMsg.User == "" { - return - } - - msg := b.buildMessage(client, inMsg) - - if strings.HasPrefix(msg.Body, "help") && msg.Command { - parts := strings.Fields(strings.ToLower(msg.Body)) - b.checkHelp(msg.Channel, parts) - goto RET - } - - for _, name := range b.PluginOrdering { - p := b.Plugins[name] - if p.Message(msg) { - break - } - } - -RET: - b.logIn <- msg - return -} - -func (b *Bot) EventRecieved(conn *irc.Client, inMsg irc.Msg) { - if inMsg.User == "" { - return - } - msg := b.buildMessage(conn, inMsg) - for _, name := range b.PluginOrdering { - p := b.Plugins[name] - if p.Event(inMsg.Cmd, msg) { - break - } - } -} - func (b *Bot) Who(channel string) []User { out := []User{} for _, u := range b.Users { diff --git a/bot/handlers.go b/bot/handlers.go index 47dcbfd..c5f7b9b 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -16,6 +16,48 @@ import ( "code.google.com/p/velour/irc" ) +// Handles incomming PRIVMSG requests +func (b *Bot) MsgRecieved(client *irc.Client, inMsg irc.Msg) { + log.Println("Recieved message: ", inMsg) + if inMsg.User == "" { + return + } + + msg := b.buildMessage(client, inMsg) + + if strings.HasPrefix(msg.Body, "help") && msg.Command { + parts := strings.Fields(strings.ToLower(msg.Body)) + b.checkHelp(msg.Channel, parts) + goto RET + } + + for _, name := range b.PluginOrdering { + p := b.Plugins[name] + if p.Message(msg) { + break + } + } + +RET: + b.logIn <- msg + return +} + +// Handle incoming events +func (b *Bot) EventRecieved(conn *irc.Client, inMsg irc.Msg) { + log.Println("Recieved event: ", inMsg) + if inMsg.User == "" { + return + } + msg := b.buildMessage(conn, inMsg) + for _, name := range b.PluginOrdering { + p := b.Plugins[name] + if p.Event(inMsg.Cmd, msg) { + break + } + } +} + // Interface used for compatibility with the Plugin interface type Handler interface { Message(message Message) bool @@ -201,7 +243,7 @@ func (b *Bot) getVar(varName string) (string, error) { case err == sql.ErrNoRows: return "", fmt.Errorf("No factoid found") case err != nil: - log.Fatal(err) + log.Fatal("getVar error: ", err) } return text, nil } diff --git a/bot/users.go b/bot/users.go index 600bebf..8e2ca34 100644 --- a/bot/users.go +++ b/bot/users.go @@ -19,7 +19,7 @@ type User struct { //bot *Bot } -var users map[string]*User +var users = map[string]*User{} func (b *Bot) GetUser(nick string) *User { if _, ok := users[nick]; !ok { diff --git a/main.go b/main.go index 4139c5d..a5ec0ed 100644 --- a/main.go +++ b/main.go @@ -57,17 +57,17 @@ func main() { Bot = bot.NewBot(Config, Client) // Bot.AddHandler(plugins.NewTestPlugin(Bot)) Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot)) - Bot.AddHandler("first", plugins.NewFirstPlugin(Bot)) - Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot)) + // Bot.AddHandler("first", plugins.NewFirstPlugin(Bot)) + // Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot)) Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot)) Bot.AddHandler("dice", plugins.NewDicePlugin(Bot)) Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot)) Bot.AddHandler("counter", plugins.NewCounterPlugin(Bot)) - Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot)) + // Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot)) Bot.AddHandler("skeleton", plugins.NewSkeletonPlugin(Bot)) Bot.AddHandler("your", plugins.NewYourPlugin(Bot)) // catches anything left, will always return true - Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot)) + // Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot)) handleConnection() diff --git a/plugins/admin.go b/plugins/admin.go index 7a58e72..b14066e 100644 --- a/plugins/admin.go +++ b/plugins/admin.go @@ -72,7 +72,7 @@ func (p *AdminPlugin) handleVariables(message bot.Message) bool { return true case err != nil: p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.") - log.Println(err) + log.Println("Admin error: ", err) return true } p.Bot.SendMessage(message.Channel, "I've already got that one.") diff --git a/plugins/counter.go b/plugins/counter.go index 11afc62..e43b9a6 100644 --- a/plugins/counter.go +++ b/plugins/counter.go @@ -101,7 +101,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool { subject := strings.ToLower(nick) itemName := strings.ToLower(parts[1]) - if _, err := p.DB.Exec(`delete from counters where nick = ? and item = ?`, subject, itemName); err != nil { + if _, err := p.DB.Exec(`delete from counter where nick = ? and item = ?`, subject, itemName); err != nil { p.Bot.SendMessage(channel, "Something went wrong removing that counter;") return true } @@ -126,7 +126,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool { } var item Item - err := p.DB.QueryRow(`select nick, item, count from counters + err := p.DB.QueryRow(`select nick, item, count from counter where nick = ? and item = ?`, subject, itemName).Scan( &item.Nick, &item.Item, &item.Count, ) @@ -184,7 +184,7 @@ func (p *CounterPlugin) update(subject, itemName string, delta int) Item { switch { case err == sql.ErrNoRows: // insert it - res, err := p.DB.Exec(`insert into counter (nick, item, count) + res, err := p.DB.Exec(`insert into counter (nick, item, count) values (?, ?, ?)`, subject, itemName, delta) if err != nil { log.Println(err)