forked from nnev/frank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.go
131 lines (106 loc) · 2.36 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"errors"
"log"
"regexp"
"strings"
"time"
)
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 Topic(channel string, topic string) {
Post("TOPIC " + channel + " :" + topic)
}
func TopicGet(channel string) (string, error) {
received := make(chan string)
topicGetRunner := func(parsed Message) {
// Example Topic:
// PREFIX=robustirc.net COMMAND=332 PARAMS=[frank #test]
p := parsed.Params
if len(p) < 2 || p[1] != channel {
// not the channel we're interested in
return
}
if parsed.Command == RPL_TOPIC {
received <- parsed.Trailing
}
if parsed.Command == RPL_NOTOPIC {
received <- ""
}
}
l := ListenerAdd("topic getter", topicGetRunner)
Post("TOPIC " + channel)
select {
case topic := <-received:
l.Remove()
return topic, nil
case <-time.After(60 * time.Second):
l.Remove()
}
return "", errors.New("failed to get topic: no reply within 60 seconds")
}
func IsPrivateQuery(p 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 Message) string {
return strings.SplitN(p.Prefix, "!", 2)[0]
}
func Hostmask(p Message) string {
split := strings.SplitN(p.Prefix, "!", 2)
if len(split) < 2 {
return ""
}
return split[1]
}
func Target(parsed Message) string {
p := parsed.Params
if len(p) == 0 {
return ""
} else {
return p[0]
}
}
func IsNickAdmin(p 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)
}