forked from Lavindar/identity-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentity.lua
464 lines (408 loc) · 17 KB
/
Identity.lua
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
-----
-- Identity Addon
-- Heavily modified by Kjallstrom, Mellonea, Kirin Tor
-- Created by Ferusnox, Heaven and Earth, Cenarion Circle
-- Inspired by Thelma Incognito Addon
-----
-----
-- INITIALIZATION
-----
-- Sets the current Identity version
local Identity_VERSION = "2.0-20003";
-- Stores the unmodified chat message
local Identity_OriginalSendChatMessage;
-- Called when Identity is loaded at UI loadtime
function Identity_OnLoad()
-- Prepare to read saved variables
this:RegisterEvent("VARIABLES_LOADED");
-- Register slash commands
SlashCmdList["IDENTITY"] = Identity_Cmd;
SLASH_IDENTITY1 = "/identity";
SLASH_IDENTITY2 = "/id";
end
-- Handle the variable load event
function Identity_OnEvent()
if (event == "VARIABLES_LOADED") then
-- Check if this is the first time Identity has been loaded
if (not IdentitySettings) then
-- Set the defaults
Identity_InitSettings();
end
-- Intercept chat events
Identity_OriginalSendChatMessage = SendChatMessage;
SendChatMessage = Identity_SendChatMessage;
-- Indicate that Identity is done loading
DEFAULT_CHAT_FRAME:AddMessage("Identity " .. Identity_VERSION .. " loaded", 0.4, 0.4, 1.0);
end
end
-- Create a fresh, default Identity configuration
function Identity_InitSettings()
IdentitySettings = {};
IdentitySettings.Enabled = true;
IdentitySettings.Format = "[%s]";
IdentitySettings.MainName = "";
IdentitySettings.NickName = "";
IdentitySettings.DisplayZone = false;
IdentitySettings.Channels = {};
IdentitySettings.Channels.Guild = false;
IdentitySettings.Channels.Officer = false;
IdentitySettings.Channels.Raid = false;
IdentitySettings.Channels.Party = false;
IdentitySettings.Channels.Tell = false;
IdentitySettings.Channels.C01 = false;
IdentitySettings.Channels.C02 = false;
IdentitySettings.Channels.C03 = false;
IdentitySettings.Channels.C04 = false;
IdentitySettings.Channels.C05 = false;
IdentitySettings.Channels.C06 = false;
IdentitySettings.Channels.C07 = false;
IdentitySettings.Channels.C08 = false;
IdentitySettings.Channels.C09 = false;
IdentitySettings.Channels.C10 = false;
end
-----
-- CHAT MESSAGE HANDLING
-----
function Identity_SendChatMessage(msg, system, language, channel)
if(msg == "") then
return
end
-- Check if msg should be ignored due to starting with .z, which is reserved Nyctermoon command
if (string.find(msg, "^%.z") ~= nil) then
-- Pass the message through unchanged
Identity_OriginalSendChatMessage(msg, system, language, channel);
return
end
-- Check if Identity is enabled
if (IdentitySettings.Enabled) then
-- Check if the main Identity is configured
if (IdentitySettings.MainName ~= "") then
-- Get the current Identity
local main = Identity_GenerateMainName();
-- Modify the message
local newmsg = main .. " " .. msg;
-- Guild channel
if (IdentitySettings.Channels.Guild and system == "GUILD") then
Identity_OriginalSendChatMessage(newmsg, system, language, channel);
return;
end
-- Officer channel
if (IdentitySettings.Channels.Officer and system == "OFFICER") then
Identity_OriginalSendChatMessage(newmsg, system, language, channel);
return;
end
-- Whispers
if (IdentitySettings.Channels.Tell and system == "WHISPER") then
-- Loop through all reserved Nyctermoon whisper commands
-- If message starts with said command, send whisper without main prefix
local nycterwhisps = {"cast","deny","dispel","set formation","set debug","set follow","set pet","set blessing","set totem","set drink","set eat","set heal","set healooc","set offheal","set offdps","set default","list stats","list status","list auras","list spells","list talents","joke","dance","reset","cancel","joke"}
for _, name in pairs(nycterwhisps) do
if (string.find(msg, "^"..name) ~= nil) then
-- Pass the message through unchanged
Identity_OriginalSendChatMessage(msg, system, language, channel);
return
end
end
Identity_OriginalSendChatMessage(newmsg, system, language, channel);
return;
end
-- Numbered channels
if (system == "CHANNEL") then
local chanID = string.format("C%02d", channel);
if (IdentitySettings.Channels[chanID]) then
Identity_OriginalSendChatMessage(newmsg, system, language, channel);
return;
end
end
end
-- Check if the nickname Identity is configured
if (IdentitySettings.NickName ~= "") then
-- Get the current Identity
local nick = Identity_GenerateNickName();
-- Modify the message
local newmsg = nick .. " " .. msg;
-- Raid channel
if (IdentitySettings.Channels.Raid and (system == "RAID" or system == "BATTLEGROUND")) then
Identity_OriginalSendChatMessage(newmsg, system, language, channel);
return;
end
-- Party channel
if (IdentitySettings.Channels.Party and system == "PARTY") then
Identity_OriginalSendChatMessage(newmsg, system, language, channel);
return;
end
end
end
-- Pass the message through unchanged
Identity_OriginalSendChatMessage(msg, system, language, channel);
end
-----
-- COMMAND HANDLING
-----
-- Handle Identity commands
function Identity_Cmd(msg)
-- Extract the command and arguments from the message
local cmd, options = Identity_ParseCmd(msg);
-- Check which command was specified, if any
if (cmd == "" or cmd == "config") then
Identity_PrintConfig();
elseif (cmd == "help") then
Identity_PrintHelp(options);
elseif (cmd == "on") then
Identity_Enable(true);
elseif (cmd == "off") then
Identity_Enable(false);
elseif (cmd == "main") then
IdentitySettings.MainName = Identity_CheckName(IdentitySettings.MainName, options);
elseif (cmd == "nick") then
IdentitySettings.NickName = Identity_CheckName(IdentitySettings.NickName, options);
elseif (cmd == "enable") then
Identity_EnableChannels(options, true);
elseif (cmd == "disable") then
Identity_EnableChannels(options, false);
elseif (cmd == "zone") then
Identity_SetZoneDisplay(options);
elseif (cmd == "format") then
Identity_SetFormat(options);
elseif (cmd == "reset") then
DEFAULT_CHAT_FRAME:AddMessage("Identity configuration cleared", 0.4, 0.4, 1.0);
Identity_InitSettings();
Identity_PrintConfig();
else
DEFAULT_CHAT_FRAME:AddMessage("Invalid Identity command '" .. msg .. "'", 1.0, 0.4, 0.4);
end
end
-- Split a command into two parts: the name of the command, and any
-- command arguments.
function Identity_ParseCmd(msg)
-- Ignore null messages
if (msg) then
-- Split the command and its arguments
local s, e, cmd = string.find(msg, "(%S+)");
if (s) then
-- Command plus any options
return cmd, string.sub(msg, e + 2);
else
-- All whitespace?
return "";
end
end
end
-- Dumps the current Identity configuration
function Identity_PrintConfig()
-- Print general Identity information
DEFAULT_CHAT_FRAME:AddMessage("----", 1.0, 1.0, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("Identity " .. Identity_VERSION .. " configuration:", 0.4, 0.4, 1.0);
if (IdentitySettings.Enabled) then
DEFAULT_CHAT_FRAME:AddMessage(" Enabled", 0.4, 1.0, 0.4);
else
DEFAULT_CHAT_FRAME:AddMessage(" Disabled", 1.0, 0.4, 0.4);
end
-- Check if the main Identity is configured
if (IdentitySettings.MainName ~= "") then
-- Get an example of the current Identity
local main = Identity_GenerateMainName();
-- Get the list of channels enabled for the current Identity
local mainChannels = Identity_GetMainChannels();
-- Display the main Identity
DEFAULT_CHAT_FRAME:AddMessage(" Main name: " .. main, 0.4, 0.4, 1.0);
if (mainChannels ~= "") then
DEFAULT_CHAT_FRAME:AddMessage(" " .. mainChannels, 0.4, 1.0, 0.4);
else
DEFAULT_CHAT_FRAME:AddMessage(" All channels disabled", 1.0, 0.4, 0.4);
end
else
DEFAULT_CHAT_FRAME:AddMessage(" No main name", 1.0, 0.4, 0.4);
end
-- Check if the nickname Identity is configured
if (IdentitySettings.NickName ~= "") then
-- Get an example of the current Identity
local nick = Identity_GenerateNickName();
-- Get the list of channels enabled for the current Identity
local nickChannels = Identity_GetNickChannels();
-- Display the nick Identity
DEFAULT_CHAT_FRAME:AddMessage(" Nickname: " .. nick, 0.4, 0.4, 1.0);
if (nickChannels ~= "") then
DEFAULT_CHAT_FRAME:AddMessage(" " .. nickChannels, 0.4, 1.0, 0.4);
else
DEFAULT_CHAT_FRAME:AddMessage(" All channels disabled", 1.0, 0.4, 0.4);
end
else
DEFAULT_CHAT_FRAME:AddMessage(" No nickname", 1.0, 0.4, 0.4);
end
DEFAULT_CHAT_FRAME:AddMessage("----", 1.0, 1.0, 1.0);
end
-- Displays the help information
function Identity_PrintHelp(cmdName)
DEFAULT_CHAT_FRAME:AddMessage("----", 1.0, 1.0, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("Identity " .. Identity_VERSION .. " help:", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("See Readme.txt for more detailed information", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id - Displays the current configuration", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id config - Displays the current configuration", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id help - Displays this help text", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id off - Disables Identity", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id on - Enables Identity", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id main <name> - Sets the name of your main", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id nick <name> - Sets your group short name", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id enable <channels> - These channels will print your Identity", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id disable <channels> - These channels will not print your Identity", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id zone on|off - Toggles your location in your Identity", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("/id reset - Clears your Identity settings", 0.4, 0.4, 1.0);
DEFAULT_CHAT_FRAME:AddMessage("----", 1.0, 1.0, 1.0);
end
-- Enables/disables Identity
function Identity_Enable(enable)
if (enable) then
DEFAULT_CHAT_FRAME:AddMessage("Identity enabled", 0.4, 1.0, 0.4);
else
DEFAULT_CHAT_FRAME:AddMessage("Identity disabled", 1.0, 0.4, 0.4);
end
IdentitySettings.Enabled = enable;
end
-- Checks the specified name
function Identity_CheckName(old, new)
-- Check that a new name specified
if (not new) then
-- Keep the existing name
return old;
elseif (new == "") then
-- Clear the name
DEFAULT_CHAT_FRAME:AddMessage("Cleared name", 0.4, 0.4, 1.0);
return new;
else
-- Update the name
DEFAULT_CHAT_FRAME:AddMessage("Name changed from '" .. old .. "' to '" .. new .. "'", 0.4, 0.4, 1.0);
return new;
end
end
-- Enables/disables a list of channels
function Identity_EnableChannels(channels, enable)
-- Check that a channel list was specified
if (not channels or channels == "") then
DEFAULT_CHAT_FRAME:AddMessage("No Identity channels specified", 1.0, 0.4, 0.4);
return;
end;
-- Iterate over the list of channels
for channel in string.gfind(channels, "%w+") do
Identity_EnableChannel(channel, enable);
end
end
-- Enables/disables the specified channel
function Identity_EnableChannel(channel, enable)
-- Update the channel setting and get the channel's canonical name
local channelName = "";
if (channel == "g" or channel == "guild") then
channelName = "Guild";
IdentitySettings.Channels.Guild = enable;
elseif (channel == "o" or channel == "officer") then
channelName = "Officer";
IdentitySettings.Channels.Officer = enable;
elseif (channel == "r" or channel == "raid") then
channelName = "Raid";
IdentitySettings.Channels.Raid = enable;
elseif (channel == "p" or channel == "party") then
channelName = "Party";
IdentitySettings.Channels.Party = enable;
elseif (channel == "w" or channel == "whisper" or channel == "t" or channel == "tell") then
channelName = "Whispers";
IdentitySettings.Channels.Tell = enable;
elseif (string.gfind(channel, "%d+")) then
channelName = channel;
local chanID = string.format("C%02d", channel + 0);
IdentitySettings.Channels[chanID] = enable;
else
DEFAULT_CHAT_FRAME:AddMessage("Invalid Identity channel '" .. channel .. "'", 1.0, 0.4, 0.4);
end
-- Print the channel's new state
if (enable) then
DEFAULT_CHAT_FRAME:AddMessage("Channel " .. channelName .. " enabled", 0.4, 1.0, 0.4);
else
DEFAULT_CHAT_FRAME:AddMessage("Channel " .. channelName .. " disabled", 1.0, 0.4, 0.4);
end
end
-- Enables/disables zone display
function Identity_SetZoneDisplay(options)
if (options == "on") then
DEFAULT_CHAT_FRAME:AddMessage("Zone display enabled", 0.4, 0.4, 1.0);
IdentitySettings.DisplayZone = true;
elseif (options == "off") then
DEFAULT_CHAT_FRAME:AddMessage("Zone display disabled", 0.4, 0.4, 1.0);
IdentitySettings.DisplayZone = false;
elseif (options == "") then
if (IdentitySettings.DisplayZone) then
DEFAULT_CHAT_FRAME:AddMessage("Zone display disabled", 0.4, 0.4, 1.0);
else
DEFAULT_CHAT_FRAME:AddMessage("Zone display enabled", 0.4, 0.4, 1.0);
end
IdentitySettings.DisplayZone = not IdentitySettings.DisplayZone;
else
DEFAULT_CHAT_FRAME:AddMessage("Invalid Identity zone argument '" .. options .. "'", 1.0, 0.4, 0.4);
end
end
-- Sets the format string to be used for name display
function Identity_SetFormat(options)
-- Check for no argument
if (not options or options == "") then
IdentitySettings.Format = "[%s]";
DEFAULT_CHAT_FRAME:AddMessage("Reset format to '" .. IdentitySettings.Format .. "'", 0.4, 0.4, 1.0);
return;
end
-- Check for the %s replacement token
if (string.gfind(options, "%%s")) then
-- Escape any other percent sign, to avoid contaminating the format string
options = string.gsub(options, "%%([^s])", "%%%%%1");
DEFAULT_CHAT_FRAME:AddMessage("Changed format to '" .. options .. "'", 0.4, 0.4, 1.0);
IdentitySettings.Format = options;
else
DEFAULT_CHAT_FRAME:AddMessage("Invalid Identity format string '" .. options .. "'", 1.0, 0.4, 0.4);
end
end
-- Formats the main name for prepending to a chat message
function Identity_GenerateMainName()
local name = IdentitySettings.MainName;
-- Check if we need to append zone information as well
if (IdentitySettings.DisplayZone) then
name = name .. ", " .. GetZoneText();
end
return string.format(IdentitySettings.Format, name);
end
-- Formats the nickname for prepending to a chat message
function Identity_GenerateNickName()
local name = IdentitySettings.NickName;
-- Check if we need to append zone information as well
if (IdentitySettings.DisplayZone) then
name = name .. ", " .. GetZoneText();
end
return string.format(IdentitySettings.Format, name);
end
-- Concatenates of list of enabled channels for the main character's name
function Identity_GetMainChannels()
local channels = "";
if (IdentitySettings.Channels.Guild) then
channels = channels .. "Guild ";
end
if (IdentitySettings.Channels.Officer) then
channels = channels .. "Officer ";
end
if (IdentitySettings.Channels.Tell) then
channels = channels .. "Whisper ";
end
for i = 1, 10 do
local chanID = string.format("C%02d", i);
if (IdentitySettings.Channels[chanID]) then
channels = channels .. i .. " ";
end
end
return channels;
end
-- Concatenates of list of enabled channels for the character's nickname
function Identity_GetNickChannels()
local channels = "";
if (IdentitySettings.Channels.Raid) then
channels = channels .. "Raid ";
end
if (IdentitySettings.Channels.Party) then
channels = channels .. "Party ";
end
return channels;
end