From 34462b2ad50680e986412c462ae0cc1e55beef27 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sun, 17 Mar 2013 16:46:39 +0000 Subject: [PATCH] Add another Line method to make life easier. --- client/line.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/client/line.go b/client/line.go index da2e708..4a8be6a 100644 --- a/client/line.go +++ b/client/line.go @@ -37,22 +37,15 @@ func (line *Line) Text() string { // Return the target of the line, usually the first Arg for the IRC verb. // If the line was broadcast from a channel, the target will be that channel. // If the line was broadcast by a user, the target will be that user. -// NOTE: Makes the assumption that all channels start with #. // TODO(fluffle): Add 005 CHANTYPES parsing for this? func (line *Line) Target() string { switch line.Cmd { case PRIVMSG, NOTICE, ACTION: - if !strings.HasPrefix(line.Args[0], "#") { + if !line.Public() { return line.Nick } case CTCP, CTCPREPLY: - // CTCP prepends the CTCP verb to line.Args, thus for the message - // :nick!user@host PRIVMSG #foo :\001BAR baz\001 - // line.Args contains: []string{"BAR", "#foo", "baz"} - // TODO(fluffle): Arguably this is broken, and we should have - // line.Args containing: []string{"#foo", "BAR", "baz"} - // ... OR change conn.Ctcp()'s argument order to be consistent. - if !strings.HasPrefix(line.Args[1], "#") { + if !line.Public() { return line.Nick } return line.Args[1] @@ -63,6 +56,28 @@ func (line *Line) Target() string { return "" } +// NOTE: Makes the assumption that all channels start with #. +func (line *Line) Public() bool { + switch line.Cmd { + case PRIVMSG, NOTICE, ACTION: + if strings.HasPrefix(line.Args[0], "#") { + return true + } + case CTCP, CTCPREPLY: + // CTCP prepends the CTCP verb to line.Args, thus for the message + // :nick!user@host PRIVMSG #foo :\001BAR baz\001 + // line.Args contains: []string{"BAR", "#foo", "baz"} + // TODO(fluffle): Arguably this is broken, and we should have + // line.Args containing: []string{"#foo", "BAR", "baz"} + // ... OR change conn.Ctcp()'s argument order to be consistent. + if strings.HasPrefix(line.Args[1], "#") { + return true + } + } + return false +} + + // parseLine() creates a Line from an incoming message from the IRC server. func parseLine(s string) *Line { line := &Line{Raw: s}