This commit is contained in:
@@ -23,14 +23,14 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
c *context.Context
|
||||
ctx *context.Context
|
||||
connections map[string]*MatrixConnection
|
||||
)
|
||||
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
ctx = cc
|
||||
connections = make(map[string]*MatrixConnection)
|
||||
|
||||
mp := MatrixPusher{}
|
||||
c.RegisterPusherInterface("matrix", pusherinterface.PusherInterface(mp))
|
||||
ctx.RegisterPusherInterface("matrix", pusherinterface.PusherInterface(mp))
|
||||
}
|
||||
|
@@ -64,14 +64,14 @@ type MatrixConnection struct {
|
||||
|
||||
// nolint
|
||||
func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte, error) {
|
||||
c.Log.Debug().Msgf("Data to send: %+v", data)
|
||||
ctx.Log.Debug().Msgf("Data to send: %+v", data)
|
||||
|
||||
apiRoot := mxc.apiRoot + endpoint
|
||||
if mxc.token != "" {
|
||||
apiRoot += fmt.Sprintf("?access_token=%s", mxc.token)
|
||||
}
|
||||
|
||||
c.Log.Debug().Msgf("Request URL: %s", apiRoot)
|
||||
ctx.Log.Debug().Msgf("Request URL: %s", apiRoot)
|
||||
|
||||
req, _ := http.NewRequest("POST", apiRoot, bytes.NewBuffer([]byte(data)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -97,14 +97,14 @@ func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte
|
||||
|
||||
// nolint
|
||||
func (mxc *MatrixConnection) doPutRequest(endpoint string, data string) ([]byte, error) {
|
||||
c.Log.Debug().Msgf("Data to send: %+v", data)
|
||||
ctx.Log.Debug().Msgf("Data to send: %+v", data)
|
||||
|
||||
apiRoot := mxc.apiRoot + endpoint
|
||||
if mxc.token != "" {
|
||||
apiRoot += fmt.Sprintf("?access_token=%s", mxc.token)
|
||||
}
|
||||
|
||||
c.Log.Debug().Msgf("Request URL: %s", apiRoot)
|
||||
ctx.Log.Debug().Msgf("Request URL: %s", apiRoot)
|
||||
|
||||
req, _ := http.NewRequest("PUT", apiRoot, bytes.NewBuffer([]byte(data)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -160,7 +160,7 @@ func (mxc *MatrixConnection) generateTnxIDSecureBytes(length int) []byte {
|
||||
randomBytes := make([]byte, length)
|
||||
|
||||
if _, err := crand.Read(randomBytes); err != nil {
|
||||
c.Log.Fatal().Msg("Unable to generate random bytes for transaction ID!")
|
||||
ctx.Log.Fatal().Msg("Unable to generate random bytes for transaction ID!")
|
||||
}
|
||||
|
||||
return randomBytes
|
||||
@@ -174,15 +174,15 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st
|
||||
mxc.roomID = roomID
|
||||
mxc.token = ""
|
||||
|
||||
c.Log.Debug().Str("conn", mxc.connName).Str("api_root", apiRoot).Msg("Trying to connect server")
|
||||
ctx.Log.Debug().Str("conn", mxc.connName).Str("api_root", apiRoot).Msg("Trying to connect server")
|
||||
|
||||
loginStr := fmt.Sprintf(`{"type": "m.login.password", "user": "%s", "password": "%s"}`, mxc.username, mxc.password)
|
||||
|
||||
c.Log.Debug().Msgf("Login string: %s", loginStr)
|
||||
ctx.Log.Debug().Msgf("Login string: %s", loginStr)
|
||||
|
||||
reply, err := mxc.doPostRequest("/login", loginStr)
|
||||
if err != nil {
|
||||
c.Log.Fatal().Msgf("Failed to login to Matrix with user '%s' (conn %s): '%s'", mxc.username, mxc.connName, err.Error())
|
||||
ctx.Log.Fatal().Msgf("Failed to login to Matrix with user '%s' (conn %s): '%s'", mxc.username, mxc.connName, err.Error())
|
||||
}
|
||||
|
||||
// Parse received JSON and get access token.
|
||||
@@ -190,7 +190,7 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st
|
||||
|
||||
err1 := json.Unmarshal(reply, &data)
|
||||
if err1 != nil {
|
||||
c.Log.Fatal().
|
||||
ctx.Log.Fatal().
|
||||
Str("username", mxc.username).
|
||||
Str("conn", mxc.connName).
|
||||
Err(err).
|
||||
@@ -200,14 +200,14 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st
|
||||
mxc.token, _ = data["access_token"].(string)
|
||||
mxc.deviceID, _ = data["deviceID"].(string)
|
||||
|
||||
c.Log.Debug().Str("conn", mxc.connName).Str("access_token", mxc.token).Str("device_id", mxc.deviceID).Msg("Login successful")
|
||||
ctx.Log.Debug().Str("conn", mxc.connName).Str("access_token", mxc.token).Str("device_id", mxc.deviceID).Msg("Login successful")
|
||||
|
||||
// We should check if we're already in room and, if not, join it.
|
||||
// We will do this by simply trying to join. We don't care about reply
|
||||
// here.
|
||||
_, err2 := mxc.doPostRequest("/rooms/"+mxc.roomID+"/join", "{}")
|
||||
if err2 != nil {
|
||||
c.Log.Fatal().Err(err).Msg("Failed to join room")
|
||||
ctx.Log.Fatal().Err(err).Msg("Failed to join room")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st
|
||||
// It will prepare a message which will be passed to mxc.SendMessage().
|
||||
func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
||||
// Prepare message body.
|
||||
messageData := c.SendToParser(message.Username, message)
|
||||
messageData := ctx.SendToParser(message.Username, message)
|
||||
|
||||
messageToSend, _ := messageData["message"].(string)
|
||||
// We'll use HTML, so reformat links accordingly (if any).
|
||||
@@ -230,7 +230,7 @@ func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
||||
// "\n" should be "<br>".
|
||||
messageToSend = strings.ReplaceAll(messageToSend, "\n", "<br>")
|
||||
|
||||
c.Log.Debug().Msgf("Crafted message: %s", messageToSend)
|
||||
ctx.Log.Debug().Msgf("Crafted message: %s", messageToSend)
|
||||
|
||||
// Send message.
|
||||
mxc.SendMessage(messageToSend)
|
||||
@@ -238,7 +238,7 @@ func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
||||
|
||||
// This function sends already prepared message to room.
|
||||
func (mxc *MatrixConnection) SendMessage(message string) {
|
||||
c.Log.Debug().Str("conn", mxc.connName).Msgf("Sending message: '%s'", message)
|
||||
ctx.Log.Debug().Str("conn", mxc.connName).Msgf("Sending message: '%s'", message)
|
||||
|
||||
// We should send notices as it is preferred behavior for bots and
|
||||
// appservices.
|
||||
@@ -251,7 +251,7 @@ func (mxc *MatrixConnection) SendMessage(message string) {
|
||||
|
||||
msgBytes, err := json.Marshal(&msg)
|
||||
if err != nil {
|
||||
c.Log.Error().Err(err).Msg("Failed to marshal message into JSON.")
|
||||
ctx.Log.Error().Err(err).Msg("Failed to marshal message into JSON.")
|
||||
|
||||
return
|
||||
}
|
||||
@@ -260,20 +260,20 @@ func (mxc *MatrixConnection) SendMessage(message string) {
|
||||
|
||||
reply, err := mxc.doPutRequest("/rooms/"+mxc.roomID+"/send/m.room.message/"+mxc.generateTnxID(), msgStr)
|
||||
if err != nil {
|
||||
c.Log.Fatal().Str("conn", mxc.connName).Str("room", mxc.roomID).Err(err).Msg("Failed to send message to room")
|
||||
ctx.Log.Fatal().Str("conn", mxc.connName).Str("room", mxc.roomID).Err(err).Msg("Failed to send message to room")
|
||||
}
|
||||
|
||||
c.Log.Debug().Msgf("Message sent, reply: %s", string(reply))
|
||||
ctx.Log.Debug().Msgf("Message sent, reply: %s", string(reply))
|
||||
}
|
||||
|
||||
func (mxc *MatrixConnection) Shutdown() {
|
||||
c.Log.Info().Str("conn", mxc.connName).Msg("Shutting down connection...")
|
||||
ctx.Log.Info().Str("conn", mxc.connName).Msg("Shutting down connection...")
|
||||
|
||||
_, err := mxc.doPostRequest("/logout", "{}")
|
||||
if err != nil {
|
||||
c.Log.Error().Err(err).Str("conn", mxc.connName).Msg("Error occurred while trying to log out from Matrix.")
|
||||
ctx.Log.Error().Err(err).Str("conn", mxc.connName).Msg("Error occurred while trying to log out from Matrix.")
|
||||
}
|
||||
|
||||
mxc.token = ""
|
||||
c.Log.Info().Str("conn", mxc.connName).Msg("Connection successfully shutted down")
|
||||
ctx.Log.Info().Str("conn", mxc.connName).Msg("Connection successfully shutted down")
|
||||
}
|
||||
|
@@ -22,15 +22,15 @@ import slackmessage "go.dev.pztrn.name/opensaps/slack/message"
|
||||
type MatrixPusher struct{}
|
||||
|
||||
func (mp MatrixPusher) Initialize() {
|
||||
c.Log.Info().Msg("Initializing Matrix protocol pusher...")
|
||||
ctx.Log.Info().Msg("Initializing Matrix protocol pusher...")
|
||||
|
||||
// Get configuration for pushers and initialize every connection.
|
||||
cfg := c.Config.GetConfig()
|
||||
cfg := ctx.Config.GetConfig()
|
||||
for name, config := range cfg.Matrix {
|
||||
c.Log.Info().Str("conn", name).Msg("Initializing connection...")
|
||||
ctx.Log.Info().Str("conn", name).Msg("Initializing connection...")
|
||||
|
||||
// Fields will be filled with conn.Initialize().
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
conn := MatrixConnection{}
|
||||
connections[name] = &conn
|
||||
|
||||
@@ -41,17 +41,17 @@ func (mp MatrixPusher) Initialize() {
|
||||
func (mp MatrixPusher) Push(connection string, data slackmessage.SlackMessage) {
|
||||
conn, found := connections[connection]
|
||||
if !found {
|
||||
c.Log.Error().Str("conn", connection).Msg("Connection not found!")
|
||||
ctx.Log.Error().Str("conn", connection).Msg("Connection not found!")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.Log.Debug().Str("conn", connection).Msg("Pushing data to connection")
|
||||
ctx.Log.Debug().Str("conn", connection).Msg("Pushing data to connection")
|
||||
conn.ProcessMessage(data)
|
||||
}
|
||||
|
||||
func (mp MatrixPusher) Shutdown() {
|
||||
c.Log.Info().Msg("Shutting down Matrix pusher...")
|
||||
ctx.Log.Info().Msg("Shutting down Matrix pusher...")
|
||||
|
||||
for _, conn := range connections {
|
||||
conn.Shutdown()
|
||||
|
@@ -23,14 +23,14 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
c *context.Context
|
||||
ctx *context.Context
|
||||
connections map[string]*TelegramConnection
|
||||
)
|
||||
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
ctx = cc
|
||||
connections = make(map[string]*TelegramConnection)
|
||||
|
||||
tp := TelegramPusher{}
|
||||
c.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp))
|
||||
ctx.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp))
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ func (tc *TelegramConnection) Initialize(connName string, cfg configstruct.Confi
|
||||
|
||||
func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
||||
// Prepare message body.
|
||||
messageData := c.SendToParser(message.Username, message)
|
||||
messageData := ctx.SendToParser(message.Username, message)
|
||||
|
||||
messageToSend, _ := messageData["message"].(string)
|
||||
// We'll use HTML, so reformat links accordingly (if any).
|
||||
@@ -49,7 +49,7 @@ func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage)
|
||||
}
|
||||
}
|
||||
|
||||
c.Log.Debug().Msgf("Crafted message: %s", messageToSend)
|
||||
ctx.Log.Debug().Msgf("Crafted message: %s", messageToSend)
|
||||
|
||||
// Send message.
|
||||
tc.SendMessage(messageToSend)
|
||||
@@ -62,7 +62,7 @@ func (tc *TelegramConnection) SendMessage(message string) {
|
||||
msgdata.Set("parse_mode", "HTML")
|
||||
|
||||
// Are we should use proxy?
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
httpTransport := &http.Transport{}
|
||||
|
||||
// nolint:nestif
|
||||
@@ -82,30 +82,30 @@ func (tc *TelegramConnection) SendMessage(message string) {
|
||||
|
||||
proxyURLParsed, err := url.Parse(proxyURL)
|
||||
if err != nil {
|
||||
c.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL")
|
||||
ctx.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL")
|
||||
} else {
|
||||
httpTransport.Proxy = http.ProxyURL(proxyURLParsed)
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
client := &http.Client{Transport: httpTransport}
|
||||
botURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.config.BotID)
|
||||
|
||||
c.Log.Debug().Msgf("Bot URL: %s", botURL)
|
||||
ctx.Log.Debug().Msgf("Bot URL: %s", botURL)
|
||||
|
||||
// ToDo: fix it.
|
||||
// nolint
|
||||
response, err := client.PostForm(botURL, msgdata)
|
||||
if err != nil {
|
||||
c.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram")
|
||||
ctx.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram")
|
||||
} else {
|
||||
c.Log.Debug().Msgf("Status: %s", response.Status)
|
||||
ctx.Log.Debug().Msgf("Status: %s", response.Status)
|
||||
if response.StatusCode != http.StatusOK {
|
||||
body := []byte{}
|
||||
_, _ = response.Body.Read(body)
|
||||
response.Body.Close()
|
||||
c.Log.Debug().Msg(string(body))
|
||||
ctx.Log.Debug().Msg(string(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -24,14 +24,14 @@ import (
|
||||
type TelegramPusher struct{}
|
||||
|
||||
func (tp TelegramPusher) Initialize() {
|
||||
c.Log.Info().Msg("Initializing Telegram protocol pusher...")
|
||||
ctx.Log.Info().Msg("Initializing Telegram protocol pusher...")
|
||||
|
||||
// Get configuration for pushers and initialize every connection.
|
||||
cfg := c.Config.GetConfig()
|
||||
cfg := ctx.Config.GetConfig()
|
||||
for name, config := range cfg.Telegram {
|
||||
c.Log.Info().Str("conn", name).Msg("Initializing connection...")
|
||||
ctx.Log.Info().Str("conn", name).Msg("Initializing connection...")
|
||||
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
conn := TelegramConnection{}
|
||||
connections[name] = &conn
|
||||
|
||||
@@ -42,17 +42,17 @@ func (tp TelegramPusher) Initialize() {
|
||||
func (tp TelegramPusher) Push(connection string, data slackmessage.SlackMessage) {
|
||||
conn, found := connections[connection]
|
||||
if !found {
|
||||
c.Log.Error().Str("conn", connection).Msg("Connection not found")
|
||||
ctx.Log.Error().Str("conn", connection).Msg("Connection not found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.Log.Debug().Str("conn", connection).Msg("Pushing data")
|
||||
ctx.Log.Debug().Str("conn", connection).Msg("Pushing data")
|
||||
conn.ProcessMessage(data)
|
||||
}
|
||||
|
||||
func (tp TelegramPusher) Shutdown() {
|
||||
c.Log.Info().Msg("Shutting down Telegram pusher...")
|
||||
ctx.Log.Info().Msg("Shutting down Telegram pusher...")
|
||||
|
||||
for _, conn := range connections {
|
||||
conn.Shutdown()
|
||||
|
Reference in New Issue
Block a user