rest: add testwire, remove some secret leaks

This commit is contained in:
Chris Sexton 2021-05-20 18:25:58 -04:00 committed by Chris Sexton
parent a1c5a4c580
commit ec47c31a80
1 changed files with 39 additions and 18 deletions

View File

@ -63,6 +63,10 @@ func (p *RestPlugin) register() {
Regex: regexp.MustCompile(`(?i)^rm wire (?P<id>\d+)$`), Regex: regexp.MustCompile(`(?i)^rm wire (?P<id>\d+)$`),
HelpText: "Removes a wire by ID (use list to view)", HelpText: "Removes a wire by ID (use list to view)",
Handler: p.rmWire}, Handler: p.rmWire},
bot.HandlerSpec{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile("(?i)^testwire `(?P<parse>[^`]+)` to (?P<url>\\S+) `(?P<returnField>[^`]+)` => (?P<text>.*)$"),
HelpText: "Tests a new REST function",
Handler: p.handleTestWire},
bot.HandlerSpec{Kind: bot.Message, IsCmd: true, bot.HandlerSpec{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile("(?i)^wire `(?P<parse>[^`]+)` to (?P<url>\\S+) `(?P<returnField>[^`]+)`$"), Regex: regexp.MustCompile("(?i)^wire `(?P<parse>[^`]+)` to (?P<url>\\S+) `(?P<returnField>[^`]+)`$"),
HelpText: "Registers a new REST function", HelpText: "Registers a new REST function",
@ -137,8 +141,8 @@ func (w wire) String() string {
return msg return msg
} }
func (p *RestPlugin) getWires() ([]*wire, error) { func (p *RestPlugin) getWires() ([]wire, error) {
wires := []*wire{} wires := []wire{}
err := p.db.Select(&wires, `select * from wires`) err := p.db.Select(&wires, `select * from wires`)
return wires, err return wires, err
} }
@ -203,34 +207,51 @@ func (p *RestPlugin) rmWire(r bot.Request) bool {
return true return true
} }
func (p *RestPlugin) mkWire(r bot.Request) (wire, error) {
var w wire
var err error
w.ParseRegex.Regexp, err = regexp.Compile(r.Values["parse"])
if err != nil {
return w, err
}
w.URL.URL, err = url.Parse(r.Values["url"])
if err != nil {
return w, err
}
w.ReturnField = r.Values["returnField"]
return w, nil
}
func (p *RestPlugin) handleWire(r bot.Request) bool { func (p *RestPlugin) handleWire(r bot.Request) bool {
var w wire var w wire
var msg string var msg string
var err error var err error
w.ParseRegex.Regexp, err = regexp.Compile(r.Values["parse"]) w, err = p.mkWire(r)
if err != nil {
msg = err.Error()
goto SEND
}
w.URL.URL, err = url.Parse(r.Values["url"])
if err != nil {
msg = err.Error()
goto SEND
}
w.ReturnField = r.Values["returnField"]
err = w.Save(p.db) err = w.Save(p.db)
if err != nil { if err != nil {
msg = err.Error() msg = err.Error()
goto SEND goto SEND
} }
p.b.RegisterRegex(p, bot.Message, w.ParseRegex.Regexp, p.mkHandler(&w)) p.b.RegisterRegex(p, bot.Message, w.ParseRegex.Regexp, p.mkHandler(w))
msg = fmt.Sprintf("Saved %s", w) msg = fmt.Sprintf("Saved %s", w)
SEND: SEND:
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true return true
} }
func (p *RestPlugin) mkHandler(w *wire) bot.ResponseHandler { func (p *RestPlugin) handleTestWire(r bot.Request) bool {
text := r.Values["text"]
w, err := p.mkWire(r)
if err != nil {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, err)
return true
}
h := p.mkHandler(w)
r.Values = bot.ParseValues(w.ParseRegex.Regexp, text)
return h(r)
}
func (p *RestPlugin) mkHandler(w wire) bot.ResponseHandler {
return func(r bot.Request) bool { return func(r bot.Request) bool {
if r.Msg.User.Name == p.b.Config().GetString("nick", "") { if r.Msg.User.Name == p.b.Config().GetString("nick", "") {
return false return false
@ -267,8 +288,8 @@ func (p *RestPlugin) mkHandler(w *wire) bot.ResponseHandler {
return true return true
} }
if resp.StatusCode < 200 || resp.StatusCode >= 300 { if resp.StatusCode < 200 || resp.StatusCode >= 300 {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Got a status %d: %s from %s", p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Got a status %d: %s",
resp.StatusCode, resp.Status, newURL)) resp.StatusCode, resp.Status))
} }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if p.handleErr(err, r) { if p.handleErr(err, r) {
@ -279,7 +300,7 @@ func (p *RestPlugin) mkHandler(w *wire) bot.ResponseHandler {
query, err := gojq.Parse(w.ReturnField) query, err := gojq.Parse(w.ReturnField)
if err != nil { if err != nil {
msg := fmt.Sprintf("Wire handler did not find return value (%s): %s => `%s`", newURL.String(), w.URL, w.ReturnField) msg := fmt.Sprintf("Wire handler did not find return value: %s => `%s`", w.URL, w.ReturnField)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true return true
} }