From 9006558a587fde9a8ad1b4f3255ddc714dce7024 Mon Sep 17 00:00:00 2001 From: donuts-are-good <96031819+donuts-are-good@users.noreply.github.com> Date: Tue, 31 May 2022 20:40:44 -0500 Subject: [PATCH] Adding o7 "Salute" Salute ( o7 ) is the intergalactic sign for a salute. Chunks should return an o7 when saluted by a worthy member. --- README.md | 1 + plugins/salute.go | 58 ++++++++++++++++++++++++++++++++++++++++++ plugins/salute_test.go | 20 +++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 plugins/salute.go create mode 100644 plugins/salute_test.go diff --git a/README.md b/README.md index 86855ea..4f2a058 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ A [Matrix](https://matrix.org) chat bot. |PGP|`(?i)^pgp: (.+@.+\..+\|[a-f0-9]+)$`|Queries keys.openpgp.org| |Palette|`(?i)^#[a-f0-9]{6}$`|Creates an solid 56x56 image of the color specified.| |RFC|`(?i)^rfc\s?([0-9]+)$`|Produces a link to tools.ietf.org.| +|Salute|`o7`|Everyone loves salutes.| |Snap|`(?i)^snap:$`|checks the current build date of OpenBSD snapshots.| |Source|`(?i)where is your (source\|code)`|Tell people where they can find more information about myself.| |Thanks|`(?i)^thank you\|thank you$\|^thanks\|thanks$\|^ty\|ty$`|Bots should be respectful. Respond to thanks.| diff --git a/plugins/salute.go b/plugins/salute.go new file mode 100644 index 0000000..220e873 --- /dev/null +++ b/plugins/salute.go @@ -0,0 +1,58 @@ +package plugins + +import ( + "fmt" + "regexp" + + "github.com/matrix-org/gomatrix" +) + +// Salute high fives! +type Salute struct { +} + +func rightSalute() string { + return "o7" +} + +// Descr describes this plugin +func (h *Salute) Descr() string { + return "Everyone loves Salutes." +} + +// Re are the regexes that high five uses +func (h *Salute) Re() string { + return fmt.Sprintf("%s", rightSalute()) +} + +// SetStore we don't need a store here. +func (h *Salute) SetStore(_ PluginStore) {} + +// Match determines if we should bother giving a salute +func (h *Salute) Match(user, msg string) bool { + re := regexp.MustCompile(h.Re()) + return ToMe(user, msg) && re.MatchString(msg) +} + +func (h *Salute) Process(from, post string) string { + s := NameRE.ReplaceAllString(from, "$1") + + rm := regexp.MustCompile(rightSalute()) + + if rm.MatchString(post) { + return fmt.Sprintf("%s o7", s) + } + + + return "o7" +} + +// RespondText to high five events +func (h *Salute) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error { + return SendText(c, ev.RoomID, h.Process(ev.Sender, post)) +} + +// Name returns the name of the Salute plugin +func (h *Salute) Name() string { + return "Salute" +} diff --git a/plugins/salute_test.go b/plugins/salute_test.go new file mode 100644 index 0000000..689e0da --- /dev/null +++ b/plugins/salute_test.go @@ -0,0 +1,20 @@ +package plugins + +import ( + "testing" +) + +func TestSaluteMatch(t *testing.T) { + testStrings := make(map[string]bool) + testStrings["o7"] = true + testStrings[`7o`] = false + testStrings["_o"] = false + testStrings["o_"] = false + + h := &Salute{} + for msg, should := range testStrings { + if h.Match("", msg) != should { + t.Errorf("Salute expected to match %q (%t); but doesn't\n", msg, should) + } + } +}