From e2327e9980518ceff8b17f49076c5b5a20829793 Mon Sep 17 00:00:00 2001 From: Aisha Tammy Date: Fri, 11 Mar 2022 14:38:28 -0500 Subject: [PATCH] add banana stabber + emote sending Signed-off-by: Aisha Tammy --- plugins/banana_stab.go | 57 ++++++++++++++++++++++++++++++++++++++++++ plugins/plugins.go | 21 ++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 plugins/banana_stab.go diff --git a/plugins/banana_stab.go b/plugins/banana_stab.go new file mode 100644 index 0000000..82228ad --- /dev/null +++ b/plugins/banana_stab.go @@ -0,0 +1,57 @@ +package plugins + +import ( + "fmt" + "regexp" + + "github.com/matrix-org/gomatrix" +) + +type BananaStab struct { +} + +// Descr describes this plugin +func (h *BananaStab) Descr() string { + return "Stab someone with some bananas." +} + +// Re matches our stabbing format +func (h *BananaStab) Re() string { + return `(?i)^stab: (.+)$` +} + +func (h *BananaStab) fix(msg string) string { + re := regexp.MustCompile(h.Re()) + stabee := re.ReplaceAllString(msg, "$1") + return stabee +} + +// Match checks for our stabee person +func (h *BananaStab) Match(_, msg string) bool { + re := regexp.MustCompile(h.Re()) + return re.MatchString(msg) +} + +// SetStore does nothing in BananaStab +func (h *BananaStab) SetStore(_ PluginStore) {} + +func (h *BananaStab) Process(from, post string) string { + stabee := h.fix(post) + stabtxt := "..." + if stabee != "" { + stabtxt = fmt.Sprintf("stabs %s with the fury of a thousand radioactive bananas", stabee) + } + //jsonmsg := "{ \"body\": \"" + stabtxt + "\", \"type\": \"m.emote\"}" + return stabtxt +} + +// RespondText stabs an unsuspecting person +func (h *BananaStab) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error { + err := SendEmote(c, ev.RoomID, h.Process(ev.Sender, post)) + return err +} + +// Name BananaStab! +func (h *BananaStab) Name() string { + return "BananaStab" +} diff --git a/plugins/plugins.go b/plugins/plugins.go index 77c5d15..473f702 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -139,6 +139,26 @@ func SendText(c *gomatrix.Client, roomID, message string) error { return nil } +// SendEmote sends an emote to a given room. It pretends to be +// "typing" by calling UserTyping for the caller. +func SendEmote(c *gomatrix.Client, roomID, message string) error { + _, err := c.UserTyping(roomID, true, 3) + if err != nil { + return err + } + + _, err = c.SendMessageEvent(roomID, "m.room.message", gomatrix.GetHTMLMessage("m.emote", message)) + if err != nil { + return err + } + + _, err = c.UserTyping(roomID, false, 0) + if err != nil { + return err + } + return nil +} + // SendHTML sends an html message to a given room. It pretends to be // "typing" by calling UserTyping for the caller. func SendHTML(c *gomatrix.Client, roomID, message string) error { @@ -262,6 +282,7 @@ type Plugins []Plugin // Plugs defines the "enabled" plugins. var Plugs = Plugins{ + &BananaStab{}, &Beat{}, &Beer{}, &BotSnack{},