Format CTCP ACTION messages in logs

This commit is contained in:
Simon Ser 2020-08-17 15:01:53 +02:00
parent 3d8ed90055
commit c654d2bac4
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 45 additions and 11 deletions

22
irc.go
View File

@ -369,3 +369,25 @@ func (js *joinSorter) Swap(i, j int) {
js.channels[i], js.channels[j] = js.channels[j], js.channels[i] js.channels[i], js.channels[j] = js.channels[j], js.channels[i]
js.keys[i], js.keys[j] = js.keys[j], js.keys[i] js.keys[i], js.keys[j] = js.keys[j], js.keys[i]
} }
// parseCTCPMessage parses a CTCP message. CTCP is defined in
// https://tools.ietf.org/html/draft-oakley-irc-ctcp-02
func parseCTCPMessage(msg *irc.Message) (cmd string, params string, ok bool) {
if (msg.Command != "PRIVMSG" && msg.Command != "NOTICE") || len(msg.Params) < 2 {
return "", "", false
}
text := msg.Params[1]
if !strings.HasPrefix(text, "\x01") {
return "", "", false
}
text = strings.Trim(text, "\x01")
words := strings.SplitN(text, " ", 2)
cmd = strings.ToUpper(words[0])
if len(words) > 1 {
params = words[1]
}
return cmd, params, true
}

View File

@ -130,7 +130,11 @@ func formatMessage(msg *irc.Message) string {
case "NOTICE": case "NOTICE":
return fmt.Sprintf("-%s- %s", msg.Prefix.Name, msg.Params[1]) return fmt.Sprintf("-%s- %s", msg.Prefix.Name, msg.Params[1])
case "PRIVMSG": case "PRIVMSG":
if cmd, params, ok := parseCTCPMessage(msg); ok && cmd == "ACTION" {
return fmt.Sprintf("* %s %s", msg.Prefix.Name, params)
} else {
return fmt.Sprintf("<%s> %s", msg.Prefix.Name, msg.Params[1]) return fmt.Sprintf("<%s> %s", msg.Prefix.Name, msg.Params[1])
}
default: default:
return "" return ""
} }
@ -144,24 +148,32 @@ func parseMessage(line, entity string, ref time.Time) (*irc.Message, time.Time,
} }
line = line[11:] line = line[11:]
var cmd, suffix string var cmd, sender, text string
if strings.HasPrefix(line, "<") { if strings.HasPrefix(line, "<") {
cmd = "PRIVMSG" cmd = "PRIVMSG"
suffix = "> " parts := strings.SplitN(line[1:], "> ", 2)
if len(parts) != 2 {
return nil, time.Time{}, nil
}
sender, text = parts[0], parts[1]
} else if strings.HasPrefix(line, "-") { } else if strings.HasPrefix(line, "-") {
cmd = "NOTICE" cmd = "NOTICE"
suffix = "- " parts := strings.SplitN(line[1:], "- ", 2)
if len(parts) != 2 {
return nil, time.Time{}, nil
}
sender, text = parts[0], parts[1]
} else if strings.HasPrefix(line, "* ") {
cmd = "PRIVMSG"
parts := strings.SplitN(line[2:], " ", 2)
if len(parts) != 2 {
return nil, time.Time{}, nil
}
sender, text = parts[0], "\x01ACTION "+parts[1]+"\x01"
} else { } else {
return nil, time.Time{}, nil return nil, time.Time{}, nil
} }
i := strings.Index(line, suffix)
if i < 0 {
return nil, time.Time{}, nil
}
sender := line[1:i]
text := line[i+2:]
year, month, day := ref.Date() year, month, day := ref.Date()
t := time.Date(year, month, day, hour, minute, second, 0, time.Local) t := time.Date(year, month, day, hour, minute, second, 0, time.Local)