Server invitation process functions
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
ycc
2023-11-14 16:32:50 +01:00
parent 65f9ee2e07
commit 432f449558
8 changed files with 134 additions and 85 deletions

View File

@ -44,7 +44,8 @@ func CreateIdentity(nickname string) *Identity {
return &id
}
func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerIdxs []int) (*meowlib.ContactCard, error) {
// Creates an invitation for a peer, returns the peer containing
func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerIdxs []int) (*Peer, *meowlib.ContactCard, error) {
var peer Peer
var myContactCard meowlib.ContactCard
peer.MyIdentity = meowlib.NewKeyPair()
@ -53,11 +54,11 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerI
peer.Name = ContactName
peer.InvitationId = uuid.New().String()
if id.MessageServers.Servers == nil {
return nil, errors.New("no message servers defined in your identity")
return nil, nil, errors.New("no message servers defined in your identity")
}
for _, i := range MessageServerIdxs {
if i > len(id.MessageServers.Servers)-1 {
return nil, errors.New("requested server out of range of defined message servers")
return nil, nil, errors.New("requested server out of range of defined message servers")
}
}
for _, i := range MessageServerIdxs {
@ -71,7 +72,7 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerI
myContactCard.InvitationId = peer.InvitationId
id.Peers = append(id.Peers, peer)
return &myContactCard, nil
return &peer, &myContactCard, nil
}
func (id *Identity) CheckInvitation(ReceivedContact *meowlib.ContactCard) (isAnswer bool, proposedNick string, receivedNick string) {

View File

@ -100,6 +100,23 @@ func (p *Peer) BuildSingleFileMessage(filename string, message []byte) ([]meowli
return msgs, nil
}
// Builds an invitation answer user message.
// it takes as input a contactcard generated by Identity.AnswerInvitation
func (p *Peer) BuildInvitationAnswserMessage(myContactCard *meowlib.ContactCard) (*meowlib.UserMessage, error) {
var msg meowlib.UserMessage
var invitation meowlib.Invitation
invitation.Step = 3
out, err := proto.Marshal(myContactCard)
if err != nil {
return nil, err
}
invitation.Payload = out
msg.Destination = p.Contact.LookupPublicKey
msg.From = p.MyIdentity.Public
msg.Type = "1"
return &msg, nil
}
//
// Messages encryption and packaging
//

View File

@ -97,6 +97,7 @@ func (ints *Server) BuildMessageRequestMessage(lookupKeys []string) ([]byte, err
}
// BuildToServerMessageInvitation creates an invitation message to server and returns it as a meowlib.ToServerMessage
// it takes as input a contactcard generated by Identity.InvitePeer
func (ints *Server) BuildToServerMessageInvitationCreation(invitation *meowlib.ContactCard, password string, timeout int, invitationIdLen int) (*meowlib.ToServerMessage, error) {
var msg meowlib.ToServerMessage
var inv meowlib.Invitation
@ -109,21 +110,21 @@ func (ints *Server) BuildToServerMessageInvitationCreation(invitation *meowlib.C
inv.Step = 1
inv.Password = password
inv.Timeout = int32(timeout)
inv.Idlen = int32(invitationIdLen)
inv.ShortcodeLen = int32(invitationIdLen)
inv.Payload = payload
msg.Invitation = &inv
return &msg, nil
}
// BuildToServerMessageInvitationRequest requests invitation with provided id from server and returns it as a meowlib.ToServerMessage
func (ints *Server) BuildToServerMessageInvitationRequest(invitationId string, password string) (*meowlib.ToServerMessage, error) {
func (ints *Server) BuildToServerMessageInvitationRequest(shortcode string, password string) (*meowlib.ToServerMessage, error) {
var msg meowlib.ToServerMessage
var inv meowlib.Invitation
msg.Type = "1"
msg.From = ints.Me.Public
inv.Step = 2
inv.Password = password
inv.Id = invitationId
inv.Shortcode = shortcode
msg.Invitation = &inv
return &msg, nil
}