2020-02-03 16:19:04 -07:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpenBSDMan responds to beer requests
|
|
|
|
type OpenBSDMan struct {
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:56:19 -07:00
|
|
|
// Descr describes this plugin
|
|
|
|
func (h *OpenBSDMan) Descr() string {
|
|
|
|
return "Produces a link to man.openbsd.org."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re matches our man format
|
|
|
|
func (h *OpenBSDMan) Re() string {
|
2020-03-09 07:34:08 -06:00
|
|
|
return `(?i)^man: ([1-9][p]?)?\s?(.+)$`
|
2020-02-05 17:34:41 -07:00
|
|
|
}
|
|
|
|
|
2020-02-03 16:19:04 -07:00
|
|
|
func (h *OpenBSDMan) fix(msg string) string {
|
2020-02-11 07:56:19 -07:00
|
|
|
re := regexp.MustCompile(h.Re())
|
2020-02-05 07:55:39 -07:00
|
|
|
resp := ""
|
|
|
|
section := re.ReplaceAllString(msg, "$1")
|
2020-02-05 17:34:41 -07:00
|
|
|
if section == msg {
|
|
|
|
return ""
|
|
|
|
}
|
2020-02-05 07:55:39 -07:00
|
|
|
if section != "" {
|
|
|
|
resp = re.ReplaceAllString(msg, "$2.$1")
|
2020-03-09 07:34:08 -06:00
|
|
|
if matched, _ := regexp.MatchString(`3p`, resp); matched {
|
|
|
|
resp = fmt.Sprintf("man3p/%s", resp)
|
|
|
|
}
|
|
|
|
|
2020-02-05 07:55:39 -07:00
|
|
|
} else {
|
|
|
|
resp = re.ReplaceAllString(msg, "$2")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2020-02-03 16:19:04 -07:00
|
|
|
}
|
|
|
|
|
2020-02-10 17:10:57 -07:00
|
|
|
// Match checks for our man page re
|
2020-05-13 17:05:01 -06:00
|
|
|
func (h *OpenBSDMan) Match(_, msg string) bool {
|
2020-02-11 07:56:19 -07:00
|
|
|
re := regexp.MustCompile(h.Re())
|
2020-02-03 16:19:04 -07:00
|
|
|
return re.MatchString(msg)
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:04:04 -07:00
|
|
|
// SetStore does nothing in OpenBSDMan
|
2020-05-13 17:05:01 -06:00
|
|
|
func (h *OpenBSDMan) SetStore(_ PluginStore) {}
|
2020-02-05 22:04:04 -07:00
|
|
|
|
2021-04-01 16:15:25 -06:00
|
|
|
func (h *OpenBSDMan) Process(from, post string) string {
|
2020-02-10 17:10:57 -07:00
|
|
|
page := h.fix(post)
|
|
|
|
if page != "" {
|
2021-04-01 16:15:25 -06:00
|
|
|
return fmt.Sprintf("https://man.openbsd.org/%s", page)
|
2020-02-03 16:19:04 -07:00
|
|
|
}
|
2021-04-01 16:15:25 -06:00
|
|
|
return "..."
|
|
|
|
}
|
2020-05-13 16:53:31 -06:00
|
|
|
|
2021-04-01 16:15:25 -06:00
|
|
|
// RespondText sends back a man page.
|
|
|
|
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
|
|
|
|
return SendText(c, ev.RoomID, h.Process(ev.Sender, post))
|
2020-02-03 16:19:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name OpenBSDMan!
|
|
|
|
func (h *OpenBSDMan) Name() string {
|
|
|
|
return "OpenBSDMan"
|
|
|
|
}
|