-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRC.txt
448 lines (385 loc) · 15.3 KB
/
IRC.txt
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
Script("Name") = "IRC"
Script("Author") = "Pyro"
Script("Major") = 0
Script("Minor") = 2
Script("Description") = "Allows StealthBot to connect to an IRC network."
Const IRC_COMMAND = "irc"
Const IRC_CMD_SUB = "subcmd"
Const IRC_CMD_ARGS = "args"
Const IRC_FOCUS_CMD = "if"
Const IRC_FOCUS_ARGS = "args"
Public ClientSettings
Public FocusChannel
Public MyNickname
Private sckIRC
Private ReceiveBuffer
Private ReceivedWelcome
Sub Event_Load()
CreateObj "Winsock", "sckIRC"
Call InitCommands()
Call InitSettings()
Set ClientSettings = GetSettings()
If ClientSettings.AutoConnect And ClientSettings.CanConnect Then
Call Connect(ClientSettings.Server, ClientSettings.Port)
End If
End Sub
Sub Event_Command(Command)
Select Case Command.Name
Case IRC_COMMAND
hasArgs = Not CBool(Len(Trim(Command.Argument(IRC_CMD_ARGS))) = 0)
args = Split(Command.Argument(IRC_CMD_ARGS))
Select Case LCase(Command.Argument(IRC_CMD_SUB))
Case "connect"
If hasArgs Then
remoteEP = IIf(UBound(args) = 0, Split(args(0), ":"), args)
Else
remoteEP = Array(ClientSettings.Server, ClientSettings.Port)
End If
If IsConnected() Then
Command.Respond "Invalid operation. Client is already connected."
Else
Call Connect(remoteEP(0), IIf(UBound(remoteEP) > 0, Int(remoteEP(1)), 6667))
End If
Case "disconnect"
sckIRC.Close
Case "send"
If Not hasArgs Then
Command.Respond "You must specify a channel and message to send."
Else
msgArgs = Split(Command.Argument(IRC_CMD_ARGS), Space(1), 2)
If UBound(msgArgs) = 0 Then
Command.Respond "You must specify a channel to send to."
Else
Send "PRIVMSG " & msgArgs(0) & " :" & msgArgs(1)
End If
End If
Case "join"
If Not hasArgs Then
Command.Respond "You must specify a channel to join."
Else
Send "JOIN " & args(0)
End If
Case "part"
If Not hasArgs Then
Command.Respond "You must specify a channel to leave."
Else
partArgs = Split(Command.Argument(IRC_CMD_ARGS), Space(1), 2)
Send "PART " & partArgs(0) & IIf(UBound(partArgs) > 0, " :" & partArgs(1), vbNullString)
End If
Case "focus"
If hasArgs Then
FocusChannel = args(0)
Command.Respond "Set IRC focus channel to: " & FocusChannel
Else
Command.Respond "The IRC focus channel is set to: " & FocusChannel
End If
Case "nick"
If hasArgs Then
Send "NICK " & args(0)
Else
Command.Respond "Current IRC nickname is: " & MyNickname
End If
Case "raw"
If Not hasArgs Then
Command.Respond "You must specify text to send."
Else
Send Command.Argument(IRC_CMD_ARGS)
End If
Case "get"
If Not hasArgs Then
Command.Respond "You must specify a setting to get."
Else
value = GetSettingsEntry(args(0))
If value = vbNullString Then value = "[ not set ]"
Command.Respond StringFormat("{0}: {1}", args(0), value)
End If
Case "set"
setArgs = Split(Command.Argument(IRC_CMD_ARGS), Space(1), 2)
If UBound(setArgs) < 1 Then
Command.Respond "You must specify a setting name and value."
Else
WriteSettingsEntry setArgs(0), setArgs(1)
Command.Respond StringFormat("Set '{0}' to '{1}'", setArgs(0), setArgs(1))
Set ClientSettings = GetSettings()
End If
End Select
Case IRC_FOCUS_CMD
If FocusChannel = vbNullString Then
Command.Respond "You must set a focus channel using the 'irc focus <channel>' command."
Else
If Len(Command.Argument(IRC_FOCUS_ARGS)) = 0 Then
Command.Respond "You must specify a message to send."
Else
Send "PRIVMSG " & FocusChannel & " :" & Command.Argument(IRC_FOCUS_ARGS)
End If
End If
End Select
End Sub
Sub Connect(RemoteHost, RemotePort)
If IsConnected() Then
AddChat Color.Orange, "[IRC] ", Color.ErrorMessageText, "Invalid operation. Client already connected."
Exit Sub
End If
With sckIRC
.RemoteHost = RemoteHost
.RemotePort = RemotePort
AddChat Color.Orange, "[IRC] ", Color.InformationText, StringFormat("Connecting to {0} ...", RemoteHost)
.Connect
End With
End Sub
Function IsConnected()
IsConnected = CBool(sckIRC.State = 7) ' sckConnected
End Function
Sub Login(Nickname, Username, RealName)
If Nickname = vbNullString Then Exit Sub
If Username = vbNullString Then Username = Nickname
If RealName = vbNullString Then RealName = Nickname
Send "NICK " & Nickname
Send "USER " & Username & " * * :" & RealName
End Sub
Sub Send(Message)
If IsConnected() Then
If Len(Message) > 510 Then
AddChat Color.Orange, "[IRC] ", Color.ErrorMessageText, "Error: Max message length exceeded. Unable to send."
Exit Sub
End If
sckIRC.SendData Message & vbCrLf
If Left(Message, 7) = "PRIVMSG" Then
parts = Split(Message, Space(1), 3)
chanName = parts(1)
text = parts(2)
If Left(text, 1) = ":" Then text = Mid(text, 2)
AddChat Color.Orange, "[IRC] ", Color.TalkNormalText, chanName & ": ", Color.Carats, "<", Color.TalkBotUsername, MyNickname, Color.Carats, "> ", Color.TalkNormalText, text
End If
End If
End Sub
Sub ProcessIrcMessage(MessageText)
Set objMsg = New IrcMessage
objMsg.Parse MessageText
If Not ReceivedWelcome Then
If objMsg.Command <> "001" Then Exit Sub
ReceivedWelcome = True
End If
Select Case objMsg.Command
Case "001"
MyNickname = objMsg.Parameters(0)
If Len(ClientSettings.AutoJoinChannels) > 0 Then
Send "JOIN " & ClientSettings.AutoJoinChannels
End If
Case "PING"
Send "PONG " & objMsg.Parameters(0)
Case "PRIVMSG"
otherNick = GetNick(objMsg.Prefix)
chanName = objMsg.Parameters(0)
text = objMsg.Parameters(1)
If chanName = MyNickname Then
AddChat Color.Orange, "[IRC] ", Color.WhisperCarats, "<From: ", Color.WhisperUsernames, otherNick, Color.WhisperCarats, "> ", Color.WhisperText, text
Else
AddChat Color.Orange, "[IRC] ", Color.TalkNormalText, chanName & ": ", Color.Carats, "<", Color.TalkUsernameNormal, otherNick, Color.Carats, "> ", Color.TalkNormalText, text
End If
Case "NICK"
If GetNick(objMsg.Prefix) = MyNickname Then
MyNickname = objMsg.Parameters(0)
AddChat Color.Orange, "[IRC] ", Color.InformationText, "You are now known as: ", Color.SuccessText, MyNickname
End If
Case "JOIN"
If GetNick(objMsg.Prefix) = MyNickname Then
AddChat Color.Orange, "[IRC] ", Color.JoinedChannelText, "-- Joined channel: ", Color.JoinedChannelName, objMsg.Parameters(0), Color.JoinedChannelText, " --"
If ClientSettings.AutoSetFocus = True Then
FocusChannel = objMsg.Parameters(0)
End If
End If
Case Else
ignored = Array("002", "003", "004", "005", "250", "251", "252", "253", "254", "255", "265", "266", "332", "333", "375", "372", "376")
For Each cmd In ignored
If objMsg.Command = cmd Then Exit Sub
Next
If ClientSettings.DebugMode > 0 Then
AddChat Color.Orange, "[IRC] ", Color.ConsoleText, "MSG: " & StringFormat("({0}): {1} => '{2}'", objMsg.Prefix, objMsg.Command, Join(objMsg.Parameters, "', '"))
End If
End Select
End Sub
Function GetNick(Prefix)
GetNick = Split(Prefix, "!")(0)
End Function
Sub sckIRC_Connect()
AddChat Color.Orange, "[IRC] ", Color.SuccessText, "Connected!"
ReceivedWelcome = False
MyNickname = vbNullString
FocusChannel = vbNullString
Call Login(ClientSettings.Nickname, ClientSettings.Username, ClientSettings.RealName)
End Sub
Sub sckIRC_Close()
AddChat Color.Orange, "[IRC] ", Color.ErrorMessageText, "Disconnected."
End Sub
Sub sckIRC_DataArrival(TotalBytes)
' Read the new data
Data = vbNullString
sckIRC.GetData Data, vbString, TotalBytes
' Append the new data to the receive buffer
ReceiveBuffer = ReceiveBuffer & Data
' Split up all of the messages from the receive buffer
Messages = Split(ReceiveBuffer, vbCrLf)
For i = 0 To (UBound(Messages) - 1)
Call ProcessIrcMessage(Messages(i))
Next
' Set the receive buffer to whatever is left
ReceiveBuffer = Messages(UBound(Messages))
End Sub
Sub InitSettings()
a = Array( _
"AutoConnect", False, _
"Server", vbNullString, _
"Port", 6667, _
"Nickname", vbNullString, _
"Username", vbNullString, _
"RealName", "stealthbot.net", _
"Password", vbNullString, _
"AutoJoinChannels", "#stealthbot", _
"AutoSetFocus", True, _
"DebugMode", "0" _
)
For i = 0 To UBound(a) Step 2
If GetSettingsEntry(a(i)) = vbNullString Then
WriteSettingsEntry a(i), a(i + 1)
End If
Next
End Sub
Function GetSettings()
Set GetSettings = New IrcSettings
Call GetSettings.Read()
End Function
Sub InitCommands()
Set cmd = OpenCommand(IRC_COMMAND)
If cmd Is Nothing Then
Set cmd = CreateCommand(IRC_COMMAND)
With cmd
cmd.RequiredRank = -1
cmd.Description = "Controls the IRC client."
Set param = .NewParameter(IRC_CMD_SUB, False, "word")
Set restrict = .NewRestriction(IRC_CMD_SUB, -1)
restrict.MatchMessage = "[Gg][Ee][Tt]"
restrict.MatchCaseSensitive = False
restrict.MatchError = "That sub-command is not available."
param.Restrictions.Add restrict
.Parameters.Add param
.Parameters.Add .NewParameter(IRC_CMD_ARGS, True, "string")
.Save
End With
Set cmd = Nothing
End If
Set cmd = OpenCommand(IRC_FOCUS_CMD)
If cmd Is Nothing Then
Set cmd = CreateCommand(IRC_FOCUS_CMD)
With cmd
cmd.RequiredRank = -1
cmd.Description = "Sends a message to the focused IRC channel."
.Parameters.Add .NewParameter(IRC_FOCUS_ARGS, False, "string")
.Save
End With
End If
End Sub
Class IrcMessage
Public Prefix
Public Command
Public Parameters()
Public HasTrailingParameter
Sub Class_Initialize()
Prefix = vbNullString
Command = vbNullString
ReDim Parameters(0)
HasTrailingParameter = False
End Sub
Public Sub Create(cmd, params)
Command = cmd
Parameters = params
End Sub
Public Function Parse(msg)
parts = Split(msg)
index = 0
If Left(parts(index), 1) = ":" Then
Prefix = Mid(parts(index), 2)
index = index + 1
End If
If UBound(parts) < index Then
' invalid message
Parse = False
Exit Function
End If
Command = parts(index)
index = index + 1
paramIndex = 0
HasTrailingParameter = False
Do While (index <= UBound(parts))
If HasTrailingParameter Then
Parameters(paramIndex) = Parameters(paramIndex) & Space(1) & parts(index)
Else
ReDim Preserve Parameters(paramIndex)
If Left(parts(index), 1) = ":" Then
HasTrailingParameter = True
Parameters(paramIndex) = Mid(parts(index), 2)
Else
Parameters(paramIndex) = parts(index)
paramIndex = paramIndex + 1
End If
End If
index = index + 1
Loop
End Function
End Class
Class IrcSettings
Public AutoConnect
Public Server
Public Port
Public Nickname
Public Username
Public RealName
Public Password
Public AutoJoinChannels
Public AutoSetFocus
Public DebugMode
Sub Class_Initialize()
AutoConnect = False
Server = vbNullString
Port = 6667
Nickname = vbNullString
Username = vbNullString
RealName = vbNullString
Password = vbNullString
AutoJoinChannels = vbNullString
AutoSetFocus = True
DebugMode = 0
End Sub
Public Sub Read()
AutoConnect = CBool((GetSettingsEntry("AutoConnect") = "True") Or (GetSettingsEntry("AutoConnect") = "1"))
Server = GetSettingsEntry("Server")
Port = Int(GetSettingsEntry("Port"))
Nickname = GetSettingsEntry("Nickname")
Username = GetSettingsEntry("Username")
RealName = GetSettingsEntry("RealName")
Password = GetSettingsEntry("Password")
AutoJoinChannels = GetSettingsEntry("AutoJoinChannels")
AutoSetFocus = CBool((GetSettingsEntry("AutoSetFocus") = "True") Or (GetSettingsEntry("AutoSetFocus") = "1"))
DebugMode = Int(GetSettingsEntry("DebugMode"))
End Sub
Public Sub Write()
WriteSettingsEntry "AutoConnect", AutoConnect
WriteSettingsEntry "Server", Server
WriteSettingsEntry "Port", Port
WriteSettingsEntry "Nickname", Nickname
WriteSettingsEntry "Username", Username
WriteSettingsEntry "RealName", RealName
WriteSettingsEntry "Password", Password
WriteSettingsEntry "AutoJoinChannels", AutoJoinChannels
WriteSettingsEntry "AutoSetFocus", AutoSetFocus
WriteSettingsEntry "DebugMode", DebugMode
End Sub
Public Function CanConnect()
CanConnect = _
(Len(Trim(Server)) > 0) And _
(Port > 0) And _
(Len(Trim(Nickname)) > 0) And _
(Len(Trim(Username)) > 0) And _
(Len(Trim(RealName)) > 0)
End Function
End Class