2026-03-11 22:06:44 +01:00
|
|
|
// Copyright (C) 2026 saces@c-base.org
|
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2026-01-31 08:13:53 +01:00
|
|
|
package mxapi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-03-17 18:16:21 +01:00
|
|
|
"errors"
|
2026-01-31 08:13:53 +01:00
|
|
|
"fmt"
|
2026-03-17 18:16:21 +01:00
|
|
|
"os"
|
2026-01-31 08:13:53 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
type LoginInfo struct {
|
|
|
|
|
DiscoverInfo DiscoverInfo `json:"discover_info"`
|
|
|
|
|
Password string `json:"password"`
|
|
|
|
|
DeviceID id.DeviceID `json:"deviceid"`
|
|
|
|
|
DeviceName string `json:"devicename"`
|
|
|
|
|
MXPassFile string `json:"mxpassfile"`
|
2026-01-31 08:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
func Login(li *LoginInfo) (string, error) {
|
2026-01-31 08:13:53 +01:00
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
if li.MXPassFile != "" {
|
|
|
|
|
if _, err := os.Stat(li.MXPassFile); err == nil {
|
|
|
|
|
return "", fmt.Errorf("mxpassfile '%s' already exists", li.MXPassFile)
|
2026-03-17 18:16:21 +01:00
|
|
|
} else if !errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return "", fmt.Errorf("error while checking mxpassfile: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
mauclient, err := mautrix.NewClient(li.DiscoverInfo.Homeserver, li.DiscoverInfo.UserID, "")
|
2026-01-31 08:13:53 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 18:16:21 +01:00
|
|
|
now := time.Now()
|
2026-04-29 00:35:12 +02:00
|
|
|
if li.DeviceID == "" {
|
|
|
|
|
li.DeviceID = id.DeviceID(fmt.Sprintf("libmxclient-%d", now.Unix()))
|
2026-03-17 18:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
if li.DeviceName == "" {
|
|
|
|
|
li.DeviceName = fmt.Sprintf("libmxclient-%s", now.Format(time.RFC3339))
|
2026-03-17 18:16:21 +01:00
|
|
|
}
|
2026-01-31 08:13:53 +01:00
|
|
|
|
|
|
|
|
resp, err := mauclient.Login(context.Background(), &mautrix.ReqLogin{
|
|
|
|
|
Type: "m.login.password",
|
|
|
|
|
Identifier: mautrix.UserIdentifier{
|
|
|
|
|
Type: "m.id.user",
|
2026-04-29 00:35:12 +02:00
|
|
|
User: li.DiscoverInfo.LoginName,
|
2026-01-31 08:13:53 +01:00
|
|
|
},
|
2026-04-29 00:35:12 +02:00
|
|
|
Password: li.Password,
|
|
|
|
|
DeviceID: li.DeviceID,
|
|
|
|
|
InitialDeviceDisplayName: li.DeviceName,
|
2026-01-31 08:13:53 +01:00
|
|
|
StoreCredentials: false,
|
|
|
|
|
StoreHomeserverURL: false,
|
|
|
|
|
RefreshToken: false,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
res := fmt.Sprintf("%s | %s | %s | %s\n", li.DiscoverInfo.Homeserver, li.DiscoverInfo.LoginName, li.DiscoverInfo.UserID.Homeserver(), resp.AccessToken)
|
2026-03-17 18:16:21 +01:00
|
|
|
|
2026-04-29 00:35:12 +02:00
|
|
|
if li.MXPassFile != "" {
|
|
|
|
|
err := os.WriteFile(li.MXPassFile, []byte(res), 0600)
|
2026-03-17 18:16:21 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("unable to write file: %w", err)
|
|
|
|
|
}
|
2026-04-29 00:35:12 +02:00
|
|
|
return "", nil
|
2026-03-17 18:16:21 +01:00
|
|
|
}
|
2026-01-31 08:13:53 +01:00
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|