This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchat.go
52 lines (46 loc) · 1.43 KB
/
chat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package dctk
import (
"github.com/aler9/go-dc/adc"
"github.com/aler9/go-dc/nmdc"
"github.com/aler9/dctk/pkg/log"
"github.com/aler9/dctk/pkg/protoadc"
)
// MessagePublic publishes a message in the hub public chat.
func (c *Client) MessagePublic(content string) {
if c.protoIsAdc() {
c.hubConn.conn.Write(&protoadc.AdcBMessage{ //nolint:govet
&adc.BroadcastPacket{ID: c.adcSessionID},
&adc.ChatMessage{Text: content},
})
} else {
c.hubConn.conn.Write(&nmdc.ChatMessage{c.conf.Nick, content}) //nolint:govet
}
}
// MessagePrivate sends a private message to a specific peer connected to the hub.
func (c *Client) MessagePrivate(dest *Peer, content string) {
if c.protoIsAdc() {
c.hubConn.conn.Write(&protoadc.AdcDMessage{ //nolint:govet
&adc.DirectPacket{ID: c.adcSessionID, To: dest.adcSessionID},
&adc.ChatMessage{Text: content},
})
} else {
c.hubConn.conn.Write(&nmdc.PrivateMessage{
From: c.conf.Nick,
Name: c.conf.Nick,
To: dest.Nick,
Text: content,
})
}
}
func (c *Client) handlePublicMessage(author *Peer, content string) {
log.Log(c.conf.LogLevel, log.LevelInfo, "[PUB] <%s> %s", author.Nick, content)
if c.OnMessagePublic != nil {
c.OnMessagePublic(author, content)
}
}
func (c *Client) handlePrivateMessage(author *Peer, content string) {
log.Log(c.conf.LogLevel, log.LevelInfo, "[PRIV] <%s> %s", author.Nick, content)
if c.OnMessagePrivate != nil {
c.OnMessagePrivate(author, content)
}
}