Compare commits

3 Commits

Author SHA1 Message Date
8b2338188b Fix randquote for terms with _ or % 2021-02-25 14:23:34 +00:00
45369b890e Fix compilation 2021-02-13 01:26:40 +00:00
3d5d18ce44 Allow people to be ignored by nick 2021-02-13 01:26:08 +00:00
6 changed files with 58 additions and 31 deletions

4
go.mod
View File

@@ -8,7 +8,8 @@ require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/mock v0.0.0-20160127222235-bd3c8e81be01 // indirect github.com/golang/mock v0.0.0-20160127222235-bd3c8e81be01 // indirect
github.com/golang/protobuf v0.0.0-20161012205335-98fa35717058 // indirect github.com/golang/protobuf v0.0.0-20161012205335-98fa35717058 // indirect
github.com/mattn/go-shellwords v1.0.10 // indirect github.com/mattn/go-shellwords v1.0.1-0.20160315040826-525bedee691b // indirect
github.com/mattn/go-sqlite3 v1.1.1-0.20160514122348-38ee283dabf1
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/nlopes/slack v0.0.2-0.20161003122758-a1a63712460a // indirect github.com/nlopes/slack v0.0.2-0.20161003122758-a1a63712460a // indirect
github.com/prometheus/client_golang v0.8.1-0.20160916180340-5636dc67ae77 // indirect github.com/prometheus/client_golang v0.8.1-0.20160916180340-5636dc67ae77 // indirect
@@ -18,5 +19,4 @@ require (
github.com/stretchr/testify v1.4.0 // indirect github.com/stretchr/testify v1.4.0 // indirect
github.com/tcolgate/hugot v0.0.0-20160913191818-ee77aa90e26c github.com/tcolgate/hugot v0.0.0-20160913191818-ee77aa90e26c
golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a // indirect golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a // indirect
modernc.org/sqlite v1.4.0
) )

21
go.sum
View File

