meowlib/client/helpers/invitationCheckHelper.go
ycc 6cfc54d943
Some checks failed
continuous-integration/drone/push Build is failing
change meowurl scheme to meow://http//url for safer uri parsing
2024-04-12 14:37:20 +02:00

127 lines
4.2 KiB
Go

package helpers
import (
"strings"
"forge.redroom.link/yves/meowlib"
"forge.redroom.link/yves/meowlib/client"
)
// InvitationCheck
// todo
/*
func InvitationCheck(invitationdata []byte) *C.char {
var jsoninv map[string]interface{}
err := json.Unmarshal([]byte(C.GoString(invitationdata)), &jsoninv)
if err != nil {
return C.CString(errorToJson(err, "InvitationCheck: "))
}
var cc *meowlib.ContactCard
if _, err := os.Stat(jsoninv["filename"].(string)); os.IsNotExist(err) {
return C.CString(errorToJson(err, "InvitationCheck: "))
}
if strings.HasSuffix(jsoninv["filename"].(string), ".mwiv") {
data, err := os.ReadFile(jsoninv["filename"].(string))
if err != nil {
return C.CString(errorToJson(err, "InvitationCheck: "))
}
cc, err = meowlib.NewContactCardFromCompressed(data)
if err != nil {
return C.CString(errorToJson(err, "InvitationCheck: "))
}
}
identity := client.GetConfig().GetIdentity()
result := map[string]string{}
if cc != nil {
isAnswer, proposed, received, invitationMessage := identity.CheckInvitation(cc)
if isAnswer { // answer to infitation
result["type"] = "answer"
result["to"] = proposed
result["from"] = received
result["invitation_message"] = invitationMessage
//fmt.Fprintln(os.Stdout, "Invitation sent to "+proposed+" received with "+received+" as suggested nickname")
} else { // finalization message
result["type"] = "finalize"
result["from"] = received
//fmt.Fprintln(os.Stdout, "Invitation sent by "+received)
}
}
val, err := json.Marshal(result)
if err != nil {
return C.CString(errorToJson(err, "InvitationCheck: "))
}
return C.CString(string(val))
}*/
// InvitationGetMessage
// Called by the invitation receiver
// invitationUrl: the url of server holding the invitation
// serverPublicKey: the public key of the server holding the invitation
// invitationPassword: the password of the invitation
func InvitationGetMessage(invitationUrl string, serverPublicKey string, invitationPassword string) ([]byte, string, error) {
meowurl := strings.Split(invitationUrl, "?")
shortcode := meowurl[1]
srv := client.CreateServerFromMeowUrl(meowurl[0])
// check if already in msg servers
dbsrv, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(srv.Url)
if err != nil {
return nil, "InvitationGetMessage: LoadServer", err
}
if dbsrv == nil {
// create a server object with url & pubkey
srv.PublicKey = serverPublicKey
srv.UserKp = meowlib.NewKeyPair()
// save it
err = client.GetConfig().GetIdentity().MessageServers.StoreServer(srv)
if err != nil {
return nil, "InvitationGetMessage: StoreServer", err
}
} else {
if dbsrv.PublicKey != serverPublicKey {
dbsrv.PublicKey = serverPublicKey
}
srv = dbsrv
}
// buildserver message
toSrvMsg, err := srv.BuildToServerMessageInvitationRequest(shortcode, invitationPassword)
if err != nil {
return nil, "InvitationGetMessage: BuildToServerMessageInvitationRequest", err
}
// processoutbound
bytemsg, err := srv.ProcessOutboundMessage(toSrvMsg)
if err != nil {
return nil, "InvitationGetMessage: ProcessOutboundMessage", err
}
return bytemsg, "", nil
}
// InvitationGetMessageReadResponse
// Called by the invitation receiver
// invitationData: the data received from the server
// invitationServerUid: the uid of the server holding the invitation
func InvitationGetMessageReadResponse(invitationData []byte, invitationServerUid string) (*meowlib.ContactCard, string, error) {
server, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid)
if err != nil {
return nil, "InvitationGetMessageReadResponse: LoadServer", err
}
// Server inbound processing : get the invitation server
serverMsg, err := server.ProcessInboundServerResponse(invitationData)
if err != nil {
return nil, "InvitationGetMessageReadResponse: ProcessInboundServerResponse", err
}
// fmt.Println("Inbound OK, Invitation Step: ", serverMsg.Invitation.Step, len(serverMsg.Invitation.Payload))
// fmt.Println("Invitation Check")
// fmt.Println(hex.EncodeToString(serverMsg.Invitation.Payload))
// contactCard decode
cc, err := meowlib.NewContactCardFromCompressed(serverMsg.Invitation.Payload)
if err != nil {
return nil, "InvitationGetMessageReadResponse: NewContactCardFromCompressed", err
}
return cc, "", nil
}