136 lines
5.0 KiB
Go
136 lines
5.0 KiB
Go
package helpers
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"forge.redroom.link/yves/meowlib/client"
|
|
)
|
|
|
|
// InvitationCreatePeer creates a new peer and returns it
|
|
// Called by invitation initiator
|
|
// name: the name of the peer
|
|
// myNickname: my nickname for that peer
|
|
// invitationMessage: the message to send to the peer
|
|
// serverUids: the list of server uids
|
|
func InvitationCreatePeer(name string, myNickname string, invitationMessage string, serverUids []string) (*client.Peer, string, error) {
|
|
|
|
mynick := myNickname
|
|
if myNickname == "" {
|
|
mynick = client.GetConfig().GetIdentity().Nickname
|
|
}
|
|
|
|
// build my contact card for that friend
|
|
peer, err := client.GetConfig().GetIdentity().InvitePeer(mynick, myNickname, serverUids, invitationMessage)
|
|
if err != nil {
|
|
return nil, "InvitationCreate: InvitePeer", err
|
|
}
|
|
client.GetConfig().GetIdentity().Save()
|
|
|
|
return peer, "", nil
|
|
}
|
|
|
|
// InvitationCreateFile creates a new peer and writes the invitation to a file
|
|
// Called by invitation initiator
|
|
// name: the name of the peer
|
|
// myNickname: my nickname for that peer
|
|
// invitationMessage: the message to send to the peer
|
|
// serverUids: the list of server uids
|
|
// format: the format of the file (qr or mwiv)
|
|
func InvitationCreateFile(name string, myNickname string, invitationMessage string, serverUids []string, format string) (*client.Peer, string, error) {
|
|
|
|
peer, errdata, err := InvitationCreatePeer(name, myNickname, invitationMessage, serverUids)
|
|
if err != nil {
|
|
return nil, errdata, err
|
|
}
|
|
c := client.GetConfig()
|
|
var filename string = ""
|
|
if format == "qr" {
|
|
filename = c.StoragePath + string(os.PathSeparator) + peer.MyName + "-" + peer.Name + ".png"
|
|
err := peer.GetMyContact().WriteQr(filename)
|
|
if err != nil {
|
|
return nil, "InvitationCreateFile: WriteQr", err
|
|
}
|
|
} else {
|
|
filename = c.StoragePath + string(os.PathSeparator) + peer.MyName + "-" + peer.Name + ".mwiv"
|
|
err := peer.GetMyContact().WriteCompressed(filename)
|
|
if err != nil {
|
|
return nil, "InvitationCreateFile: WriteCompressed", err
|
|
}
|
|
}
|
|
return peer, "", nil
|
|
}
|
|
|
|
// InvitationCreateMessage creates a new invitation message for an invited peer
|
|
// Called by invitation initiator
|
|
// invitationId: the invitation id of the peer
|
|
// invitationServerUid: the uid of the server for sending the invitation
|
|
// timeOut: the timeout for the invitation
|
|
// urlLen: the length of the invitation url
|
|
// password: the password for the invitation
|
|
func InvitationCreateMessage(invitationId string, invitationServerUid string, timeOut int, urlLen int, password string) ([]byte, string, error) {
|
|
|
|
// lookup for peer with "invitation_id"
|
|
var myContact *meowlib.ContactCard
|
|
for i := 0; i < len(client.GetConfig().GetIdentity().Peers); i++ {
|
|
if client.GetConfig().GetIdentity().Peers[i].InvitationId == invitationId {
|
|
myContact = client.GetConfig().GetIdentity().Peers[i].GetMyContact()
|
|
break
|
|
}
|
|
}
|
|
// todo handle not found !!
|
|
// lookup for message server with "invitation_server"
|
|
invitationServer, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid) //.GetServerByIdx(int(jsoninv["invitation_server"].(float64)))
|
|
if err != nil {
|
|
return nil, "InvitationCreateMessage: LoadServer", err
|
|
}
|
|
// call server.buildinviattion
|
|
msg, err := invitationServer.BuildToServerMessageInvitationCreation(myContact, password, timeOut, urlLen)
|
|
if err != nil {
|
|
return nil, "InvitationCreateMessage: BuildToServerMessageInvitationCreation", err
|
|
}
|
|
// fmt.Println("Invitation Create")
|
|
// fmt.Println(hex.EncodeToString(msg.Invitation.Payload))
|
|
bytemsg, err := invitationServer.ProcessOutboundMessage(msg)
|
|
if err != nil {
|
|
return nil, "InvitationCreateMessage: ProcessOutboundMessage", err
|
|
}
|
|
return bytemsg, "", nil
|
|
}
|
|
|
|
// InvitationCreateReadResponse reads the response of an invitation creation (url, expiry)
|
|
// Called by invitation initiator
|
|
// invitationServerUid: the uid of the server where we sent the invitation
|
|
// invitationResponse: the response we got from the server
|
|
func InvitationCreateReadResponse(invitationServerUid string, invitationResponse []byte) (*meowlib.Invitation, string, error) {
|
|
|
|
server, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid)
|
|
if err != nil {
|
|
return nil, "InvitationCreateReadResponse: LoadServer", err
|
|
}
|
|
serverMsg, err := server.ProcessInboundServerResponse(invitationResponse)
|
|
if err != nil {
|
|
return nil, "InvitationCreateReadResponse: ProcessInboundServerResponse", err
|
|
}
|
|
|
|
return serverMsg.Invitation, "", nil
|
|
}
|
|
|
|
// InvitationSetUrlInfo sets the url info for an invitation
|
|
// Called by invitation initiator
|
|
// invitationId: the invitation id of the peer
|
|
// url: the url of the invitation we got from the server
|
|
func InvitationSetUrlInfo(invitationId string, url string, expiry int64) {
|
|
|
|
for i := 0; i < len(client.GetConfig().GetIdentity().Peers); i++ {
|
|
if client.GetConfig().GetIdentity().Peers[i].InvitationId == invitationId {
|
|
client.GetConfig().GetIdentity().Peers[i].InvitationUrl = url
|
|
client.GetConfig().GetIdentity().Peers[i].InvitationExpiry = time.Unix(expiry, 0)
|
|
break
|
|
}
|
|
}
|
|
client.GetConfig().GetIdentity().Save()
|
|
|
|
}
|