2021-05-29 16:43:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-09 20:40:03 +00:00
|
|
|
"context"
|
2024-08-10 18:30:14 +00:00
|
|
|
"errors"
|
2021-05-29 20:05:12 +00:00
|
|
|
"fmt"
|
2024-08-09 20:40:03 +00:00
|
|
|
"net/http"
|
2021-05-29 20:05:12 +00:00
|
|
|
"strings"
|
2024-08-10 18:30:14 +00:00
|
|
|
"sync"
|
2021-07-14 16:53:03 +00:00
|
|
|
"time"
|
2021-05-29 16:43:11 +00:00
|
|
|
|
2021-05-29 20:05:12 +00:00
|
|
|
"github.com/gomarkdown/markdown"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-06-24 16:01:52 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
2021-07-16 14:41:08 +00:00
|
|
|
"maunium.net/go/mautrix"
|
2021-07-13 13:53:33 +00:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2021-05-29 16:43:11 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
var (
|
|
|
|
DEVICE_ID = id.DeviceID("jfa-go")
|
|
|
|
)
|
|
|
|
|
2021-05-29 16:43:11 +00:00
|
|
|
type MatrixDaemon struct {
|
2024-08-10 18:30:14 +00:00
|
|
|
Stopped bool
|
|
|
|
bot *mautrix.Client
|
|
|
|
userID id.UserID
|
|
|
|
homeserver string
|
|
|
|
tokens map[string]UnverifiedUser // Map of tokens to users
|
|
|
|
languages map[id.RoomID]string // Map of roomIDs to language codes
|
|
|
|
Encryption bool
|
|
|
|
crypto *Crypto
|
|
|
|
app *appContext
|
|
|
|
start int64
|
|
|
|
cancellation sync.WaitGroup
|
|
|
|
cancel context.CancelFunc
|
2021-05-29 16:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UnverifiedUser struct {
|
|
|
|
Verified bool
|
|
|
|
User *MatrixUser
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:41:08 +00:00
|
|
|
var matrixFilter = mautrix.Filter{
|
|
|
|
Room: mautrix.RoomFilter{
|
|
|
|
Timeline: mautrix.FilterPart{
|
2021-07-13 13:53:33 +00:00
|
|
|
Types: []event.Type{
|
2021-07-16 13:33:51 +00:00
|
|
|
event.EventMessage,
|
|
|
|
event.EventEncrypted,
|
|
|
|
event.StateMember,
|
2021-05-29 16:43:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EventFields: []string{
|
|
|
|
"type",
|
|
|
|
"event_id",
|
|
|
|
"room_id",
|
|
|
|
"state_key",
|
|
|
|
"sender",
|
2021-07-16 13:33:51 +00:00
|
|
|
"content",
|
|
|
|
"timestamp",
|
|
|
|
// "content.body",
|
|
|
|
// "content.membership",
|
2021-05-29 16:43:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
func (d *MatrixDaemon) renderUserID(uid id.UserID) id.UserID {
|
|
|
|
if uid[0] != '@' {
|
|
|
|
uid = "@" + uid
|
|
|
|
}
|
|
|
|
if !strings.ContainsRune(string(uid), ':') {
|
|
|
|
uid = id.UserID(string(uid) + ":" + d.homeserver)
|
|
|
|
}
|
|
|
|
return uid
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:43:11 +00:00
|
|
|
func newMatrixDaemon(app *appContext) (d *MatrixDaemon, err error) {
|
|
|
|
matrix := app.config.Section("matrix")
|
|
|
|
token := matrix.Key("token").String()
|
|
|
|
d = &MatrixDaemon{
|
2024-08-10 18:30:14 +00:00
|
|
|
userID: id.UserID(matrix.Key("user_id").String()),
|
|
|
|
homeserver: matrix.Key("homeserver").String(),
|
|
|
|
tokens: map[string]UnverifiedUser{},
|
|
|
|
languages: map[id.RoomID]string{},
|
|
|
|
app: app,
|
|
|
|
start: time.Now().UnixNano() / 1e6,
|
|
|
|
}
|
|
|
|
d.userID = d.renderUserID(d.userID)
|
|
|
|
d.bot, err = mautrix.NewClient(d.homeserver, d.userID, token)
|
2021-05-29 16:43:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
d.bot.DeviceID = DEVICE_ID
|
2021-07-16 13:33:51 +00:00
|
|
|
// resp, err := d.bot.CreateFilter(&matrixFilter)
|
|
|
|
// if err != nil {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// d.bot.Store.SaveFilterID(d.userID, resp.FilterID)
|
2023-06-20 11:19:24 +00:00
|
|
|
for _, user := range app.storage.GetMatrix() {
|
2021-05-29 16:43:11 +00:00
|
|
|
if user.Lang != "" {
|
2021-07-13 13:53:33 +00:00
|
|
|
d.languages[id.RoomID(user.RoomID)] = user.Lang
|
2021-05-29 16:43:11 +00:00
|
|
|
}
|
2021-07-13 18:02:16 +00:00
|
|
|
}
|
2021-07-16 13:33:51 +00:00
|
|
|
err = InitMatrixCrypto(d)
|
2021-05-29 16:43:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-09 20:40:03 +00:00
|
|
|
// SetTransport sets the http.Transport to use for requests. Can be used to set a proxy.
|
|
|
|
func (d *MatrixDaemon) SetTransport(t *http.Transport) {
|
|
|
|
d.bot.Client.Transport = t
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:35:34 +00:00
|
|
|
func (d *MatrixDaemon) generateAccessToken(homeserver, username, password string) (string, error) {
|
2021-07-16 14:41:08 +00:00
|
|
|
req := &mautrix.ReqLogin{
|
|
|
|
Type: mautrix.AuthTypePassword,
|
|
|
|
Identifier: mautrix.UserIdentifier{
|
|
|
|
Type: mautrix.IdentifierTypeUser,
|
2021-07-13 13:53:33 +00:00
|
|
|
User: username,
|
2021-05-30 21:35:34 +00:00
|
|
|
},
|
|
|
|
Password: password,
|
2024-08-10 18:30:14 +00:00
|
|
|
DeviceID: DEVICE_ID,
|
2021-05-30 21:35:34 +00:00
|
|
|
}
|
2021-07-16 14:41:08 +00:00
|
|
|
bot, err := mautrix.NewClient(homeserver, id.UserID(username), "")
|
2021-05-30 21:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-08-09 20:40:03 +00:00
|
|
|
resp, err := bot.Login(context.TODO(), req)
|
2021-05-30 21:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:43:11 +00:00
|
|
|
func (d *MatrixDaemon) run() {
|
2021-07-16 14:41:08 +00:00
|
|
|
syncer := d.bot.Syncer.(*mautrix.DefaultSyncer)
|
2021-07-13 18:02:16 +00:00
|
|
|
syncer.OnEventType(event.EventMessage, d.handleMessage)
|
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
d.app.info.Printf(lm.StartDaemon, lm.Matrix)
|
|
|
|
|
|
|
|
var syncCtx context.Context
|
|
|
|
syncCtx, d.cancel = context.WithCancel(context.Background())
|
|
|
|
d.cancellation.Add(1)
|
|
|
|
|
|
|
|
if err := d.bot.SyncWithContext(syncCtx); err != nil && !errors.Is(err, context.Canceled) {
|
2024-08-01 19:17:05 +00:00
|
|
|
d.app.err.Printf(lm.FailedSyncMatrix, err)
|
2021-05-29 16:43:11 +00:00
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
d.cancellation.Done()
|
2021-05-29 16:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) Shutdown() {
|
2024-08-10 18:30:14 +00:00
|
|
|
d.cancel()
|
|
|
|
d.cancellation.Wait()
|
2021-05-29 16:43:11 +00:00
|
|
|
d.Stopped = true
|
|
|
|
}
|
|
|
|
|
2024-08-09 20:40:03 +00:00
|
|
|
func (d *MatrixDaemon) handleMessage(ctx context.Context, evt *event.Event) {
|
2021-07-14 16:53:03 +00:00
|
|
|
if evt.Timestamp < d.start {
|
|
|
|
return
|
|
|
|
}
|
2021-07-13 13:53:33 +00:00
|
|
|
if evt.Sender == d.userID {
|
2021-05-29 20:05:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
lang := "en-us"
|
2021-07-13 13:53:33 +00:00
|
|
|
if l, ok := d.languages[evt.RoomID]; ok {
|
2021-05-29 20:05:12 +00:00
|
|
|
if _, ok := d.app.storage.lang.Telegram[l]; ok {
|
|
|
|
lang = l
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 13:53:33 +00:00
|
|
|
sects := strings.Split(evt.Content.Raw["body"].(string), " ")
|
2021-05-29 20:05:12 +00:00
|
|
|
switch sects[0] {
|
|
|
|
case "!lang":
|
|
|
|
if len(sects) == 2 {
|
2021-07-13 13:53:33 +00:00
|
|
|
d.commandLang(evt, sects[1], lang)
|
2021-05-29 20:05:12 +00:00
|
|
|
} else {
|
2021-07-13 13:53:33 +00:00
|
|
|
d.commandLang(evt, "", lang)
|
2021-05-29 20:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 13:53:33 +00:00
|
|
|
func (d *MatrixDaemon) commandLang(evt *event.Event, code, lang string) {
|
2021-05-29 20:05:12 +00:00
|
|
|
if code == "" {
|
|
|
|
list := "!lang <lang>\n"
|
|
|
|
for c := range d.app.storage.lang.Telegram {
|
|
|
|
list += fmt.Sprintf("%s: %s\n", c, d.app.storage.lang.Telegram[c].Meta.Name)
|
|
|
|
}
|
|
|
|
_, err := d.bot.SendText(
|
2024-08-09 20:40:03 +00:00
|
|
|
context.TODO(),
|
2021-07-13 13:53:33 +00:00
|
|
|
evt.RoomID,
|
2021-05-29 20:05:12 +00:00
|
|
|
list,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Matrix, evt.Sender, err)
|
2021-05-29 20:05:12 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := d.app.storage.lang.Telegram[code]; !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-13 13:53:33 +00:00
|
|
|
d.languages[evt.RoomID] = code
|
2023-06-20 11:19:24 +00:00
|
|
|
if u, ok := d.app.storage.GetMatrixKey(string(evt.RoomID)); ok {
|
2021-05-29 20:05:12 +00:00
|
|
|
u.Lang = code
|
2023-06-20 11:19:24 +00:00
|
|
|
d.app.storage.SetMatrixKey(string(evt.RoomID), u)
|
2021-05-29 20:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-29 16:43:11 +00:00
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
func (d *MatrixDaemon) CreateRoom(userID string) (roomID id.RoomID, err error) {
|
2021-07-16 14:41:08 +00:00
|
|
|
var room *mautrix.RespCreateRoom
|
2024-08-09 20:40:03 +00:00
|
|
|
room, err = d.bot.CreateRoom(context.TODO(), &mautrix.ReqCreateRoom{
|
2021-05-29 16:43:11 +00:00
|
|
|
Visibility: "private",
|
2021-07-13 13:53:33 +00:00
|
|
|
Invite: []id.UserID{id.UserID(userID)},
|
2021-05-30 10:47:41 +00:00
|
|
|
Topic: d.app.config.Section("matrix").Key("topic").String(),
|
2021-07-16 13:33:51 +00:00
|
|
|
IsDirect: true,
|
2021-05-29 16:43:11 +00:00
|
|
|
})
|
2021-05-30 10:47:41 +00:00
|
|
|
if err != nil {
|
2021-07-13 18:02:16 +00:00
|
|
|
return
|
2021-05-30 10:47:41 +00:00
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
// encrypted = EncryptRoom(d, room, id.UserID(userID))
|
2021-07-13 18:02:16 +00:00
|
|
|
roomID = room.RoomID
|
2024-08-10 18:30:14 +00:00
|
|
|
err = EncryptRoom(d, roomID)
|
2021-07-13 18:02:16 +00:00
|
|
|
return
|
2021-05-30 10:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
|
2024-08-10 18:30:14 +00:00
|
|
|
roomID, err := d.CreateRoom(userID)
|
2021-05-29 16:43:11 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
d.app.err.Printf(lm.FailedCreateMatrixRoom, userID, err)
|
2021-05-29 16:43:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
lang := "en-us"
|
|
|
|
pin := genAuthToken()
|
|
|
|
d.tokens[pin] = UnverifiedUser{
|
|
|
|
false,
|
|
|
|
&MatrixUser{
|
2024-08-10 18:30:14 +00:00
|
|
|
RoomID: string(roomID),
|
|
|
|
UserID: userID,
|
|
|
|
Lang: lang,
|
2021-05-29 16:43:11 +00:00
|
|
|
},
|
|
|
|
}
|
2021-07-16 14:41:08 +00:00
|
|
|
err = d.sendToRoom(
|
2021-07-16 13:33:51 +00:00
|
|
|
&event.MessageEventContent{
|
|
|
|
MsgType: event.MsgText,
|
|
|
|
Body: d.app.storage.lang.Telegram[lang].Strings.get("matrixStartMessage") + "\n\n" + pin + "\n\n" +
|
|
|
|
d.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "!lang"}),
|
|
|
|
},
|
2021-05-30 10:47:41 +00:00
|
|
|
roomID,
|
2021-05-29 16:43:11 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
d.app.err.Printf(lm.FailedMessage, lm.Matrix, userID, err)
|
2021-05-29 16:43:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:41:08 +00:00
|
|
|
func (d *MatrixDaemon) sendToRoom(content *event.MessageEventContent, roomID id.RoomID) (err error) {
|
2024-08-10 18:30:14 +00:00
|
|
|
return d.send(content, roomID)
|
|
|
|
/*if encrypted, ok := d.isEncrypted[roomID]; ok && encrypted {
|
2021-07-16 13:33:51 +00:00
|
|
|
err = SendEncrypted(d, content, roomID)
|
|
|
|
} else {
|
2024-08-09 20:40:03 +00:00
|
|
|
_, err = d.bot.SendMessageEvent(context.TODO(), roomID, event.EventMessage, content, mautrix.ReqSendEvent{})
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|
2024-08-10 18:30:14 +00:00
|
|
|
return*/
|
2021-07-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:41:08 +00:00
|
|
|
func (d *MatrixDaemon) send(content *event.MessageEventContent, roomID id.RoomID) (err error) {
|
2024-08-09 20:40:03 +00:00
|
|
|
_, err = d.bot.SendMessageEvent(context.TODO(), roomID, event.EventMessage, content, mautrix.ReqSendEvent{})
|
2021-07-16 14:41:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:02:16 +00:00
|
|
|
func (d *MatrixDaemon) Send(message *Message, users ...MatrixUser) (err error) {
|
2021-05-29 20:05:12 +00:00
|
|
|
md := ""
|
|
|
|
if message.Markdown != "" {
|
|
|
|
// Convert images to links
|
2023-06-20 20:43:25 +00:00
|
|
|
md = string(markdown.ToHTML([]byte(strings.ReplaceAll(message.Markdown, "![", "[")), nil, markdownRenderer))
|
2021-05-29 20:05:12 +00:00
|
|
|
}
|
2021-07-16 13:33:51 +00:00
|
|
|
content := &event.MessageEventContent{
|
2021-07-13 18:02:16 +00:00
|
|
|
MsgType: "m.text",
|
|
|
|
Body: message.Text,
|
|
|
|
}
|
|
|
|
if md != "" {
|
|
|
|
content.FormattedBody = md
|
|
|
|
content.Format = "org.matrix.custom.html"
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
2021-07-16 14:41:08 +00:00
|
|
|
err = d.sendToRoom(content, id.RoomID(user.RoomID))
|
2021-05-29 20:05:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-24 16:01:52 +00:00
|
|
|
// UserExists returns whether or not a user with the given User ID exists.
|
|
|
|
func (d *MatrixDaemon) UserExists(userID string) bool {
|
|
|
|
c, err := d.app.storage.db.Count(&MatrixUser{}, badgerhold.Where("UserID").Eq(userID))
|
|
|
|
return err != nil || c > 0
|
|
|
|
}
|
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
// Exists returns whether or not the given user exists.
|
|
|
|
func (d *MatrixDaemon) Exists(user ContactMethodUser) bool {
|
|
|
|
return d.UserExists(user.Name())
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:43:11 +00:00
|
|
|
// User enters ID on sign-up, a PIN is sent to them. They enter it on sign-up.
|
|
|
|
|
|
|
|
// Message the user first, to avoid E2EE by default
|
2024-08-03 20:23:59 +00:00
|
|
|
|
|
|
|
func (d *MatrixDaemon) PIN(req newUserDTO) string { return req.MatrixPIN }
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) Name() string { return lm.Matrix }
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) Required() bool {
|
|
|
|
return d.app.config.Section("telegram").Key("required").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) UniqueRequired() bool {
|
|
|
|
return d.app.config.Section("telegram").Key("require_unique").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenVerified returns whether or not a token with the given PIN has been verified, and the token itself.
|
|
|
|
func (d *MatrixDaemon) TokenVerified(pin string) (token UnverifiedUser, ok bool) {
|
|
|
|
token, ok = d.tokens[pin]
|
|
|
|
// delete(t.verifiedTokens, pin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVerifiedToken removes the token with the given PIN.
|
|
|
|
func (d *MatrixDaemon) DeleteVerifiedToken(PIN string) {
|
|
|
|
delete(d.tokens, PIN)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) UserVerified(PIN string) (ContactMethodUser, bool) {
|
|
|
|
token, ok := d.TokenVerified(PIN)
|
|
|
|
if !ok {
|
|
|
|
return &MatrixUser{}, false
|
|
|
|
}
|
|
|
|
return token.User, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MatrixDaemon) PostVerificationTasks(string, ContactMethodUser) error { return nil }
|
|
|
|
|
|
|
|
func (m *MatrixUser) Name() string { return m.UserID }
|
|
|
|
func (m *MatrixUser) SetMethodID(id any) { m.UserID = id.(string) }
|
|
|
|
func (m *MatrixUser) MethodID() any { return m.UserID }
|
|
|
|
func (m *MatrixUser) SetJellyfin(id string) { m.JellyfinID = id }
|
|
|
|
func (m *MatrixUser) Jellyfin() string { return m.JellyfinID }
|
|
|
|
func (m *MatrixUser) SetAllowContactFromDTO(req newUserDTO) { m.Contact = req.MatrixContact }
|
|
|
|
func (m *MatrixUser) SetAllowContact(contact bool) { m.Contact = contact }
|
|
|
|
func (m *MatrixUser) AllowContact() bool { return m.Contact }
|
|
|
|
func (m *MatrixUser) Store(st *Storage) {
|
|
|
|
st.SetMatrixKey(m.Jellyfin(), *m)
|
|
|
|
}
|