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
4 changed files with 55 additions and 7 deletions

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

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "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)
} }