From 2610003cdb16e7d6f43721fa2bcc580c4bc728b3 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 26 Nov 2017 18:11:25 +1300 Subject: [PATCH] adc: working public and private messages --HG-- branch : adc --- AdcProtocol.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ TODO.txt | 4 +--- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/AdcProtocol.go b/AdcProtocol.go index 1dbe48e..7671c6c 100644 --- a/AdcProtocol.go +++ b/AdcProtocol.go @@ -71,6 +71,27 @@ func (this *AdcProtocol) pid2cid(pid_base32 string) (string, error) { return cid_base32, nil } +func (this *AdcProtocol) SID2Nick(sid string) (string, bool) { + this.hc.usersMut.Lock() + defer this.hc.usersMut.Unlock() + + nick, ok := this.hc.userSIDs[sid] + return nick, ok +} + +func (this *AdcProtocol) Nick2SID(targetNick string) (string, bool) { + this.hc.usersMut.Lock() + defer this.hc.usersMut.Unlock() + + for sid, nick := range this.hc.userSIDs { + if nick == targetNick { + return sid, true + } + } + + return "", false +} + func (this *AdcProtocol) ProcessCommand(msg string) { if len(msg) == 0 { @@ -264,6 +285,7 @@ func (this *AdcProtocol) ProcessCommand(msg string) { case "BMSG": // Message from a user + // BMSG ZVF4 hi if len(parts) < 3 { this.malformed(parts) return @@ -315,6 +337,26 @@ func (this *AdcProtocol) ProcessCommand(msg string) { case "EMSG": // Private message from other user // EMSG I5RO FMWH test\spm PMI5RO + // EMSG sender recip==us message [flags...] + if len(parts) < 4 { + this.malformed(parts) + return + } + + if parts[2] != this.sid { + this.logError(fmt.Errorf("Recieved a PM intended for someone else (got SID=%s expected SID=%s)", parts[2], this.sid)) + return + } + + senderSid := parts[1] + senderNick, ok := this.SID2Nick(parts[1]) + if !ok { + this.logError(fmt.Errorf("Recieved a PM from an unknown user (SID=%s)", senderSid)) + return + } + + msg := this.unescape(parts[3]) + this.hc.processEvent(HubEvent{EventType: EVENT_PRIVATE, Nick: senderNick, Message: msg}) // Ignored messages // ```````````````` @@ -514,9 +556,15 @@ func (this *AdcProtocol) unescape(encoded string) string { } func (this *AdcProtocol) SayPublic(msg string) { + this.hc.SayRaw("BMSG " + this.sid + " " + this.escape(msg) + "\n") } func (this *AdcProtocol) SayPrivate(user, message string) { + if sid, ok := this.Nick2SID(user); ok { + this.hc.SayRaw("DMSG " + this.sid + " " + sid + " " + this.escape(message) + "\n") + } else { + this.logError(fmt.Errorf("Unknown user '%s'", user)) + } } func (this *AdcProtocol) ProtoMessageSeparator() string { diff --git a/TODO.txt b/TODO.txt index 0dbe648..e0c97bd 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,9 +2,7 @@ NMDC: - Implement ZPipe ($ZOn) ADC: -- SayPublic, SayPrivate -- Recieve PMs - Test that user info updates work + - they're incremental: BINF Z3BA HO1 - Usercommands - ??? -