2017-08-28 01:13:45 +05:00
|
|
|
// OpenSAPS - Open Slack API server for everyone.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package matrixpusher
|
|
|
|
|
|
|
|
import (
|
2018-02-02 09:17:40 +05:00
|
|
|
// stdlib
|
|
|
|
"bytes"
|
|
|
|
crand "crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
// local
|
2019-12-15 22:17:01 +05:00
|
|
|
slackmessage "go.dev.pztrn.name/opensaps/slack/message"
|
2017-08-28 01:13:45 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Constants for random transaction ID.
|
|
|
|
const (
|
2018-02-02 09:17:40 +05:00
|
|
|
letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // 36 possibilities
|
|
|
|
letterIdxBits = 6 // 6 bits to represent 64 possibilities / indexes
|
|
|
|
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
2017-08-28 01:13:45 +05:00
|
|
|
)
|
|
|
|
|
2017-09-13 16:02:56 +05:00
|
|
|
type MatrixMessage struct {
|
2018-02-02 09:17:40 +05:00
|
|
|
MsgType string `json:"msgtype"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
FormattedBody string `json:"formatted_body"`
|
2017-09-13 16:02:56 +05:00
|
|
|
}
|
|
|
|
|
2017-08-28 01:13:45 +05:00
|
|
|
type MatrixConnection struct {
|
2018-02-02 09:17:40 +05:00
|
|
|
// API root for connection.
|
2019-12-15 22:17:01 +05:00
|
|
|
apiRoot string
|
2018-02-02 09:17:40 +05:00
|
|
|
// Connection name.
|
2019-12-15 22:17:01 +05:00
|
|
|
connName string
|
2018-02-02 09:17:40 +05:00
|
|
|
// Our device ID.
|
2019-12-15 22:17:01 +05:00
|
|
|
deviceID string
|
2018-02-02 09:17:40 +05:00
|
|
|
// Password for user.
|
|
|
|
password string
|
|
|
|
// Room ID.
|
2019-12-15 22:17:01 +05:00
|
|
|
roomID string
|
2018-02-02 09:17:40 +05:00
|
|
|
// Token we obtained after logging in.
|
|
|
|
token string
|
|
|
|
// Our username for logging in to server.
|
|
|
|
username string
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
// nolint
|
2017-08-28 01:13:45 +05:00
|
|
|
func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte, error) {
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Debugln("Data to send:", data)
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
apiRoot := mxc.apiRoot + endpoint
|
2018-02-02 09:17:40 +05:00
|
|
|
if mxc.token != "" {
|
2019-12-15 22:17:01 +05:00
|
|
|
apiRoot += fmt.Sprintf("?access_token=%s", mxc.token)
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
|
|
|
c.Log.Debugln("Request URL:", apiRoot)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", apiRoot, bytes.NewBuffer([]byte(data)))
|
2018-02-02 09:17:40 +05:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
return nil, errors.New("Failed to perform POST request to Matrix as '" +
|
|
|
|
mxc.username + "' (conn " + mxc.connName + "): " + err.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
if resp.Status == "200 OK" {
|
|
|
|
// Return body.
|
|
|
|
return body, nil
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
|
|
|
return nil, errors.New("Status: " + resp.Status + ", body: " + string(body))
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
// nolint
|
2017-08-28 01:13:45 +05:00
|
|
|
func (mxc *MatrixConnection) doPutRequest(endpoint string, data string) ([]byte, error) {
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Debugln("Data to send:", data)
|
2019-12-15 22:17:01 +05:00
|
|
|
|
|
|
|
apiRoot := mxc.apiRoot + endpoint
|
2018-02-02 09:17:40 +05:00
|
|
|
if mxc.token != "" {
|
2019-12-15 22:17:01 +05:00
|
|
|
apiRoot += fmt.Sprintf("?access_token=%s", mxc.token)
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
|
|
|
c.Log.Debugln("Request URL:", apiRoot)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", apiRoot, bytes.NewBuffer([]byte(data)))
|
2018-02-02 09:17:40 +05:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
return nil, errors.New("Failed to perform PUT request to Matrix as '" +
|
|
|
|
mxc.username + "' (conn " + mxc.connName + "): " + err.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
if resp.Status == "200 OK" {
|
|
|
|
// Return body.
|
|
|
|
return body, nil
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
|
|
|
return nil, errors.New("Status: " + resp.Status + ", body: " + string(body))
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
func (mxc *MatrixConnection) generateTnxID() string {
|
2018-02-02 09:17:40 +05:00
|
|
|
// Random tnxid - 16 chars.
|
|
|
|
length := 16
|
|
|
|
|
|
|
|
result := make([]byte, length)
|
|
|
|
bufferSize := int(float64(length) * 1.3)
|
|
|
|
|
|
|
|
// Making sure that we have only letters and numbers and resulted
|
|
|
|
// string will be exactly requested length.
|
|
|
|
for i, j, randomBytes := 0, 0, []byte{}; i < length; j++ {
|
|
|
|
if j%bufferSize == 0 {
|
2019-12-15 22:17:01 +05:00
|
|
|
randomBytes = mxc.generateTnxIDSecureBytes(bufferSize)
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
if idx := int(randomBytes[j%length] & letterIdxMask); idx < len(letterBytes) {
|
|
|
|
result[i] = letterBytes[idx]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
return string(result)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
func (mxc *MatrixConnection) generateTnxIDSecureBytes(length int) []byte {
|
2018-02-02 09:17:40 +05:00
|
|
|
// Get random bytes.
|
|
|
|
var randomBytes = make([]byte, length)
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
_, err := crand.Read(randomBytes)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Fatalln("Unable to generate random bytes for transaction ID!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return randomBytes
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user string, password string, roomID string) {
|
|
|
|
mxc.connName = connName
|
|
|
|
mxc.apiRoot = apiRoot
|
2018-02-02 09:17:40 +05:00
|
|
|
mxc.username = user
|
|
|
|
mxc.password = password
|
2019-12-15 22:17:01 +05:00
|
|
|
mxc.roomID = roomID
|
2018-02-02 09:17:40 +05:00
|
|
|
mxc.token = ""
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Debugln("Trying to connect to", mxc.connName, "("+apiRoot+")")
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
loginStr := fmt.Sprintf(`{"type": "m.login.password", "user": "%s", "password": "%s"}`, mxc.username, mxc.password)
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Debugln("Login string:", loginStr)
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
reply, err := mxc.doPostRequest("/login", loginStr)
|
|
|
|
if err != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Fatalf("Failed to login to Matrix with user '%s' (conn %s): '%s'", mxc.username, mxc.connName, err.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
// Parse received JSON and get access token.
|
2019-03-06 05:15:54 +05:00
|
|
|
data := make(map[string]interface{})
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
err1 := json.Unmarshal(reply, &data)
|
|
|
|
if err1 != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Fatalf("Failed to parse received JSON from Matrix for user '%s' (conn %s): %s (data was: %s)",
|
|
|
|
mxc.username, mxc.connName, err1.Error(), reply)
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2019-03-06 05:15:54 +05:00
|
|
|
mxc.token = data["access_token"].(string)
|
2019-12-15 22:17:01 +05:00
|
|
|
mxc.deviceID = data["deviceID"].(string)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Debugf("Login successful for conn '%s', access token is '%s', our deviceID is '%s'",
|
|
|
|
mxc.connName, mxc.token, mxc.deviceID)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
// 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.
|
2019-12-15 22:17:01 +05:00
|
|
|
_, err2 := mxc.doPostRequest("/rooms/"+mxc.roomID+"/join", "{}")
|
2018-02-02 09:17:40 +05:00
|
|
|
if err2 != nil {
|
|
|
|
c.Log.Fatalf("Failed to join room: %s", err2.Error())
|
|
|
|
}
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function launches when new data was received thru Slack API.
|
|
|
|
// It will prepare a message which will be passed to mxc.SendMessage().
|
|
|
|
func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
2018-02-02 09:17:40 +05:00
|
|
|
// Prepare message body.
|
2019-03-05 19:31:13 +05:00
|
|
|
messageData := c.SendToParser(message.Username, message)
|
|
|
|
|
|
|
|
messageToSend := messageData["message"].(string)
|
|
|
|
// We'll use HTML, so reformat links accordingly (if any).
|
|
|
|
linksRaw, linksFound := messageData["links"]
|
|
|
|
if linksFound {
|
|
|
|
links := linksRaw.([][]string)
|
|
|
|
for _, link := range links {
|
2019-03-06 05:08:00 +05:00
|
|
|
messageToSend = strings.Replace(messageToSend, link[0], `<a href="`+link[1]+`">`+link[2]+`</a>`, -1)
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:31:13 +05:00
|
|
|
// "\n" should be "<br>".
|
|
|
|
messageToSend = strings.Replace(messageToSend, "\n", "<br>", -1)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
2019-03-05 19:31:13 +05:00
|
|
|
c.Log.Debugln("Crafted message:", messageToSend)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
// Send message.
|
2019-03-05 19:31:13 +05:00
|
|
|
mxc.SendMessage(messageToSend)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function sends already prepared message to room.
|
|
|
|
func (mxc *MatrixConnection) SendMessage(message string) {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Debugf("Sending message to connection '%s': '%s'", mxc.connName, message)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
// We should send notices as it is preferred behavior for bots and
|
2018-02-02 09:17:40 +05:00
|
|
|
// appservices.
|
|
|
|
msg := MatrixMessage{}
|
|
|
|
msg.MsgType = "m.notice"
|
|
|
|
msg.Body = message
|
|
|
|
msg.Format = "org.matrix.custom.html"
|
|
|
|
msg.FormattedBody = message
|
|
|
|
|
|
|
|
msgBytes, err := json.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Errorln("Failed to marshal message into JSON:", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
msgStr := string(msgBytes)
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
reply, err := mxc.doPutRequest("/rooms/"+mxc.roomID+"/send/m.room.message/"+mxc.generateTnxID(), msgStr)
|
2018-02-02 09:17:40 +05:00
|
|
|
if err != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Fatalf("Failed to send message to room '%s' (conn: '%s'): %s", mxc.roomID, mxc.connName, err.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Debugf("Message sent, reply: %s", string(reply))
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mxc *MatrixConnection) Shutdown() {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Infof("Shutting down connection '%s'...", mxc.connName)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
_, err := mxc.doPostRequest("/logout", "{}")
|
|
|
|
if err != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Errorf("Error occurred while trying to log out from Matrix (conn %s): %s", mxc.connName, err.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
mxc.token = ""
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Infof("Connection '%s' successfully shutted down", mxc.connName)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|