forked from 2ndBillingCycle/specialsounds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecialSounds.lua
1802 lines (1591 loc) · 57.3 KB
/
SpecialSounds.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
- Lexer ✓
- Parser ✓
- Settings Printer ✓
- Settings Setter ✓
- Settings Getter ✓
- Hook function ✓
- - Receive Channel Message
- - Settings Getter on sever and channel
- - Recurse through matches, /SPLAY-ing every match's sound
- /show_or_add_or_delete ✓
- - Autofills with current context
- - /show ✓
- - - /showall ✓
- - /add ✓
- /delete ✓
- /deleteall ✓
- /lex ✓
- - Prints tokens
- /parse ✓
- - Dry run with /debug on
- /debug ✓
- /echo ✓
- on/off: echos command
- Tab completion ✗
- - watching for the Tab key, to autofill actions, server names, channels, etc
- Sound file validation ✗
- - File exists
- - Write a custom WAV parser ✗
- /link ✗
- - Detect if not installed in plugins folder
- - Suggest to /ssound /link to link from where it is into the plugins folder
- /patterns ✗
- - on/off treat server, channel, match as patterns or not
The are a few things I want to improve:
- Delete also treats the server and channel as patterns, and matches the number ✓
based on the order /show would print them out in
- - Show needs to add a number ✓
- Parenthesis should only have to be escaped once for the lexer, and that same ✓
escaping should work for the pattern special character escaping as well, so that
% doesn't need to be doubled up to escape parenthesis.
- The message for invalid sound files is only printed once, but it's still ✗
validated every time. Maybe a cache on is_valid_sound()
All of these should /parse, and should not need the /action
Run all through and in order, they should delete all the settings they add
/SSOUND /add server #channel sound H:\sound.wav match (matc%(h pattern)
/SSOUND /add server #channel sound H:\sound.wav
/SSOUND /add server #channel match pattern
/SSOUND /add server match pattern
/SSOUND /add server sound sound
/SSOUND /add #channel match word
/SSOUND /add #channel sound sound.wav
/SSOUND /add match (pitter patter)
/SSOUND /add sound rain.wav
/SSOUND /add (match) match match sound sound
/SSOUND /show server #channel
/SSOUND /show server
/SSOUND /show #channel
/SSOUND /show (match)
/SSOUND /show
/SSOUND /delete server #channel 2
/SSOUND /delete server #channel 1
/SSOUND /delete server 1
/SSOUND /delete #channel 1
/SSOUND /delete (match) 1
/SSOUND /delete 1
--]]
---[[
hexchat.register(
"SpecialSounds",
"6",
"Set special sound to play on message"
)
--]]
--[[ Debug
-- Mock hexchat
local hexchat = {}
hexchat.pluginprefs = {}
hexchat.EAT_HEXCHAT = true
hexchat.EAT_NONE = true
hexchat.get_info = function (str)
if str == "channel" then
return "#a"
else
return "a"
end
end
hexchat.get_context = function ()
return {
get_info = function (ctx, str)
if str == "channel" then
return "#a"
else
return "a"
end
end
}
end
hexchat.strip = function (str) return str end
hexchat.command = function (str) print("hexchat.command:\n" .. str) end
hexchat.hook_command = function (a, b, c) end
hexchat.hook_print = function (str, func) return function () end end
--]]
local version = "8"
local command_name = "SSOUND"
local settings_prefix = command_name .. "_"
local hook_objects = {}
local settings = {}
local quotepattern = '(['..("%().[]*+-?"):gsub("(.)", "%%%1")..'])'
string.escape_pattern = function(str)
str = (str:gsub(quotepattern, "%%%1")
:gsub("^^", "%%^")
:gsub("$$", "%%%$"))
return str
end
string.escape_quotes = function (str)
if type(str) ~= "string" then
print_error("Cannot quote escape string")
return str
end
if str:match('"') or str:match("%s") then
str = '"' .. str:gsub('"', '\\"') .. '"'
end
return str
end
-- Neither this lexer nor parser are pretty. A very pretty one is at:
-- https://github.com/stravant/LuaMinify/blob/master/ParseLua.lua
-- Currently, no fancy printing is done
-- In the future, these could be set to make a private message, or something
local function print_error (message)
if type(message) ~= "string" then
error("Bad string for print_error")
end
print(message .. "\n\n")
end
local function print_message (message)
if type(message) ~= "string" then
error("Bad string for print_message")
end
print(message .. "\n\n")
end
---[[ Debug
local function printvars (message, tbl)
print(message)
for key, var in pairs(tbl) do
print(tostring(key) .. ": " .. tostring(var))
end
return true
end
--]]
local function lex (str)
--[[
This function is passed the command invocation as seen b HexChat,
with the expectation that the command name is stripped:
/SSOUND rest of command
to
rest of command
If this is not done, this lexer/tokenizer will still function correctly,
and will simply pass "/SSOUND" as the first symbol token.
--]]
--[[
Make sure not to length check potentially nil variables
before nil checking first.
Because the hook_command takes the second word_eols value, and,
if the command is simply "/SSOUND", the word_eols table is only
1 item in length, all the rest of the table items will return
as nil.
--]]
if type(str) ~= "string" or #str < 1 then
str = ""
end
--[[
The general idea is we're given the command invocation as a string, and we
walk through the string, looking at each character in order, and deciding
what to do based on what we see.
Lua supports tail-call elimination in pretty cool ways, so for each "branch"
of the state machine, we wrap up by either calling the main loop function again,
this time with the state set to the next character, or to a branch for processing
a particular token.
All this does is split up the command into basic components:
- parenthesis groups: anything wrapped between two parenthesis
- Every other group of characters separated by spaces
Each component, or token, is assumed to be separated from the next by 1 or more spaces.
The processing of the tokens is the parser's job.
Here we just make sure we have valid tokens:
- Parenthesis groups have a balanced number of parenthesis:
- - (() (%()) (hello)) <-- all bad
- We don't have malformed tokens:
- - Currently, everything goes except, starting a token with )
]]
local position = 1 -- Tracks of position in the command string
local parenthesis_count = 0 -- ( -> +1 and ) -> -1
-- So if the number of parenthesis match up, this should be 0
local parenthesis_group = "" -- The characters in the current parenthesis group, including wrapping parenthesis
local symbol = "" -- The characters in the current symbol
local tokens = {} -- An array of the tokens encountered, in the order they're encountered
--[[
The format of tokens is:
{
{name=token_name, value=token value},
{name=token_name, value=token_value},
...
}
--]]
---[[ Special errors for specific cases
local function unbalanced_parenthesis_error (group)
local missing_paren = parenthesis_count > 0 and ")" or "("
missing_paren = missing_paren:rep(math.abs( parenthesis_count ))
print_error(([[
Unbalanced parenthesis: missing %s
This is the group:
%s
The full command is:
/%s %s]]):format(missing_paren, parenthesis_group, command_name, str)
)
end
local function unexpected_character_error (char)
print_error(([[
Found unexpected character:
%s
Here:
/%s %s
%s]]):format(char, command_name, str, (" "):rep(position -1 + #command_name + 2) .. "^")
)
end
--]]
---[[ Because these functions have "indirect recursive definitions",
-- they need to be declared as variables before being defined,
-- so that they are in scope for each other in their definitions.
-- The variable is looked up at run-time, so they can start out
-- undefined, or nil
local lex_parenthesis_group, lex_symbol, inner_lex
-- This is the branch for the parenthesis group token
function lex_parenthesis_group ()
if position > #str then
-- If we've suddenly reached the end of the string without a closing ) then this is an error
unbalanced_parenthesis_error(parenthesis_group)
return false
end
-- For every branch, including the main one, we start out by grabbing the current character
char = str:sub(position, position)
if char == "%" then
next_char = str:sub(position + 1, position + 1)
-- A % can escape a paren, so if it is, consume both without changing the parenthesis_count
-- and if not, consume just the %
if next_char:match("[()]") then
position = position + 2
parenthesis_group = parenthesis_group .. char .. next_char
return lex_parenthesis_group()
else
position = position + 1
parenthesis_group = parenthesis_group .. char
return lex_parenthesis_group()
end
elseif char == "(" then
parenthesis_count = parenthesis_count + 1
position = position + 1
parenthesis_group = parenthesis_group .. char
return lex_parenthesis_group()
elseif char == ")" then
parenthesis_count = parenthesis_count - 1
position = position + 1
parenthesis_group = parenthesis_group .. char
-- Only if the parenthesis are balanced should we wrap up this parenthesis group
-- Anything else is an error
if parenthesis_count == 0 then
tokens[#tokens + 1] = {name="parenthesis_group", value=parenthesis_group}
parenthesis_group = ""
return inner_lex()
end
return lex_parenthesis_group()
else
position = position + 1
parenthesis_group = parenthesis_group .. char
return lex_parenthesis_group()
end
end
-- Branch for any token that's not a parenthesis group
function lex_symbol (symbol_name)
local symbol_name = symbol_name or "text"
if position > #str then
--[[
If we've reached the end of the string, and we have something for a symbol,
add it.
Either way, return back to the main loop.
Branches never terminate lexing on their own.
In case different exit codes need to be given in the future, having the branches
return to the main loop means updating those return values in 1 place only.
--]]
if #symbol > 0 then
tokens[#tokens + 1] = {name=symbol_name, value=symbol}
symbol = ""
end
return inner_lex()
end
char = str:sub(position, position)
if char:match("%s") then
-- A space character denotes the end of a symbol,
-- so even if there's lots of spaces in a row, we return to the main loop, and let that handle them
if #symbol > 0 then
position = position + 1
tokens[#tokens + 1] = {name=symbol_name, value=symbol}
symbol = ""
end
return inner_lex()
elseif symbol_name == "number" and not char:match("%d") then
position = position + 1
symbol = symbol .. char
symbol_name = "text"
return lex_symbol(symbol_name)
else
-- Everything else is a valid symbol character, including ( and )
position = position + 1
symbol = symbol .. char
return lex_symbol(symbol_name)
end
end
function inner_lex ()
if position > #str then
return true
end
char = str:sub(position, position)
if char == "#" then
position = position + 1
tokens[#tokens + 1] = {name="hashmark", value=char}
return inner_lex()
elseif char:match("%s") then
position = position + 1
return inner_lex()
elseif char == "(" then
-- Jump to the lex_parenthesis group branch without changing state
return lex_parenthesis_group()
elseif char == ")" then
unexpected_character_error(char)
return false
elseif char == "/" then
return lex_symbol("action")
elseif char:match("%d") then
return lex_symbol("number")
else
-- Jump to the lex_symbol branch without changing state
return lex_symbol()
end
end
-- Check the return value of lexing by lexing the command string, and grabbing the function's output.
-- Truthy values mean success, falsey values indicate failure.
local exit_value = inner_lex()
if not exit_value then
return exit_value
end
-- This should not return an empty table of tokens
return tokens
end
--]]
local function strip_parenthesis_group (group_string)
if not group_string then
print_error("No group string passed")
return group_string
end
local group_string = group_string:sub(2, -2) -- Strip wrapping parenthesis
return group_string
end
local function unstrip_parenthesis_group (group_string)
if type(group_string) ~= "string" then
print_error("No group string passed")
return group_string
end
-- If the string has spaces in it, or if it's literally "match" or "sound", then wrap and escape it
if group_string:match("%s") or group_string:match("^sound$") or group_string:match("^match$") then
group_string = "(" .. group_string .. ")"
end
return group_string
end
local function print_settings (server, channel, sound, match, number)
if type(server) ~= "string" or server == "" then
server = "()"
end
if type(channel) ~= "string" or channel == "" then
channel = "()"
end
if type(sound) ~= "string" or sound == "" then
sound = "()"
end
if type(match) ~= "string" or match == "" then
match = "()"
end
if type(number) ~= "number" then
number = nil
end
local str = ([[
Server: %s
Channel: #%s
Sound: %s
Match: %s]]):format(
unstrip_parenthesis_group(server),
unstrip_parenthesis_group(channel),
unstrip_parenthesis_group(sound),
unstrip_parenthesis_group(match)
)
if number then
str = str .. ("\nNumber: %s"):format( tostring(number) )
end
print_message(str)
end
local function serialize_string (str)
if type(str) ~= "string" then
print_error("Cannot serialize string")
return false
elseif #str == 0 then
return "[[]]"
end
return "[[" .. str:gsub("([%[%]])", "%%%1") .. "]]"
end
local function deserialize_string (str)
-- Must be a string, and must have at least "[[]]"
if type(str) ~= "string" or #str < 4 or (not str:match("%[%[.-%]%]")) then
print_error("Cannot deserialize string")
return false
end
return str:sub(3,-3):gsub("%%([%[%]])", "%1")
end
local function make_settings_key (server, channel)
if type(server) ~= "string" or type(channel) ~= "string" then
print_error("Cannot make settings key")
return false
end
return settings_prefix .. serialize_string(server) .. serialize_string(channel)
end
local function serialize_sound_match (tbl)
if type(tbl) ~= "table" or type(tbl[1]) ~= "table" then -- NOTE: Better to check everything for validity
print_error("Cannot serialize table")
return false
end
local settings_string = ""
for i, sound_match in ipairs(tbl) do
settings_string = settings_string ..
([[
sound = %s
match = %s
]]):format(
serialize_string(sound_match.sound),
serialize_string(sound_match.match)
)
end
return settings_string
end
local function deserialize_sound_match (sound_match_string)
local sound_match_pattern = "sound = (%[%[.-%]%])\nmatch = (%[%[.-%]%])"
if type(sound_match_string) ~= "string" or not sound_match_string:match(sound_match_pattern) then
print_error("Cannot deserialize table")
return function () return nil end
end
local matches = sound_match_string:gmatch(sound_match_pattern)
return function()
local sound, match = matches()
if type(sound) ~= "string" or type(match) ~= "string" then
return sound, match
end
return deserialize_string(sound), deserialize_string(match)
end
end
local function retrieve_settings_value (server, channel)
local server = server or ""
local channel = channel or ""
local settings_key = make_settings_key(server, channel)
if not settings_key then return settings_key end
if settings[settings_key] then
return settings[settings_key]
else
settings[settings_key] = hexchat.pluginprefs[settings_key]
return hexchat.pluginprefs[settings_key]
end
end
local function retrieve_settings (server, channel)
local server = server or ""
local channel = channel or ""
local settings_value = retrieve_settings_value(server, channel)
if not settings_value then
return function() return nil end
end
if type(settings_value) ~= "string" then
print_error("Bad configuration information")
return function() return false end
end
local sound_match = deserialize_sound_match(settings_value)
local count = 0
return function()
local sound, match = sound_match()
if not sound and not match and count == 0 then
count = count + 1
return "",""
elseif type(sound) ~= "string" or type(match) ~= "string" then
return nil -- Indicate iterator end
end
count = count + 1
return sound, match
end
end
local function match_settings (server, channel)
if type(server) ~= "string" or type(channel) ~= "string" then
return function () return nil end
end
local valid = {}
for key, val in pairs(settings) do
if key:match(settings_prefix .. "%[%[.-%]%]%[%[.-%]%]") and
val:match("sound = %[%[.-%]%]\nmatch = %[%[.-%]%]") then
valid[#valid + 1] = {key=key,val=val}
end
end
local keys_processed = {}
for i, tbl in ipairs(valid) do
local _server, _channel = tbl.key:match("(%[%[.-%]%])(%[%[.-%]%])")
keys_processed[#keys_processed + 1] = {
server = deserialize_string(_server),
channel = deserialize_string(_channel),
sound_match = {}
}
for sound, match in deserialize_sound_match(tbl.val) do
if type(sound) == "string" and type(match) == "string" then
local slen = #keys_processed[#keys_processed].sound_match
keys_processed[#keys_processed].sound_match[slen + 1] = {
sound = sound,
match = match
}
end
end
end
--[=[ Debug
for i, tbl in ipairs(keys_processed) do
print(([[
server = %s
channel = %s
sound_match:
]]):format(tbl.server, tbl.channel))
for i, stbl in ipairs(tbl.sound_match) do
print(([[
sound = %s
match = %s
]]):format(stbl.sound, stbl.match))
end
end
--]=]
if #keys_processed < 1 then
return function () return nil end
end
local key_index = 1
local sound_match_index = 1
return function ()
while true do
if key_index > #keys_processed then
return nil, nil, nil, nil
end
if sound_match_index > #keys_processed[key_index].sound_match then
key_index = key_index + 1
sound_match_index = 1
else
local _server = keys_processed[key_index].server
local _channel = keys_processed[key_index].channel
if server:match(_server) and channel:match(_channel) then
local sound_match = keys_processed[key_index].sound_match[sound_match_index]
local sound = sound_match.sound
local match = sound_match.match
if type(sound) == "string" and type(match) == "string" then
sound_match_index = sound_match_index + 1
return _server, _channel, sound, match
end
end
sound_match_index = sound_match_index + 1
end
end
end
end
local function search_settings (server, channel)
if type(server) ~= "string" or type(channel) ~= "string" then
return function () return nil end
end
local valid = {}
for key, val in pairs(settings) do
if key:match(settings_prefix .. "%[%[.-%]%]%[%[.-%]%]") and
val:match("sound = %[%[.-%]%]\nmatch = %[%[.-%]%]") then
valid[#valid + 1] = {key=key,val=val}
end
end
local keys_processed = {}
for i, tbl in ipairs(valid) do
local _server, _channel = tbl.key:match("(%[%[.-%]%])(%[%[.-%]%])")
keys_processed[#keys_processed + 1] = {
server = deserialize_string(_server),
channel = deserialize_string(_channel),
sound_match = {}
}
for sound, match in deserialize_sound_match(tbl.val) do
if type(sound) == "string" and type(match) == "string" then
local slen = #keys_processed[#keys_processed].sound_match
keys_processed[#keys_processed].sound_match[slen + 1] = {
sound = sound,
match = match
}
end
end
end
--[=[ Debug
for i, tbl in ipairs(keys_processed) do
print(([[
server = %s
channel = %s
sound_match:
]]):format(tbl.server, tbl.channel))
for i, stbl in ipairs(tbl.sound_match) do
print(([[
sound = %s
match = %s
]]):format(stbl.sound, stbl.match))
end
end
--]=]
if #keys_processed < 1 then
return function () return nil end
end
local key_index = 1
local sound_match_index = 1
return function ()
while true do
if key_index > #keys_processed then
return nil, nil, nil, nil
end
if sound_match_index > #keys_processed[key_index].sound_match then
key_index = key_index + 1
sound_match_index = 1
else
local _server = keys_processed[key_index].server
local _channel = keys_processed[key_index].channel
if _server:match(server) and _channel:match(channel) then
local sound_match = keys_processed[key_index].sound_match[sound_match_index]
local sound = sound_match.sound
local match = sound_match.match
if type(sound) == "string" and type(match) == "string" then
sound_match_index = sound_match_index + 1
return _server, _channel, sound, match
end
end
sound_match_index = sound_match_index + 1
end
end
end
end
local function all_servers_and_channels_settings ()
local settings_keys = {}
local settings_values = {}
for key, val in pairs(hexchat.pluginprefs) do
if key:match(settings_prefix .. "%[%[.-%]%]%[%[.-%]%]") and val:match("sound = %[%[.-%]%]\nmatch = %[%[.-%]%]") then
settings_keys[#settings_keys + 1] = key
settings_values[#settings_values + 1] = deserialize_sound_match(val)
end
end
local key_index = 1
local continue = false
return function()
while true do
if key_index > #settings_keys then
return nil
end
local server, channel = settings_keys[key_index]:match(
settings_prefix .. "(%[%[.-%]%])(%[%[.-%]%])")
if continue or type(server) ~= "string" or type(channel) ~= "string" then
continue = true -- Skip this key, since it's malformed, but there are still more keys
else
server = deserialize_string(server)
channel = deserialize_string(channel)
end
local sound, match
if not continue and server and channel and type(settings_values[key_index]) == "function" then
sound, match = settings_values[key_index]() -- We stored an iterator, so execute it
elseif not continue then
if settings.debug_on then
print_error(([[
Bad settings for
server: %s
channel: %s]]):format(tostring(server), tostring(channel)))
end
continue = true
else
continue = true
end
if continue or type(sound) ~= "string" then
continue = true
else
return server, channel, sound, match
end
key_index = key_index + 1
continue = false
end
end
end
local function store_settings (server, channel, sound, match)
local server = server or ""
local channel = channel or ""
local sound = sound or ""
local match = match or ""
local settings_key = make_settings_key(server, channel)
if not settings_key then return settings_key end
local current_settings_value = retrieve_settings_value(server, channel)
local current_tables = {}
local matched = false
if type(current_settings_value) == "string" then
for curr_sound, curr_match in deserialize_sound_match(current_settings_value) do
-- If we only have a sound, and an entry only has a match, add our sound to its blank sound, and visa versa
-- If our sound or match matches and entry's that's also missing something, fill in the missing one with ours
if curr_sound == "" and #sound > 0 and (curr_match == match or match == "") then
-- If they're missing a sound, and we have a sound,
-- and either our pattern matches their pattern or we don't have a patter
matched = true
current_tables[#current_tables + 1] = {sound=sound, match=curr_match}
print_message(([[
Overwriting settings with:
Server: %s
Channel: #%s
Sound: %s
Match: %s]]):format(
unstrip_parenthesis_group(server),
unstrip_parenthesis_group(channel),
unstrip_parenthesis_group(sound),
unstrip_parenthesis_group(curr_match)))
elseif curr_match == "" and #match > 0 and (curr_sound == sound or sound == "") then
matched = true
current_tables[#current_tables + 1] = {sound=curr_sound, match=match}
print_message(([[
Overwriting settings with:
Server: %s
Channel: #%s
Sound: %s
Match: %s]]):format(
unstrip_parenthesis_group(server),
unstrip_parenthesis_group(channel),
unstrip_parenthesis_group(curr_sound),
unstrip_parenthesis_group(match)))
else
current_tables[#current_tables + 1] = {sound=curr_sound, match=curr_match}
end
end
end
-- If nothing matches, add an entry
if not matched then -- Because of the local sound = sound or "", sound and match will always be strings,
-- so no need to ttype check
current_tables[#current_tables + 1] = {sound=sound, match=match}
end
local settings_value = serialize_sound_match(current_tables)
if not settings_value then
return false
end
settings[settings_key] = settings_value
hexchat.pluginprefs[settings_key] = settings_value
if matched then
return "overwritten"
end
return true
end
local function set_settings (server, channel, sound, match)
local server = server or ""
local channel = channel or ""
local sound = sound or ""
local match = match or ""
if settings.debug_on then
print_message(("Set settings for: %s #%s"):format(
unstrip_parenthesis_group(server),
unstrip_parenthesis_group(channel)
))
end
local exit_value = store_settings(server, channel, sound, match)
if not exit_value then
print_error("Error storing settings")
print_settings(server, channel, sound, match)
return false
end
if exit_value ~= "overwritten" then
print_settings(server, channel, sound, match)
end
return true
end
local function get_settings (server, channel)
local server = server or ""
local channel = channel or ""
if settings.debug_on then
print_message(("Get settings for: %s #%s"):format(
server ~= "" and server or "()",
channel ~= "" and channel or "()"
))
end
local index = 1
for _server, _channel, sound, match in search_settings(server, channel) do
if type(server) ~= "string" or type(channel) ~= "string" or
type(sound) ~= "string" or type(match) ~= "string" then
print_error("Malformed settings")
return false
end
print_settings(_server, _channel, sound, match, index)
index = index + 1
end
return true
end
local function delete_setting (server, channel, number)
if type(server) ~= "string" or type(channel) ~= "string" or type(number) ~= "number" then
print_error("Cannot understand delete command")
return false
end
local error_message = ([[
No settings found matching:
Server: %s
Channel: #%s
Number: %s]]):format(
unstrip_parenthesis_group(server),
unstrip_parenthesis_group(channel),
tostring(number)
)
local index = 1
local matched = false
local matching_server
local matching_channel
local matching_sound
local matching_match
for _server, _channel, sound, match in search_settings(server, channel) do
if type(_server) == "string" and
type(_channel) == "string" and
type(sound) == "string" and
type(match) == "string" and
index == number then
matching_server = _server
matching_channel = _channel
matching_sound = sound
matching_match = match
matched = true
end
index = index + 1
end
if not matched then
print_message(error_message)
return false
end
local new_sound_match_pairs = {}
matched = false
for sound, match in retrieve_settings(matching_server, matching_channel) do
if sound == matching_sound and match == matching_match and not matched then
matched = true
-- If the item to be copied has all the same properties as the item to be deleted,
-- do nothing, so the item is never added, thereby deleting it
-- Only do this once, though
if settings.debug_on then
print_message(([[
Deleting setting:
Server: %s
Channel: #%s
Sound: %s
Match: %s
Number: %s]]):format(
unstrip_parenthesis_group(matching_server),
unstrip_parenthesis_group(matching_channel),
unstrip_parenthesis_group(sound),
unstrip_parenthesis_group(match),
tostring(number)
))
end
else
new_sound_match_pairs[#new_sound_match_pairs + 1] = {sound=sound, match=match}
end
end
if not matched then
print_message(error_message)
return false
end
-- If there are any items left after deleting, serialize them into a proper string
-- If not, leave it nil, so that when the key is set to nil, it's deleted entirely
local new_settings_value
if #new_sound_match_pairs > 0 then
new_settings_value = serialize_sound_match(new_sound_match_pairs)
if not new_settings_value then return new_settings_value end
end
local settings_key = make_settings_key(matching_server, matching_channel)
if not settings_key then return settings_key end
settings[settings_key] = new_settings_value
hexchat.pluginprefs[settings_key] = new_settings_value
return true
end
local function delete_all_settings ()
local settings_keys = {}
for key, val in pairs(hexchat.pluginprefs) do
if key:match(settings_prefix .. "%[%[.-%]%]%[%[.-%]%]") and val:match("sound = %[%[.-%]%]\nmatch = %[%[.-%]%]") then
settings_keys[#settings_keys + 1] = key
end
end
if settings.debug_on and #settings_keys > 0 then
print_message("Deleting all settings")
elseif #settings_keys < 1 then
print_message("No settings to delete")
return true
end