Parse ident and host, rename irc.Message.Nick to Sender
This commit is contained in:
parent
8829793290
commit
e76beca4a0
10 changed files with 95 additions and 83 deletions
|
@ -30,7 +30,7 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Client struct {
|
||||
Config Config
|
||||
Config *Config
|
||||
|
||||
Messages chan *Message
|
||||
ConnectionChanged chan ConnectionState
|
||||
|
@ -57,7 +57,7 @@ type Client struct {
|
|||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewClient(config Config) *Client {
|
||||
func NewClient(config *Config) *Client {
|
||||
if config.Port == "" {
|
||||
if config.TLS {
|
||||
config.Port = "6697"
|
||||
|
@ -74,11 +74,18 @@ func NewClient(config Config) *Client {
|
|||
config.Realname = config.Nick
|
||||
}
|
||||
|
||||
c := Client{
|
||||
wantedCapabilities := append([]string{}, clientWantedCaps...)
|
||||
|
||||
if config.SASL != nil {
|
||||
wantedCapabilities = append(wantedCapabilities, "sasl")
|
||||
}
|
||||
|
||||
return &Client{
|
||||
Config: config,
|
||||
nick: config.Nick,
|
||||
Features: NewFeatures(),
|
||||
Messages: make(chan *Message, 32),
|
||||
wantedCapabilities: wantedCapabilities,
|
||||
requestedCapabilities: map[string][]string{},
|
||||
enabledCapabilities: map[string][]string{},
|
||||
ConnectionChanged: make(chan ConnectionState, 4),
|
||||
|
@ -93,14 +100,6 @@ func NewClient(config Config) *Client {
|
|||
Jitter: true,
|
||||
},
|
||||
}
|
||||
|
||||
c.wantedCapabilities = append(c.wantedCapabilities, clientWantedCaps...)
|
||||
|
||||
if config.SASL != nil {
|
||||
c.wantedCapabilities = append(c.wantedCapabilities, "sasl")
|
||||
}
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c *Client) GetNick() string {
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
)
|
||||
|
||||
func testClientSend() (*Client, chan string) {
|
||||
c := NewClient(Config{})
|
||||
c := NewClient(&Config{})
|
||||
conn := &mockConn{hook: make(chan string, 16)}
|
||||
c.conn = conn
|
||||
c.sendRecv.Add(1)
|
||||
|
@ -147,7 +147,6 @@ func TestRegister(t *testing.T) {
|
|||
c.Config.Nick = "nick"
|
||||
c.Config.Username = "user"
|
||||
c.Config.Realname = "rn"
|
||||
t.Log(c.Config)
|
||||
c.register()
|
||||
assert.Equal(t, "CAP LS 302\r\n", <-out)
|
||||
assert.Equal(t, "NICK nick\r\n", <-out)
|
||||
|
|
|
@ -200,12 +200,12 @@ func (c *Client) recv() {
|
|||
go c.write("PONG :" + msg.LastParam())
|
||||
|
||||
case JOIN:
|
||||
if c.EqualFold(msg.Nick, c.GetNick()) {
|
||||
if c.Is(msg.Sender) {
|
||||
c.addChannel(msg.Params[0])
|
||||
}
|
||||
|
||||
case NICK:
|
||||
if c.EqualFold(msg.Nick, c.GetNick()) {
|
||||
if c.Is(msg.Sender) {
|
||||
c.setNick(msg.LastParam())
|
||||
}
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ func (i *mockIrcd) handle(conn net.Conn) {
|
|||
}
|
||||
|
||||
func TestConnect(t *testing.T) {
|
||||
c := NewClient(Config{
|
||||
c := NewClient(&Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: "45678",
|
||||
})
|
||||
|
@ -87,7 +87,7 @@ func TestConnect(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestConnectTLS(t *testing.T) {
|
||||
c := NewClient(Config{
|
||||
c := NewClient(&Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: "45679",
|
||||
TLS: true,
|
||||
|
@ -100,12 +100,12 @@ func TestConnectTLS(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestConnectDefaultPorts(t *testing.T) {
|
||||
c := NewClient(Config{
|
||||
c := NewClient(&Config{
|
||||
Host: "127.0.0.1",
|
||||
})
|
||||
assert.Equal(t, "6667", c.Config.Port)
|
||||
|
||||
c = NewClient(Config{
|
||||
c = NewClient(&Config{
|
||||
Host: "127.0.0.1",
|
||||
TLS: true,
|
||||
})
|
||||
|
@ -125,7 +125,7 @@ func TestWrite(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRecv(t *testing.T) {
|
||||
c := NewClient(Config{})
|
||||
c := NewClient(&Config{})
|
||||
conn := &mockConn{hook: make(chan string, 16)}
|
||||
c.conn = conn
|
||||
|
||||
|
@ -145,7 +145,7 @@ func TestRecv(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRecvTriggersReconnect(t *testing.T) {
|
||||
c := NewClient(Config{})
|
||||
c := NewClient(&Config{})
|
||||
c.conn = &mockConn{}
|
||||
c.scan = bufio.NewScanner(bytes.NewBufferString("001 bob\r\n"))
|
||||
done := make(chan struct{})
|
||||
|
@ -168,7 +168,7 @@ func TestRecvTriggersReconnect(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
c := NewClient(Config{})
|
||||
c := NewClient(&Config{})
|
||||
close(c.quit)
|
||||
ok := false
|
||||
done := make(chan struct{})
|
||||
|
|
|
@ -45,25 +45,25 @@ func EncodeCTCP(ctcp *CTCP) string {
|
|||
func (c *Client) handleCTCP(ctcp *CTCP, msg *Message) {
|
||||
switch ctcp.Command {
|
||||
case "CLIENTINFO":
|
||||
c.ReplyCTCP(msg.Nick, ctcp.Command, ClientInfo)
|
||||
c.ReplyCTCP(msg.Sender, ctcp.Command, ClientInfo)
|
||||
|
||||
case "FINGER", "VERSION":
|
||||
if c.Config.Version != "" {
|
||||
c.ReplyCTCP(msg.Nick, ctcp.Command, c.Config.Version)
|
||||
c.ReplyCTCP(msg.Sender, ctcp.Command, c.Config.Version)
|
||||
}
|
||||
|
||||
case "PING":
|
||||
c.ReplyCTCP(msg.Nick, ctcp.Command, ctcp.Params)
|
||||
c.ReplyCTCP(msg.Sender, ctcp.Command, ctcp.Params)
|
||||
|
||||
case "SOURCE":
|
||||
if c.Config.Source != "" {
|
||||
c.ReplyCTCP(msg.Nick, ctcp.Command, c.Config.Source)
|
||||
c.ReplyCTCP(msg.Sender, ctcp.Command, c.Config.Source)
|
||||
}
|
||||
|
||||
case "TIME":
|
||||
c.ReplyCTCP(msg.Nick, ctcp.Command, time.Now().UTC().Format(time.RFC3339))
|
||||
c.ReplyCTCP(msg.Sender, ctcp.Command, time.Now().UTC().Format(time.RFC3339))
|
||||
|
||||
case "USERINFO":
|
||||
c.ReplyCTCP(msg.Nick, ctcp.Command, fmt.Sprintf("%s (%s)", c.GetNick(), c.Config.Realname))
|
||||
c.ReplyCTCP(msg.Sender, ctcp.Command, fmt.Sprintf("%s (%s)", c.GetNick(), c.Config.Realname))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,9 @@ import (
|
|||
|
||||
type Message struct {
|
||||
Tags map[string]string
|
||||
Prefix string
|
||||
Nick string
|
||||
Sender string
|
||||
Ident string
|
||||
Host string
|
||||
Command string
|
||||
Params []string
|
||||
}
|
||||
|
@ -20,7 +21,7 @@ func (m *Message) LastParam() string {
|
|||
}
|
||||
|
||||
func (m *Message) IsFromServer() bool {
|
||||
return m.Nick == "" || strings.Contains(m.Nick, ".")
|
||||
return m.Sender == "" || strings.Contains(m.Sender, ".")
|
||||
}
|
||||
|
||||
func (m *Message) ToCTCP() *CTCP {
|
||||
|
@ -65,14 +66,23 @@ func ParseMessage(line string) *Message {
|
|||
if next == -1 {
|
||||
return nil
|
||||
}
|
||||
msg.Prefix = line[1:next]
|
||||
prefix := line[1:next]
|
||||
|
||||
if i := strings.Index(msg.Prefix, "!"); i > 0 {
|
||||
msg.Nick = msg.Prefix[:i]
|
||||
} else if i := strings.Index(msg.Prefix, "@"); i > 0 {
|
||||
msg.Nick = msg.Prefix[:i]
|
||||
if i := strings.Index(prefix, "!"); i > 0 {
|
||||
msg.Sender = prefix[:i]
|
||||
prefix = prefix[i+1:]
|
||||
|
||||
if i = strings.Index(prefix, "@"); i > 0 {
|
||||
msg.Ident = prefix[:i]
|
||||
msg.Host = prefix[i+1:]
|
||||
} else {
|
||||
msg.Ident = prefix
|
||||
}
|
||||
} else if i = strings.Index(prefix, "@"); i > 0 {
|
||||
msg.Sender = prefix[:i]
|
||||
msg.Host = prefix[i+1:]
|
||||
} else {
|
||||
msg.Nick = msg.Prefix
|
||||
msg.Sender = prefix
|
||||
}
|
||||
|
||||
line = line[next+1:]
|
||||
|
|
|
@ -14,16 +14,16 @@ func TestParseMessage(t *testing.T) {
|
|||
{
|
||||
":user CMD #chan :some message",
|
||||
&Message{
|
||||
Prefix: "user",
|
||||
Nick: "user",
|
||||
Sender: "user",
|
||||
Command: "CMD",
|
||||
Params: []string{"#chan", "some message"},
|
||||
},
|
||||
}, {
|
||||
":nick!user@host.com CMD a b",
|
||||
&Message{
|
||||
Prefix: "nick!user@host.com",
|
||||
Nick: "nick",
|
||||
Sender: "nick",
|
||||
Ident: "user",
|
||||
Host: "host.com",
|
||||
Command: "CMD",
|
||||
Params: []string{"a", "b"},
|
||||
},
|
||||
|
@ -53,15 +53,16 @@ func TestParseMessage(t *testing.T) {
|
|||
}, {
|
||||
":nick@host.com CMD",
|
||||
&Message{
|
||||
Prefix: "nick@host.com",
|
||||
Nick: "nick",
|
||||
Sender: "nick",
|
||||
Host: "host.com",
|
||||
Command: "CMD",
|
||||
},
|
||||
}, {
|
||||
":ni@ck!user!name@host!.com CMD",
|
||||
&Message{
|
||||
Prefix: "ni@ck!user!name@host!.com",
|
||||
Nick: "ni@ck",
|
||||
Sender: "ni@ck",
|
||||
Ident: "user!name",
|
||||
Host: "host!.com",
|
||||
Command: "CMD",
|
||||
},
|
||||
}, {
|
||||
|
@ -114,18 +115,20 @@ func TestParseMessage(t *testing.T) {
|
|||
Tags: map[string]string{
|
||||
"x": "y",
|
||||
},
|
||||
Prefix: "nick!user@host.com",
|
||||
Nick: "nick",
|
||||
Sender: "nick",
|
||||
Ident: "user",
|
||||
Host: "host.com",
|
||||
Command: "CMD",
|
||||
},
|
||||
}, {
|
||||
"@x=y :nick!user@host.com CMD :pie and cake",
|
||||
"@x=y :nick!user@host.com CMD :pie and cake",
|
||||
&Message{
|
||||
Tags: map[string]string{
|
||||
"x": "y",
|
||||
},
|
||||
Prefix: "nick!user@host.com",
|
||||
Nick: "nick",
|
||||
Sender: "nick",
|
||||
Ident: "user",
|
||||
Host: "host.com",
|
||||
Command: "CMD",
|
||||
Params: []string{"pie and cake"},
|
||||
},
|
||||
|
@ -135,8 +138,9 @@ func TestParseMessage(t *testing.T) {
|
|||
Tags: map[string]string{
|
||||
"x": "y",
|
||||
},
|
||||
Prefix: "nick!user@host.com",
|
||||
Nick: "nick",
|
||||
Sender: "nick",
|
||||
Ident: "user",
|
||||
Host: "host.com",
|
||||
Command: "CMD",
|
||||
Params: []string{"beans", "rainbows", "pie and cake"},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue