2024-03-19 00:31:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
2024-06-10 07:10:09 +00:00
|
|
|
"log/slog"
|
2024-03-19 00:31:35 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
|
|
|
)
|
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
func (cli *StatusCLI) sendContactRequest(ctx context.Context, toID string) error {
|
2024-04-09 15:44:43 +00:00
|
|
|
cli.logger.Info("send contact request, contact public key: ", toID)
|
2024-03-19 00:31:35 +00:00
|
|
|
request := &requests.SendContactRequest{
|
|
|
|
ID: toID,
|
|
|
|
Message: "Hello!",
|
|
|
|
}
|
2024-06-10 07:10:09 +00:00
|
|
|
resp, err := cli.messenger.SendContactRequest(ctx, request)
|
2024-04-09 15:44:43 +00:00
|
|
|
cli.logger.Info("function SendContactRequest response.messages: ", resp.Messages())
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
func (cli *StatusCLI) sendContactRequestAcceptance(ctx context.Context, msgID string) error {
|
2024-04-09 15:44:43 +00:00
|
|
|
cli.logger.Info("accept contact request, message ID: ", msgID)
|
2024-06-10 07:10:09 +00:00
|
|
|
resp, err := cli.messenger.AcceptContactRequest(ctx, &requests.AcceptContactRequest{ID: types.Hex2Bytes(msgID)})
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
cli.logger.Info("function AcceptContactRequest response: ", resp.Messages())
|
2024-03-19 00:31:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
func (cli *StatusCLI) sendDirectMessage(ctx context.Context, text string) error {
|
|
|
|
if len(cli.messenger.MutualContacts()) == 0 {
|
2024-03-19 00:31:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
chat := cli.messenger.Chat(cli.messenger.MutualContacts()[0].ID)
|
2024-06-07 10:17:49 +00:00
|
|
|
cli.logger.Info("will send message to contact: ", chat.ID)
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(cli.messenger.GetTransport())
|
2024-03-19 00:31:35 +00:00
|
|
|
inputMessage := common.NewMessage()
|
|
|
|
inputMessage.ChatId = chat.ID
|
|
|
|
inputMessage.LocalChatID = chat.ID
|
|
|
|
inputMessage.Clock = clock
|
|
|
|
inputMessage.Timestamp = timestamp
|
|
|
|
inputMessage.MessageType = protobuf.MessageType_ONE_TO_ONE
|
|
|
|
inputMessage.ContentType = protobuf.ChatMessage_TEXT_PLAIN
|
|
|
|
inputMessage.Text = text
|
|
|
|
|
2024-06-07 10:17:49 +00:00
|
|
|
resp, err := cli.messenger.SendChatMessage(ctx, inputMessage)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-07 10:17:49 +00:00
|
|
|
for _, message := range resp.Messages() {
|
|
|
|
cli.logger.Infof("sent message: %v (ID=%v)", message.Text, message.ID)
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:31:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
func (cli *StatusCLI) retrieveMessagesLoop(ctx context.Context, tick time.Duration, msgCh chan string, wg *sync.WaitGroup) {
|
2024-03-19 00:31:35 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
cli.logger.Info("retrieve messages...")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
response, err := cli.messenger.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
cli.logger.Error("failed to retrieve raw messages", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
if response == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, message := range response.Messages() {
|
2024-06-07 10:17:49 +00:00
|
|
|
cli.logger.Infof("message received: %v (ID=%v)", message.Text, message.ID)
|
2024-04-09 15:44:43 +00:00
|
|
|
if message.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_SENT {
|
|
|
|
msgCh <- message.ID
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
// interactiveSendMessageLoop reads input from stdin and sends it as a direct message to the first mutual contact.
|
|
|
|
//
|
|
|
|
// If multiple CLIs are provided, it will send messages in a round-robin fashion:
|
|
|
|
// 1st input message will be from Alice, 2nd from Bob, 3rd from Alice, and so on.
|
|
|
|
func interactiveSendMessageLoop(ctx context.Context, clis ...*StatusCLI) {
|
2024-03-19 00:31:35 +00:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
2024-06-10 07:10:09 +00:00
|
|
|
i := -1
|
|
|
|
n := len(clis)
|
|
|
|
if n == 0 {
|
|
|
|
slog.Error("at least 1 CLI needed")
|
|
|
|
return
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
for {
|
2024-06-10 07:10:09 +00:00
|
|
|
i++
|
|
|
|
if i >= n {
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
cli := clis[i] // round robin cli selection
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
if len(cli.messenger.MutualContacts()) == 0 {
|
|
|
|
// waits for 1 second before trying again
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cli.logger.Info("Enter your message to send: (type 'quit' or 'q' to exit)")
|
|
|
|
|
|
|
|
message, err := readInput(ctx, reader)
|
|
|
|
if err != nil {
|
|
|
|
if err == context.Canceled {
|
2024-03-19 00:31:35 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-10 07:10:09 +00:00
|
|
|
cli.logger.Error("failed to read input", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
message = strings.TrimSpace(message)
|
|
|
|
if message == "quit" || message == "q" || strings.Contains(message, "\x03") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if message == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err = cli.sendDirectMessage(ctx, message); err != nil {
|
|
|
|
cli.logger.Error("failed to send direct message: ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
// readInput reads input from the reader and respects context cancellation
|
|
|
|
func readInput(ctx context.Context, reader *bufio.Reader) (string, error) {
|
|
|
|
inputCh := make(chan string, 1)
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
|
|
|
|
// Start a goroutine to read input
|
|
|
|
go func() {
|
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
2024-03-19 00:31:35 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-10 07:10:09 +00:00
|
|
|
inputCh <- input
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return "", ctx.Err()
|
|
|
|
case input := <-inputCh:
|
|
|
|
return input, nil
|
|
|
|
case err := <-errCh:
|
|
|
|
return "", err
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
|
|
|
}
|