Skip to content

Commit

Permalink
Add forgotten files.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 9, 2024
1 parent e115d1b commit 1298768
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/pong/envelopes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"

"github.com/bahner/go-ma-actor/entity"
log "github.com/sirupsen/logrus"
)

func handleEnvelopeEvents(ctx context.Context, a *entity.Entity) {

log.Debugf("Starting handleEnvelopeEvents for %s", a.DID.String())

for {
log.Info("Waiting for messages...")
env, ok := <-a.Envelopes
if !ok {
log.Debugf("Message channel closed, exiting...")
return
}

m, err := env.Open(a.Keyset.EncryptionKey.PrivKey[:])
if err != nil {
log.Errorf("Error opening envelope: %v", err)
if m.Verify() != nil {
log.Debugf("Failed to open envelope and verify message: %v", m)
continue
}
}

log.Debugf("Replying privately to message %v from %s", m.Content, m.From)
reply(ctx, a, m)

}
}
69 changes: 69 additions & 0 deletions ui/broadcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ui

import (
"context"
"fmt"
"strings"

"github.com/bahner/go-ma-actor/alias"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/msg"
log "github.com/sirupsen/logrus"
)

func (ui *ChatUI) handleBroadcastCommand(args []string) {

if len(args) > 1 {

if len(args) < 3 {
ui.displaySystemMessage("Public announcement can't be empty")
return
}

recipient := args[1]
if !did.IsValidDID(recipient) {
recipient = alias.LookupEntityNick(recipient)
}
if recipient == "" {
ui.displaySystemMessage(fmt.Sprintf("Invalid DID: %s", args[1]))
return
}

var message string
if len(args) > 3 {
message = strings.Join(args[2:], " ")
} else {
message = args[2]
}
msgBytes := []byte(message)
if log.GetLevel() == log.DebugLevel {
ui.displaySystemMessage(fmt.Sprintf("Broadcasting %s to %s", message, recipient))
} else {
ui.displaySystemMessage(fmt.Sprintf("Broadcasting to %s", recipient))
}

msg, err := msg.New(ui.a.DID.String(), recipient, msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
if err != nil {
ui.displaySystemMessage(fmt.Sprintf("Broadcast creation error: %s", err))
}

resp, err := entity.GetOrCreate(recipient)
if err != nil {
ui.displaySystemMessage(fmt.Sprintf("Entity creation error: %s", err))
}

err = msg.Broadcast(context.Background(), resp.Topic)
if err != nil {
ui.displaySystemMessage(fmt.Sprintf("Broadcast error: %s", err))
}
log.Debugf("Message broadcasted to topic: %s", ui.e.Topic.String())
} else {
ui.handleHelpMsgCommand(args)
}
}

func (ui *ChatUI) handleHelpMsgCommand(args []string) {
ui.displaySystemMessage("Usage: /broadcast <DID|NICK> <message>")
ui.displaySystemMessage("Sends a public announcement to the specified DID")
}

0 comments on commit 1298768

Please sign in to comment.