2020-02-26 15:07:33 +00:00
|
|
|
package main
|
2020-02-16 18:30:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-28 21:08:18 +00:00
|
|
|
"os"
|
2020-02-17 18:02:26 +00:00
|
|
|
"strings"
|
2020-02-16 18:30:49 +00:00
|
|
|
|
2020-02-17 08:41:08 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-02-16 22:27:03 +00:00
|
|
|
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
|
2020-02-17 18:02:26 +00:00
|
|
|
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
|
2020-02-16 18:30:49 +00:00
|
|
|
)
|
|
|
|
|
2020-02-21 13:27:42 +00:00
|
|
|
var mx *mxlib.Client
|
|
|
|
|
2020-03-02 20:51:13 +00:00
|
|
|
func StartAppService(errch chan error) (*http.Server, error) {
|
2020-02-26 15:07:33 +00:00
|
|
|
mx = mxlib.NewClient(config.Server, registration.AsToken)
|
2020-02-21 13:27:42 +00:00
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
err := InitDb()
|
|
|
|
if err != nil {
|
2020-03-02 20:51:13 +00:00
|
|
|
return nil, err
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 21:08:18 +00:00
|
|
|
if dbKvGet("ezbr_initialized") != "yes" {
|
|
|
|
err = mx.RegisterUser(registration.SenderLocalpart)
|
|
|
|
if mxe, ok := err.(*mxlib.MxError); !ok || mxe.ErrCode != "M_USER_IN_USE" {
|
2020-03-02 20:51:13 +00:00
|
|
|
return nil, err
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
2020-02-28 21:08:18 +00:00
|
|
|
|
|
|
|
_, st := os.Stat(config.AvatarFile)
|
|
|
|
if !os.IsNotExist(st) {
|
|
|
|
err = mx.ProfileAvatar(ezbrMxId(), &connector.FileMediaObject{
|
|
|
|
Path: config.AvatarFile,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-03-02 20:51:13 +00:00
|
|
|
return nil, err
|
2020-02-28 21:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:57:53 +00:00
|
|
|
err = mx.ProfileDisplayname(ezbrMxId(), fmt.Sprintf("Easybridge (%s)", EASYBRIDGE_SYSTEM_PROTOCOL))
|
|
|
|
if err != nil {
|
2020-03-02 20:51:13 +00:00
|
|
|
return nil, err
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
2020-02-28 21:08:18 +00:00
|
|
|
|
|
|
|
dbKvPut("ezbr_initialized", "yes")
|
2020-02-21 14:12:22 +00:00
|
|
|
}
|
2020-02-17 14:30:01 +00:00
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/_matrix/app/v1/transactions/{txnId}", handleTxn)
|
2020-02-16 21:07:41 +00:00
|
|
|
router.HandleFunc("/transactions/{txnId}", handleTxn)
|
2020-02-16 18:30:49 +00:00
|
|
|
|
2020-03-02 20:51:13 +00:00
|
|
|
log.Printf("Starting HTTP server on %s", config.ASBindAddr)
|
|
|
|
http_server := &http.Server{
|
|
|
|
Addr: config.ASBindAddr,
|
|
|
|
Handler: checkTokenAndLog(router),
|
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
go func() {
|
2020-03-02 20:51:13 +00:00
|
|
|
err := http_server.ListenAndServe()
|
2020-02-16 18:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-28 16:42:15 +00:00
|
|
|
// Notify users that Easybridge has restarted
|
|
|
|
go func() {
|
|
|
|
var users []DbAccountConfig
|
|
|
|
db.Model(&DbAccountConfig{}).Select("mx_user_id").Group("mx_user_id").Find(&users)
|
|
|
|
for _, u := range users {
|
|
|
|
ezbrSystemSendf(u.MxUserID,
|
|
|
|
"Easybridge has restarted, please visit %s or open configuration widget to reconnect to your accounts.",
|
|
|
|
config.WebURL)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-02 20:51:13 +00:00
|
|
|
return http_server, nil
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkTokenAndLog(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
if strings.Join(r.Form["access_token"], "") != registration.HsToken {
|
|
|
|
http.Error(w, "Wrong or no token provided", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-27 09:40:52 +00:00
|
|
|
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, strings.Split(r.URL.String(), "?")[0])
|
2020-02-16 18:30:49 +00:00
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleTxn(w http.ResponseWriter, r *http.Request) {
|
2020-02-16 21:57:30 +00:00
|
|
|
if r.Method == "PUT" {
|
|
|
|
var txn mxlib.Transaction
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&txn)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2020-02-17 20:04:21 +00:00
|
|
|
log.Warnf("JSON decode error: %s\n", err)
|
2020-02-16 21:57:30 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
|
2020-02-27 09:35:09 +00:00
|
|
|
log.Debugf("Processing transaction %s (%d events)", mux.Vars(r)["txnId"], len(txn.Events))
|
|
|
|
log.Tracef("Transaction content: %#v\n", txn)
|
2020-02-16 22:27:03 +00:00
|
|
|
|
|
|
|
for i := range txn.Events {
|
2020-02-17 20:04:21 +00:00
|
|
|
ev := &txn.Events[i]
|
2020-02-29 09:51:09 +00:00
|
|
|
if isBridgedIdentifier(ev.Sender) {
|
2020-02-17 20:12:30 +00:00
|
|
|
// Don't do anything with ezbr events that come back to us
|
2020-02-17 20:04:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = handleTxnEvent(ev)
|
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSend(ev.Sender, fmt.Sprintf("Could not process %s (%s): %s", ev.Type, ev.Sender, err))
|
|
|
|
}
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 21:57:30 +00:00
|
|
|
fmt.Fprintf(w, "{}\n")
|
|
|
|
} else {
|
|
|
|
http.Error(w, "Expected PUT request", http.StatusBadRequest)
|
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
2020-02-16 22:27:03 +00:00
|
|
|
|
2020-02-17 20:04:21 +00:00
|
|
|
func handleTxnEvent(e *mxlib.Event) error {
|
2020-02-16 22:27:03 +00:00
|
|
|
if e.Type == "m.room.message" {
|
|
|
|
ev := &connector.Event{
|
|
|
|
Type: connector.EVENT_MESSAGE,
|
|
|
|
Text: e.Content["body"].(string),
|
2020-02-26 15:51:15 +00:00
|
|
|
Id: e.EventId,
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
|
|
|
typ := e.Content["msgtype"].(string)
|
|
|
|
if typ == "m.emote" {
|
|
|
|
ev.Type = connector.EVENT_MESSAGE
|
2020-02-21 17:08:40 +00:00
|
|
|
} else if typ == "m.file" || typ == "m.image" {
|
|
|
|
ev.Text = ""
|
2020-02-29 17:30:43 +00:00
|
|
|
ev.Attachments = []connector.SMediaObject{
|
|
|
|
connector.SMediaObject{mx.ParseMediaInfo(e.Content)},
|
|
|
|
}
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 14:30:01 +00:00
|
|
|
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
|
2020-02-17 20:04:21 +00:00
|
|
|
if pm_room.Protocol == EASYBRIDGE_SYSTEM_PROTOCOL {
|
|
|
|
handleSystemMessage(e.Sender, e.Content["body"].(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// If this is a private message room
|
2020-02-16 22:27:03 +00:00
|
|
|
acct := FindAccount(pm_room.MxUserID, pm_room.AccountName)
|
2020-02-17 20:04:21 +00:00
|
|
|
if acct == nil {
|
|
|
|
return fmt.Errorf("Not connected to %s", pm_room.AccountName)
|
|
|
|
} else if e.Sender == pm_room.MxUserID {
|
2020-02-16 22:27:03 +00:00
|
|
|
ev.Author = acct.Conn.User()
|
|
|
|
ev.Recipient = pm_room.UserID
|
2020-02-29 09:01:42 +00:00
|
|
|
_, err := acct.Conn.Send(ev)
|
|
|
|
return err
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
2020-02-17 20:04:21 +00:00
|
|
|
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
|
|
|
|
// If this is a regular room
|
2020-02-16 22:27:03 +00:00
|
|
|
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
|
|
|
|
if acct != nil {
|
|
|
|
ev.Author = acct.Conn.User()
|
|
|
|
ev.Room = room.RoomID
|
2020-02-29 09:01:42 +00:00
|
|
|
created_ev_id, err := acct.Conn.Send(ev)
|
|
|
|
if err == nil && created_ev_id != "" {
|
|
|
|
cache_key := fmt.Sprintf("%s/event_seen/%s/%s",
|
|
|
|
room.Protocol, e.RoomId, created_ev_id)
|
|
|
|
dbKvPut(cache_key, "yes")
|
|
|
|
}
|
|
|
|
return err
|
2020-02-17 15:28:32 +00:00
|
|
|
} else {
|
2020-02-21 13:27:42 +00:00
|
|
|
mx.RoomKick(e.RoomId, e.Sender, fmt.Sprintf("Not present in %s on %s, please talk with Easybridge to rejoin", room.RoomID, room.Protocol))
|
2020-02-17 20:04:21 +00:00
|
|
|
return fmt.Errorf("not joined %s on %s", room.RoomID, room.Protocol)
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
2020-02-17 20:04:21 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Room not bridged")
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
2020-02-17 14:30:01 +00:00
|
|
|
} else if e.Type == "m.room.member" {
|
|
|
|
ms := e.Content["membership"].(string)
|
|
|
|
if ms == "leave" {
|
|
|
|
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
|
2020-02-28 11:16:59 +00:00
|
|
|
// If user leaves a PM room, we must drop it
|
|
|
|
dbDeletePmRoom(pm_room)
|
2020-02-17 14:30:01 +00:00
|
|
|
them_mx := userMxId(pm_room.Protocol, pm_room.UserID)
|
2020-02-21 13:27:42 +00:00
|
|
|
mx.RoomLeaveAs(e.RoomId, them_mx)
|
2020-02-17 20:04:21 +00:00
|
|
|
return nil
|
|
|
|
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
|
|
|
|
// If leaving a public room, leave from server as well
|
2020-02-17 14:30:01 +00:00
|
|
|
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
|
|
|
|
if acct != nil {
|
|
|
|
acct.Conn.Leave(room.RoomID)
|
2020-02-28 11:16:59 +00:00
|
|
|
acct.delAutojoin(room.RoomID)
|
2020-02-17 20:04:21 +00:00
|
|
|
return nil
|
2020-02-17 15:28:32 +00:00
|
|
|
} else {
|
2020-02-21 13:27:42 +00:00
|
|
|
mx.RoomKick(e.RoomId, e.Sender, fmt.Sprintf("Not present in %s on %s, please talk with Easybridge to rejoin", room.RoomID, room.Protocol))
|
2020-02-17 20:04:21 +00:00
|
|
|
return fmt.Errorf("not joined %s on %s", room.RoomID, room.Protocol)
|
2020-02-17 14:30:01 +00:00
|
|
|
}
|
2020-02-17 20:04:21 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Room not bridged")
|
2020-02-17 14:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 14:30:01 +00:00
|
|
|
} else if e.Type == "m.room.topic" {
|
|
|
|
if room := dbIsPublicRoom(e.RoomId); room != nil {
|
|
|
|
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
|
|
|
|
if acct != nil {
|
2020-02-17 20:04:21 +00:00
|
|
|
return acct.Conn.SetRoomInfo(room.RoomID, &connector.RoomInfo{
|
2020-02-17 14:30:01 +00:00
|
|
|
Topic: e.Content["topic"].(string),
|
|
|
|
})
|
2020-02-17 20:04:21 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Could not find room account for %s %s %s", e.Sender, room.Protocol, room.RoomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSystemMessage(mxid string, msg string) {
|
|
|
|
cmd := strings.Fields(msg)
|
|
|
|
switch cmd[0] {
|
|
|
|
case "help":
|
|
|
|
ezbrSystemSend(mxid, "Welcome to Easybridge! Here is a list of available commands:")
|
|
|
|
ezbrSystemSend(mxid, "- help: request help")
|
|
|
|
ezbrSystemSend(mxid, "- list: list accounts")
|
|
|
|
ezbrSystemSend(mxid, "- accounts: list accounts")
|
|
|
|
ezbrSystemSend(mxid, "- join <protocol or account> <room id>: join public chat room")
|
2020-02-26 15:03:55 +00:00
|
|
|
ezbrSystemSend(mxid, "- talk <protocol or account> <user id>: open private conversation to contact")
|
2020-02-17 20:12:30 +00:00
|
|
|
case "list", "account", "accounts":
|
2020-02-17 20:04:21 +00:00
|
|
|
one := false
|
|
|
|
if accts, ok := registeredAccounts[mxid]; ok {
|
|
|
|
for name, acct := range accts {
|
|
|
|
one = true
|
|
|
|
ezbrSystemSendf(mxid, "- %s (%s)", name, acct.Protocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !one {
|
|
|
|
ezbrSystemSendf(mxid, "No account currently configured")
|
|
|
|
}
|
|
|
|
case "join":
|
2020-02-26 15:03:55 +00:00
|
|
|
account := findAccount(mxid, cmd[1])
|
2020-02-17 20:04:21 +00:00
|
|
|
if account != nil {
|
|
|
|
err := account.Conn.Join(connector.RoomID(cmd[2]))
|
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSendf(mxid, "%s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ezbrSystemSendf(mxid, "No account with name or using protocol %s", cmd[1])
|
|
|
|
}
|
2020-02-28 21:49:31 +00:00
|
|
|
case "query", "talk":
|
2020-02-26 15:03:55 +00:00
|
|
|
account := findAccount(mxid, cmd[1])
|
2020-02-17 20:04:21 +00:00
|
|
|
if account != nil {
|
|
|
|
quser := connector.UserID(cmd[2])
|
2020-02-17 20:19:28 +00:00
|
|
|
err := account.Conn.Invite(quser, connector.RoomID(""))
|
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSendf(mxid, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-17 20:04:21 +00:00
|
|
|
quser_mxid, err := dbGetMxUser(account.Protocol, quser)
|
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSendf(mxid, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = dbGetMxPmRoom(account.Protocol, quser, quser_mxid, mxid, account.AccountName)
|
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSendf(mxid, "%s", err)
|
2020-02-17 14:30:01 +00:00
|
|
|
}
|
2020-02-17 20:04:21 +00:00
|
|
|
} else {
|
|
|
|
ezbrSystemSendf(mxid, "No account with name or using protocol %s", cmd[1])
|
2020-02-17 14:30:01 +00:00
|
|
|
}
|
2020-02-17 20:04:21 +00:00
|
|
|
default:
|
|
|
|
ezbrSystemSend(mxid, "Unrecognized command. Type `help` if you need some help!")
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 15:03:55 +00:00
|
|
|
|
|
|
|
func findAccount(mxid string, q string) *Account {
|
|
|
|
if accts, ok := registeredAccounts[mxid]; ok {
|
|
|
|
for name, acct := range accts {
|
2020-02-28 09:39:37 +00:00
|
|
|
if strings.EqualFold(name, q) || strings.EqualFold(acct.Protocol, q) {
|
2020-02-26 15:03:55 +00:00
|
|
|
return acct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|