Make plugins pass up errors.

This commit is contained in:
Aaron Bieber 2020-05-13 16:53:31 -06:00
parent 6d849ab75a
commit 7971d61938
21 changed files with 768 additions and 750 deletions

20
main.go
View File

@ -189,7 +189,10 @@ func main() {
break
}
for _, room := range strings.Split(alertRooms, ",") {
plugins.SendMDNotice(cli, room, PrintErrataMD(&erratum))
err = plugins.SendMDNotice(cli, room, PrintErrataMD(&erratum))
if err != nil {
fmt.Println(err)
}
}
}
c = c + 1
@ -222,7 +225,10 @@ func main() {
val := kvRE.ReplaceAllString(post, "$2")
store.Set(key, val)
log.Printf("Setting %q to %q", key, val)
plugins.SendMD(cli, ev.RoomID, fmt.Sprintf("Set **%q** = *%q*", key, val))
err := plugins.SendMD(cli, ev.RoomID, fmt.Sprintf("Set **%q** = *%q*", key, val))
if err != nil {
log.Println(err)
}
return
}
}
@ -254,7 +260,10 @@ func main() {
p.SetStore(store)
start := time.Now()
p.RespondText(cli, ev, username, post)
err := p.RespondText(cli, ev, username, post)
if err != nil {
fmt.Println(err)
}
elapsed := time.Since(start)
if verbose {
log.Printf("%s took %s to run\n", p.Name(), elapsed)
@ -264,7 +273,10 @@ func main() {
}
}
if len(helps) > 0 {
plugins.SendMD(cli, ev.RoomID, strings.Join(helps, "\n"))
err := plugins.SendMD(cli, ev.RoomID, strings.Join(helps, "\n"))
if err != nil {
log.Println(err)
}
}
})

View File

@ -32,12 +32,12 @@ func (h *Beat) Match(user, msg string) bool {
func (h *Beat) SetStore(s PluginStore) {}
// RespondText to beat request events
func (h *Beat) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Beat) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
n := time.Now()
utc1 := n.Unix() + 3600
r := utc1 % 86400
bt := float32(r) / 86.4
SendText(c, ev.RoomID, fmt.Sprintf("@%03d", int32(bt)))
return SendText(c, ev.RoomID, fmt.Sprintf("@%03d", int32(bt)))
}
// Name beat

View File

@ -116,7 +116,7 @@ func (h *Beer) pretty(b BeerResp, random bool) string {
func (h *Beer) SetStore(s PluginStore) {}
// RespondText to looking up of beer requests
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
beer := h.fix(post)
if beer != "" {
var beers = &BeerResp{}
@ -131,18 +131,19 @@ func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post st
}
err := req.DoJSON()
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
return SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
}
switch {
case beers.Nhits == 0:
SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
return SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
case beers.Nhits == 1:
SendText(c, ev.RoomID, h.pretty(*beers, false))
return SendText(c, ev.RoomID, h.pretty(*beers, false))
case beers.Nhits > 1:
SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", beers.Nhits, h.pretty(*beers, true)))
return SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", beers.Nhits, h.pretty(*beers, true)))
}
}
return nil
}
// Name Beer!

View File

@ -45,11 +45,12 @@ func (h *BotSnack) resp() string {
func (h *BotSnack) SetStore(s PluginStore) {}
// RespondText to botsnack events
func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) {
SendText(c, ev.RoomID, h.resp())
return SendText(c, ev.RoomID, h.resp())
}
return nil
}
// Name BotSnack

View File

