2019-05-27 23:21:53 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2021-07-29 16:33:03 +00:00
|
|
|
"embed"
|
2019-05-27 23:21:53 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-05-08 14:31:27 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-07-21 18:52:45 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2019-05-27 23:21:53 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-05-08 14:31:27 +00:00
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
)
|
|
|
|
|
2021-07-29 16:33:03 +00:00
|
|
|
//go:embed *.html
|
|
|
|
var embeddedFS embed.FS
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
type CliPlugin struct {
|
|
|
|
bot bot.Bot
|
|
|
|
db *sqlx.DB
|
|
|
|
cache string
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *CliPlugin {
|
|
|
|
cp := &CliPlugin{
|
|
|
|
bot: b,
|
|
|
|
}
|
|
|
|
cp.registerWeb()
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CliPlugin) registerWeb() {
|
2021-07-21 18:52:45 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.HandleFunc("/api", p.handleWebAPI)
|
|
|
|
r.HandleFunc("/", p.handleWeb)
|
2021-07-28 15:32:59 +00:00
|
|
|
p.bot.RegisterWebName(r, "/cli", "CLI")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CliPlugin) GetRouter() (http.Handler, string) {
|
|
|
|
return nil, ""
|
2019-05-27 23:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CliPlugin) handleWebAPI(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
fmt.Fprintf(w, "Incorrect HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
info := struct {
|
2019-06-13 14:04:06 +00:00
|
|
|
User string `json:"user"`
|
|
|
|
Payload string `json:"payload"`
|
|
|
|
Password string `json:"password"`
|
2019-05-27 23:21:53 +00:00
|
|
|
}{}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&info)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug().
|
|
|
|
Interface("postbody", info).
|
|
|
|
Msg("Got a POST")
|
2021-07-21 13:54:29 +00:00
|
|
|
if !p.bot.CheckPassword("", info.Password) {
|
2019-06-13 14:04:06 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
j, _ := json.Marshal(struct{ Err string }{Err: "Invalid Password"})
|
|
|
|
w.Write(j)
|
|
|
|
return
|
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
|
|
|
|
p.bot.Receive(p, bot.Message, msg.Message{
|
|
|
|
User: &user.User{
|
|
|
|
ID: info.User,
|
|
|
|
Name: info.User,
|
|
|
|
Admin: false,
|
|
|
|
},
|
|
|
|
Channel: "web",
|
|
|
|
Body: info.Payload,
|
|
|
|
Raw: info.Payload,
|
|
|
|
Command: true,
|
|
|
|
Time: time.Now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
info.User = p.bot.WhoAmI()
|
|
|
|
info.Payload = p.cache
|
|
|
|
p.cache = ""
|
|
|
|
|
|
|
|
data, err := json.Marshal(info)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CliPlugin) handleWeb(w http.ResponseWriter, r *http.Request) {
|
2021-07-29 16:33:03 +00:00
|
|
|
index, _ := embeddedFS.ReadFile("index.html")
|
|
|
|
w.Write(index)
|
2019-05-27 23:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Completing the Connector interface, but will not actually be a connector
|
|
|
|
func (p *CliPlugin) RegisterEvent(cb bot.Callback) {}
|
|
|
|
func (p *CliPlugin) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
|
|
|
switch kind {
|
|
|
|
case bot.Message:
|
|
|
|
fallthrough
|
|
|
|
case bot.Action:
|
|
|
|
fallthrough
|
|
|
|
case bot.Reply:
|
|
|
|
fallthrough
|
|
|
|
case bot.Reaction:
|
|
|
|
p.cache += args[1].(string) + "\n"
|
|
|
|
}
|
|
|
|
id := fmt.Sprintf("%d", p.counter)
|
|
|
|
p.counter++
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
func (p *CliPlugin) GetEmojiList() map[string]string { return nil }
|
|
|
|
func (p *CliPlugin) Serve() error { return nil }
|
|
|
|
func (p *CliPlugin) Who(s string) []string { return nil }
|
2020-12-02 14:53:57 +00:00
|
|
|
func (p *CliPlugin) Profile(name string) (user.User, error) {
|
2020-05-08 14:31:27 +00:00
|
|
|
return user.User{}, fmt.Errorf("unimplemented")
|
|
|
|
}
|
2020-12-02 14:53:57 +00:00
|
|
|
func (p *CliPlugin) Emojy(name string) string { return name }
|
|
|
|
func (p *CliPlugin) URLFormat(title, url string) string {
|
|
|
|
return fmt.Sprintf("%s (%s)", title, url)
|
|
|
|
}
|
2021-11-20 20:28:53 +00:00
|
|
|
func (p *CliPlugin) GetChannelName(id string) string { return id }
|
|
|
|
func (p *CliPlugin) GetChannelID(name string) string { return name }
|
|
|
|
func (p *CliPlugin) GetRoles() ([]bot.Role, error) { return []bot.Role{}, nil }
|
|
|
|
func (p *CliPlugin) SetRole(userID, roleID string) error { return nil }
|