move match check a bit

This commit is contained in:
Aaron Bieber 2020-02-10 17:10:57 -07:00
parent 5d73848661
commit 02b75ee7b0
12 changed files with 104 additions and 108 deletions

View File

@ -152,8 +152,10 @@ func main() {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
p.SetStore(store)
p.RespondText(cli, ev, username, post)
if p.Match(username, post) {
p.SetStore(store)
p.RespondText(cli, ev, username, post)
}
}
}
}

View File

@ -83,7 +83,8 @@ func (h *Beer) fix(msg string) string {
return re.ReplaceAllString(msg, "$1")
}
func (h *Beer) match(msg string) bool {
// Match determines if we should call the response for Beer
func (h *Beer) Match(user, msg string) bool {
re := regexp.MustCompile(h.re())
return re.MatchString(msg)
}
@ -138,23 +139,21 @@ func (h *Beer) SetStore(s PluginStore) {}
// RespondText to looking up of beer requests
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if h.match(post) {
beer := h.fix(post)
if beer != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
brr, err := h.get(beer)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
}
beer := h.fix(post)
if beer != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
brr, err := h.get(beer)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
}
switch {
case brr.Nhits == 0:
SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
case brr.Nhits == 1:
SendText(c, ev.RoomID, h.pretty(*brr, false))
case brr.Nhits > 1:
SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", brr.Nhits, h.pretty(*brr, true)))
}
switch {
case brr.Nhits == 0:
SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
case brr.Nhits == 1:
SendText(c, ev.RoomID, h.pretty(*brr, false))
case brr.Nhits > 1:
SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", brr.Nhits, h.pretty(*brr, true)))
}
}
}

View File

@ -13,7 +13,8 @@ import (
type BotSnack struct {
}
func (h *BotSnack) match(msg string) bool {
// Match determines if we should execute BotSnack
func (h *BotSnack) Match(user, msg string) bool {
re := regexp.MustCompile(`(?i)botsnack`)
return re.MatchString(msg)
}
@ -38,10 +39,8 @@ func (h *BotSnack) SetStore(s PluginStore) {}
func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, h.resp())
}
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, h.resp())
}
}

View File

@ -12,9 +12,10 @@ import (
type Hi struct {
}
func (h *Hi) match(msg string) bool {
// Match determines if we are highfiving
func (h *Hi) Match(user, msg string) bool {
re := regexp.MustCompile(`(?i)^hi|hi$`)
return re.MatchString(msg)
return re.MatchString(msg) && ToMe(user, msg)
}
// SetStore we don't need a store here
@ -22,14 +23,10 @@ func (h *Hi) SetStore(s PluginStore) {}
// RespondText to hi events
func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
}
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
// Name hi

View File

@ -15,19 +15,22 @@ type HighFive struct {
// SetStore we don't need a store here.
func (h *HighFive) SetStore(s PluginStore) {}
// Match determines if we should bother giving a high five
func (h *HighFive) Match(user, msg string) bool {
return ToMe(user, msg)
}
// RespondText to high five events
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if strings.Contains(post, "o/") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
}
if strings.Contains(post, "\\o") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
}
if strings.Contains(post, "o/") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
}
if strings.Contains(post, "\\o") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
}
}

View File

@ -13,9 +13,10 @@ import (
type LoveYou struct {
}
func (h *LoveYou) match(msg string) bool {
// Match checks for 'i love you' and a reference to the bot name
func (h *LoveYou) Match(user, msg string) bool {
re := regexp.MustCompile(`(?i)i love you`)
return re.MatchString(msg)
return re.MatchString(msg) && ToMe(user, msg)
}
func (h *LoveYou) resp() string {
@ -37,13 +38,8 @@ func (h *LoveYou) SetStore(s PluginStore) {}
// RespondText to love events
func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, h.resp())
}
}
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, h.resp())
}
// Name i love you

View File

@ -32,7 +32,8 @@ func (h *OpenBSDMan) fix(msg string) string {
return resp
}
func (h *OpenBSDMan) match(msg string) bool {
// Match checks for our man page re
func (h *OpenBSDMan) Match(user, msg string) bool {
re := regexp.MustCompile(h.re())
return re.MatchString(msg)
}
@ -42,12 +43,10 @@ func (h *OpenBSDMan) SetStore(s PluginStore) {}
// RespondText sends back a man page.
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if h.match(post) {
page := h.fix(post)
if page != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("https://man.openbsd.org/%s", page))
}
page := h.fix(post)
if page != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("https://man.openbsd.org/%s", page))
}
}

View File