@ -35,16 +35,16 @@ func (h *Covid) fix(msg string) string {
}
// Match determines if we should call the response for Covid
func (h *Covid) Match(user, msg string) bool {
func (h *Covid) Match(_, msg string) bool {
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here.
func (h *Covid) SetStore(s PluginStore) {}
func (h *Covid) SetStore(_ PluginStore) {}
// RespondText to looking up of beer requests
func (h *Covid) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Covid) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
state := h.fix(post)
if state != "" {
var states = make(map[string]State)
@ -66,8 +66,9 @@ func (h *Covid) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post s
state = i
}
}
SendMD(c, ev.RoomID, fmt.Sprintf("_%s_: confirmed cases: **%d**, recovered: _%d_, deaths: _%d_", state, s.Confirmed, s.Recovered, s.Deaths))
return SendMD(c, ev.RoomID, fmt.Sprintf("_%s_: confirmed cases: **%d**, recovered: _%d_, deaths: _%d_", state, s.Confirmed, s.Recovered, s.Deaths))
}
return nil
}
// Name Covid!

View File

@ -51,13 +51,12 @@ func (h *Feder) Match(user, msg string) bool {
func (h *Feder) SetStore(s PluginStore) {}
// RespondText to looking up of federation check requests
func (h *Feder) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Feder) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
homeServer := h.fix(post)
if homeServer != "" {
u, err := url.Parse(fmt.Sprintf("https://%s", homeServer))
if err != nil {
SendText(c, ev.RoomID, "that's not a real host name.")
return
return SendText(c, ev.RoomID, "that's not a real host name.")
}
homeServer = u.Hostname()
@ -77,8 +76,7 @@ func (h *Feder) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post s
err = req.DoJSON()
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look up the federation status (%s)", ev.Sender, err))
return
return SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look up the federation status (%s)", ev.Sender, err))
}
stat := "broken"
@ -87,14 +85,14 @@ func (h *Feder) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post s
}
if fed.Info.Error != "" {
SendText(c, ev.RoomID, fmt.Sprintf("%s seems to be broken, maybe it isn't a homeserver?\n%s",
return SendText(c, ev.RoomID, fmt.Sprintf("%s seems to be broken, maybe it isn't a homeserver?\n%s",
homeServer, fed.Info.Error))
} else {
SendText(c, ev.RoomID, fmt.Sprintf("%s is running %s (%s) and is %s.",
return SendText(c, ev.RoomID, fmt.Sprintf("%s is running %s (%s) and is %s.",
homeServer, fed.Info.Name, fed.Info.Version, stat))
}
}
return nil
}
// Name Feder!

View File

@ -86,7 +86,7 @@ func (h *Ham) pretty(resp *LicenseResp) string {
}
// RespondText to looking up of federation check requests
func (h *Ham) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Ham) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
call := h.fix(post)
if call != "" {
furl := fmt.Sprintf("%s%s",
@ -104,16 +104,16 @@ func (h *Ham) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post str
err := req.DoJSON()
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look things up in ULS (%s)", ev.Sender, err))
return
return SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look things up in ULS (%s)", ev.Sender, err))
}
if res.Status == "OK" {
SendText(c, ev.RoomID, h.pretty(res))
return SendText(c, ev.RoomID, h.pretty(res))
} else {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look things up in ULS. The response was not OK.", ev.Sender))
return SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look things up in ULS. The response was not OK.", ev.Sender))
}
}
return nil
}
// Name Ham!

View File

@ -31,10 +31,10 @@ func (h *Hi) Match(user, msg string) bool {
func (h *Hi) SetStore(s PluginStore) {}
// RespondText to hi events
func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
return SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
// Name hi

View File

@ -40,22 +40,23 @@ func (h *HighFive) Match(user, msg string) bool {
}
// RespondText to high five events
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
rm := regexp.MustCompile(rightFive())
lm := regexp.MustCompile(leftFive())
if rm.MatchString(post) {
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
_ = SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
time.Sleep(time.Second * 5)
SendText(c, ev.RoomID, fmt.Sprintf("now go wash your hands, %s", s))
return SendText(c, ev.RoomID, fmt.Sprintf("now go wash your hands, %s", s))
}
if lm.MatchString(post) {
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
_ = SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
time.Sleep(time.Second * 5)
SendText(c, ev.RoomID, fmt.Sprintf("now go wash your hands, %s", s))
return SendText(c, ev.RoomID, fmt.Sprintf("now go wash your hands, %s", s))
}
return nil
}
// Name returns the name of the HighFive plugin

