-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiocicoso.go
212 lines (175 loc) · 4.95 KB
/
radiocicoso.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"github.com/thoj/go-ircevent"
"strings"
"regexp"
"math/rand"
)
const (
nickname string = "radiocicoso"
server string = "irc.freenode.net:6667"
)
var channels = []string{"#radiocicletta"}
var goodguys = []string{"leonardo", "Cassapanco", "ineff", "autoscatto", "Biappi"}
func handlerHelp(e *irc.Event) (string, string) {
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return "Per chiedermi qualcosa, usa un comando" +
" preceduto da una chiocciola (ad" +
" es. @help). per conoscere la descrizione di tutti i comandi" +
" scrivi @longhelp\nComandi disponibili:" +
" @help @longhelp @cosera @oggi @inonda @ascolto @podcast",
replyto
}
func handlerLonghelp(e *irc.Event) (string, string) {
return "Lista dei comandi disponibili:\n \n" +
"@help : mostra il messaggio di aiuto\n" +
"@longhelp : mostra tutti i comandi disponibili\n" +
"@cosera : mostra le informazioni sul brano appena passato\n" +
"@oggi : mostra i programmi in onda oggi in radio\n" +
"@inonda : mostra il programma ora in onda\n" +
"@ascolto : come fare per ascoltare radiocicletta\n" +
"@podcast : elenca gli ultimi 5 podcast\n",
e.Nick
}
func handlerAscolto(e *irc.Event) (string, string) {
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return "Puoi ascoltare radiocicletta in diversi modi:\n" +
"• Dal tuo browser, collegandoti al sito " +
"http://www.radiocicletta.it e usando il player del sito\n" +
"• Usando il tuo programma preferito (VLC, iTunes, RealPlayer...) " +
"inserendo nella playlist l'indirizzo " +
"http://stream.radiocicletta.it/stream\n",
replyto
}
func handlerPodcast(e *irc.Event) (string, string) {
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return GetMixcloudPodcasts(), replyto
}
func handlerInonda(e *irc.Event) (string, string) {
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return GetNowOnair(), replyto
}
func handlerOggi(e *irc.Event) (string, string) {
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return GetTodaySchedule(), replyto
}
func handlerCosera(e *irc.Event) (string, string) {
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return GetStreamMetadata(), replyto
}
func handlerHello(e *irc.Event, a ...string) (string, string){
var replyto string
answers := []string{
"o/",
"hello",
"ciao!",
"servus",
"ciaociao",
"cia'",
}
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
return answers[rand.Intn(len(answers))], replyto
}
func handlerNoembed(e *irc.Event, args ...string) (string, string){
var replyto string
if e.Arguments[0] == nickname {
replyto = e.Nick
} else {
replyto = e.Arguments[0]
}
if len(args) > 0{
uri := args[0]
reply := GetNoembedData(uri)
return reply, replyto
}
return "", replyto
}
type IrcReaction struct {
Pattern *regexp.Regexp
Handler func(*irc.Event, ...string) (string, string)
}
func (i *IrcReaction) Match(pattern string) bool {
return i.Pattern.MatchString(pattern)
}
func main() {
ircconn := irc.IRC(nickname, nickname)
cmdqueryhandlers := map[string]func(*irc.Event) (string, string){
"@help" : handlerHelp,
"@longhelp": handlerLonghelp,
"@ascolto": handlerAscolto,
"@podcast": handlerPodcast,
"@cosera": handlerCosera,
"@inonda": handlerInonda,
"@oggi": handlerOggi,
}
reactions := []IrcReaction{
IrcReaction{regexp.MustCompile("https?://[^ ]+"), handlerNoembed},
IrcReaction{regexp.MustCompile("ciao|buon(giorno|di|asera|anotte)|hello"), handlerHello},
}
ircconn.AddCallback("PRIVMSG", func(event *irc.Event) {
var tokens []string = strings.Fields(event.Message())
var msg, replyto string
for _, k := range tokens {
if handler, ok := cmdqueryhandlers[k]; ok == true {
msg, replyto = handler(event)
for _, m := range strings.Split(msg, "\n") {
if m != "" {
ircconn.Privmsgf(replyto, m)
}
}
break
} else {
for _, rx := range reactions {
if rx.Match(k) {
msg, replyto = rx.Handler(event, k)
for _, m := range strings.Split(msg, "\n") {
if m != "" {
ircconn.Privmsgf(replyto, m)
}
}
break
}
}
}
}
})
ircconn.Connect(server)
for _, channel := range channels {
ircconn.Join(channel)
}
ircconn.Loop()
}