diff --git a/client/server.go b/client/server.go index 7797997..4391550 100644 --- a/client/server.go +++ b/client/server.go @@ -27,17 +27,14 @@ type Server struct { AllowedDelay int `json:"allowed_delay,omitempty"` } -type ServerList struct { - Name string - Servers []Server -} - -func ServerFromUrl(url string) *Server { +// CreateServerFromUrl creates a server from a basic url, ex : https://my.meowserver.example:8443/meow/ +func CreateServerFromUrl(url string) *Server { var is Server is.ServerData.Url = url return &is } +// Create a server from a server card func CreateServerFromServerCard(server *meowlib.ServerCard) *Server { var is Server is.ServerData = *server @@ -45,12 +42,6 @@ func CreateServerFromServerCard(server *meowlib.ServerCard) *Server { return &is } -func (sl *ServerList) AddUrls(urls []string) { - for _, url := range urls { - sl.Servers = append(sl.Servers, *ServerFromUrl(url)) - } -} - // AsymEncryptMessage prepares a message to send to a specific internal server func (ints *Server) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) { var enc *meowlib.EncryptedMessage @@ -106,7 +97,7 @@ func (ints *Server) BuildMessageRequestMessage(lookupKeys []string) ([]byte, err } // BuildToServerMessageInvitation creates an invitation message to server and returns it as a meowlib.ToServerMessage -func (ints *Server) BuildToServerMessageInvitationCreation(invitation *meowlib.ContactCard, password string, timeout int, urllen int) (*meowlib.ToServerMessage, error) { +func (ints *Server) BuildToServerMessageInvitationCreation(invitation *meowlib.ContactCard, password string, timeout int, invitationIdLen int) (*meowlib.ToServerMessage, error) { var msg meowlib.ToServerMessage var inv meowlib.Invitation payload, err := invitation.Compress() @@ -118,25 +109,26 @@ func (ints *Server) BuildToServerMessageInvitationCreation(invitation *meowlib.C inv.Step = 1 inv.Password = password inv.Timeout = int32(timeout) - inv.Idlen = int32(urllen) + inv.Idlen = 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(url string, password string) (*meowlib.ToServerMessage, error) { +func (ints *Server) BuildToServerMessageInvitationRequest(invitationId 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 = url + inv.Id = invitationId msg.Invitation = &inv return &msg, nil } +// PackServerMessage func (ints *Server) PackServerMessage(payload []byte, signature []byte) (protoPackedMessage []byte, err error) { var msg meowlib.PackedServerMessage msg.From = ints.Me.Public diff --git a/client/serverlist.go b/client/serverlist.go new file mode 100644 index 0000000..fc71b4b --- /dev/null +++ b/client/serverlist.go @@ -0,0 +1,49 @@ +package client + +import "errors" + +// ServerList manages lists that are used in Identity +// - Message servers list +// - ArchiveServers lists +// - Owned servers lists +// - Matriochka paths +type ServerList struct { + Name string + Servers []Server +} + +// FilterByIdxs returns a filtered server list filtered according to an index list +func (sl *ServerList) FilterByIdxs(MessageServerIdxs []int) (filtered *ServerList, err error) { + filtered.Servers = []Server{} + for _, i := range MessageServerIdxs { + if i > len(sl.Servers)-1 { + return nil, errors.New("requested server out of range of defined message servers") + } + } + for _, i := range MessageServerIdxs { + filtered.Servers = append(filtered.Servers, sl.Servers[i]) + } + return filtered, nil +} + +// GetServerByIdx returns a server from it's index +func (sl *ServerList) GetServerByIdx(idx int) (server *Server, err error) { + return &sl.Servers[idx], nil +} + +// GetServerByPubkey returns a server from it's public key +func (sl *ServerList) GetServerByPubkey(pubkey string) (filtered *Server) { + for _, srv := range sl.Servers { + if srv.ServerData.PublicKey == pubkey { + return &srv + } + } + return nil +} + +// AddUrls is a simple utility functon used mainly as a shortcut for testing purposes +func (sl *ServerList) AddUrls(urls []string) { + for _, url := range urls { + sl.Servers = append(sl.Servers, *CreateServerFromUrl(url)) + } +}