From 9b020ee3873319b3ed29f7c4f985495cc1afeca9 Mon Sep 17 00:00:00 2001 From: Kaleb Elwert Date: Tue, 10 Jan 2017 19:47:06 -0800 Subject: [PATCH] Add missing Client.FromChannel --- client.go | 12 ++++++++++++ client_test.go | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/client.go b/client.go index 724e32e..4e8a515 100644 --- a/client.go +++ b/client.go @@ -105,3 +105,15 @@ func (c *Client) Run() error { func (c *Client) CurrentNick() string { return c.currentNick } + +// FromChannel takes a Message representing a PRIVMSG and returns if that +// message came from a channel or directly from a user. +func (c *Client) FromChannel(m *Message) bool { + if len(m.Params) < 1 { + return false + } + + // The first param is the target, so if this doesn't match the current nick, + // the message came from a channel. + return m.Params[0] != c.currentNick +} diff --git a/client_test.go b/client_test.go index 8a32fa6..d1ce79f 100644 --- a/client_test.go +++ b/client_test.go @@ -154,4 +154,13 @@ func TestClientHandler(t *testing.T) { Params: []string{"\x01VERSION"}, }, }, handler.Messages()) + + m := MustParseMessage("PRIVMSG test_nick :hello world") + assert.False(t, c.FromChannel(m)) + + m = MustParseMessage("PRIVMSG #a_channel :hello world") + assert.True(t, c.FromChannel(m)) + + m = MustParseMessage("PING") + assert.False(t, c.FromChannel(m)) }