-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirc.go
206 lines (189 loc) · 5.26 KB
/
irc.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
package main
import (
"errors"
"fmt"
"log"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/thoj/go-ircevent"
)
func connectToTracker() {
log.Printf("Connection to %s:%d commencing.\n", tc.IRC.Server, tc.IRC.Port)
server := tc.IRC.Server + ":" + strconv.Itoa(tc.IRC.Port)
if err := ircClient.Connect(server); err != nil {
IRCConfigError <- err
}
if ircClient.Connected() {
ircStatus.Set("Connected")
ircConnectTimestamp.Set(time.Now().Unix())
registerNick()
// give the server a chance to see the user before attempting to watch the IRC channel.
time.Sleep(5 * time.Second)
watchIRCChannel()
}
}
func watchIRCChannel() {
if tc.IRC.InviteCmd != "" {
invite := strings.Replace(tc.IRC.InviteCmd, "%n%", tc.IRC.Nick, -1)
invite = strings.Replace(invite, "%k%", tc.IRC.Key, -1)
PrintDebugf("Sending invite to %s: %s\n", tc.IRC.ChannelOwner, invite)
ircStatus.Set("Requesting Invite")
ircClient.Privmsgf(tc.IRC.ChannelOwner, invite)
} else {
if tc.IRC.WatchChannel != "" {
log.Printf("Joining channel %s", tc.IRC.WatchChannel)
ircClient.Join(tc.IRC.WatchChannel)
}
}
}
func registerNick() {
if tc.IRC.Nick == "" {
PrintDebugln("No nickname set. IRC will not work properly.")
return
}
ircClient.Nick(tc.IRC.Nick)
if !tc.IRC.Registered {
ircClient.Privmsgf("nickserv", "register %s %s", tc.IRC.Key, tc.Operations.Email)
}
if ircClient.Connected() && tc.IRC.Registered {
PrintDebugln("identifying to nickserv")
ircClient.Privmsgf("nickserv", "identify %s", tc.IRC.Key)
}
}
func msgToUser(e *irc.Event) {
PrintDebugf("msgToUser: %s", e.Message())
msg := e.Message()
if e.User == "NickServ" {
if strings.Contains(msg, "isn't") || strings.Contains(msg, "incorrect") {
IRCConfigError <- errors.New(msg)
} else if strings.Contains(msg, "registered") {
ircStatus.Set("Nick Ready")
} else if strings.Contains(msg, "assword") {
ircStatus.Set("Nick Registered")
}
} else {
PrintDebugf("msgToUser: checking message for show announcement.")
go matchAnnounce(e)
}
}
func matchAnnounce(e *irc.Event) {
PrintDebugf("matchAnnounce: %s\n", e.Message())
metricUpdate <- time.Now().Unix()
aMatch := announceLine.FindStringSubmatch(e.Message())
if aMatch != nil {
PrintDebugln("matchAnnounce: IRC message is a valid announce line.")
ep, err := ParseTorrentString(aMatch[1])
if err != nil {
PrintDebugf("Error parsing string: %s\n", err)
return
}
isNew := ep.IsNewEpisode()
if !isNew {
PrintDebugln("We already have this episode.")
return
}
if !ep.ValidEpisodeQuality(aMatch[1]) {
PrintDebugf("Episode %s isn't the right quality.\n", aMatch[1])
return
}
ff, err := NewFileFetch(aMatch[2])
if err != nil {
log.Println(err)
}
err = ff.RetrieveEpisode()
if err != nil {
log.Printf("FAIL: episode not retrieved: %s\n", err)
}
err = ep.AddEpisode()
if err != nil {
log.Printf("Episode is downloading, but didn't update the db: %s\n", err)
}
}
}
func handleInvite(e *irc.Event) {
PrintDebugf("handleInvite: %s\n", e.Message())
if tc.IRC.WatchChannel == "" {
log.Println("Ignoring invite event because no channels are tracked.")
return
}
PrintDebugf("Handling IRC invite event: %s", e.Message())
c := e.Connection
if strings.Index(e.Message(), tc.IRC.WatchChannel) != -1 {
PrintDebugln("IRC channel invitation successful. Joining Now.")
c.Join(tc.IRC.WatchChannel)
ircStatus.Set("Watching Channel")
if c.Log != nil {
c.Log.SetPrefix(tc.IRC.WatchChannel + ": ")
}
}
}
func _InitIRC() {
ircClient = irc.IRC(tc.IRC.Nick, tc.IRC.Nick)
ircClient.Password = tc.IRC.Key
ircClient.PingFreq = time.Duration(tc.IRC.PingFreq) * time.Minute
// Callbacks for various IRC events.
ircClient.AddCallback("invite", handleInvite)
ircClient.AddCallback("msg", matchAnnounce)
ircClient.AddCallback("privmsg", matchAnnounce)
ar, _ := url.QueryUnescape(tc.IRC.AnnounceRegexp)
announceLine = regexp.MustCompile(ar)
ircStatus.Set("Ready")
}
func _TrackIRCStatus() {
for {
select {
// Turn on and off the IRC service.
case e := <-IRCEnabled:
if e && ircClient == nil {
_InitIRC()
if ircClient == nil {
IRCConfigError <- errors.New("IRC Configuration Issues. Not Connected.")
}
} else if e && ircClient != nil && !ircClient.Connected() {
connectToTracker()
} else if !e && ircClient.Connected() {
ircClient.Disconnect()
}
// Update the IRC configuration.
case cfg := <-IRCConfigChanged:
if cfg {
if ircClient != nil {
ircClient.Disconnect()
}
if tc.Operations.WatchMethods["irc"] {
_InitIRC()
if ircClient == nil {
IRCConfigError <- errors.New("IRC Configuration Issues. Not Connected.")
}
connectToTracker()
}
}
// Updates the lastest timestamp metric.
case ts := <-metricUpdate:
last, _ := strconv.Atoi(ircUpdateTimestamp.String())
if ts > int64(last) {
ircUpdateTimestamp.Set(ts)
}
case err := <-IRCConfigError:
if err == nil {
continue
}
log.Println(err)
ircStatus.Set(fmt.Sprintf("Config Error: %s\n", err))
ircClient.Disconnect()
return
}
}
}
func StartIRC() {
_InitIRC()
connectToTracker()
go _TrackIRCStatus()
IRCEnabled <- tc.Operations.WatchMethods["irc"]
IRCConfigChanged <- false
IRCConfigError <- nil
metricUpdate <- 0
}