View File

@ -46,8 +46,8 @@ func (h *LoveYou) resp() string {
func (h *LoveYou) SetStore(s PluginStore) {}
// RespondText to love events
func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
SendText(c, ev.RoomID, h.resp())
func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
return SendText(c, ev.RoomID, h.resp())
}
// Name i love you

View File

@ -51,11 +51,13 @@ func (h *OpenBSDMan) Match(user, msg string) bool {
func (h *OpenBSDMan) SetStore(s PluginStore) {}
// RespondText sends back a man page.
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
page := h.fix(post)
if page != "" {
SendText(c, ev.RoomID, fmt.Sprintf("https://man.openbsd.org/%s", page))
return SendText(c, ev.RoomID, fmt.Sprintf("https://man.openbsd.org/%s", page))
}
return nil
}
// Name OpenBSDMan!

View File

@ -24,13 +24,13 @@ func (h *Palette) Re() string {
}
// Match determines if we are asking for a color
func (h *Palette) Match(user, msg string) bool {
func (h *Palette) Match(_, msg string) bool {
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here
func (h *Palette) SetStore(s PluginStore) {}
func (h *Palette) SetStore(_ PluginStore) {}
func (h *Palette) parseHexColor(s string) (*color.RGBA, error) {
c := &color.RGBA{
@ -57,7 +57,7 @@ func isEdge(x, y int) bool {
}
// RespondText to color request events
func (h *Palette) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Palette) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
const width, height = 56, 56
img := image.NewRGBA(image.Rect(0, 0, 56, 56))
@ -67,10 +67,10 @@ func (h *Palette) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post
B: 0x00,
A: 0xff,
}
color, err := h.parseHexColor(post)
clr, err := h.parseHexColor(post)
if err != nil {
fmt.Println(err)
SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
return SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
}
for y := 0; y < height; y++ {
@ -78,7 +78,7 @@ func (h *Palette) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post
if isEdge(x, y) {
img.Set(x, y, border)
} else {
img.Set(x, y, color)
img.Set(x, y, clr)
}
}
}
@ -86,7 +86,9 @@ func (h *Palette) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post
err = SendImage(c, ev.RoomID, img)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
// Name color

View File

@ -39,7 +39,7 @@ type Plugin interface {
Re() string
// RespondText responds to a "m.text" event
RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string)
RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string) error
// SetStore exposes the top level MCStore to a plugin
SetStore(s PluginStore)
@ -58,7 +58,7 @@ func ToMe(user, message string) bool {
// RemoveName removes the friendly name from a given message
func RemoveName(user, message string) string {
n := NameRE.ReplaceAllString(user, "$1")
return strings.ReplaceAll(message, n + ": ", "")
return strings.ReplaceAll(message, n+": ", "")
}
// HTTPRequest has the bits for making http requests

View File

@ -24,30 +24,28 @@ func (p *Snap) Re() string {
}
// Match determines if we should call the response for Snap
func (p *Snap) Match(user, msg string) bool {
func (p *Snap) Match(_, msg string) bool {
re := regexp.MustCompile(p.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here.
func (p *Snap) SetStore(s PluginStore) {}
func (p *Snap) SetStore(_ PluginStore) {}
// RespondText to looking up of federation check requests
func (p *Snap) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (p *Snap) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, _ string) error {
resp, err := http.Get("https://ftp.usa.openbsd.org/pub/OpenBSD/snapshots/amd64/BUILDINFO")
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
return
return SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
return
return SendText(c, ev.RoomID, fmt.Sprintf("%s", err))
}
SendText(c, ev.RoomID, string(body))
return SendText(c, ev.RoomID, string(body))
}
// Name Snap!

View File

@ -31,10 +31,10 @@ func (h *Source) Match(user, msg string) bool {
func (h *Source) SetStore(s PluginStore) {}
// RespondText to questions about TheSource™©®⑨
func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
return SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
}
// Name Source

View File

@ -33,7 +33,7 @@ func (h *Thanks) Match(user, msg string) bool {
func (h *Thanks) SetStore(s PluginStore) {}
// RespondText to welcome back events
func (h *Thanks) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Thanks) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
a := []string{
fmt.Sprintf("welcome %s", s),
@ -45,7 +45,7 @@ func (h *Thanks) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post
rand.Seed(time.Now().Unix())
SendText(c, ev.RoomID, a[rand.Intn(len(a))])
return SendText(c, ev.RoomID, a[rand.Intn(len(a))])
}
// Name Thanks

File diff suppressed because it is too large Load Diff

View File

@ -42,10 +42,10 @@ func (v *Version) print(to string) string {
func (v *Version) SetStore(s PluginStore) {}
// RespondText to version events
func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
SendText(c, ev.RoomID, v.print(s))
return SendText(c, ev.RoomID, v.print(s))
}
// Name Version

View File

@ -29,7 +29,7 @@ type WeatherResp struct {
}
func (w *WeatherResp) conditions() string {
s := []string{}
var s []string
for _, cond := range w.Weather {
s = append(s, cond.Description)
}
@ -133,7 +133,7 @@ func (h *Weather) get(loc string) (*WeatherResp, error) {
}
var w = &WeatherResp{}
err = json.Unmarshal([]byte(body), w)
err = json.Unmarshal(body, w)
if err != nil {
return nil, err
}
@ -163,14 +163,14 @@ func (h *Weather) fix(msg string) string {
}
// RespondText to looking up of weather lookup requests
func (h *Weather) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Weather) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
weather := h.fix(post)
if weather != "" {
wd, err := h.get(weather)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look up the weather. %s", ev.Sender, err))
return SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look up the weather. %s", ev.Sender, err))
}
SendText(c, ev.RoomID,
return SendText(c, ev.RoomID,
fmt.Sprintf("%s: %s (%s) Humidity: %s%%, %s",
wd.Name,
wd.f(),
@ -179,6 +179,7 @@ func (h *Weather) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post
wd.conditions(),
))
}
return nil
}
// Name Weather!

View File

@ -31,10 +31,10 @@ func (h *Wb) Match(user, msg string) bool {
func (h *Wb) SetStore(s PluginStore) {}
// RespondText to welcome back events
func (h *Wb) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
func (h *Wb) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) error {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
SendText(c, ev.RoomID, fmt.Sprintf("thanks %s!", s))
return SendText(c, ev.RoomID, fmt.Sprintf("thanks %s!", s))
}
// Name Wb

View File

@ -19,7 +19,7 @@ type MCStore struct {
func NewStore(path string) (*MCStore, error) {
flatTransform := func(s string) []string { return []string{} }
db := diskv.New(diskv.Options{
BasePath: "db",
BasePath: path,
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
})
@ -32,7 +32,7 @@ func NewStore(path string) (*MCStore, error) {
// Set takes a key value pair and shoves it in a db.
func (s *MCStore) Set(key string, value string) {
v := []byte(value)
s.db.Write(key, v)
_ = s.db.Write(key, v)
}
// Get retrives a value from the db
@ -71,7 +71,7 @@ func (s *MCStore) SaveFilterID(userID, filterID string) {
// LoadFilterID exposed for gomatrix
func (s *MCStore) LoadFilterID(userID string) string {
filter, _ := s.Get(fmt.Sprintf("filter_%s", userID))
return string(filter)
return filter
}
// SaveNextBatch exposed for gomatrix
@ -82,7 +82,7 @@ func (s *MCStore) SaveNextBatch(userID, nextBatchToken string) {
// LoadNextBatch exposed for gomatrix
func (s *MCStore) LoadNextBatch(userID string) string {
batch, _ := s.Get(fmt.Sprintf("batch_%s", userID))
return string(batch)
return batch
}
// SaveRoom exposed for gomatrix