@ -7,18 +7,27 @@ import (
"github.com/matrix-org/gomatrix"
)
// PluginStore matches MCStore so that the main store can be used by plugins.
// PluginStore matches MCStore. This allows the main store to be used by
// plugins.
type PluginStore interface {
Set(key, values string)
Get(key string) (string, error)
}
// Plugin defines the functions a plugin must implement to be used by
// Plugin defines the interface a plugin must implement to be used by
// mcchunkie.
type Plugin interface {
//Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
// RespondText responds to a "m.text" event
RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string)
// Name should return the human readable name of the bot
Name() string
// Match determines if the plugin's main Respond function should be
// called
Match(user, message string) bool
// SetStore exposes the top level MCStore to a plugin
SetStore(s PluginStore)
}
@ -28,7 +37,8 @@ var NameRE = regexp.MustCompile(`@(.+):.+$`)
// ToMe returns true of the message pertains to the bot
func ToMe(user, message string) bool {
return strings.Contains(message, user)
u := NameRE.ReplaceAllString(user, "$1")
return strings.Contains(message, u)
}
// SendText sends a text message to a given room. It pretends to be

View File

@ -12,9 +12,10 @@ import (
type Source struct {
}
func (h *Source) match(msg string) bool {
// Match determins if someone is asking about the source code
func (h *Source) Match(user, msg string) bool {
re := regexp.MustCompile(`(?i)where is your (source|code)`)
return re.MatchString(msg)
return re.MatchString(msg) && ToMe(user, msg)
}
// SetStore does nothing in here
@ -22,14 +23,10 @@ func (h *Source) SetStore(s PluginStore) {}
// RespondText to questions about TheSource™©®⑨
func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
}
}
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
}
// Name Source

View File

@ -13,9 +13,11 @@ import (
type Version struct {
}
func (v *Version) match(msg string) bool {
// Match checks for "version" anywhere. Might want to tighten this one down at
// some point
func (v *Version) Match(user, msg string) bool {
re := regexp.MustCompile(`(?i)version$`)
return re.MatchString(msg)
return re.MatchString(msg) && ToMe(user, msg)
}
func (v *Version) print(to string) string {
@ -23,18 +25,14 @@ func (v *Version) print(to string) string {
}
// SetStore does nothing in here
func (h *Version) SetStore(s PluginStore) {}
func (v *Version) SetStore(s PluginStore) {}
// RespondText to version events
func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if v.match(post) {
log.Printf("%s: responding to '%s'", v.Name(), ev.Sender)
SendText(c, ev.RoomID, v.print(s))
}
}
log.Printf("%s: responding to '%s'", v.Name(), ev.Sender)
SendText(c, ev.RoomID, v.print(s))
}
// Name Version

View File

@ -146,7 +146,8 @@ func (h *Weather) re() string {
return `(?i)^weather: (\d+)$`
}
func (h *Weather) match(msg string) bool {
// Match checks for "weather: " messages
func (h *Weather) Match(user, msg string) bool {
re := regexp.MustCompile(h.re())
return re.MatchString(msg)
}
@ -158,23 +159,21 @@ func (h *Weather) fix(msg string) string {
// RespondText to looking up of weather lookup requests
func (h *Weather) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if h.match(post) {
weather := h.fix(post)
if weather != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
wd, err := h.get(weather)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look up the weather. %s", ev.Sender, err))
}
SendText(c, ev.RoomID,
fmt.Sprintf("%s: %s (%s) Humidity: %s%%, %s",
wd.Name,
wd.f(),
wd.c(),
wd.humidity(),
wd.conditions(),
))
weather := h.fix(post)
if weather != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
wd, err := h.get(weather)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look up the weather. %s", ev.Sender, err))
}
SendText(c, ev.RoomID,
fmt.Sprintf("%s: %s (%s) Humidity: %s%%, %s",
wd.Name,
wd.f(),
wd.c(),
wd.humidity(),
wd.conditions(),
))
}
}

View File

@ -12,9 +12,10 @@ import (
type Wb struct {
}
func (h *Wb) match(msg string) bool {
// Match determins if we are welcomed back
func (h *Wb) Match(user, msg string) bool {
re := regexp.MustCompile(`(?i)^welcome back|welcome back$|^wb|wb$`)
return re.MatchString(msg)
return re.MatchString(msg) && ToMe(user, msg)
}
// SetStore we don't need a store here
@ -22,14 +23,10 @@ func (h *Wb) SetStore(s PluginStore) {}
// RespondText to welcome back events
func (h *Wb) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("thanks %s!", s))
}
}
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("thanks %s!", s))
}
// Name Wb