148 lines
4.8 KiB
Go
148 lines
4.8 KiB
Go
|
package helpers
|
||
|
|
||
|
import (
|
||
|
"C"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strings"
|
||
|
|
||
|
"forge.redroom.link/yves/meowlib"
|
||
|
"forge.redroom.link/yves/meowlib/client"
|
||
|
)
|
||
|
import (
|
||
|
"errors"
|
||
|
|
||
|
"google.golang.org/protobuf/proto"
|
||
|
)
|
||
|
|
||
|
// 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) ([]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
|
||
|
}
|
||
|
|
||
|
// jsonsrv, err := json.Marshal(peer)
|
||
|
// if err != nil {
|
||
|
// return gobin2c(berrorToJson(err, "InvitationAnswerMessage: Marshal"))
|
||
|
// }
|
||
|
//fmt.Println(string(jsonsrv))
|
||
|
// Prepare cyphered + packed user message
|
||
|
packedMsg, err := peer.ProcessOutboundUserMessage(answermsg)
|
||
|
if err != nil {
|
||
|
return nil, "InvitationAnswerMessage: ProcessOutboundUserMessage", err
|
||
|
}
|
||
|
// jsonsrv, err = json.Marshal(invitationServer)
|
||
|
// if err != nil {
|
||
|
// return gobin2c(berrorToJson(err, "InvitationAnswerMessage: Marshal"))
|
||
|
// }
|
||
|
//fmt.Println(string(jsonsrv))
|
||
|
// Creating Server message for transporting the user message
|
||
|
toServerMessage := invitationServer.BuildToServerMessageFromUserMessage(packedMsg)
|
||
|
toServerMessage.Invitation = &meowlib.Invitation{Step: 3}
|
||
|
toServerMessage.Invitation.From = peer.MyIdentity.Public
|
||
|
pld, err := proto.Marshal(packedMsg)
|
||
|
if err != nil {
|
||
|
return nil, "InvitationAnswerMessage: proto.Marshal", err
|
||
|
}
|
||
|
toServerMessage.Invitation.Payload = pld
|
||
|
bytemsg, err := invitationServer.ProcessOutboundMessage(toServerMessage)
|
||
|
if err != nil {
|
||
|
return nil, "InvitationAnswerMessage: ProcessOutboundMessage", err
|
||
|
}
|
||
|
return bytemsg, "", nil
|
||
|
}
|