@@ -10,12 +10,8 @@ github.com/golang/mock v0.0.0-20160127222235-bd3c8e81be01 h1:rtnkE5nwSSDcZrbt4gc
github.com/golang/mock v0.0.0-20160127222235-bd3c8e81be01/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v0.0.0-20160127222235-bd3c8e81be01/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v0.0.0-20161012205335-98fa35717058 h1:xVBSuG3IwzD01VUAgfPihkonFpb8hX+WmT5PLrKQZFw= github.com/golang/protobuf v0.0.0-20161012205335-98fa35717058 h1:xVBSuG3IwzD01VUAgfPihkonFpb8hX+WmT5PLrKQZFw=
github.com/golang/protobuf v0.0.0-20161012205335-98fa35717058/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v0.0.0-20161012205335-98fa35717058/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-shellwords v1.0.1-0.20160315040826-525bedee691b h1:Ozgf2cEfAGiCi6mWGxZDmTKOSyXibCwEFtCxOo9znsM= github.com/mattn/go-shellwords v1.0.1-0.20160315040826-525bedee691b h1:Ozgf2cEfAGiCi6mWGxZDmTKOSyXibCwEFtCxOo9znsM=
github.com/mattn/go-shellwords v1.0.1-0.20160315040826-525bedee691b/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o= github.com/mattn/go-shellwords v1.0.1-0.20160315040826-525bedee691b/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/mattn/go-shellwords v1.0.10 h1:Y7Xqm8piKOO3v10Thp7Z36h4FYFjt5xB//6XvOrs2Gw=
github.com/mattn/go-shellwords v1.0.10/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mattn/go-sqlite3 v1.1.1-0.20160514122348-38ee283dabf1 h1:h808yN0vzMNyekOZGLRM3OiB8BBLgNqE180rKydRh8U= github.com/mattn/go-sqlite3 v1.1.1-0.20160514122348-38ee283dabf1 h1:h808yN0vzMNyekOZGLRM3OiB8BBLgNqE180rKydRh8U=
github.com/mattn/go-sqlite3 v1.1.1-0.20160514122348-38ee283dabf1/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.1.1-0.20160514122348-38ee283dabf1/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
@@ -32,8 +28,6 @@ github.com/prometheus/common v0.0.0-20161002210234-85637ea67b04 h1:ScZ/BRzCsrcF/
github.com/prometheus/common v0.0.0-20161002210234-85637ea67b04/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.0.0-20161002210234-85637ea67b04/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20160411190841-abf152e5f3e9 h1:ex32PG6WhE5zviWS08vcXTwX2IkaH9zpeYZZvrmj3/U= github.com/prometheus/procfs v0.0.0-20160411190841-abf152e5f3e9 h1:ex32PG6WhE5zviWS08vcXTwX2IkaH9zpeYZZvrmj3/U=
github.com/prometheus/procfs v0.0.0-20160411190841-abf152e5f3e9/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20160411190841-abf152e5f3e9/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@@ -41,22 +35,7 @@ github.com/tcolgate/hugot v0.0.0-20160913191818-ee77aa90e26c h1:U50Vqdn7g4Z6/cD6
github.com/tcolgate/hugot v0.0.0-20160913191818-ee77aa90e26c/go.mod h1:HXNEaTfN+jlbEF7qmGiqDoSCjc+dnezVyway1fZRgfE= github.com/tcolgate/hugot v0.0.0-20160913191818-ee77aa90e26c/go.mod h1:HXNEaTfN+jlbEF7qmGiqDoSCjc+dnezVyway1fZRgfE=
golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a h1:YEFEcqrj8fWeC0px2Ha5IrK20xodii3wn+N+jzuFRKQ= golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a h1:YEFEcqrj8fWeC0px2Ha5IrK20xodii3wn+N+jzuFRKQ=
golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 h1:DvY3Zkh7KabQE/kfzMvYvKirSiguP9Q/veMtkYyf0o8=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
modernc.org/httpfs v1.0.0 h1:LtuKNg6JMiaBKVQHKd6Phhvk+2GFp+pUcmDQgRjrds0=
modernc.org/httpfs v1.0.0/go.mod h1:BSkfoMUcahSijQD5J/Vu4UMOxzmEf5SNRwyXC4PJBEw=
modernc.org/libc v1.0.0 h1:MgQgjhJDgugN7makTrP2K503x+vKBy0EIv1o8uSuG0k=
modernc.org/libc v1.0.0/go.mod h1:iulSAj8W71uuiT8V3cOzJfRvrhNMz2kawtuJy18EEE4=
modernc.org/mathutil v1.1.1 h1:FeylZSVX8S+58VsyJlkEj2bcpdytmp9MmDKZkKx8OIE=
modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.0.0 h1:Tm1p6vBp/U/SGR9/EeFhMvGzaVpUWeePopZhhIpW2YE=
modernc.org/memory v1.0.0/go.mod h1:TXr4iJDvK3g0hW+sV+Kohu7BoeHfqw7QEFZWkBExdZc=
modernc.org/sqlite v1.4.0 h1:rpQmt8iwSzPF9DuPRJ5EvesY+FLUohuaMTW1TJ9mtvY=
modernc.org/sqlite v1.4.0/go.mod h1:XnAfpewk8pgSsRFCaAMiAI4ljY/XHANDdf0Jxc0h5R4=
modernc.org/tcl v1.1.0 h1:rOb4Dtcf0aidHBu8Ctz3FkW+PQpgCrChEIQ9+LUPst0=
modernc.org/tcl v1.1.0/go.mod h1:8qvhdwj80wWqMugFAW2sduRIsdWBKOmxu+VwCXfE1X4=

33
handlers/ignore/ignore.go Normal file
View File

@@ -0,0 +1,33 @@
package ignore
import (
"context"
"regexp"
"github.com/tcolgate/hugot"
)
type HearsIgnore struct {
hugot.HearsHandler
Ignore []*regexp.Regexp
}
func NewHears(next hugot.HearsHandler, who ...string) hugot.HearsHandler {
var ignoreCompiled []*regexp.Regexp
for _, str := range who {
ignoreCompiled = append(ignoreCompiled, regexp.MustCompile(str))
}
return &HearsIgnore{HearsHandler: next, Ignore: ignoreCompiled}
}
func (i *HearsIgnore) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, matches [][]string) {
for _, re := range i.Ignore {
if re.MatchString(m.From) {
return
}
}
i.HearsHandler.Heard(ctx, w, m, matches)
}

View File

