Compare commits
3 Commits
modernc-sq
...
main
Author | SHA1 | Date | |
---|---|---|---|
8b2338188b | |||
45369b890e | |||
3d5d18ce44 |
33
handlers/ignore/ignore.go
Normal file
33
handlers/ignore/ignore.go
Normal 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)
|
||||||
|
}
|
@@ -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 = `
|
||||||
|
@@ -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)
|
||||||
|
|
||||||
|
16
lysenko.go
16
lysenko.go
@@ -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("edb.AddQuoteHandler{QuoteDB: db})
|
mux.HandleHears(ignore.NewHears("edb.AddQuoteHandler{QuoteDB: db}, ignoresList...))
|
||||||
mux.HandleHears(deadline.NewHears("edb.LastQuoteHandler{QuoteDB: db}))
|
mux.HandleHears(ignore.NewHears(deadline.NewHears("edb.LastQuoteHandler{QuoteDB: db}), ignoresList...))
|
||||||
mux.HandleHears(deadline.NewHears("edb.FindQuoteHandler{QuoteDB: db}))
|
mux.HandleHears(ignore.NewHears(deadline.NewHears("edb.FindQuoteHandler{QuoteDB: db}), ignoresList...))
|
||||||
mux.HandleHears(deadline.NewHears("edb.RandQuoteHandler{QuoteDB: db}))
|
mux.HandleHears(ignore.NewHears(deadline.NewHears("edb.RandQuoteHandler{QuoteDB: db}), ignoresList...))
|
||||||
mux.HandleHears(deadline.NewHears("edb.QuoteHandler{QuoteDB: db}))
|
mux.HandleHears(ignore.NewHears(deadline.NewHears("edb.QuoteHandler{QuoteDB: db}), ignoresList...))
|
||||||
|
|
||||||
hugot.ListenAndServe(ctx, mux, adapter)
|
hugot.ListenAndServe(ctx, mux, adapter)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user