-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJSON For mIRC.mrc
More file actions
1658 lines (1382 loc) · 41.8 KB
/
JSON For mIRC.mrc
File metadata and controls
1658 lines (1382 loc) · 41.8 KB
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
;; Check to make sure mIRC/AdiIRC is of an applicable version
on *:LOAD:{
;; Check for 'JSON For Mirc.js' file because it's required
var %JsFile = $scriptdirJSON For Mirc.js
if (!$file(%JsFile)) {
echo -ag [JSON For mIRC] The 'JSON For Mirc.js' file is required to work, that file is already included in the .zip file, place it in the same folder as you did for the .mrc file
.unload -rs $qt($script)
}
;; Adiirc check
if ($~adiircexe) {
if ($version < 3.0) {
echo -ag [JSON For mIRC] AdiIRC v3.0 or later is required
.unload -rs $qt($script)
}
;; tsc64.dll check, this extra dll is required for adiirc 64bits version to work
if ($bits == 64) && (!$jfm_64bitTest) {
echo -ag [JSON For mIRC] tsc64.dll v1.1.0.0 or later is required - Download and install it (restart the client after the installation) from: https://tablacus.github.io/scriptcontrol_en.html
.unload -rs $qt($script)
}
}
;; mIRC check
elseif ($version < 7.53) {
echo -ag [JSON For mIRC] mIRC v7.53 or later is required
.unload -rs $qt($script)
}
}
;; clean up incase mirc/adiirc closed due to a crash
on *:START:{
JSONShutDown
}
;; Free resources when mIRC exits
on *:EXIT:{
JSONShutDown
}
;; Free resources when the script is unloaded
on *:UNLOAD:{
JSONShutDown
}
;;===================================;;
;; ;;
;; PUBLIC COMMANDS ;;
;; ;;
;;===================================;;
;; /JSONOpen -dbfuUwtN @Name @Input
;; Creates a JSON handle instance
;;
;; -b: @input is a bvar (*)
;; -f: @input is a file (*)
;; -u: @input is from a url (*)
;; -U: @input is from a url but its data should not be parsed (*)
;; -i: Ignore all certificate errors (**)
;; -R: Do not follow redirects (**)
;; -w: The handle should wait for /JSONHttpFetch to be called to perform the url request (**)
;; -d: Closes the handler after the script finishes (**)
;;
;; (*) : Only one can be specified at a time
;; (**): Only applicable if -u or -U is used
;;
;; @Name - String - Required
;; The name to use to reference the JSON handler
;; Cannot be a numerical value
;; Disallowed Characters: ? * : and space
;;
;; @Input - String - Required
;; The input json to parse
;; Assumed to be text
;; If -b is used, the input is contained in the specified bvar
;; if -f is used, the input is contained in the specified file
;; if -u is used, the input is a URL that returns the json to parse
alias JSONOpen {
;; Insure the alias was called as a command
if ($isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc, Error)) {
hdel SReject/JSONForMirc Error
}
;; Local variable declarations
var %Switches, %Error, %Com = $false, %Type = text, %Wait = $false, %Parse = $true, %Insecure = $false, %Redirects = $true, %BVar, %BUnset = $true
;; Remove switches from other input parameters
if (-* iswm $1) {
%Switches = $mid($1, 2-)
tokenize 32 $2-
}
;; Call the com interface initializer
if ($jfm_ComInit) {
%Error = $v1
}
;; Basic switch validation
elseif ($regex(%Switches, /([^dbfuUwiR])/)) {
%Error = SWITCH_INVALID: $+ $regml(1)
}
elseif ($regex(%Switches, /([dbfuUwiR]).*?\1/)) {
%Error = SWITCH_DUPLICATE: $+ $regml(1)
}
elseif ($regex(%Switches, /([bfuU])/g) > 1) {
%Error = SWITCH_CONFLICT: $+ $regml(1)
}
elseif (u !isin %Switches) && (w isincs %Switches) {
%Error = SWITCH_NOT_APPLICABLE:w
}
elseif (u !isin %Switches) && (i isincs %Switches) {
%Error = SWITCH_NOT_APPLICABLE:i
}
elseif (u !isin %Switches) && (R isincs %Switches) {
%Error = SWITCH_NOT_APPLICABLE:R
}
;; Validate handler name input
elseif ($0 < 2) {
%Error = PARAMETER_MISSING
}
elseif ($regex($1, /(?:^\d+$)|[*:? ]/i)) {
%Error = NAME_INVALID
}
elseif ($com(JSON: $+ $1)) {
%Error = NAME_IN_USE
}
;; Validate URL where appropriate
elseif (u isin %Switches) && ($0 != 2) {
%Error = PARAMETER_INVALID:URL
}
;; Validate bvar where appropriate
elseif (b isincs %Switches) && ($0 != 2 || !$bvar($2, 0)) {
%Error = PARAMETER_INVALID:BVAR
}
;; Validate file where appropriate
elseif (f isincs %Switches) && (!$file($2-).size) {
%Error = PARAMETER_INVALID:FILE
}
;; All checks passed
else {
%Com = JSON: $+ $1
%BVar = $jfm_TmpBVar
;; If input is a bvar indicate it is the bvar to read from and that it
;; Should NOT be unset after processing
if (b isincs %Switches) {
%Bvar = $2
%BUnset = $false
}
;; If the input is a url store if the request should wait, and set the
;; bvar to the URL to request
elseif (u isin %Switches) {
if (w isincs %Switches) {
%Wait = $true
}
if (U isincs %Switches) {
%Parse = $false
}
if (i isincs %Switches) {
%Insecure = $true
}
if (R isincs %Switches) {
%Redirects = $false
}
%Type = http
bset -t %BVar 1 $2
}
;; If the input is a file, read the file into a bvar
elseif (f isincs %Switches) {
bread $qt($file($2-).longfn) 0 $file($file($2-).longfn).size %BVar
}
;; If the input is text, store the text in a bvar
else {
bset -t %BVar 1 $2-
}
;; Attempt to create the json handler and if an error occurs retrieve the error
if (!$com(SReject/JSONForMirc/JSONEngine, JSONCreate, 1, bstr, %Type, &bstr, %BVar, bool, %Parse, bool, %Insecure, bool, %Redirects, dispatch* %Com)) || ($comerr) || (!$com(%Com)) {
%Error = $jfm_GetError
}
;; Attempt to call the parse method if the handler should not wait for the http request
elseif (%Type !== http) || (%Type == http && !%Wait) {
;; Pause jfm related timers
;; Work-around for mIRC's timers possibly activating while the com processes
.timerSReject/JSON:?* -p
%Error = $jfm_Exec(%Com, parse)
;; resume all jfm related timers
.timerSReject/JSON:?* -r
}
}
;; Error handling: if an client error occured, store the error message then clear the error state
:error
if ($error) {
%Error = $v1
}
reseterror
;; Unset the bvar if it was temporary
if (%BUnset) {
bunset %BVar
}
;; If the error variable is filled:
;; Store the error in the hashtable
;; Start a timer to close the handle when script-execution finishes
if (%Error) {
hadd -mu0 SReject/JSONForMirc Error %Error
if (%Com) && ($com(%Com)) {
.timerSReject/ $+ %Com -iom 1 0 JSONClose $unsafe($1)
}
}
;; Otherwise, if the -d switch was specified start a timer to close the com
elseif (d isincs %Switches) {
.timerSReject/ $+ %Com -iom 1 0 JSONClose $unsafe($1)
}
}
;; /JSONHttpMethod @Name @Method
;; Sets a json's pending HTTP method
;;
;; @Name - string
;; The name of the JSON handler
;;
;; @Method - string
;; The HTTP method to use
alias JSONHttpMethod {
;; Insure the alias was called as a command
if ($isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc, Error)) {
hdel SReject/JSONForMirc Error
}
;; Local variable declarations
var %Error, %Com, %Method
;; Call the com interface initializer
if ($jfm_ComInit) {
%Error = $v1
}
;; Basic input validation
elseif ($0 < 2) {
%Error = PARAMETER_MISSING
}
elseif ($0 > 2) {
%Error = PARAMETER_INVALID
}
;; Validate @name parameter
elseif ($regex($1, /(?:^\d+$)|[*:? ]/i)) {
%Error = NAME_INVALID
}
elseif (!$com(JSON: $+ $1)) {
%Error = HANDLE_DOES_NOT_EXIST
}
else {
;; Store the com name, trim excess whitespace from the method parameter then validate the method
%Com = JSON: $+ $1
%Method = $regsubex($2, /(^\s+)|(\s*)$/g, )
if (!$len(%Method)) {
%Error = INVALID_METHOD
}
;; If the method is valid attemp to store it with the handle
elseif ($jfm_Exec(%Com, httpSetMethod, %Method)) {
%Error = $v1
}
}
;; Error handling: if an client error occured, store the error message then clear the error state
:error
if ($error) {
%Error = $v1
}
reseterror
;; If an error occured store the error
if (%Error) {
hadd -mu0 SReject/JSONForMirc Error %Error
}
}
;; /JSONHttpHeader @Name @Header @Value
;; Stores the specified HTTP request header
;;
;; @Name - String - Required
;; The open json handler name to store the header for
;;
;; @Header - String - Required
;; The header name to store
;;
;; @Value - String - Required
;; The value of the header
alias JSONHttpHeader {
;; Insure the alias was called as a command
if ($isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc, Error)) {
hdel SReject/JSONForMirc Error
}
;; Local variable declarations
var %Error, %Com, %Header
;; Call the Com interfave initializer
if ($jfm_ComInit) {
%Error = $v1
}
;; Basic input validation
elseif ($0 < 3) {
%Error = PARAMETER_MISSING
}
;; Validate @name parameter
elseif ($regex($1, /(?:^\d+$)|[*:? ]/i)) {
%Error = INVALID_NAME
}
elseif (!$com(JSON: $+ $1)) {
%Error = HANDLE_DOES_NOT_EXIST
}
else {
;; Store the json handler name, trim whitespace from the header name, then validate the header
%Com = JSON: $+ $1
%Header = $regsubex($2, /(^\s+)|(\s*:\s*$)/g, )
if (!$len($2)) {
%Error = HEADER_EMPTY
}
elseif ($regex($2, [\r:\n])) {
%Error = HEADER_INVALID
}
;; If the header is valid, attempt to store the header with the handle
elseif ($jfm_Exec(%Com, httpSetHeader, %Header, $3-)) {
%Error = $v1
}
}
;; Error handling: if an client error occured, store the error message then clear the error state
:error
if ($error) {
%Error = $v1
}
reseterror
;; If an error occured store the error
if (%Error) {
hadd -mu0 SReject/JSONForMirc Error %Error
}
}
;; /JSONHttpFetch -bf @Name @Data
;; Performs a pending HTTP request
;;
;; -b: Data is stored in the specified bvar
;; -f: Data is stored in the specified file
;;
;; @Name - string - Required
;; The name of an open JSON handler with a pending HTTP request
;;
;; @Data - Optional
;; Data to send with the HTTP request
alias JSONHttpFetch {
;; Insure the alias is called as a command
if ($isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc, Error)) {
hdel SReject/JSONForMirc Error
}
;; Local variable declarations
var %Switches, %Error, %Com, %BVar, %BUnset
;; Remove switches from other input parameters
if (-* iswm $1) {
%Switches = $mid($1, 2-)
tokenize 32 $2-
}
;; Call the Com interface intializier
if ($jfm_ComInit) {
%Error = $v1
}
;; Basic input validatition
if ($0 == 0) || (%Switches != $null && $0 < 2) {
%Error = PARAMETER_MISSING
}
;; Validate switches
elseif ($regex(%Switches, ([^bf]))) {
%Error = SWITCH_INVALID: $+ $regml(1)
}
elseif ($regex($1, /(?:^\d+$)|[*:? ]/i)) {
%Error = NAME_INVALID
}
;; Validate @name
elseif (!$com(JSON: $+ $1)) {
%Error = HANDLE_DOES_NOT_EXIST
}
;; Validate specified bvar when applicatable
elseif (b isincs %Switches) && (&* !iswm $2 || $0 > 2) {
%Error = BVAR_INVALID
}
;; Validate specified file when applicatable
elseif (f isincs %Switches) && (!$isfile($2-)) {
%Error = FILE_DOESNOT_EXIST
}
else {
;; Store the com handler name
%Com = JSON: $+ $1
;; If @data was specified
if ($0 > 1) {
;; Get a temp bvar name
;; Indicate the bvar should be unset when the alias finishes
%BVar = $jfm_tmpbvar
%BUnset = $true
;; If the -b switch is specified, use the @data's value as the bvar data to send
;; Indicate the bvar should NOT be unset when the alias finishes
if (b isincs %Switches) {
%BVar = $2
%BUnset = $false
}
;; If the -f switch is specified, read the file's contents into the temp bvar
elseif (f isincs %Switches) {
bread $qt($file($2-).longfn) 0 $file($2-).size %BVar
}
;; If no switches were specified, store the @data in the temp bvar
else {
bset -t %BVar 1 $2-
}
;; Attempt to store the data to send
if (!$com(%Com, httpSetData, 1, array &ui1, %BVar, ui4, $bvar(%BVar, 0)) || $comerr) {
%Error = $jfm_GetError
}
}
;; Call the js-side parse function for the handler
if (!%Error) {
.timerSReject/JSON:?* -p
%Error = $jfm_Exec(%Com, parse)
.timerSReject/JSON:?* -r
}
}
;; Error handling: if an client error occured, store the error message then clear the error state
:error
if ($error) {
%Error = $error
}
reseterror
;; Clear the bvar if indicated it should be unset
if (%BUnset) {
bunset %BVar
}
;; If an error occured store the error
if (%Error) {
hadd -mu0 SReject/JSONForMirc Error %Error
}
}
;; /JSONClose -w @Name
;; Closes an open JSON handler and all child handlers
;;
;; -w: The name is a wildcard
;;
;; @Name - string - Required
;; The name of the JSON handler to close
alias JSONClose {
;; Insure the alias is called as a command
if ($isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc, Error)) {
hdel SReject/JSONForMirc Error
}
;; Local variable declarations
var %Switches, %Error, %Match, %Com, %X = 1
;; Remove switches from other input parameters
if (-* iswm $1) {
%Switches = $mid($1, 2-)
tokenize 32 $2-
}
;; Basic input validation
if ($0 < 1) {
%Error = PARAMTER_MISSING
}
elseif ($0 > 1) {
%Error = PARAMETER_INVALID
}
;; Validate switches
elseif ($regex(%Switches, /([^w])/)) {
%Error = SWITCH_UNKNOWN: $+ $regml(1)
}
;; Validate @Name
elseif (: isin $1) && (w isincs %Switches || JSON:* !iswmcs $1) {
%Error = PARAMETER_INVALID
}
else {
;; Format @Name to match as a regex
%Match = $1
if (JSON:* iswmcs $1) {
%Match = $gettok($1, 2-, 58)
}
%Match = $replacecs(%Match, \E, \E\\E\Q)
if (w isincs %Switches) {
%Match = $replacecs(%Match, ?, \E[^:]\Q, *, \E[^:]*\Q)
}
%Match = /^JSON:\Q $+ %Match $+ \E(?::\d+)?$/i
%Match = $replacecs(%Match,\Q\E,)
;; Loop over all comes
while (%X <= $com(0)) {
%Com = $com(%X)
;; Check if the com name matches to formatted @name
if ($regex(%Com, %Match)) {
;; Close the com and turn off timers associated to the com
.comclose %Com
if ($timer(SReject/ $+ %Com)) {
.timerSReject/ $+ %Com off
}
}
;; Otherwise move on to the next com
else {
inc %X
}
}
}
;; Error handling: if an client error occured, store the error message then clear the error state
:error
if ($error) {
%Error = $error
}
reseterror
;; If an error occured store the error
if (%Error) {
hadd -mu0 SReject/JSONForMirc Error %Error
}
}
;; /JSONList
;; Lists all open JSON handlers
alias JSONList {
;; Insure the alias was called as a command
if ($isid) {
return
}
;; Local variable declarations
var %X = 1, %I = 0
;; Loop over all open coms
while ($com(%X)) {
;; If the com is a json handler, output the name
if (JSON:?* iswm $v1) {
inc %I
echo $color(info) -age * $chr(35) $+ %I $+ : $v2
}
inc %X
}
;; If no json handlers were found, output such
if (!%I) {
echo $color(info) -age * No active JSON handlers
}
}
;; /JSONShutDown
;; Closes all JSON handler coms and unsets all global variables
alias JSONShutDown {
;; Insure the alias was called as a command
if ($isid) {
return
}
;; Close all json instances
JSONClose -w *
;; Close the JSON engine and shell coms
if ($com(SReject/JSONForMirc/JSONEngine)) {
.comclose $v1
}
if ($com(SReject/JSONForMirc/JSONShell)) {
.comclose $v1
}
;; Free the hashtable
if ($hget(SReject/JSONForMirc)) {
hfree $v1
}
}
; /JSONHelp @Param
;; Opening the online doc for help
;;
;; @Param - string - Optional
;; The identifier or the command that you want help
alias JSONHelp {
;; If the given parameter is /command
if ($1) && ($left($1,1) == $chr(47)) { url https://sreject.github.io/JSON-For-Mirc/reference#cmd/ $+ $right($1,-1) | return }
;; If the given parameter is $identifier
if ($1) && ($left($1,1) == $chr(36)) { url https://sreject.github.io/JSON-For-Mirc/reference#id/ $+ $right($1,-1) | return }
;; If no parameters opening the general reference page
url https://sreject.github.io/JSON-For-Mirc/reference
}
;;======================================;;
;; ;;
;; PUBLIC IDENTIFIERS ;;
;; ;;
;;======================================;;
;; $JSON(@Name|Ref|N, [@file,] [@Members...]).@Prop
;; Returns information pretaining to an open JSON handler
alias JSON {
;; Insure the alias was called as an identifier and that atleast one parameter has been stored
if (!$isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc, Error)) {
hdel SReject/JSONForMirc Error
}
;; Local variable declartions
var %X, %Params, %Error, %Com, %I = 0, %Prefix, %Prop, %Suffix, %Offset = 2, %Type, %Output, %Result, %ChildCom, %Call
;; If the tofile prop has been specified, the member offset is 3, not 2
if (*ToFile iswm $prop) {
%Offset = 3
}
;; if there are members specified
if ($0 >= %Offset) {
;; loop over members, building the parameters list
%X = %Offset
while (%x <= $0) {
%Params = %Params $+ ,bstr,$ $+ %X
inc %x
}
}
;; If the alias was called without any inputs
if (!$0) || ($0 == 1 && $1 == $null) {
%Error = PARAMETER_MISSING
goto error
}
;; If the alias was called with the only parameter being 0 and a prop
if ($0 == 1) && ($1 == 0) && ($prop !== $null) {
%Error = PROP_NOT_APPLICABLE
goto error
}
;; If the @name parameter starts with JSON assume its the name of the JSON com
if ($regex(name, $1, /^JSON:[^:?*]+(?::\d+)?$/i)) {
%Com = $1
}
;; Otherwise, do basic validation on the @Name parameter
elseif (: isin $1 || * isin $1 || ? isin $1) || ($1 == 0 && $0 !== 1) {
%Error = INVALID_NAME
}
;; If @Name is a numerical value
elseif ($1 isnum 0- && . !isin $1) {
;; Loop over all coms
%X = 1
while ($com(%X)) {
;; If the com is a json handler and
;; the handler's index matches that of input
;; assume the handler is the one requested and make use of it for further operations
if ($regex($v1, /^JSON:[^:]+$/)) {
inc %I
if (%I === $1) {
%Com = $com(%X)
break
}
}
inc %X
}
;; If @Name is 0 return the total number of JSON handlers
if ($1 === 0) {
return %I
}
}
;; Otherwise assume @Name is the name of a JSON handler, as-is
else {
%Com = JSON: $+ $1
}
;; If the deduced com doesn't exist store the error
if (!%Error) && (!$com(%Com)) {
%Error = HANDLER_NOT_FOUND
}
;; Basic property validation
elseif (* isin $prop) || (? isin $prop) {
%Error = INVALID_PROP
}
else {
;; Divide up the prop as needed, following the format of [fuzzy][prop_name][tobvar|tofile]
if ($regex($prop, /^((?:fuzzy)?)(.*?)((?:to(?:bvar|file))?)?$/i)) {
%Prefix = $regml(1)
%Prop = $regml(2)
%Suffix = $regml(3)
}
;; URL props have been depreciated, switch the prop to the HTTP equivulant
%Prop = $regsubex(%Prop, /^url/i, http)
;; If the suffix is 'tofile', validate the 2nd parameter
if (%Suffix == tofile) {
if ($0 < 2) {
%Error = INVALID_PARAMETERS
}
elseif (!$len($2) || $isfile($2) || (!$regex($2, /[\\\/]/) && " isin $2)) {
%Error = INVALID_FILE
}
else {
%Output = $longfn($2)
}
}
}
;; If an error has occured, skip to error handling
if (%Error) {
goto error
}
;; If @Name is an index and no prop has been specified the result is the com name
;; So create a new bvar for the result and store the com name in it
elseif ($0 == 1) && (!$prop) {
%Result = $jfm_TmpBvar
bset -t %Result 1 %Com
}
;; If the prop is isChild:
;; Create a new bvar for the result
;; Deduce if the specified handler is a child
elseif (%Prop == isChild) {
%Result = $jfm_TmpBvar
if (JSON:?*:?* iswm %com) {
bset -t %Result 1 $true
}
else {
bset -t %Result 1 $false
}
}
;; These props do not require the json data to be walked or an input to be specified:
;; Attempt to call the respective js function
;; Retrieve the result
elseif ($wildtok(state|error|input|inputType|httpParse|httpHead|httpStatus|httpStatusText|httpHeaders|httpBody|httpResponse, %Prop, 1, 124)) {
if ($jfm_Exec(%Com, $v1)) {
%Error = $v1
}
else {
%Result = $hget(SReject/JSONForMirc, Exec)
}
}
;; If the prop is httpheader:
;; Validate input parameters
;; Attempt to retrieve the specified header
;; Retrieve the result
elseif (%Prop == httpHeader) {
if ($calc($0 - %Offset) < 0) {
%Error = INVALID_PARAMETERS
}
elseif ($jfm_Exec(%Com, httpHeader, $eval($ $+ %Offset, 2))) {
%Error = $v1
}
else {
%Result = $hget(SReject/JSONForMirc, Exec)
}
}
;; These props require that the json handler be walked before processing the prop itself
elseif (%Prop == $null) || ($wildtok(path|pathLength|type|isContainer|length|value|string|debug, %Prop, 1, 124)) {
%Prop = $v1
;; If members have been specified then the JSON handler's json needs to be walked
if ($0 >= %Offset) {
%ChildCom = JSON: $+ $gettok(%Com, 2, 58) $+ :
;; Get a unique com name for the handler
%X = $ticks $+ 000000
while ($com(%ChildCom $+ %X)) {
inc %X
}
%ChildCom = %ChildCom $+ %X
;; Build the call 'string' to be evaluated
%Call = $!com( $+ %Com $+ ,walk,1,bool, $+ $iif(fuzzy == %Prefix, $true, $false) $+ %Params $+ ,dispatch* %ChildCom $+ )
;; Attempt to call the js-side's walk function: walk(isFuzzy, member, ...)
;; If an error occurs, store the error and skip to error handling
if (!$eval(%Call, 2)) || ($comerr) || (!$com(%ChildCom)) {
%Error = $jfm_GetError
goto error
}
;; Otherwise, close the child com after script execution, update the %Com variable to indicate the child com
.timerSReject/ $+ %ChildCom -iom 1 0 JSONClose %ChildCom
%Com = %ChildCom
}
;; No Prop? then the result is the child com's name
if (!%Prop) {
%Result = $jfm_TmpBvar
bset -t %Result 1 %Com
}
;; If the prop isn't value, just call the js method to return data requested by the prop
elseif (%Prop !== value) {
if ($jfm_Exec(%Com, $v1)) {
%Error = $v1
}
else {
%Result = $hget(SReject/JSONForMirc, Exec)
}
}
;; If the prop is value:
;; Attempt to get it's cast-type
;; Check the value's cast type
;; Attempt to retrieve the value
;; Fill the result variable with its value
elseif ($jfm_Exec(%Com, type)) {
%Error = $v1
}
elseif ($bvar($hget(SReject/JSONForMirc, Exec), 1-).text == object) || ($v1 == array) {
%Error = INVALID_TYPE
}
elseif ($jfm_Exec(%Com, value)) {
%Error = $v1
}
else {
%Result = $hget(SReject/JSONForMirc, Exec)
}
}
;; Otherwise, report the specified prop is invalid
else {
%Error = UNKNOWN_PROP
}
;; If no error has occured up to this point
if (!%Error) {
;; If the tofile suffix was specified, write the result to file
if (%Suffix == tofile) {
bwrite $qt(%Output) -1 -1 %Result
bunset %Result
%Result = %Output
}
;; If the tobvar suffix was specified, return the result bvar
elseif (%Suffix !== tobvar) {
%Result = $bvar(%Result, 1, 4000).text
}
}
;; Error handling: if an client error occured, store the error message then clear the error state
:error
if ($error) {
%Error = $error
}
reseterror
;; If an error occured store the error
if (%Error) {
hadd -mu0 SReject/JSONForMirc Error %Error
}
else {
return %Result
}
}
;; $JSONForValues(@Name|Ref|N, /callback[, sub-members])
alias JSONForValues {
;; Insure the alias was called as an identifier
if (!$isid) {
return
}
;; Unset the global error variable incase the last call ended in error
if ($hget(SReject/JSONForMirc,Error)) {
hdel SReject/JSONForMirc Error
}
var %Error, %Call, %X = 2, %JSON, %BVar, %N, %Value, %Result = 0
;; Build call parameter portion:
;; ,forValues,1[,bstr,$N,...]
%Call = ,forValues,1
while (%X < $0) {
inc %X
%Call = %Call $+ ,bstr, $+ $ $+ %X
}
;; Basic input validation
if ($0 < 2) {
%Error = INVALID_PARAMETERS
}
elseif ($1 === 0) {
%Error = INVALID_HANDLE
}
;; Validate @Name|Ref|N
elseif (!$1) || ($1 == 0) || (!$regex($1, /^((?:[^?:*]+)|(?:JSON:[^?:*]+(?::\d+)))$/)) {
%Error = NAME_INVALID
}
else {