This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
81 lines (68 loc) · 1.81 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"regexp"
"strings"
)
const (
runeIrcBold = '\x02'
runeIrcColor = '\x03'
runeIrcReset = '\x0f'
runeIrcReverse = '\x16'
runeIrcItalic = '\x1d'
runeIrcStrikethrough = '\x1e'
runeIrcUnderline = '\x1f'
)
var (
rxIrcColor = regexp.MustCompile(`[` + strings.Join([]string{
string(runeIrcBold),
string(runeIrcUnderline),
string(runeIrcReset),
string(runeIrcReverse),
string(runeIrcItalic),
string(runeIrcStrikethrough),
}, "") + `]|` + string(runeIrcColor) + `(\d\d?(,\d\d?)?)?`)
)
func stripIrcFormatting(text string) string {
return rxIrcColor.ReplaceAllLiteralString(text, "")
}
const (
runeCTCPDelimiter = '\x01'
runeCTCPParamDelimiter = ' '
)
var (
rxCTCP = regexp.MustCompile(`^` +
string(runeCTCPDelimiter) +
`([^` + string(runeCTCPParamDelimiter) + `]+)(` + string(runeCTCPParamDelimiter) + `.+)` + // not using \s is intentional here
string(runeCTCPDelimiter) + `?` +
`$`)
)
type ctcpMessage struct {
Command string
Params []string
}
func (msg *ctcpMessage) String() string {
str := string(runeCTCPDelimiter) + strings.ToUpper(msg.Command)
if msg.Params != nil && len(msg.Params) > 0 {
str += string(runeCTCPParamDelimiter) + strings.Join(msg.Params, string(runeCTCPParamDelimiter))
}
str += string(runeCTCPDelimiter)
return str
}
func (msg *ctcpMessage) ParamLine() string {
return strings.Join(msg.Params, string(runeCTCPParamDelimiter))
}
func parseCTCP(msg string) (parsedMessage *ctcpMessage, ok bool) {
matches := rxCTCP.FindStringSubmatch(msg)
if matches == nil {
return
}
parsedMessage = &ctcpMessage{}
parsedMessage.Command = matches[1]
if len(matches) > 2 {
parsedMessage.Params = strings.Split(matches[2], string(runeCTCPParamDelimiter))
} else {
parsedMessage.Params = []string{}
}
ok = true
return
}