2026-04-11 22:05:30 +02:00
|
|
|
package files
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
|
"forge.redroom.link/yves/meowlib/client"
|
|
|
|
|
"forge.redroom.link/yves/meowlib/client/invitation/messages"
|
2026-04-12 13:38:15 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2026-04-11 22:05:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Step2ReadAndAnswer reads an InvitationInitPayload from a .mwiv file, creates the
|
2026-04-12 13:38:15 +02:00
|
|
|
// invitee's peer entry, and writes the encrypted ContactCard (PackedUserMessage) to a
|
|
|
|
|
// .mwiv file for the initiator to pick up and process in step 3.
|
2026-04-11 22:05:30 +02:00
|
|
|
func Step2ReadAndAnswer(invitationFile string, nickname string, myNickname string, serverUids []string) error {
|
|
|
|
|
if _, err := os.Stat(invitationFile); os.IsNotExist(err) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !strings.HasSuffix(invitationFile, ".mwiv") {
|
|
|
|
|
return errors.New("only .mwiv files are supported")
|
|
|
|
|
}
|
|
|
|
|
data, err := os.ReadFile(invitationFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
payload, err := meowlib.NewInvitationInitPayloadFromCompressed(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mynick := myNickname
|
|
|
|
|
if mynick == "" {
|
|
|
|
|
mynick = client.GetConfig().GetIdentity().Nickname
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:38:15 +02:00
|
|
|
packed, peer, err := messages.Step2InviteeCreatesInitiatorAndEncryptedContactCard(payload, nickname, mynick, serverUids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wrap the PackedUserMessage in an Invitation so the initiator (step3) has the
|
|
|
|
|
// invitee's public key available for signature verification without an extra file.
|
|
|
|
|
packedBytes, err := proto.Marshal(packed)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
invitation := &meowlib.Invitation{
|
|
|
|
|
Uuid: peer.InvitationId,
|
|
|
|
|
Step: 2,
|
|
|
|
|
From: peer.MyIdentity.Public,
|
|
|
|
|
Payload: packedBytes,
|
|
|
|
|
}
|
|
|
|
|
out, err := proto.Marshal(invitation)
|
2026-04-11 22:05:30 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c := client.GetConfig()
|
|
|
|
|
filename := c.StoragePath + string(os.PathSeparator) + mynick + "-" + nickname + ".mwiv"
|
2026-04-12 13:38:15 +02:00
|
|
|
return os.WriteFile(filename, out, 0600)
|
2026-04-11 22:05:30 +02:00
|
|
|
}
|