2022-09-06 09:30:45 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
2022-10-29 20:07:35 +02:00
|
|
|
"github.com/google/uuid"
|
2022-09-19 14:09:32 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-09-06 09:30:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Peer struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
// Conversation []InternalMessage `json:"conversation,omitempty"`
|
|
|
|
// My own keys for that peer
|
|
|
|
Me meowlib.KeyPair `json:"me,omitempty"`
|
|
|
|
EncryptionKp meowlib.KeyPair `json:"conversation_kp,omitempty"`
|
|
|
|
LookupKp meowlib.KeyPair `json:"lookup_kp,omitempty"`
|
|
|
|
// Peer keys and infos
|
|
|
|
Contact meowlib.ContactCard `json:"contact,omitempty"`
|
|
|
|
// Internal management attributes
|
|
|
|
Visible bool `json:"visible,omitempty"`
|
|
|
|
VisiblePassword string `json:"visible_password,omitempty"`
|
|
|
|
PasswordType string `json:"password_type,omitempty"`
|
|
|
|
Blocked bool `json:"blocked,omitempty"`
|
|
|
|
MessageNotification string `json:"message_notification,omitempty"`
|
|
|
|
OnionMode bool `json:"onion_mode,omitempty"`
|
|
|
|
LastMessage time.Time `json:"last_message,omitempty"`
|
2022-09-18 18:09:27 +02:00
|
|
|
MessageDb string `json:"message_db,omitempty"` // sql url for messages storage
|
|
|
|
DbPassword string `json:"db_password,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PeerList []Peer
|
|
|
|
|
|
|
|
type Group struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Members []Peer `json:"members,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PeerList) GetFromPublicKey(publickey string) *Peer {
|
|
|
|
for _, peer := range *pl {
|
|
|
|
if peer.Contact.ContactPublicKey == publickey {
|
|
|
|
return &peer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PeerList) GetFromName(name string) *Peer {
|
|
|
|
for _, peer := range *pl {
|
|
|
|
if peer.Contact.Name == name {
|
|
|
|
return &peer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
func (p *Peer) BuildSimpleUserUMessage(message []byte) (*meowlib.UserMessage, error) {
|
2022-09-19 14:09:32 +02:00
|
|
|
var msg meowlib.UserMessage
|
|
|
|
msg.From = p.Me.Public
|
|
|
|
msg.Data = message
|
|
|
|
msg.Type = "1"
|
2022-10-29 20:07:35 +02:00
|
|
|
msg.Status.LocalUuid = uuid.New().String()
|
2022-10-22 14:41:48 +02:00
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) SerializeUserMessage(msg *meowlib.UserMessage) ([]byte, error) {
|
|
|
|
out, err := proto.Marshal(msg)
|
2022-09-19 14:09:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
// AsymEncryptMessage prepares a message to send to a specific peer contact
|
2022-09-18 18:09:27 +02:00
|
|
|
func (p *Peer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, Servers []*meowlib.Server, err error) {
|
2022-09-18 21:17:28 +02:00
|
|
|
EncryptedMessage, Signature, err = meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.Me.Private, Message)
|
2022-09-06 09:30:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2022-09-18 18:09:27 +02:00
|
|
|
return nil, nil, nil, err
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
2022-09-18 18:09:27 +02:00
|
|
|
return EncryptedMessage, Signature, p.Contact.PullServers, err
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
// AsymDecryptMessage reads a message from a specific peer contact
|
|
|
|
func (p *Peer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMessage []byte, err error) {
|
2022-09-18 21:17:28 +02:00
|
|
|
DecryptedMessage, err = meowlib.AsymDecryptAndCheck(p.Me.Private, p.Contact.ContactPublicKey, Message, Signature)
|
2022-09-06 09:30:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
return DecryptedMessage, err
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
2022-09-18 18:09:27 +02:00
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
// PackUserMessage will package the previously encrypted message for sending it to the peer in protobuff format
|
|
|
|
func (p *Peer) PackUserMessage(message []byte, signature []byte) meowlib.PackedUserMessage {
|
2022-09-18 18:09:27 +02:00
|
|
|
var msg meowlib.PackedUserMessage
|
|
|
|
msg.Destination = p.Contact.LookupPublicKey
|
|
|
|
msg.Payload = message
|
|
|
|
msg.Signature = signature
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
func (ints *InternalServer) UnPackUserMessage(protoPackedMessage []byte) (payload []byte, signature []byte, err error) {
|
|
|
|
msg := &meowlib.PackedServerMessage{}
|
|
|
|
if err := proto.Unmarshal(protoPackedMessage, msg); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return msg.Payload, msg.Signature, nil
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:09:27 +02:00
|
|
|
func (p *Peer) GetConversationRequest() meowlib.ToServerMessage_ConversationRequest {
|
|
|
|
var cr meowlib.ToServerMessage_ConversationRequest
|
|
|
|
return cr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) StoreMessage(msg []byte) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) LoadMessage(uid string) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) LoadLastMessages(qty int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func (p *Peer) GetLastMessageUuid(msg []byte) {
|
2022-09-18 18:09:27 +02:00
|
|
|
|
|
|
|
}
|