TBR: Show ping replies only to sender
The ping replies are sent to everyone, but they only make sense to
whoever sent the ping in the first place.
Add a token to outgoing pings. If the ping replies don't have that token,
ignore them.
Change-Id: Ic96c35fb40cd66f56bd62006191d6c99f1ddc8c0
diff --git a/vmsg/chat.go b/vmsg/chat.go
index 4ba6455..1b1bd5a 100644
--- a/vmsg/chat.go
+++ b/vmsg/chat.go
@@ -12,6 +12,7 @@
"fmt"
"io"
"io/ioutil"
+ "math/rand"
"mime/multipart"
"os"
"path/filepath"
@@ -134,6 +135,9 @@
); err != nil {
return err
}
+
+ pingId := fmt.Sprintf("%08x", rand.Uint32())
+
if err := g.SetKeybinding("messageInput", gocui.KeyEnter, 0,
func(g *gocui.Gui, v *gocui.View) error {
defer scrollDown()
@@ -152,7 +156,7 @@
help()
return nil
case mtxt == "/ping":
- mtxt = fmt.Sprintf("\x01PING %d", time.Now().UnixNano())
+ mtxt = fmt.Sprintf("\x01PING %s %d", pingId, time.Now().UnixNano())
case mtxt == "/quit":
return gocui.Quit
case strings.HasPrefix(mtxt, "/send"):
@@ -199,7 +203,11 @@
ctx.Errorf("sendMessage failed: %v", err)
}
} else if strings.HasPrefix(msgText, "\x01PONG ") {
- if i, err := strconv.ParseInt(msgText[6:], 10, 64); err == nil {
+ p := strings.Split(msgText, " ")
+ if len(p) != 3 || p[1] != pingId {
+ continue
+ }
+ if i, err := strconv.ParseInt(p[2], 10, 64); err == nil {
t := time.Unix(0, i)
fmt.Fprintf(&buf, "PING reply from %s: %s", msg.SenderBlessings, time.Since(t))
}