From a34afa97adf076ef6cb7c335d4343b150885fbe3 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 29 Mar 2016 12:34:04 -0400 Subject: [PATCH] Switch web query to a regex --- bot/bot.go | 18 +++++++++++++++--- plugins/fact/factoid.go | 8 ++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index c1c8832..cf5613d 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -7,13 +7,13 @@ import ( "html/template" "log" "net/http" + "regexp" "strings" "time" "github.com/jmoiron/sqlx" + "github.com/mattn/go-sqlite3" "github.com/velour/catbase/config" - - _ "github.com/mattn/go-sqlite3" ) // Bot type provides storage for bot-wide information, configs, and database connections @@ -97,9 +97,21 @@ type Variable struct { Variable, Value string } +func init() { + regex := func(re, s string) (bool, error) { + return regexp.MatchString(re, s) + } + sql.Register("sqlite3_custom", + &sqlite3.SQLiteDriver{ + ConnectHook: func(conn *sqlite3.SQLiteConn) error { + return conn.RegisterFunc("REGEXP", regex, true) + }, + }) +} + // NewBot creates a Bot for a given connection and set of handlers. func NewBot(config *config.Config, connector Connector) *Bot { - sqlDB, err := sqlx.Open("sqlite3", config.DB.File) + sqlDB, err := sqlx.Open("sqlite3_custom", config.DB.File) if err != nil { log.Fatal(err) } diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index 1f9d90c..e7e10e3 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -104,8 +104,12 @@ func getFacts(db *sqlx.DB, fact string) ([]*factoid, error) { accessed, count from factoid - where fact like ?;`, - fmt.Sprintf("%%%s%%", fact)) + where fact regexp ?;`, + fact) + if err != nil { + log.Printf("Error regexping for facts: %s", err) + return nil, err + } for rows.Next() { var f factoid var tmpCreated int64