1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-07-12 08:11:17 +00:00

Add a version of iopred's Message() and Target() Line methods, and tests.

This commit is contained in:
Alex Bramley 2013-03-10 15:55:16 +00:00
parent 332ff0a27d
commit e6cf0933b5
2 changed files with 78 additions and 0 deletions

View file

@ -25,6 +25,44 @@ func (l *Line) Copy() *Line {
return &nl
}
// Return the contents of the text portion of a line. This only really
// makes sense for lines with a :text part, but there are a lot of them.
func (line *Line) Text() string {
if len(line.Args) > 0 {
return line.Args[len(line.Args)-1]
}
return ""
}
// 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], "#") {
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], "#") {
return line.Nick
}
return line.Args[1]
}
if len(line.Args) > 0 {
return line.Args[0]
}
return ""
}
// parseLine() creates a Line from an incoming message from the IRC server.
func parseLine(s string) *Line {
line := &Line{Raw: s}