2024-03-23 20:09:14 +01:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"C"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"forge.redroom.link/yves/meowlib/client"
|
|
|
|
)
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InvitationAnswer
|
|
|
|
func InvitationAnswer(cc *meowlib.ContactCard, nickname string, myNickname string, serverUids []string) (*client.Peer, string, error) {
|
|
|
|
|
|
|
|
mynick := myNickname
|
|
|
|
// my nickname for that contact
|
|
|
|
|
|
|
|
if myNickname == "" {
|
|
|
|
mynick = client.GetConfig().GetIdentity().Nickname
|
|
|
|
}
|
|
|
|
|
|
|
|
// build my contact card for that friend
|
|
|
|
peer := client.GetConfig().GetIdentity().AnswerInvitation(mynick, nickname, serverUids, cc)
|
|
|
|
|
|
|
|
//peerstr, err := json.Marshal(peer)
|
|
|
|
//fmt.Println("InvitationAnswer: " + string(peerstr))
|
|
|
|
c := client.GetConfig()
|
|
|
|
c.GetIdentity().Save()
|
|
|
|
|
|
|
|
return peer, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvitationAnswerFile
|
|
|
|
func InvitationAnswerFile(invitationFile string, nickname string, myNickname string, serverUids []string) (string, error) {
|
|
|
|
format := "qr"
|
|
|
|
var filename string = ""
|
|
|
|
var cc *meowlib.ContactCard
|
|
|
|
c := client.GetConfig()
|
|
|
|
if _, err := os.Stat(invitationFile); os.IsNotExist(err) {
|
|
|
|
return "InvitationAnswerFile : os.Stat", err
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(invitationFile, ".mwiv") {
|
|
|
|
format = "mwiv"
|
|
|
|
data, err := os.ReadFile(invitationFile)
|
|
|
|
if err != nil {
|
|
|
|
return "InvitationAnswerFile : os.ReadFile", err
|
|
|
|
}
|
|
|
|
cc, err = meowlib.NewContactCardFromCompressed(data)
|
|
|
|
if err != nil {
|
|
|
|
return "InvitationAnswerFile : NewContactCardFromCompressed", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
identity := client.GetConfig().GetIdentity()
|
|
|
|
if cc != nil {
|
|
|
|
isAnswer, proposed, received, _ := identity.CheckInvitation(cc)
|
|
|
|
if isAnswer {
|
|
|
|
fmt.Fprintln(os.Stdout, "This is already a response "+proposed+" to your invitation.")
|
|
|
|
fmt.Fprintln(os.Stdout, "You cannot answer again.")
|
|
|
|
fmt.Fprintln(os.Stdout, "You should finalize it by importing "+proposed+" contact card to your meow.")
|
|
|
|
fmt.Fprintln(os.Stdout, "Use : 'meow invitation finalize "+invitationFile+"' to do it.")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
mynick := myNickname
|
|
|
|
// my nickname for that contact
|
|
|
|
|
|
|
|
if myNickname == "" {
|
|
|
|
mynick = client.GetConfig().GetIdentity().Nickname
|
|
|
|
}
|
|
|
|
|
|
|
|
response := identity.AnswerInvitation(mynick, nickname, serverUids, cc)
|
|
|
|
fmt.Fprintln(os.Stdout, "Invitation sent by "+received)
|
|
|
|
if format == "qr" {
|
|
|
|
filename = c.StoragePath + string(os.PathSeparator) + mynick + "-" + nickname + ".png"
|
|
|
|
response.GetMyContact().WriteQr(filename)
|
|
|
|
} else {
|
|
|
|
filename = c.StoragePath + string(os.PathSeparator) + mynick + "-" + nickname + ".mwiv"
|
|
|
|
response.GetMyContact().WriteCompressed(filename)
|
|
|
|
}
|
|
|
|
client.GetConfig().GetIdentity().Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvitationAnswerMessage
|
2024-03-26 23:42:56 +01:00
|
|
|
func InvitationAnswerMessage(invitationId string, invitationServerUid string, timeout int) ([]byte, string, error) {
|
2024-03-23 20:09:14 +01:00
|
|
|
|
|
|
|
// find the peer with that invitation id
|
|
|
|
var peer *client.Peer
|
|
|
|
for i := len(client.GetConfig().GetIdentity().Peers) - 1; i >= 0; i-- { //! to allow self invitation : testing only, findinc the received peer before myself
|
|
|
|
// for i := 0; i < len(client.GetConfig().GetIdentity().Peers); i++ {
|
|
|
|
if client.GetConfig().GetIdentity().Peers[i].InvitationId == invitationId {
|
|
|
|
peer = client.GetConfig().GetIdentity().Peers[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if peer == nil {
|
|
|
|
// declare a custom go error for no peer found
|
|
|
|
return nil, "InvitationAnswerMessage: loop for peer", errors.New("no peer with that invitation id")
|
|
|
|
}
|
|
|
|
answermsg, err := peer.BuildInvitationAnswerMessage(peer.GetMyContact())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "InvitationAnswerMessage: BuildInvitationAnswserMessage", err
|
|
|
|
}
|
|
|
|
// Server: get the invitation server
|
|
|
|
invitationServer, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "InvitationAnswerMessage: LoadServer", err
|
|
|
|
}
|
|
|
|
|
2024-03-26 20:22:56 +01:00
|
|
|
// this will be the invitation's payload
|
2024-03-23 20:09:14 +01:00
|
|
|
packedMsg, err := peer.ProcessOutboundUserMessage(answermsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "InvitationAnswerMessage: ProcessOutboundUserMessage", err
|
|
|
|
}
|
|
|
|
// Creating Server message for transporting the user message
|
2024-03-31 18:22:17 +02:00
|
|
|
toServerMessage, err := invitationServer.BuildToServerMessageInvitationAnswer(packedMsg, peer.MyIdentity.Public, invitationId, timeout)
|
2024-03-23 20:09:14 +01:00
|
|
|
if err != nil {
|
2024-03-26 23:42:56 +01:00
|
|
|
return nil, "InvitationAnswerMessage: BuildToServerMessageInvitationAnswer", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
2024-03-26 23:42:56 +01:00
|
|
|
|
|
|
|
// Server outbound processing
|
2024-03-23 20:09:14 +01:00
|
|
|
bytemsg, err := invitationServer.ProcessOutboundMessage(toServerMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "InvitationAnswerMessage: ProcessOutboundMessage", err
|
|
|
|
}
|
|
|
|
return bytemsg, "", nil
|
|
|
|
}
|
2024-03-26 23:42:56 +01:00
|
|
|
|
|
|
|
// InvitationAnswerMessageReadResponse
|
|
|
|
// Called by the invitation receiver
|
|
|
|
// invitationData: the data received from the server
|
|
|
|
// invitationServerUid: the uid of the server holding the invitation
|
|
|
|
func InvitationAnswerMessageReadResponse(invitationData []byte, invitationServerUid string) (*meowlib.Invitation, string, error) {
|
|
|
|
|
|
|
|
server, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid)
|
|
|
|
if err != nil {
|
2024-03-28 00:45:40 +01:00
|
|
|
return nil, "InvitationAnswerMessageReadResponse: LoadServer", err
|
2024-03-26 23:42:56 +01:00
|
|
|
}
|
|
|
|
// Server inbound processing : get the invitation server
|
|
|
|
serverMsg, err := server.ProcessInboundServerResponse(invitationData)
|
|
|
|
if err != nil {
|
2024-03-28 00:45:40 +01:00
|
|
|
return nil, "InvitationAnswerMessageReadResponse: ProcessInboundServerResponse", err
|
2024-03-26 23:42:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return serverMsg.Invitation, "", nil
|
|
|
|
|
|
|
|
}
|