103 lines
4.1 KiB
Go
103 lines
4.1 KiB
Go
package helpers
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"forge.redroom.link/yves/meowlib/client"
|
|
)
|
|
|
|
// InvitationStep1CreatePeer creates a minimal pending peer and returns the InvitationInitPayload
|
|
// to be transmitted to the invitee (STEP_1).
|
|
func InvitationStep1CreatePeer(contactName string, myNickname string, invitationMessage string, serverUids []string) (*meowlib.InvitationInitPayload, *client.Peer, string, error) {
|
|
mynick := myNickname
|
|
if myNickname == "" {
|
|
mynick = client.GetConfig().GetIdentity().Nickname
|
|
}
|
|
payload, peer, err := client.GetConfig().GetIdentity().InvitationStep1(mynick, contactName, serverUids, invitationMessage)
|
|
if err != nil {
|
|
return nil, nil, "InvitationStep1CreatePeer: InvitationStep1", err
|
|
}
|
|
client.GetConfig().GetIdentity().Save()
|
|
return payload, peer, "", nil
|
|
}
|
|
|
|
// InvitationStep1File creates a pending peer and writes the InvitationInitPayload to a file
|
|
// (format: "qr" for QR-code PNG, anything else for compressed binary .mwiv).
|
|
func InvitationStep1File(contactName string, myNickname string, invitationMessage string, serverUids []string, format string) (*client.Peer, string, error) {
|
|
payload, peer, errdata, err := InvitationStep1CreatePeer(contactName, myNickname, invitationMessage, serverUids)
|
|
if err != nil {
|
|
return nil, errdata, err
|
|
}
|
|
c := client.GetConfig()
|
|
if format == "qr" {
|
|
filename := c.StoragePath + string(os.PathSeparator) + peer.MyName + "-" + peer.Name + ".png"
|
|
if err := payload.WriteQr(filename); err != nil {
|
|
return nil, "InvitationStep1File: WriteQr", err
|
|
}
|
|
} else {
|
|
filename := c.StoragePath + string(os.PathSeparator) + peer.MyName + "-" + peer.Name + ".mwiv"
|
|
if err := payload.WriteCompressed(filename); err != nil {
|
|
return nil, "InvitationStep1File: WriteCompressed", err
|
|
}
|
|
}
|
|
return peer, "", nil
|
|
}
|
|
|
|
// InvitationStep1Message builds and returns the packed server message that posts the
|
|
// InvitationInitPayload to the invitation server (STEP_1 through-server variant).
|
|
func InvitationStep1Message(invitationId string, invitationServerUid string, timeOut int, urlLen int, password string) ([]byte, string, error) {
|
|
peer := client.GetConfig().GetIdentity().Peers.GetFromInvitationId(invitationId)
|
|
if peer == nil {
|
|
return nil, "InvitationStep1Message: peer not found", nil
|
|
}
|
|
if peer.InvitationKp == nil {
|
|
return nil, "InvitationStep1Message: peer has no InvitationKp", nil
|
|
}
|
|
initPayload := &meowlib.InvitationInitPayload{
|
|
Uuid: peer.InvitationId,
|
|
Name: peer.MyName,
|
|
PublicKey: peer.InvitationKp.Public,
|
|
InvitationMessage: peer.InvitationMessage,
|
|
}
|
|
invitationServer, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid)
|
|
if err != nil {
|
|
return nil, "InvitationStep1Message: LoadServer", err
|
|
}
|
|
msg, err := invitationServer.BuildToServerMessageInvitationStep1(initPayload, password, timeOut, urlLen)
|
|
if err != nil {
|
|
return nil, "InvitationStep1Message: BuildToServerMessageInvitationStep1", err
|
|
}
|
|
bytemsg, err := invitationServer.ProcessOutboundMessage(msg)
|
|
if err != nil {
|
|
return nil, "InvitationStep1Message: ProcessOutboundMessage", err
|
|
}
|
|
return bytemsg, "", nil
|
|
}
|
|
|
|
// InvitationStep1ReadResponse reads the server response to a Step1 message (shortcode URL + expiry).
|
|
func InvitationStep1ReadResponse(invitationServerUid string, invitationResponse []byte) (*meowlib.Invitation, string, error) {
|
|
server, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid)
|
|
if err != nil {
|
|
return nil, "InvitationStep1ReadResponse: LoadServer", err
|
|
}
|
|
serverMsg, err := server.ProcessInboundServerResponse(invitationResponse)
|
|
if err != nil {
|
|
return nil, "InvitationStep1ReadResponse: ProcessInboundServerResponse", err
|
|
}
|
|
return serverMsg.Invitation, "", nil
|
|
}
|
|
|
|
// InvitationSetUrlInfo stores the shortcode URL and expiry on the pending peer.
|
|
func InvitationSetUrlInfo(invitationId string, url string, expiry int64) {
|
|
id := client.GetConfig().GetIdentity()
|
|
peer := id.Peers.GetFromInvitationId(invitationId)
|
|
if peer == nil {
|
|
return
|
|
}
|
|
peer.InvitationUrl = url
|
|
peer.InvitationExpiry = time.Unix(expiry, 0)
|
|
id.Peers.StorePeer(peer)
|
|
}
|