2020-02-23 14:35:07 -07:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Snap responds to OpenBSD snapshot checks
|
|
|
|
type Snap struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Descr describes this plugin
|
|
|
|
func (p *Snap) Descr() string {
|
|
|
|
return "checks the current build date of OpenBSD snapshots."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re returns the federation check matching string
|
|
|
|
func (p *Snap) Re() string {
|
|
|
|
return `(?i)^snap:$`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match determines if we should call the response for Snap
|
2020-05-13 16:53:31 -06:00
|
|
|
func (p *Snap) Match(_, msg string) bool {
|
2020-02-23 14:35:07 -07:00
|
|
|
re := regexp.MustCompile(p.Re())
|
|
|
|
return re.MatchString(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStore we don't need a store here.
|
2020-05-13 16:53:31 -06:00
|
|
|
func (p *Snap) SetStore(_ PluginStore) {}
|
2020-02-23 14:35:07 -07:00
|
|
|
|
|
|
|
// RespondText to looking up of federation check requests
|
2020-05-13 16:53:31 -06:00
|
|
|
func (p *Snap) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, _ string) error {
|
2020-02-23 14:35:07 -07:00
|
|
|
resp, err := http.Get("https://ftp.usa.openbsd.org/pub/OpenBSD/snapshots/amd64/BUILDINFO")
|
|
|
|
if err != nil {
|
2020-05-13 16:53:31 -06:00
|
|
|
return SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
|
2020-02-23 14:35:07 -07:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-05-13 16:53:31 -06:00
|
|
|
return SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
|
2020-02-23 14:35:07 -07:00
|
|
|
}
|
|
|
|
|
2020-05-13 16:53:31 -06:00
|
|
|
return SendText(c, ev.RoomID, string(body))
|
2020-02-23 14:35:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name Snap!
|
|
|
|
func (p *Snap) Name() string {
|
|
|
|
return "Snap"
|
|
|
|
}
|