forked from Jessidhia/go-lastfm-bot
/
all.go
executable file
·112 lines (94 loc) · 2.71 KB
/
all.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
package main
import (
"flag"
"fmt"
"github.com/fluffle/goirc/client"
"log"
"strings"
)
func addWhoHandlers(irc *client.Conn) {
if !flag.Parsed() {
flag.Parse()
}
irc.AddHandler("352", whoHandler)
irc.AddHandler("315", whoHandler)
return
}
// >> :irc.cccp-project.net 352 Ziltoid #qqkthx Southrop h.i.n.a * Southrop Gr+ :0 Southrop
// >> :irc.cccp-project.net 352 Ziltoid #qqkthx kovensky Rizon-180E162B.bluebottle.net.au * Kov|abx G :0 Diogo Franco
// >> :irc.cccp-project.net 315 Ziltoid #qqkthx :End of /WHO list.
var whoHandlerLimit = make(chan bool, 1)
func whoHandler(irc *client.Conn, line *client.Line) {
whoHandlerLimit <- true
switch line.Cmd {
case "352":
if _, ok := whoResult[line.Args[1]]; !ok {
log.Println("Creating result slice for channel", line.Args[1])
whoResult[line.Args[1]] = []string{}
}
whoResult[line.Args[1]] = append(whoResult[line.Args[1]], line.Args[5])
case "315":
log.Println("End of WHO for channel", line.Args[1])
close(whoChannel[line.Args[1]])
}
<-whoHandlerLimit
return
}
var (
whoChannel = make(map[string]chan bool)
whoResult = make(map[string][]string)
rateLimit = make(chan bool, 6)
)
func reportAllNowPlaying(irc *client.Conn, asker, channel string) {
if !(strings.HasPrefix(channel, "#") || strings.HasPrefix(channel, "&")) {
log.Println("User", asker, "asked What's Playing...... via PM")
irc.Privmsg(channel, fmt.Sprintf("%s: this only works on channels", asker))
return
}
log.Println("User", asker, "requested What's Playing on channel", channel)
if !checkIdentified(irc, asker) {
r := fmt.Sprintf("%s: you must be identified with NickServ to use this command", asker)
log.Println(r)
irc.Privmsg(channel, r)
return
}
if _, ok := whoChannel[channel]; ok {
log.Println("Channel", channel, "is already executing a What's Playing request")
return
}
whoChannel[channel] = make(chan bool, 1)
go irc.Who(channel)
for _ = range whoChannel[channel] { // wait until channel is closed
}
delete(whoChannel, channel)
reportChan := make(chan bool)
totalReport := len(whoResult[channel]) - 1
msg := fmt.Sprintf("Reporting now playing for %d nicks in channel %s", totalReport, channel)
log.Println(msg)
irc.Notice(asker, msg)
for _, nick := range whoResult[channel] {
if nick != irc.Me.Nick {
n := nick
go func() {
rateLimit <- true
reportChan <- reportNowPlaying(irc, channel, asker, n, true)
<-rateLimit
}()
}
}
delete(whoResult, channel)
okCount, totalCount := 0, 0
for r := range reportChan {
if r {
okCount++
}
if totalCount++; totalCount == totalReport {
break
}
}
close(reportChan)
msg = fmt.Sprintf("Reported for %d of %d nicks", okCount, totalCount)
log.Println(msg)
irc.Notice(asker, msg)
return
}