2024-03-23 20:09:14 +01:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-03-29 20:49:18 +01:00
|
|
|
"log"
|
2024-03-23 20:09:14 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2024-03-28 18:41:30 +01:00
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"forge.redroom.link/yves/meowlib/client"
|
2024-03-23 20:09:14 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReceivedMessage struct {
|
|
|
|
Text string
|
|
|
|
files []string
|
|
|
|
Server string
|
|
|
|
Sent uint64
|
|
|
|
Received uint64
|
|
|
|
LocalUuid string
|
|
|
|
LocalSequence uint64
|
|
|
|
AppData string
|
|
|
|
Location meowlib.Location
|
|
|
|
}
|
|
|
|
|
2024-03-28 18:41:30 +01:00
|
|
|
// CheckForMessages checks for messages on a single server
|
|
|
|
func CheckForMessages(storage_path string, job *client.RequestsJob) (int, string, error) {
|
|
|
|
|
2024-03-23 20:09:14 +01:00
|
|
|
count := 0
|
2024-03-28 18:41:30 +01:00
|
|
|
|
2024-03-23 20:09:14 +01:00
|
|
|
// if folder does not exist, create it
|
2024-03-28 18:41:30 +01:00
|
|
|
if _, err := os.Stat(filepath.Join(storage_path, "inbox")); os.IsNotExist(err) {
|
|
|
|
err := os.MkdirAll(filepath.Join(storage_path, "inbox"), 0700)
|
2024-03-23 20:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, "CheckMessages: MkdirAll", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//convert server to a server object
|
2024-03-28 18:41:30 +01:00
|
|
|
|
2024-03-23 20:09:14 +01:00
|
|
|
var crl []*meowlib.ConversationRequest
|
|
|
|
// build conversation requests
|
2024-03-28 18:41:30 +01:00
|
|
|
if job.LookupKeys != nil {
|
|
|
|
for _, key := range job.LookupKeys {
|
2024-03-23 20:09:14 +01:00
|
|
|
var cr meowlib.ConversationRequest
|
2024-03-28 18:41:30 +01:00
|
|
|
cr.LookupKey = key.Public
|
2024-03-23 20:09:14 +01:00
|
|
|
cr.SendTimestamp = time.Now().UTC().Unix()
|
|
|
|
// todo sign it
|
|
|
|
//cr.LookupSignature =
|
|
|
|
crl = append(crl, &cr)
|
|
|
|
}
|
2024-03-29 15:59:33 +01:00
|
|
|
// get server public key
|
|
|
|
if job.Server.PublicKey == "" {
|
|
|
|
key, err := meowlib.HttpGetId(job.Server.Url)
|
|
|
|
if err != nil {
|
|
|
|
return -1, "CheckMessages: HttpGetId", err
|
|
|
|
}
|
|
|
|
job.Server.PublicKey = key["publicKey"]
|
|
|
|
}
|
2024-03-23 20:09:14 +01:00
|
|
|
// build server message
|
|
|
|
var toSrv meowlib.ToServerMessage
|
|
|
|
toSrv.PullRequest = crl
|
2024-03-28 18:41:30 +01:00
|
|
|
toSrv.From = job.Server.UserKp.Public
|
|
|
|
data, err := job.Server.ProcessOutboundMessage(&toSrv)
|
2024-03-23 20:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, "CheckMessages: ProcessOutboundMessage", err
|
|
|
|
}
|
|
|
|
|
2024-03-28 18:41:30 +01:00
|
|
|
response, err := meowlib.HttpPostMessage(job.Server.Url, data)
|
2024-03-23 20:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, "CheckMessages: httpPostMessage", err
|
|
|
|
}
|
2024-03-28 18:41:30 +01:00
|
|
|
fs_msg, err := job.Server.ProcessInboundServerResponse(response)
|
2024-03-23 20:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, "CheckMessages: ProcessInboundServerResponse", err
|
|
|
|
}
|
2024-03-31 17:58:41 +02:00
|
|
|
if len(fs_msg.Chat) > 0 || (fs_msg.Invitation != nil && fs_msg.Invitation.Step == 3) {
|
2024-03-29 19:25:15 +01:00
|
|
|
// chat or invitation answer => save the server message
|
2024-03-28 18:41:30 +01:00
|
|
|
|
2024-03-23 20:09:14 +01:00
|
|
|
out, err := proto.Marshal(fs_msg)
|
|
|
|
if err != nil {
|
|
|
|
return -1, "CheckMessages: protobuf marshal", err
|
|
|
|
}
|
2024-03-28 18:41:30 +01:00
|
|
|
if err := os.WriteFile(filepath.Join(storage_path, "inbox", strconv.FormatInt(time.Now().UTC().UnixNano(), 10)), out, 0644); err != nil {
|
2024-03-23 20:09:14 +01:00
|
|
|
return -1, "CheckMessages: WriteFile", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = len(fs_msg.Chat)
|
2024-03-29 19:25:15 +01:00
|
|
|
} else {
|
|
|
|
// manage non uszer messages like devices or server
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return count, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveCheckJobs
|
|
|
|
func SaveCheckJobs() (string, error) {
|
|
|
|
me := client.GetConfig().GetIdentity()
|
|
|
|
err := me.SaveBackgroundJob()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "CheckMessages: json.Marshal", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadMessage
|
2024-04-01 20:56:45 +02:00
|
|
|
func ReadMessage(messageFilename string) ([]string, []string, string, error) {
|
2024-03-29 16:22:26 +01:00
|
|
|
|
2024-03-29 16:17:07 +01:00
|
|
|
messagesOverview := []string{}
|
|
|
|
filenames := []string{}
|
2024-03-29 16:42:03 +01:00
|
|
|
identity := client.GetConfig().GetIdentity()
|
2024-03-23 20:09:14 +01:00
|
|
|
// read message file
|
|
|
|
msg, err := os.ReadFile(messageFilename)
|
|
|
|
if err != nil {
|
2024-03-29 16:17:07 +01:00
|
|
|
return nil, nil, "ReadMessage: ReadFile", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
// protobuf unmarshal message
|
|
|
|
var fromServerMessage meowlib.FromServerMessage
|
|
|
|
err = proto.Unmarshal(msg, &fromServerMessage)
|
|
|
|
if err != nil {
|
2024-03-29 16:17:07 +01:00
|
|
|
return nil, nil, "ReadMessage: Unmarshal FromServerMessage", err
|
2024-03-28 18:41:30 +01:00
|
|
|
}
|
|
|
|
// check if invitation answer
|
|
|
|
if fromServerMessage.Invitation != nil {
|
2024-04-05 18:07:00 +02:00
|
|
|
invitationGetAnswerReadResponse(fromServerMessage.Invitation)
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
// Chat messages
|
|
|
|
if len(fromServerMessage.Chat) > 0 {
|
|
|
|
for _, packedUserMessage := range fromServerMessage.Chat {
|
|
|
|
|
|
|
|
// find the peer with that lookup key
|
2024-03-29 16:42:03 +01:00
|
|
|
peer := identity.Peers.GetFromMyLookupKey(packedUserMessage.Destination)
|
2024-03-23 20:09:14 +01:00
|
|
|
if peer == nil {
|
2024-03-29 16:17:07 +01:00
|
|
|
return nil, nil, "ReadMessage: GetFromMyLookupKey", errors.New("no visible peer for that message")
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
// Unpack the message
|
|
|
|
usermsg, err := peer.ProcessInboundUserMessage(packedUserMessage.Payload, packedUserMessage.Signature)
|
|
|
|
if err != nil {
|
2024-03-29 16:17:07 +01:00
|
|
|
return nil, nil, "ReadMessage: ProcessInboundUserMessage", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
2024-03-28 18:41:30 +01:00
|
|
|
|
2024-03-29 19:31:48 +01:00
|
|
|
//fmt.Println("From:", usermsg.From)
|
|
|
|
//jsonUserMessage, _ := json.Marshal(usermsg)
|
|
|
|
//fmt.Println(string(jsonUserMessage))
|
2024-03-29 20:49:18 +01:00
|
|
|
peer = client.GetConfig().GetIdentity().Peers.GetFromPublicKey(usermsg.From)
|
2024-03-29 16:17:07 +01:00
|
|
|
|
2024-03-29 20:49:18 +01:00
|
|
|
// detach files
|
2024-03-23 20:09:14 +01:00
|
|
|
if usermsg.Files != nil {
|
2024-03-29 20:49:18 +01:00
|
|
|
// create files folder
|
2024-04-01 20:56:45 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(client.GetConfig().StoragePath, identity.Uuid, "files")); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(filepath.Join(client.GetConfig().StoragePath, identity.Uuid, "files"), 0700)
|
2024-03-29 20:49:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 20:09:14 +01:00
|
|
|
for _, file := range usermsg.Files {
|
|
|
|
filename := uuid.New().String() + "_" + file.Filename
|
2024-03-29 20:49:18 +01:00
|
|
|
filenames = append(filenames, peer.Name+" sent: "+filename)
|
2024-03-23 20:09:14 +01:00
|
|
|
// detach file
|
2024-04-01 20:56:45 +02:00
|
|
|
os.WriteFile(filepath.Join(client.GetConfig().StoragePath, identity.Uuid, "files", filename), file.Data, 0600)
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
//? result["invitation finalized"] = peer.Name
|
|
|
|
}
|
|
|
|
// user message
|
2024-03-29 20:49:18 +01:00
|
|
|
|
2024-03-29 19:31:48 +01:00
|
|
|
messagesOverview = append(messagesOverview, peer.Name+" > "+string(usermsg.Data))
|
2024-03-23 20:09:14 +01:00
|
|
|
// add message to storage
|
|
|
|
err = peer.StoreMessage(usermsg, filenames)
|
|
|
|
if err != nil {
|
2024-03-29 16:17:07 +01:00
|
|
|
return nil, nil, "ReadMessage: StoreMessage", err
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:08:09 +01:00
|
|
|
err = os.Remove(messageFilename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, "ReadMessage: Remove", err
|
|
|
|
}
|
2024-03-23 20:09:14 +01:00
|
|
|
|
2024-03-29 21:08:09 +01:00
|
|
|
// list of messages & detached files
|
2024-03-29 16:18:24 +01:00
|
|
|
return messagesOverview, filenames, "", nil
|
2024-03-23 20:09:14 +01:00
|
|
|
}
|