adc: working public and private messages

--HG--
branch : adc
This commit is contained in:
mappu 2017-11-26 18:11:25 +13:00
parent 433bf712b9
commit 2610003cdb
2 changed files with 49 additions and 3 deletions

View File

@ -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 {

View File

@ -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
- ???