2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-06-11 11:52:42 +00:00
|
|
|
registerAction(func() Actor { return &ActorRespond{} })
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
type ActorRespond struct {
|
|
|
|
Respond *string `json:"respond" yaml:"respond"`
|
|
|
|
RespondAsReply *bool `json:"respond_as_reply" yaml:"respond_as_reply"`
|
|
|
|
RespondFallback *string `json:"respond_fallback" yaml:"respond_fallback"`
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
func (a ActorRespond) Execute(c *irc.Client, m *irc.Message, r *Rule) (preventCooldown bool, err error) {
|
2021-06-11 11:52:42 +00:00
|
|
|
if a.Respond == nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
msg, err := formatMessage(*a.Respond, m, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
if a.RespondFallback == nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
if msg, err = formatMessage(*a.RespondFallback, m, r, nil); err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response fallback")
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-06-02 13:31:16 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
ircMessage := &irc.Message{
|
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
|
|
|
m.Params[0],
|
|
|
|
msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.RespondAsReply != nil && *a.RespondAsReply {
|
|
|
|
id, ok := m.GetTag("id")
|
|
|
|
if ok {
|
|
|
|
if ircMessage.Tags == nil {
|
|
|
|
ircMessage.Tags = make(irc.Tags)
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
ircMessage.Tags["reply-parent-msg-id"] = irc.TagValue(id)
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-06-02 13:31:16 +00:00
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2021-06-11 11:52:42 +00:00
|
|
|
c.WriteMessage(ircMessage),
|
|
|
|
"sending response",
|
|
|
|
)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
|
|
|
func (a ActorRespond) IsAsync() bool { return false }
|
|
|
|
func (a ActorRespond) Name() string { return "respond" }
|