2024-03-23 20:09:14 +01:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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
|
|
|
//fmt.Println(jsonjob)
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
// 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-28 18:41:30 +01:00
|
|
|
if len(fs_msg.Chat) == 0 && fs_msg.Invitation == nil {
|
|
|
|
|
2024-03-23 20:09:14 +01:00
|
|
|
// todo: manage non usermessage, like serverlists
|
2024-03-28 18:41:30 +01:00
|
|
|
|
|
|
|
} else { // chat or invitation answer => save the server message
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
func ReadMessage(messageFilename string, storagePath string) (string, string, error) {
|
|
|
|
result := map[string]interface{}{}
|
|
|
|
|
|
|
|
// read message file
|
|
|
|
msg, err := os.ReadFile(messageFilename)
|
|
|
|
if err != nil {
|
2024-03-28 18:41:30 +01:00
|
|
|
return "", "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 {
|
|
|
|
return "", "ReadMessage: Unmarshal FromServerMessage", err
|
2024-03-28 18:41:30 +01:00
|
|
|
}
|
|
|
|
// check if invitation answer
|
|
|
|
if fromServerMessage.Invitation != nil {
|
|
|
|
|
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
|
|
|
|
peer := client.GetConfig().GetIdentity().Peers.GetFromMyLookupKey(packedUserMessage.Destination)
|
|
|
|
if peer == nil {
|
|
|
|
return "", "ReadMessage: GetFromMyLookupKey", errors.New("no visible peer for that message")
|
|
|
|
}
|
|
|
|
// Unpack the message
|
|
|
|
usermsg, err := peer.ProcessInboundUserMessage(packedUserMessage.Payload, packedUserMessage.Signature)
|
|
|
|
if err != nil {
|
|
|
|
return "", "ReadMessage: ProcessInboundUserMessage", err
|
|
|
|
}
|
2024-03-28 18:41:30 +01:00
|
|
|
|
2024-03-23 20:09:14 +01:00
|
|
|
fmt.Println("From:", usermsg.From)
|
|
|
|
jsonUserMessage, _ := json.Marshal(usermsg)
|
|
|
|
fmt.Println(string(jsonUserMessage))
|
|
|
|
// detach files
|
|
|
|
filenames := []string{}
|
|
|
|
if usermsg.Files != nil {
|
|
|
|
for _, file := range usermsg.Files {
|
|
|
|
filename := uuid.New().String() + "_" + file.Filename
|
|
|
|
filenames = append(filenames, filename)
|
|
|
|
// detach file
|
|
|
|
os.WriteFile(filepath.Join(storagePath, "files", filename), file.Data, 0600)
|
|
|
|
}
|
|
|
|
//? result["invitation finalized"] = peer.Name
|
|
|
|
}
|
|
|
|
// user message
|
|
|
|
result["message"] = string(usermsg.Data)
|
|
|
|
// add message to storage
|
|
|
|
err = peer.StoreMessage(usermsg, filenames)
|
|
|
|
if err != nil {
|
|
|
|
return "", "ReadMessage: StoreMessage", err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt.Println("have read fromServerMessage, will unmarshal packed")
|
|
|
|
// // protobuf unmarshal message //! WRONG !!!!
|
|
|
|
// var packedMessage meowlib.PackedUserMessage
|
|
|
|
// err = proto.Unmarshal(userMessage, &packedMessage)
|
|
|
|
// if err != nil {
|
|
|
|
// return C.CString(errorToJson(err, "ReadMessage: Unmarshal PackedUserMessage"))
|
|
|
|
// }
|
|
|
|
// fmt.Println("Destination:", packedMessage.Destination)
|
|
|
|
// fmt.Println("Payload lengh:", len(packedMessage.Payload))
|
|
|
|
// server invitation finalize or more ?
|
|
|
|
if fromServerMessage.Invitation != nil {
|
|
|
|
fmt.Println("Invitation from:", fromServerMessage.Invitation.From)
|
|
|
|
// find the peer with that lookup key
|
|
|
|
// todo get from invitation id
|
|
|
|
//! FOLOWING statement is WRONG !
|
|
|
|
peer := client.GetConfig().GetIdentity().Peers.GetFromPublicKey(fromServerMessage.Invitation.From)
|
|
|
|
if peer == nil {
|
|
|
|
return "", "ReadMessage: GetFromPublicKey", errors.New("no visible peer for that message")
|
|
|
|
}
|
|
|
|
peer.ContactPublicKey = fromServerMessage.Invitation.From
|
|
|
|
str, _ := json.Marshal(peer)
|
|
|
|
fmt.Println("Peer:", string(str))
|
|
|
|
// invitation message
|
|
|
|
var receivedContactCard meowlib.ContactCard
|
|
|
|
err := proto.Unmarshal(fromServerMessage.Invitation.Payload, &receivedContactCard)
|
|
|
|
if err != nil {
|
|
|
|
return "", "ReadMessage: Unmarshal ContactCard", err
|
|
|
|
}
|
|
|
|
err = client.GetConfig().GetIdentity().FinalizeInvitation(&receivedContactCard)
|
|
|
|
if err != nil {
|
|
|
|
return "", "ReadMessage: FinalizeInvitation", err
|
|
|
|
}
|
|
|
|
result["invitation finalized"] = peer.Name
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// message
|
|
|
|
// including list of detached files
|
|
|
|
val, err := json.Marshal(result)
|
|
|
|
if err != nil {
|
|
|
|
return "", "ReadMessage: json.Marshal", err
|
|
|
|
}
|
|
|
|
os.Remove(messageFilename)
|
|
|
|
return string(val), "", nil
|
|
|
|
}
|