154 lines
5.2 KiB
Go
154 lines
5.2 KiB
Go
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
|
|
func InvitationAnswerMessage(invitationId string, invitationServerUid string, timeout int) ([]byte, string, error) {
|
|
|
|
// 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
|
|
}
|
|
|
|
// this will be the invitation's payload
|
|
packedMsg, err := peer.ProcessOutboundUserMessage(answermsg)
|
|
if err != nil {
|
|
return nil, "InvitationAnswerMessage: ProcessOutboundUserMessage", err
|
|
}
|
|
// Creating Server message for transporting the user message
|
|
toServerMessage, err := invitationServer.BuildToServerMessageInvitationAnswer(packedMsg, peer.MyIdentity.Public, invitationId, timeout)
|
|
if err != nil {
|
|
return nil, "InvitationAnswerMessage: BuildToServerMessageInvitationAnswer", err
|
|
}
|
|
|
|
// Server outbound processing
|
|
bytemsg, err := invitationServer.ProcessOutboundMessage(toServerMessage)
|
|
if err != nil {
|
|
return nil, "InvitationAnswerMessage: ProcessOutboundMessage", err
|
|
}
|
|
return bytemsg, "", nil
|
|
}
|
|
|
|
// 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 {
|
|
return nil, "InvitationAnswerMessageReadResponse: LoadServer", err
|
|
}
|
|
// Server inbound processing : get the invitation server
|
|
serverMsg, err := server.ProcessInboundServerResponse(invitationData)
|
|
if err != nil {
|
|
return nil, "InvitationAnswerMessageReadResponse: ProcessInboundServerResponse", err
|
|
}
|
|
|
|
return serverMsg.Invitation, "", nil
|
|
|
|
}
|