move match check a bit
This commit is contained in:
parent
5d73848661
commit
02b75ee7b0
2
main.go
2
main.go
@ -152,11 +152,13 @@ func main() {
|
||||
if mtype, ok := ev.MessageType(); ok {
|
||||
switch mtype {
|
||||
case "m.text":
|
||||
if p.Match(username, post) {
|
||||
p.SetStore(store)
|
||||
p.RespondText(cli, ev, username, post)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if avatar != "" {
|
||||
|
@ -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,7 +139,6 @@ 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)
|
||||
@ -157,7 +157,6 @@ func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post st
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name Beer!
|
||||
func (h *Beer) Name() string {
|
||||
|
@ -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,12 +39,10 @@ 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name BotSnack
|
||||
func (h *BotSnack) Name() string {
|
||||
|
@ -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,15 +23,11 @@ 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name hi
|
||||
func (h *Hi) Name() string {
|
||||
|
@ -15,11 +15,15 @@ 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))
|
||||
@ -29,7 +33,6 @@ func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, pos
|
||||
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name returns the name of the HighFive plugin
|
||||
func (h *HighFive) Name() string {
|
||||
|
@ -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,14 +38,9 @@ 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name i love you
|
||||
func (h *LoveYou) Name() string {
|
||||
|
@ -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,14 +43,12 @@ 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name OpenBSDMan!
|
||||
func (h *OpenBSDMan) Name() string {
|
||||
|
@ -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
|
||||
|
@ -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,15 +23,11 @@ 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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name Source
|
||||
func (h *Source) Name() string {
|
||||
|
@ -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,19 +25,15 @@ 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name Version
|
||||
func (v *Version) Name() string {
|
||||
|
@ -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,7 +159,6 @@ 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)
|
||||
@ -176,7 +176,6 @@ func (h *Weather) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name Weather!
|
||||
func (h *Weather) Name() string {
|
||||
|
@ -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,15 +23,11 @@ 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name Wb
|
||||
func (h *Wb) Name() string {
|
||||
|
Loading…
Reference in New Issue
Block a user