2020-01-31 19:58:17 -07:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-13 15:05:08 -06:00
|
|
|
"regexp"
|
2020-01-31 19:58:17 -07:00
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HighFive high fives!
|
|
|
|
type HighFive struct {
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:56:19 -07:00
|
|
|
func rightFive() string {
|
|
|
|
return "o/"
|
|
|
|
}
|
|
|
|
|
|
|
|
func leftFive() string {
|
2020-03-13 15:05:08 -06:00
|
|
|
return `\\o`
|
2020-02-11 07:56:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Descr describes this plugin
|
|
|
|
func (h *HighFive) Descr() string {
|
|
|
|
return "Everyone loves highfives."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re are the regexes that high five uses
|
|
|
|
func (h *HighFive) Re() string {
|
|
|
|
return fmt.Sprintf("%s|%s", rightFive(), leftFive())
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:04:04 -07:00
|
|
|
// SetStore we don't need a store here.
|
2020-05-13 17:05:01 -06:00
|
|
|
func (h *HighFive) SetStore(_ PluginStore) {}
|
2020-02-05 22:04:04 -07:00
|
|
|
|
2020-02-10 17:10:57 -07:00
|
|
|
// Match determines if we should bother giving a high five
|
|
|
|
func (h *HighFive) Match(user, msg string) bool {
|
2020-03-13 15:05:08 -06:00
|
|
|
re := regexp.MustCompile(h.Re())
|
|
|
|
return ToMe(user, msg) && re.MatchString(msg)
|
2020-02-10 17:10:57 -07:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:15:25 -06:00
|
|
|
func (h *HighFive) Process(from, post string) string {
|
|
|
|
s := NameRE.ReplaceAllString(from, "$1")
|
2020-02-10 17:10:57 -07:00
|
|
|
|
2020-03-13 15:05:08 -06:00
|
|
|
rm := regexp.MustCompile(rightFive())
|
|
|
|
lm := regexp.MustCompile(leftFive())
|
|
|
|
|
|
|
|
if rm.MatchString(post) {
|
2021-04-01 16:15:25 -06:00
|
|
|
return fmt.Sprintf("\\o %s", s)
|
2020-02-10 17:10:57 -07:00
|
|
|
}
|
2021-04-01 16:15:25 -06:00
|
|
|
|
2020-03-13 15:05:08 -06:00
|
|
|
if lm.MatchString(post) {
|
2021-04-01 16:15:25 -06:00
|
|
|
return fmt.Sprintf("%s o/", s)
|
2020-01-31 19:58:17 -07:00
|
|
|
}
|
2021-04-01 16:15:25 -06:00
|
|
|
|
|
|
|
return "\\o/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// RespondText to high five events
|
|
|
|
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
|
|
|
|
return SendText(c, ev.RoomID, h.Process(ev.Sender, post))
|
2020-01-31 19:58:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the HighFive plugin
|
|
|
|
func (h *HighFive) Name() string {
|
|
|
|
return "HighFive"
|
|
|
|
}
|