mcchunkie/plugins/ham.go

121 lines
2.8 KiB
Go
Raw Normal View History

2020-03-04 08:33:11 -07:00
package plugins
import (
"fmt"
"regexp"
"strings"
2020-03-04 08:33:11 -07:00
"time"
"github.com/matrix-org/gomatrix"
)
// LicenseResp represents a response from http://hamdb.org/api
2020-03-04 08:33:11 -07:00
type LicenseResp struct {
Hamdb struct {
Version string `json:"version"`
Callsign struct {
Call string `json:"call"`
Class string `json:"class"`
Expires string `json:"expires"`
Status string `json:"status"`
Grid string `json:"grid"`
Lat string `json:"lat"`
Lon string `json:"lon"`
Fname string `json:"fname"`
Mi string `json:"mi"`
Name string `json:"name"`
Suffix string `json:"suffix"`
Addr1 string `json:"addr1"`
Addr2 string `json:"addr2"`
State string `json:"state"`
Zip string `json:"zip"`
Country string `json:"country"`
} `json:"callsign"`
Messages struct {
Status string `json:"status"`
} `json:"messages"`
} `json:"hamdb"`
2020-03-04 08:33:11 -07:00
}
// Ham for querying the fcc'd uls
type Ham struct{}
// Descr describes this plugin
func (h *Ham) Descr() string {
return "queries HamDB.org for a given callsign."
2020-03-04 08:33:11 -07:00
}
// Re returns the federation check matching string
func (h *Ham) Re() string {
return `(?i)^ham: (\w+)$`
}
func (h *Ham) fix(msg string) string {
re := regexp.MustCompile(h.Re())
return re.ReplaceAllString(msg, "$1")
}
// Match determines if we should call the response for Ham
2020-05-13 17:05:01 -06:00
func (h *Ham) Match(_, msg string) bool {
2020-03-04 08:33:11 -07:00
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here.
2020-05-13 17:05:01 -06:00
func (h *Ham) SetStore(_ PluginStore) {}
2020-03-04 08:33:11 -07:00
func (h *Ham) pretty(resp *LicenseResp) string {
var s []string
s = append(s, fmt.Sprintf("%s: %s %s (expires: %s) %s (%s)\n",
resp.Hamdb.Callsign.Call,
resp.Hamdb.Callsign.Fname, resp.Hamdb.Callsign.Name,
resp.Hamdb.Callsign.Expires,
resp.Hamdb.Callsign.Country,
resp.Hamdb.Callsign.Grid,
))
return strings.Join(s, " ")
2020-03-04 08:33:11 -07:00
}
// Process does the heavy lifting
func (h *Ham) Process(from, post string) string {
2020-03-04 08:33:11 -07:00
call := h.fix(post)
if call != "" {
furl := fmt.Sprintf("http://api.hamdb.org/v1/%s/json/mcchunkie",
2020-03-04 08:33:11 -07:00
call,
)
var res = &LicenseResp{}
var req = HTTPRequest{
Timeout: 15 * time.Second,
2020-03-04 08:33:11 -07:00
URL: furl,
Method: "GET",
ResBody: res,
}
err := req.DoJSON()
if err != nil {
return fmt.Sprintf("sorry %s, I can't look things up in ULS (%s)", from, err)
2020-03-04 08:33:11 -07:00
}
if res.Hamdb.Messages.Status == "OK" {
return h.pretty(res)
2020-03-04 08:33:11 -07:00
}
return fmt.Sprintf("sorry %s, I can't look things up in ULS. The response was not OK.", from)
2020-03-04 08:33:11 -07:00
}
return "invalid callsign"
}
// RespondText to looking up of federation check requests
func (h *Ham) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
return SendText(c, ev.RoomID, h.Process(ev.Sender, post))
2020-03-04 08:33:11 -07:00
}
// Name Ham!
func (h *Ham) Name() string {
return "Ham"
}