more click, more api, even more fixes

This commit is contained in:
saces 2026-03-17 18:16:21 +01:00
parent 8fcf9b4785
commit cc8c77e780
18 changed files with 318 additions and 189 deletions

View file

@ -4,9 +4,11 @@ package mxapi
import (
"context"
"crypto/sha256"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"os"
"time"
"maunium.net/go/mautrix"
@ -14,10 +16,15 @@ import (
)
type login_data struct {
Homeserver string `json:"homeserver"`
Mxid string `json:"mxid"`
Loginname string `json:"loginname"`
Password string `json:"password"`
Homeserver string `json:"homeserver"`
Mxid string `json:"mxid"`
Loginname string `json:"loginname"`
Password string `json:"password"`
DeviceID string `json:"deviceid"`
DeviceName string `json:"devicename"`
MXPassFile string `json:"mxpassfile"`
MakeMasterKey bool `json:"make_master_key"`
MakeRecoveryKey bool `json:"make_recovery_key"`
}
func Login(data string) (string, error) {
@ -27,12 +34,27 @@ func Login(data string) (string, error) {
return "", err
}
if ld.MXPassFile != "" {
if _, err := os.Stat(ld.MXPassFile); err == nil {
return "", fmt.Errorf("mxpassfile '%s' already exists", ld.MXPassFile)
} else if !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("error while checking mxpassfile: %v", err)
}
}
mauclient, err := mautrix.NewClient(ld.Homeserver, id.UserID(ld.Mxid), "")
if err != nil {
return "", err
}
deviceName := fmt.Sprintf("smalbot-%d", time.Now().Unix())
now := time.Now()
if ld.DeviceID == "" {
ld.DeviceID = fmt.Sprintf("libmxclient-%d", now.Unix())
}
if ld.DeviceName == "" {
ld.DeviceName = fmt.Sprintf("libmxclient-%s", now.Format(time.RFC3339))
}
resp, err := mauclient.Login(context.Background(), &mautrix.ReqLogin{
Type: "m.login.password",
@ -41,8 +63,8 @@ func Login(data string) (string, error) {
User: ld.Loginname,
},
Password: ld.Password,
DeviceID: id.DeviceID(deviceName),
InitialDeviceDisplayName: deviceName,
DeviceID: id.DeviceID(ld.DeviceID),
InitialDeviceDisplayName: ld.DeviceName,
StoreCredentials: false,
StoreHomeserverURL: false,
RefreshToken: false,
@ -51,8 +73,27 @@ func Login(data string) (string, error) {
return "", err
}
tpl := "%s | %s | %s | %s\nrecovery | | | %x\nmaster | | | %x\n"
res := fmt.Sprintf(tpl, ld.Homeserver, ld.Loginname, id.UserID(ld.Mxid).Homeserver(), resp.AccessToken, sha256.Sum256([]byte(ld.Password)), sha256.Sum256([]byte(deviceName)))
res := fmt.Sprintf("%s | %s | %s | %s\n", ld.Homeserver, ld.Loginname, id.UserID(ld.Mxid).Homeserver(), resp.AccessToken)
if ld.MakeMasterKey {
masterkey := make([]byte, 32)
rand.Read(masterkey)
res = fmt.Sprintf("%smaster | | | %x\n", res, masterkey)
}
if ld.MakeRecoveryKey {
recoverykey := make([]byte, 32)
rand.Read(recoverykey)
res = fmt.Sprintf("%srecovery | | | %x\n", res, recoverykey)
}
if ld.MXPassFile != "" {
err := os.WriteFile(ld.MXPassFile, []byte(res), 0600)
if err != nil {
return "", fmt.Errorf("unable to write file: %w", err)
}
return "SUCCESS.", nil
}
return res, nil
}