From e318cb2d62e67d5b12f3eb3af7b46e559388bff4 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Mon, 3 Feb 2020 16:19:04 -0700 Subject: [PATCH] add openbsd man page shortcut --- plugins/openbsd_man.go | 39 +++++++++++++++++++++++++++++++++++++++ plugins/plugins.go | 1 + 2 files changed, 40 insertions(+) create mode 100644 plugins/openbsd_man.go diff --git a/plugins/openbsd_man.go b/plugins/openbsd_man.go new file mode 100644 index 0000000..c9e4a5d --- /dev/null +++ b/plugins/openbsd_man.go @@ -0,0 +1,39 @@ +package plugins + +import ( + "fmt" + "log" + "regexp" + + "github.com/matrix-org/gomatrix" +) + +// OpenBSDMan responds to beer requests +type OpenBSDMan struct { +} + +func (h *OpenBSDMan) fix(msg string) string { + re := regexp.MustCompile(`(?i)^man: `) + return re.ReplaceAllString(msg, "$1") +} + +func (h *OpenBSDMan) match(msg string) bool { + re := regexp.MustCompile(`(?i)^man: `) + return re.MatchString(msg) +} + +// 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 { + return "OpenBSDMan" +} diff --git a/plugins/plugins.go b/plugins/plugins.go index 2fa07bc..34b6511 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -52,6 +52,7 @@ var Plugs = Plugins{ &HighFive{}, &Hi{}, &LoveYou{}, + &OpenBSDMan{}, &Source{}, &Version{}, }