-
Notifications
You must be signed in to change notification settings - Fork 7
/
tools.go
86 lines (70 loc) · 1.5 KB
/
tools.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
82
83
84
85
86
package main
import (
"log"
"regexp"
"strings"
"gopkg.in/sorcix/irc.v2"
)
func Post(msg string) {
log.Printf(">>> %s", msg)
if err := session.PostMessage(msg); err != nil {
log.Fatalf("Could not post message to RobustIRC: %v", err)
}
}
func Privmsg(user string, msg string) {
Post("PRIVMSG " + user + " :" + msg)
}
func IsPrivateQuery(p *irc.Message) bool {
return p.Command == "PRIVMSG" && Target(p) == *nick
}
func Join(channel string) {
channel = strings.TrimSpace(channel)
channel = strings.TrimPrefix(channel, "#")
if channel == "" {
return
}
log.Printf("joining #%s", channel)
if *nickserv_password != "" {
Privmsg("chanserv", "invite #"+channel)
}
Post("JOIN #" + channel)
}
func Nick(p *irc.Message) string {
return p.Prefix.Name
}
func Hostmask(p *irc.Message) string {
return p.Prefix.Host
}
func Target(parsed *irc.Message) string {
p := parsed.Params
if len(p) == 0 {
return ""
} else {
return p[0]
}
}
func IsNickAdmin(p *irc.Message) bool {
nick := Nick(p)
admins := regexp.MustCompile("\\s+").Split(*admins, -1)
for _, admin := range admins {
if *verbose {
log.Printf("debug admin: checking if |%s|==|%s| (=%v)", nick, admin, nick == admin)
}
if nick == admin {
return true
}
}
return false
}
func IsIn(needle string, haystack []string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
func clean(text string) string {
text = whitespaceRegex.ReplaceAllString(text, " ")
return strings.TrimSpace(text)
}