-
Notifications
You must be signed in to change notification settings - Fork 14
/
irc.go
231 lines (191 loc) · 5.67 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"strconv"
"strings"
"time"
hirc "github.com/husio/irc"
)
var (
stopIRC = make(map[string](chan string))
ircGlobal irc
ircLoad = make(chan string)
)
func ircIdentityHandler(botName string) (nickname, password string) {
for _, bot := range ircGlobal.Bots {
if bot.BotName == botName {
nickname = bot.Config.Server.Nickname
password = bot.Config.Server.Password
}
}
return
}
//ircMessageHandler the IRC listener that manages inbound messaging
func ircMessageHandler(conn hirc.Conn, botName string) {
nickname, password := ircIdentityHandler(botName)
message, err := conn.ReadMessage()
if err != nil {
Log.Errorf("cannot read message: %s", err)
return
}
// make these easier to send and recieve
channel := message.Params[0]
author := message.Nick()
messageIn := message.Trailing
// Log.Debugf("started handle")
Log.Debug("irc inbound " + message.String())
// keep alive messaging
if message.Command == "PING" {
conn.Send("PONG " + messageIn)
Log.Debug("PONG Sent")
return
}
// for authentication
if message.Command == "NOTICE" {
if strings.Contains(strings.ToLower(messageIn), "this nickname is registered") {
conn.Send("%s IDENTIFY %s %s", author, nickname, password)
}
return
}
// message handling
if message.Command == "PRIVMSG" {
Log.Debug("channel: " + channel) // channel
Log.Debug("author: " + author) // user
Log.Debug("messageIn: " + messageIn) // actual message
// get all the configs
prefix := getPrefix("irc", botName, botName)
channelCommands := getCommands("irc", botName, "", channel)
channelKeywords := getKeywords("irc", botName, "", channel)
channelParsing := getParsing("irc", botName, "", channel)
if author == nickname {
Log.Debug("User is the bot and being ignored.")
return
}
// if the user nickname matches bot or blacklisted.
for _, user := range getBlacklist("discord", botName, "", channel) {
if user == author {
Log.Debugf("user %s is blacklisted", author)
return
}
}
// if bot is DM'd
if channel == nickname {
Log.Debug("This was a DM")
_, mention := getMentions("irc", botName, "", channel)
sendIRCMessage(conn, channel, author, prefix, mention.Response)
return
}
//
// Message Handling
//
if messageIn != "" {
Log.Debug("Message Content: " + messageIn)
if !strings.HasPrefix(messageIn, prefix) {
Log.Debug("sending to \"" + channel)
parseKeyword(messageIn, botName, channelKeywords, channelParsing)
} else {
Log.Debug("sending to \"" + channel)
parseCommand(strings.TrimPrefix(messageIn, prefix), botName, channelCommands)
}
return
}
// Log.Debug(message.Raw)
}
return
}
// kick irc user
func kickIRCUser() {
}
// ban irc user
func banIRCUser() {
}
//sendIRCMessage function to send messages separate of the listener
func sendIRCMessage(conn hirc.Conn, channelName string, user string, prefix string, responseArray []string) {
// send nothing if there is nothing in the array
if len(responseArray) == 0 {
return
}
// send a line per item in the array.
for _, response := range responseArray {
Log.Debugf("line sent: " + response)
response = strings.Replace(response, "&user&", user, -1)
response = strings.Replace(response, "&prefix&", prefix, -1)
conn.Send("PRIVMSG " + "#" + channelName + " :" + response)
time.Sleep(time.Millisecond * 300)
}
// log the message that was sent in debug mode.
Log.Debugf("IRC Message Sent: %s", responseArray)
}
// service handling
// start all the bots
func startIRCBots() {
Log.Infof("Starting IRC server connections\n")
// range over the bots available to start
for _, bot := range ircGlobal.Bots {
Log.Infof("Connecting to %s\n", bot.BotName)
// spin up a channel to tell the bot to shutdown later
stopIRC[bot.BotName] = make(chan string)
// start the bot
go startIRCConnection(bot)
// wait on bot being able to start.
<-ircLoad
}
Log.Infof("irc service started\n")
// inform main process that the bot is started
servStart <- "irc_online"
}
// when a shutdown is sent close out services properly
func stopIRCBots() {
Log.Infof("stopping irc connections")
// loop through bots and send shutdowns
for _, bot := range ircGlobal.Bots {
Log.Infof("stopping %s", bot.BotName)
// send stop to bot
stopIRC[bot.BotName] <- ""
// wait for bot to send a stop back
<-stopIRC[bot.BotName]
// close channel
close(stopIRC[bot.BotName])
Log.Infof("stopped %s", bot.BotName)
}
Log.Infof("irc connections stopped")
// return shutdown signal on channel
servStopped <- "irc_stopped"
}
// start connections to irc
func startIRCConnection(ircConfig ircBot) {
host := ircConfig.Config.Server.Address + ":" + strconv.Itoa(ircConfig.Config.Server.Port)
Log.Debugf("Connecting on %s\n", host)
// Connect to the server
conn, err := hirc.Connect(host)
if err != nil {
Log.Errorf("cannot connect to %s: %s\n", host, err)
}
Log.Debugf("Connected to %s\n", host)
// send user info
conn.Send("USER %s %s * :"+ircConfig.Config.Server.RealName, ircConfig.Config.Server.Ident, host)
conn.Send("NICK %s", ircConfig.Config.Server.Nickname)
time.Sleep(time.Millisecond * 100)
ircLoad <- ""
for _, group := range ircConfig.ChanGroups {
for _, channel := range group.ChannelIDs {
Log.Debugf("joining %s", channel)
if !strings.HasPrefix(channel, "#") {
channel = "#" + channel
}
conn.Send("JOIN %s", channel)
}
}
for {
// listen for stop on channel
select {
case <-stopIRC[ircConfig.BotName]:
Log.Debugf("closing channel for %s", ircConfig.BotName)
conn.Close()
stopIRC[ircConfig.BotName] <- ""
Log.Debugf("%s channel closed", ircConfig.BotName)
return
default:
ircMessageHandler(*conn, ircConfig.BotName)
}
}
}