2017-02-18 23:10:22 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-12-07 23:48:44 +01:00
|
|
|
"encoding/json"
|
2018-06-09 12:47:40 +02:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-02-27 00:33:21 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2017-02-18 23:10:22 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2019-01-31 17:06:36 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2020-06-25 00:25:10 +02:00
|
|
|
ring "github.com/zfjagann/golang-ring"
|
2017-02-18 23:10:22 +01:00
|
|
|
)
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
type API struct {
|
2017-02-18 23:10:22 +01:00
|
|
|
Messages ring.Ring
|
|
|
|
sync.RWMutex
|
2018-03-04 23:52:14 +01:00
|
|
|
*bridge.Config
|
2017-02-18 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
type Message struct {
|
2017-02-18 23:10:22 +01:00
|
|
|
Text string `json:"text"`
|
|
|
|
Username string `json:"username"`
|
2017-06-18 15:44:54 +02:00
|
|
|
UserID string `json:"userid"`
|
2017-02-18 23:10:22 +01:00
|
|
|
Avatar string `json:"avatar"`
|
2017-06-07 23:54:50 +02:00
|
|
|
Gateway string `json:"gateway"`
|
2017-02-18 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2018-11-15 20:43:43 +01:00
|
|
|
b := &API{Config: cfg}
|
2017-02-18 23:10:22 +01:00
|
|
|
e := echo.New()
|
2018-02-21 00:49:10 +01:00
|
|
|
e.HideBanner = true
|
|
|
|
e.HidePort = true
|
2017-02-18 23:10:22 +01:00
|
|
|
b.Messages = ring.Ring{}
|
2018-11-05 21:53:51 +01:00
|
|
|
if b.GetInt("Buffer") != 0 {
|
|
|
|
b.Messages.SetCapacity(b.GetInt("Buffer"))
|
|
|
|
}
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetString("Token") != "" {
|
2017-06-06 00:05:32 +02:00
|
|
|
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
2018-03-04 23:52:14 +01:00
|
|
|
return key == b.GetString("Token"), nil
|
2017-06-06 00:05:32 +02:00
|
|
|
}))
|
|
|
|
}
|
2020-06-25 00:25:10 +02:00
|
|
|
|
|
|
|
// Set RemoteNickFormat to a sane default
|
|
|
|
if !b.IsKeySet("RemoteNickFormat") {
|
|
|
|
b.Log.Debugln("RemoteNickFormat is unset, defaulting to \"{NICK}\"")
|
|
|
|
b.Config.Config.Viper().Set(b.GetConfigKey("RemoteNickFormat"), "{NICK}")
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:11:59 +01:00
|
|
|
e.GET("/api/health", b.handleHealthcheck)
|
2017-02-18 23:10:22 +01:00
|
|
|
e.GET("/api/messages", b.handleMessages)
|
2017-12-07 23:48:44 +01:00
|
|
|
e.GET("/api/stream", b.handleStream)
|
2017-02-18 23:10:22 +01:00
|
|
|
e.POST("/api/message", b.handlePostMessage)
|
|
|
|
go func() {
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetString("BindAddress") == "" {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Fatalf("No BindAddress configured.")
|
2018-02-21 00:49:10 +01:00
|
|
|
}
|
2018-03-04 23:52:14 +01:00
|
|
|
b.Log.Infof("Listening on %s", b.GetString("BindAddress"))
|
|
|
|
b.Log.Fatal(e.Start(b.GetString("BindAddress")))
|
2017-02-18 23:10:22 +01:00
|
|
|
}()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) Connect() error {
|
2017-02-18 23:10:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) Disconnect() error {
|
2017-02-18 23:10:22 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) JoinChannel(channel config.ChannelInfo) error {
|
2017-02-18 23:10:22 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) Send(msg config.Message) (string, error) {
|
2017-02-18 23:10:22 +01:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
2017-09-11 22:45:15 +02:00
|
|
|
// ignore delete messages
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2017-09-11 22:45:15 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
2017-02-18 23:10:22 +01:00
|
|
|
b.Messages.Enqueue(&msg)
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", nil
|
2017-02-18 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) handleHealthcheck(c echo.Context) error {
|
2018-11-07 09:11:59 +01:00
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) handlePostMessage(c echo.Context) error {
|
2018-02-20 18:36:44 +01:00
|
|
|
message := config.Message{}
|
|
|
|
if err := c.Bind(&message); err != nil {
|
2017-02-18 23:10:22 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-02-20 18:36:44 +01:00
|
|
|
// these values are fixed
|
|
|
|
message.Channel = "api"
|
|
|
|
message.Protocol = "api"
|
|
|
|
message.Account = b.Account
|
|
|
|
message.ID = ""
|
|
|
|
message.Timestamp = time.Now()
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Debugf("Sending message from %s on %s to gateway", message.Username, "api")
|
2018-02-20 18:36:44 +01:00
|
|
|
b.Remote <- message
|
2017-02-18 23:10:22 +01:00
|
|
|
return c.JSON(http.StatusOK, message)
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) handleMessages(c echo.Context) error {
|
2017-02-18 23:10:22 +01:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
2017-06-05 23:08:36 +02:00
|
|
|
c.JSONPretty(http.StatusOK, b.Messages.Values(), " ")
|
2017-02-18 23:10:22 +01:00
|
|
|
b.Messages = ring.Ring{}
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-07 23:48:44 +01:00
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
func (b *API) handleStream(c echo.Context) error {
|
2017-12-07 23:48:44 +01:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
|
|
c.Response().WriteHeader(http.StatusOK)
|
2018-11-02 16:35:13 +01:00
|
|
|
greet := config.Message{
|
2018-11-15 20:43:43 +01:00
|
|
|
Event: config.EventAPIConnected,
|
2018-11-07 20:36:50 +01:00
|
|
|
Timestamp: time.Now(),
|
2018-11-02 16:35:13 +01:00
|
|
|
}
|
|
|
|
if err := json.NewEncoder(c.Response()).Encode(greet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Response().Flush()
|
2017-12-07 23:48:44 +01:00
|
|
|
for {
|
2019-01-31 17:06:36 +01:00
|
|
|
msg := b.Messages.Dequeue()
|
|
|
|
if msg != nil {
|
|
|
|
if err := json.NewEncoder(c.Response()).Encode(msg); err != nil {
|
|
|
|
return err
|
2017-12-07 23:48:44 +01:00
|
|
|
}
|
2019-01-31 17:06:36 +01:00
|
|
|
c.Response().Flush()
|
2017-12-07 23:48:44 +01:00
|
|
|
}
|
2019-01-31 17:06:36 +01:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2017-12-07 23:48:44 +01:00
|
|
|
}
|
|
|
|
}
|