Merge pull request #2 from donuts-are-good/addSalute
Adding o7 "Salute"
This commit is contained in:
commit
ea4310d50f
@ -24,6 +24,7 @@ A [Matrix](https://matrix.org) chat bot.
|
|||||||
|PGP|`(?i)^pgp: (.+@.+\..+\|[a-f0-9]+)$`|Queries keys.openpgp.org|
|
|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.|
|
|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.|
|
|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.|
|
|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.|
|
|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.|
|
|Thanks|`(?i)^thank you\|thank you$\|^thanks\|thanks$\|^ty\|ty$`|Bots should be respectful. Respond to thanks.|
|
||||||
|
58
plugins/salute.go
Normal file
58
plugins/salute.go
Normal file
@ -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"
|
||||||
|
}
|
20
plugins/salute_test.go
Normal file
20
plugins/salute_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user