@@ -41,7 +41,7 @@ var (
findQuoteById = `SELECT ` + quoteCols + ` FROM quotes WHERE id = ? LIMIT 1` findQuoteById = `SELECT ` + quoteCols + ` FROM quotes WHERE id = ? LIMIT 1`
findQuoteByQuoteId = `SELECT ` + quoteCols + ` FROM quotes WHERE quote_id = ? AND channel = ? LIMIT 1` findQuoteByQuoteId = `SELECT ` + quoteCols + ` FROM quotes WHERE quote_id = ? AND channel = ? LIMIT 1`
findLastQuote = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? ORDER BY quote_id DESC LIMIT 1` findLastQuote = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? ORDER BY quote_id DESC LIMIT 1`
findQuotesByTerm = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? AND data LIKE ? ORDER BY quote_id DESC` findQuotesByTerm = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? AND data LIKE ? ESCAPE '\' ORDER BY quote_id DESC`
findRandomQuote = `SELECT ` + quoteCols + ` FROM quotes WHERE deleted_at IS NULL AND channel = ? ORDER BY RANDOM() LIMIT 1` findRandomQuote = `SELECT ` + quoteCols + ` FROM quotes WHERE deleted_at IS NULL AND channel = ? ORDER BY RANDOM() LIMIT 1`
insertQuote = ` insertQuote = `

View File

@@ -3,8 +3,9 @@ package quotedb
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
_ "modernc.org/sqlite" _ "github.com/mattn/go-sqlite3"
"log" "log"
"strings"
"time" "time"
) )
@@ -85,8 +86,16 @@ func (q *QuoteDB) FindLastQuote(channel string) (*Quote, error) {
return quote, err return quote, err
} }
func escapeLike(text string) string {
text = strings.ReplaceAll(text, `\`, ``)
text = strings.ReplaceAll(text, `%`, `\%`)
text = strings.ReplaceAll(text, `_`, `\_`)
return text
}
func (q QuoteDB) FindQuotesByString(search string, channel string) ([]*Quote, error) { func (q QuoteDB) FindQuotesByString(search string, channel string) ([]*Quote, error) {
term := fmt.Sprintf("%%%s%%", search) term := fmt.Sprintf("%%%s%%", escapeLike(search))
rows, err := q.DB.Query(findQuotesByTerm, channel, term) rows, err := q.DB.Query(findQuotesByTerm, channel, term)

View File

@@ -16,11 +16,13 @@ import (
"ur.gs/lysenko/adapters" "ur.gs/lysenko/adapters"
"ur.gs/lysenko/handlers/deadline" "ur.gs/lysenko/handlers/deadline"
"ur.gs/lysenko/handlers/ignore"
"ur.gs/lysenko/handlers/quotedb" "ur.gs/lysenko/handlers/quotedb"
) )
var ( var (
channels = flag.String("channels", "##testing,##test", "Channels to join (separated by comma)") channels = flag.String("channels", "##testing,##test", "Channels to join (separated by comma)")
ignores = flag.String("ignore", "", "Nicks to ignore (separated by comma)")
cooldown = flag.Int("cooldown", 120, "Command rate-limit, in seconds") cooldown = flag.Int("cooldown", 120, "Command rate-limit, in seconds")
host = flag.String("host", "chat.freenode.net", "Server host[:port]") host = flag.String("host", "chat.freenode.net", "Server host[:port]")
ident = flag.String("ident", "lysenko", "Lysenko Bot") ident = flag.String("ident", "lysenko", "Lysenko Bot")
@@ -59,6 +61,10 @@ func ircAdapter() hugot.Adapter {
func main() { func main() {
flag.Parse() flag.Parse()
var ignoresList []string
if *ignores != "" {
ignoresList = strings.Split(*ignores, ",")
}
adapter := adapters.NewPrefixAdapter(ircAdapter(), "!") adapter := adapters.NewPrefixAdapter(ircAdapter(), "!")
adapter.PrivateHelpOnly = true adapter.PrivateHelpOnly = true
@@ -75,11 +81,11 @@ func main() {
deadline.Cooldown = time.Second * time.Duration(*cooldown) deadline.Cooldown = time.Second * time.Duration(*cooldown)
mux := hugot.DefaultMux mux := hugot.DefaultMux
mux.HandleHears(&quotedb.AddQuoteHandler{QuoteDB: db}) mux.HandleHears(ignore.NewHears(&quotedb.AddQuoteHandler{QuoteDB: db}, ignoresList...))
mux.HandleHears(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db})) mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db}), ignoresList...))
mux.HandleHears(deadline.NewHears(&quotedb.FindQuoteHandler{QuoteDB: db})) mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.FindQuoteHandler{QuoteDB: db}), ignoresList...))
mux.HandleHears(deadline.NewHears(&quotedb.RandQuoteHandler{QuoteDB: db})) mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.RandQuoteHandler{QuoteDB: db}), ignoresList...))
mux.HandleHears(deadline.NewHears(&quotedb.QuoteHandler{QuoteDB: db})) mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.QuoteHandler{QuoteDB: db}), ignoresList...))
hugot.ListenAndServe(ctx, mux, adapter) hugot.ListenAndServe(ctx, mux, adapter)
} }