-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathslv-cbapi.mrc
2250 lines (2036 loc) · 71.1 KB
/
slv-cbapi.mrc
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
alias CbVersion { set %cb_version beta5 slv 20220318 }
;
; .---------------------------------------------------------------------------.
; | | > Needs: JSONFormIRC 2.0.2001+ |
; | :: CBFTP API :: | > Usage: /CbHelp /CbSet /CbSite /CbInvite |
; | --------------- | /CbSearch /CbDupe /CbReq /CbAddsite |
; | beta | /CbSpread /CbFxp /CbList /CbInc ..etc |
; | | ( Or right click for CBFTP API menu ) |
; `---------------------------------------------------------------------------'
; " ...but what does it doooo? well, not much "
;
; :: USAGE
; -----------------------------------------------------------------------------
; This is meant to show how the Cbftp JSON REST API can be used with mIRC.
; All API endpoints should be there as Aliases. Commands can be send to
; one or more sites. Most responses should be handled reasonably well (though
; that part is messy). Some management of sites/sections is also possible.
;
; This is not a trade tool. If you want one you're better off using the UDP API
; and diff client. As-is I could maybe see siteops using this as an admin tool.
;
;
; :: INSTALLATION
; -----------------------------------------------------------------------------
; > Get https://github.com/SReject/JSON-For-Mirc
; > Copy .mrc and .js files from 'src' dir to mIRC\scripts dir
; > Copy this script to the same dir
; > Load both .mrc files as remote scripts, JSONFormIRC first
;
; :: CHANGELOG
; -----------------------------------------------------------------------------
; > beta5: fix settings dialog
; > beta3: added transfer, filelist, incomplete cmd, site mgmt (and more),
; menu's and dialogs, improved config
; > PoC: added site cmds, getsites (1st public rls)
;
; :: CONFIGURATION
; -----------------------------------------------------------------------------
; > Settings can be changed in multiple ways:
; - initial setup in script, see 'CbInit' below
; - by using alias /CbSet
; - using gui menu: right click in window, select 'CBFTP API' then 'Settings'
;
; > Set Cbftp REST API url (http/json) including plaintext password (*)
; Example: https://:<API_Password>@127.0.0.1:443
;
; > Set sites: "SITE1","SITE2" OR 'all' (default)
;
; > Optionally limit number of output characters in results
; or use '0' for 'unlimited' (default: 4000)
;
; > In cases of issues, try: /CbSet debug 3
;
; (*) NOTE: Alternatively, leave password empty in API url and instead set:
; 1) in fish10 plugin: add a new key for user 'cbftp'
; 2) use micryption
; ..this will use decrypted key as password in API url
;
;
; :: INITIAL SETUP
; -----------------------------------------------------------------------------
;
alias CbInit {
set %cb_url https://:bestpass@127.0.0.1:62443
set %cb_sites all
set %cb_login myusername:SecretPassword
set %cb_maxchars 4000
;; Show credits from SITE STAT output in own window *new*
set %cb_creditwin 0
set %cb_creditwname MyCredits
}
;
; -----------------------------------------------------------------------------
; CONFIG END
; -----------------------------------------------------------------------------
on 1:LOAD: {
CbVersion
}
alias CbApiAbout {
.CbVersion
var %n = $chr(13)
var %s =
; return output for dialog
if ($1) {
var %n = $chr(13)
var %a = $null
var %a = %a .-------------------------. %n
var %a = %a $chr(124) .. :: CBFTP API :: .. $chr(124) %n
var %a = %a $chr(124) %cb_version $chr(124) %n
var %a = %a `-------------------------' %n
return %a %n
}
; echo to status window
else {
echo -as .----------------------.
echo -as $chr(124) %s %s :: CBFTP API :: %s %s %s $chr(124)
echo -as $chr(124) %s %cb_version %s $chr(124)
echo -as `----------------------'
}
}
; API SITE COMMAND EXAMPLES:
; --------------------------
; Raw Commands:
; /CbRawCmd PWD, /CbRawCmd STAT -al /Path
; For SITE commands, call CbSiteCmd:
; /CbSiteCmd WHO, /CbSiteCmd KICK foo, /CbSiteCmd DELUSER bar
; Or use aliases, set %cb_cmd to e.g. 'SITE USERS' and call CbCmd like:
; /CbStat, CbDupe or /CbRequest
; These aliases can be also used as indentifier e.g. //echo test: $CbList(arg1, aag2, arg3)
; /CbGetSites /CbSections /CbList
;
; By default cmds are send to %cb_sites as set by '/CbSet sites' or Settings menu
;
; To use specific site(s) for cmd and tmp override %cb_sites for ONE run of CbCmd:
; set %cb_cmd_sites to SITE1,SITE1,...
; or use alias: /CbCmdSites
alias CbCmdSites {
set %cb_cmd_sites $1- | echo -ast CBAPI: cb_cmd_sites set to: $1-
}
;; RAW <cmd> [args]
alias CbRawCmd {
set %cb_cmd RAW | CbCmd $1-
}
;; SITE <cmd> [args]
alias CbSiteCmd {
set %cb_cmd SITE | CbCmd $1-
}
;; SITE INVITE [nick]
alias CbInvite {
if ($1) {
var %nick = $1
if ($2) {
set %cb_cmd_sites $2
}
}
else {
var %i = 0 | while ($scon(0) > %i) {
if ( $scon(%i).$network = $1 ) {
var %nick = $scon(%i).me
}
inc %i
}
}
if (%nick) { set %cb_cmd SITE INVITE | CbCmd %nick }
}
;; SITE DUPE <query>
alias CbDupe {
if ($1) {
set %cb_cmd SITE DUPE
echo -ast CBDUPE: " $+ $1- $+ " ( $+ $CbArray(%cb_sites) $+ )
CbCmd $1-
}
else { CbError DUPE query }
}
;; SITE SEARCH <query>
alias CbSearch {
if ($1) {
set %cb_cmd SITE SEARCH
echo -ast CBSEARCH: " $+ $1- $+ " on sites: $CbArray(%cb_sites)
CbCmd $1-
}
else { CbError SEARCH query }
}
;; SITE REQUEST <release>
alias CbReq {
if ($1) { set %cb_cmd SITE REQUEST | CbCmd $1- }
else { CbError REQ release }
}
;; SITE STAT
alias CbStat {
set %cb_cmd SITE STAT | CbCmd
}
;; GENERAL INFO
alias CbInfo {
set %cb_cmd INFO | CbCmd
}
; GET SITES COMMAND:
; ------------------
; Handles all 'sites' related stuff e.g. list all sites or details for 1 site
; By default sends 'GET /sites' to cbftpd api and returns formatted site names
; Used by: /CbSite, 'SITE INVITE' and the site list in the 'Settings' menu
;; CbGetSites [site] [section] or [filter] (default: all sites)
alias CbGetSites {
.CbCheck skip_sites
JSONClose -w cbftp*
var %cb_hn = cbftp $+ $rand(1,10000)
var %cb_urn = /sites
; List all sites (default)
var %mode = all
if ($1) {
; Section filter
if (section= isin $1) {
%cb_urn = %cb_urn $+ ? $+ $1
}
; Get sections for a site
elseif (/sections isin $1-) {
%cb_urn = %cb_urn $+ / $+ $1-
%mode = site_sections
}
; Single section for site
elseif ($1 && $2) {
%cb_urn = %cb_urn $+ / $+ $1 $+ /sections/ $+ $2
%mode = site_onesec
}
; Show site details
else {
%mode = site_details
%cb_urn = %cb_urn $+ / $+ $1
}
}
var %cb_method = GET
JSONOpen -uwi %cb_hn $CbUri(%cb_url, %cb_urn)
CbHttpHeaders %cb_hn
JSONHttpFetch %cb_hn
if (%cb_debug >= 2) {
echo -ast DEBUG: CbGetSites HttpDebug: | echo $CbHttpDebug(%cb_url,%cb_urn,%cb_hn,%cb_method,%cb_data)
}
if (!$JSONError) {
if (%cb_debug >= 2) {
echo -ast DEBUG: CbGetSites OK (no JSONError)
}
if (%mode == site_details) {
var %attr = addresses user max_logins
var %ntok = $numtok(%attr,32)
var %i = 1
var %result = $null
while (%i <= %ntok) {
var %tok = $gettok(%attr,%i,32)
%result = %result %tok $+ : $json(%cb_hn, $+ %tok $+).String $+ $iif(%i < %ntok, $chr(44))
inc %i
}
if ($json(%cb_hn,sections).length > 0) {
var %j = 0
var %sections = $null
while (%j < $json(%cb_hn,sections).length) {
%sections = %sections $json(%cb_hn,sections,%j,name).String
inc %j
}
%result = %result $+ , sections: %sections
}
if ($json(%cb_hn,affils).length > 0) {
%affils = $regsubex($json(%cb_hn,affils).String,^\[(.*)\]$,\1))
%result = %result $+ , affils: $replace(%affils,$chr(44), $chr(32))
}
echo -as CBSITES: $+ $1 $+ > %result
}
elseif (%mode == site_sections) {
var %i = 0
var %sections = $null
var %len = $json(%cb_hn).length
while (%i < %len) {
var %sections = %sections $replace($json(%cb_hn,%i,name).String,",) $+ : $&
$json(%cb_hn,%i,path).String $+ $iif($calc(%i + 1) < %len, $chr(44))
inc %i
}
echo -as CBSITES: SECTIONS > %sections ( $+ %len $+ sections)
}
elseif (%mode == site_onesec) {
noop $regsub(res,$json(%cb_hn).HttpBody,/[{}]\s?/g,,%section)
echo -as CBSITES: $1 > SECTION: %section
}
;; All sites, dont format if called as identifier
else {
set %cb_getsites $replace($regsubex($json(%cb_hn).String,^\[(.*)\]$,\1),",)
JSONClose -w cbftp*
if ($isid) {
return %cb_getsites
}
else {
if (%cb_sites != all) {
var %i = 1
var %sites = $null
while (%i <= $numtok(%cb_getsites,44)) {
if ($numtok(%cb_getsites,44)) {
if (" $+ $gettok(%cb_getsites,%i,44) $+ " isin %cb_sites) {
%sites = %sites $gettok(%cb_getsites,%i,44)
}
else {
%sites = %sites 14 $+ $gettok(%cb_getsites,%i,44) $+
}
}
inc %i
}
}
else {
%sites = $replace(%cb_getsites, $chr(44), $chr(32))
}
}
; .echo -as CBSITES: $replace(%cb_getsites,$chr(44), $chr(44) $chr(32)) (enabled: all)
.echo -as CBSITES: %sites
}
if (%cb_debug >= 2) {
echo -ast DEBUG: CbGetSites $ $+ json.debug = $json(%cb_hn).debug
echo -ast DEBUG: CbGetSites cb_getsites= $+ %cb_getsites
}
}
else {
if ($isid) {
return $false
}
else {
JSONClose %cb_hn
echo -ast CBSITES: ERROR > JSONError $JSONError $JSON(%cb_hn).error (hn: %cb_hn uri: $CbUri(%cb_url) $+ )
}
}
}
;; CbSite calls CbGetSites (alias 'CbSites')
;; tries UPPERCASE sitename if lowercase site not found
alias CbSite {
if ($1 && !$regex($1,/section=|/sections/) && !$2 && !$3) {
if ($CbGetSites($1-) == $false) {
if (%cb_debug >= 2) { echo -ast DEBUG: CbSite $1 not found, trying uppercase... }
noop $CbGetSites($upper($1) $2-))
}
}
else {
CbGetSites $1-
}
}
alias CbSites { CbSite $1- }
; TRANSFER CMDS & SITE MGMT API EXAMPLES:
; ---------------------------------------
;; GET /spreadjobs : [filter]
alias CbGetSpread {
set %cb_cmd GETSPREAD | CbCmd $1-
}
;; GET /transferjobs : [filter]
alias CbGetXfer {
set %cb_cmd GETTRANSFERS | CbCmd $1-
}
;; GET /sections [section] (no arg: show all)
alias CbSection {
set %cb_isid $isid
set %cb_cmd GETSECTIONS | CbCmd $1-
}
alias CbSections { set %cb_isid $isid | CbSection $1- }
;; GET /filelist : <site> [path] [timeout]
alias CbList {
if ($1) {
set %cb_isid $isid
set %cb_cmd FILELIST | CbCmd $1-
}
else { CbError LIST site }
}
;; INCOMPLETES : <section> <dstsite> <srcsite> (uses filelist)
alias CbInc {
if (($1 && (/ !isin $1)) && $2 && $3) {
set %cb_cmd INCOMPLETES | CbCmd $2 $1 5 $3
}
else { CbError INC section $+ $chr(44) dstsite or srcsite }
}
;; POST /spreadjobs/JOBNAME/reset
alias CbResetSpread {
if ($1) { set %cb_cmd RESETSPREAD | CbCmd $1- }
else { CbError SPREAD release }
}
;; POST /spreadjobs/JOBNAME/abort
alias CbAbortSpread {
if ($1) { set %cb_cmd ABORTSPREAD | CbCmd $1- }
else { CbError SPREAD release }
}
;; POST /transferjobs/JOBNAME/reset (no body)
alias CbResetXfer {
if ($1) { set %cb_cmd RESETTRANSFER | CbCmd $1- }
else { CbError TRANSFER release }
}
;; POST /transferjobs/JOBNAME/abort (no body)
alias CbAbortXfer {
if ($1) { set %cb_cmd ABORTTRANSFER | CbCmd $1- }
else { CbError TRANSFER release }
}
;; DELETE /sites/SITE1 : <site>
alias CbDelSite {
if ($1) { set %cb_cmd DELSITE | CbCmd $1- }
else { CbError SITE site to delete }
}
;; DELETE /sites/SITE1/sections/SECTIONNAME : <site> <section>
alias CbDelSiteSection {
if ($1) { set %cb_cmd DELSITESECTION | CbCmd $1- }
else { CbError SITE site and section to delete }
}
;; DELETE /sections/SECTIONNAME : <section>
alias CbDelSection {
if ($1) { set %cb_cmd DELSECTION | CbCmd $1- }
else { CbError SECTION section to delete }
}
; JSON TEMPLATES
; --------------
; Commands like CbFxp, CbAddSite and CbSpread use JSON 'templates' in POST requests
; Usage: - always replace chars { } with $chr(123), 125 and [ ] with 91, 93
; - these 2 chars $& mean 'continue on next line'
; - changes json values using regexps, seems the 'best' way...
;; POST /transferjobs > FXP: <section> <srcsite> <dstsite> <release>
alias CbFxp {
if (($1) && ($2) && ($3) && ($4)) {
var %cb_tmpl_fxp_job = $chr(123) $&
"src_site": " $+ $2 $+ ", $&
"src_section": " $+ $1 $+ ", $&
"dst_site": " $+ $3 $+ ", $&
"dst_section": " $+ $1 $+ ", $&
"name": " $+ $4 $+ " $&
$chr(125)
; NOTE:
; dst_path: dst_section or dst_path
; src_path: optional
if (%cb_debug >= 1) { echo -ast DEBUG: CbFxp cb_tmpl_fxp_job = %cb_tmpl_fxp_job }
set %cb_cmd TRANSFER | CbCmd %cb_tmpl_fxp_job
}
else {
CbError FXP section $+ $chr(44) srcsite $+ $chr(44) dstsite or release
}
}
;; POST /transferjobs > DOWN: <site> <section> <release> [dstpath]
alias CbDown {
if (($1) && ($2) && ($3)) {
var %cb_tmpl_down_job = $chr(123) $&
"src_site": " $+ $1 $+ ", $&
"src_section": " $+ $2 $+ ", $&
"name": " $+ $3 $+ ", $&
"dst_path": " $+ $iif($4,$v1,/tmp) $+ " $&
$chr(125)
; NOTE:
; src_section: src_section or src_path
; dst_path: optional
if (%cb_debug >= 1) { echo -ast DEBUG: CbDown cb_tmpl_down_job = %cb_tmpl_down_job }
set %cb_cmd TRANSFER | CbCmd %cb_tmpl_down_job
}
else {
CbError DOWN site $+ $chr(44) release or section
}
}
;; POST /transferjobs > UP: <site> <section> <release> [dstpath]
alias CbUp {
if (($1) && ($2) && ($3)) {
var %cb_tmpl_up_job = $chr(123) $&
"src_site": " $+ $1 $+ ", $&
"src_section": " $+ $2+ ", $&
"name": " $+ $3 $+ ", $&
"dst_path": " $+ $iif($4,$v1,/tmp) $+ " $&
$chr(125)
; NOTE:
; dst_path: dst_section or dst_path
; src_path: optional
if (%cb_debug >= 1) { echo -ast DEBUG: CbUp cb_tmpl_up_job = %cb_tmpl_up_job }
set %cb_cmd TRANSFER | CbCmd %cb_tmpl_up_job
}
else {
CbError UP site $+ $chr(44) section or release
}
}
;; POST /sites > ADD: <site> <bnc> or <bnc1,bnc2>
alias CbAddSite {
if (($1) && ($2)) {
var %name = $1
var %bncs = $2-
;
; site 'template'
;
var %cb_tmpl_site = $chr(123) $&
"name": "", $&
"addresses": "", $&
"allow_download": "YES", $&
"allow_upload": "YES", $&
"base_path": "/", $&
"broken_pasv": false, $&
"cepr": true, $&
"cpsv": true, $&
"disabled": false, $&
"force_binary_mode": false, $&
"leave_free_slot": true, $&
"list_command": "STAT_L", $&
"max_idle_time": 60, $&
"max_logins": 3, $&
"max_sim_down": 2, $&
"max_sim_down_complete": 0, $&
"max_sim_down_pre": 0, $&
"max_sim_down_transferjob": 0, $&
"max_sim_up": 3, $&
"password": "", $&
"pret": false, $&
"priority": "HIGH", $&
"sscn": false, $&
"stay_logged_in": false, $&
"tls_mode": "AUTH_TLS", $&
"tls_transfer_policy": "PREFER_OFF", $&
"transfer_protocol": "IPV4_ONLY", $&
"transfer_source_policy": "ALLOW", $&
"transfer_target_policy": "BLOCK", $&
"user": "", $&
"xdupe": true $&
$chr(125)
; NOTE:
;
; optional attributes
;
; "allow_download": (YES/NO/MATCH_ONLY)
; "allow_upload": (YES/NO)
; "avg_speed": { "S1": 20000, "S2": 30000 } speed SITE1 -> SITE2
; "list_command": (STAT_L/LIST)
; "priority": (VERY_LOW/LOW/NORMAL/HIGH/VERY_HIGH)
; "list_frequency": (VERY_LOW/FIXED_LOW/FIXED_AVERAGE/
; FIXED_HIGH/FIXED_VERY_HIGH/AUTO/
; DYNAMIC_LOW/DYNAMIC_AVERAGE/
; DYNAMIC_HIGH/DYNAMIC_VERY_HIGH)
; "proxy_type": (GLOBAL/NONE/USE)
; "skiplist": [ {"action": (ALLOW/DENY/UNIQUE/SIMILAR) } ]
; "tls_mode": "AUTH_TLS": (NONE/AUTH_TLS/IMPLICIT)
; "tls_transfer_policy": (ALWAYS_OFF/PREFER_OFF/...)
; "transfer_protocol": (IPV4_ONLY/PREFER_IPV4/...)
; "transfer_source_policy": (ALLOW/BLOCK)
;; removed: "sections": $chr(91) $chr(123) "name": "SEC1", "path": "" $chr(125) $chr(93) , $&
;; removed: noop $regsubex(cb,%cb_tmpl_site, ("name": "SEC1", "path":) "" ,\1 "/sec1", %cb_tmpl_site)
noop $regsubex(cb_site, %cb_tmpl_site, ("name":) "" $+ $chr(44), \1 " $+ %name $+ " $+ $chr(44), %cb_tmpl_site)
noop $regsubex(cb_site, %cb_tmpl_site, ("addresses":) "" $+ $chr(44),\1 $+ $CbArray(%bncs) $+ $chr(44), %cb_tmpl_site)
noop $regsubex(cb_site, %cb_tmpl_site, ("user":) "" $+ $chr(44), \1 " $+ $CbLogin(u %cb_login) $+ " $+ $chr(44), %cb_tmpl_site)
noop $regsubex(cb_site, %cb_tmpl_site, ("password":) "" $+ $chr(44), \1 " $+ $CbLogin(p %cb_login) $+ " $+ $chr(44), %cb_tmpl_site)
if (%cb_debug >= 1) { echo -ast DEBUG: CbAddSite cb_tmpl_site = %cb_tmpl_site }
;return
set %cb_cmd ADDSITE | CbCmd %cb_tmpl_site
}
else {
CbError SITE site and bncs(s)
}
}
;; POST /sites/SITE1/sections : <site> <section> <path>
alias CbAddSiteSection {
if ($1 && $2 && &3) {
var %cb_tmpl_section = $chr(123) "name": " $+ $2 $+ ", "path": " $+ $3 $+ " $chr(125)
if (%cb_debug >= 1) { echo -ast DEBUG: CbAddSiteSection cb_tmpl_section = %cb_tmpl_section }
set %cb_cmd ADDSITESECTION | CbCmd $1 %cb_tmpl_section
}
else {
CbError SECTION site $+ $chr(44) section and path to add
}
}
;; POST /sections > ADD: <section>
alias CbAddSection {
if ($1) {
var %name = $1
;
; section 'template'
;
var %cb_tmpl_section = $chr(123) $&
"name": "", $&
"skiplist": $chr(91) $&
$chr(123) $&
"action": "DENY", $&
"dir": false, $&
"file": true, $&
"pattern": "", $&
"regex": false, $&
"scope": "ALL" $&
$chr(125) $&
$chr(93) $&
$chr(125)
; NOTE:
; "hotkey": optional, 0-9
; "action":(ALLOW/DENY/UNIQUE/SIMILAR)
; "scope": (IN_RACE/ALL)
noop $regsubex(cb_section, %cb_tmpl_section, ("name":) "" $+ $chr(44), \1 " $+ %name $+ " $+ $chr(44), %cb_tmpl_section)
if (%cb_debug >= 1) { echo -ast DEBUG: CbAddSection %cb_tmpl_section }
set %cb_cmd ADDSECTION | CbCmd %cb_tmpl_section
}
else {
CbError ADDSECTION section
}
}
;; POST /spreadjobs > STARTNEW : <section> <release> [sites]
alias CbSpread {
if (($1) && ($2)) {
%cb_spread_sites = %cb_sites
if ($3) {
var %cb_spread_sites = $3
}
if (%cb_debug >= 1) { echo -ast DEBUG: CbSpread cb_spread_sites = %cb_spread_sites }
;
; spread 'template'
;
var %cb_tmpl_spread = $chr(123) $&
"section": " $+ $1 $+ ", $&
"name": " $+ $2 $+ ", $&
"sites": "", $&
"sites_dlonly": "", $&
"sites_all": true, $&
"reset": true, $&
"profile": "RACE" $&
$chr(125)
; NOTE:
; sites_dlonly: (optional)
; sites_all: (optional) whether to add all sites with the section defined
; reset: (optional) whether to reset the job if it already exists
; profile: RACE/DISTRIBUTE/PREPARE
noop $regsubex(cb_spread, %cb_tmpl_spread, ("section":) "" $+ $chr(44), \1 " $+ $1 $+ " $+ $chr(44), %cb_tmpl_spread)
noop $regsubex(cb_spread, %cb_tmpl_spread, ("name":) "" $+ $chr(44), \1 " $+ $2 $+ " $+ $chr(44), %cb_tmpl_spread)
if (%cb_spread_sites == all) {
if (%cb_debug > 1) { echo -ast DEBUG: CbSpread got 'all' }
noop $regsubex(cb_spread, %cb_tmpl_spread, "sites": "" $+ $chr(44),,%cb_tmpl_spread)
}
else {
noop $regsubex(cb_spread, %cb_tmpl_spread, ("sites":) "" $+ $chr(44), \1 $CbArray(%cb_spread_sites) $chr(44), %cb_tmpl_spread)
noop $regsubex(cb_spread, %cb_tmpl_spread, "sites_all": true $+ $chr(44),,%cb_tmpl_spread)
}
if (%cb_debug >= 1) { echo -ast DEBUG: CbSpread cb_tmpl_spread = %cb_tmpl_spread }
return
set %cb_cmd SPREAD | CbCmd %cb_tmpl_spread
}
else {
CbError SPREAD section and release
}
}
;; PATCH /sites/SITE1 : <site> <bnc> or <bnc1,bnc2>
alias CbModSite {
if (($1) && ($2)) {
var %cb_site = $chr(123) "name": " $+ $1 $+ ", "addresses": $CbArray($2) $chr(125)
if (%cb_debug >= 1) { echo -ast DEBUG: CbModSite %cb_tmpl_site }
set %cb_cmd MODSITE | CbCmd $1 %cb_tmpl_site
}
else {
CbError SITE site and bncs(s) to modify
}
}
;; PATCH /sections/SECTIONNAME : <section> <newname>
alias CbModSection {
if ($1) {
var %cb_tmpl_section = $chr(123) "name": " $+ $2 $+ " $chr(125)
if (%cb_debug >= 1) { echo -ast DEBUG: CbModSection %cb_tmpl_section = %cb_tmpl_section }
set %cb_cmd MODSECTION | CbCmd $1 %cb_tmpl_section
}
else {
CbError SECTION section
}
}
;; PATCH /sites/SITE1/sections/SECTIONNAME : <site> <section> <path>
alias CbModSiteSection {
if ($1 && $2 && &3) {
var %cb_tmpl_section = $chr(123) "path": " $+ $3 $+ " $chr(125)
if (%cb_debug >= 1) { echo -ast DEBUG: CbModSiteSection cb_tmpl_section = %cb_tmpl_section }
set %cb_cmd MODSITESECTION | CbCmd $1 $2 %cb_tmpl_section
}
else {
CbError SECTION site $+ $chr(44) section and path to add
}
}
; CbCmd: SEND/RECEIVE JSON
; ------------------------
; Using 'JSON For Mirc', CbCmd sends command to the CB API and handles result
alias -l CbCmd {
var %cb_args = $1-
.CbCheck
if (%cb_cmd_sites) {
set %cb_savesites %cb_sites
set %cb_sites %cb_cmd_sites
}
if (!%cb_sites) {
echo -ast CBCMD: INFO > no sites selected, using 'all' (try /CbHelp)
set %cb_sites all
unset %cb_savesites
}
if (!%cb_cmd) {
CbError SPREAD command | return
}
;; Handle SITE and RAW command to /raw endpoint for 1 or more or all site(s)
; NOTE:
; sites: run on these sites
; sites_with_sections: run on sites with these sections defined
; sites_all: run on all sites
; path: the path to cwd to before running command
; path_section: section to cwd to before running command
; timeout: max wait before failing
; async: if false, wait for command to finish before responding.
; if true, respond with a request id and let command run in the background
if ($regex(%cb_cmd,/^(SITE\s?|RAW)/)) {
if (%cb_cmd == RAW) {
set %cb_cmd %cb_args
unset %cb_args
}
var %cb_urn = /raw
var %cb_method = POST
if (%cb_sites == all) {
var %cb_data = {"command": " $+ %cb_cmd $+ $iif(%cb_args,$chr(32) $v1) $+ ", $&
"sites_all": true, $&
"path":"/", $&
"timeout":10, $&
"async":false}
}
else {
var %cb_data = {"command": " $+ %cb_cmd $+ $iif(%cb_args,$chr(32) $v1) $+ ", $&
"sites": $CbArray(%cb_sites) $+ , $&
"path":"/", $&
"timeout":10, $&
"async":false}
}
unset %cb_cmd_sites
}
;; Handle other commands: sites, sections, transfer, ...
;; set api url, http request method and json data
elseif (%cb_cmd == ADDSITE) {
var %cb_urn = /sites
var %cb_method = POST
var %cb_data = $1-
}
elseif (%cb_cmd == DELSITE) {
var %cb_urn = /sites/ $+ $1
var %cb_method = DELETE
}
elseif (%cb_cmd == SPREAD) {
var %cb_urn = /spreadjobs
var %cb_method = POST
var %cb_data = $1-
}
elseif (%cb_cmd == TRANSFER) {
var %cb_urn = /transferjobs
var %cb_method = POST
}
elseif (%cb_cmd == ABORTSPREAD) {
var %cb_urn = /spreadjobs/ $+ $1 $+ /abort
var %cb_method = POST
}
elseif (%cb_cmd == RESETSPREAD) {
var %cb_urn = /spreadjobs/ $+ $1 $+ /reset
var %cb_method = POST
}
elseif (%cb_cmd == ABORTTRANSFER) {
var %cb_urn = /transferjobs/ $+ $1 $+ /abort
var %cb_method = POST
}
elseif (%cb_cmd == RESETTRANSFER) {
var %cb_urn = /transferjobs/ $+ $1 $+ /reset
var %cb_method = POST
}
elseif (%cb_cmd == ADDSECTION) {
var %cb_urn = /sections
var %cb_method = POST
var %cb_data = $2-
}
elseif (%cb_cmd == GETSPREAD) {
var %cb_urn = /spreadjobs $+ $iif($1-,/ $+ $v1)
var %cb_method = GET
}
elseif (%cb_cmd == GETSECTIONS) {
var %cb_urn = /sections $+ $iif($1-,/ $+ $v1)
var %cb_method = GET
}
elseif (%cb_cmd == GETTRANSFERS) {
var %cb_urn = /transferjob $+ $iif($1-,/ $+ $v1)
var %cb_method = GET
}
elseif (%cb_cmd == DELSECTION) {
var %cb_urn = /sections/ $+ $1
var %cb_method = DELETE
}
elseif (%cb_cmd == ADDSITESECTION) {
var %cb_urn = /sites/ $+ $1 $+ /sections
var %cb_method = POST
var %cb_data = $2-
}
elseif (%cb_cmd == DELSITESECTION) {
var %cb_urn = /sites/ $+ $1 $+ /sections/ $+ $2
var %cb_method = DELETE
}
elseif (%cb_cmd == MODSITE) {
var %cb_urn = /sites/ $+ $1
var %cb_method = PATCH
var %cb_data = $2-
}
elseif (%cb_cmd == MODSECTION) {
var %cb_urn = /sections/ $+ $1
var %cb_method = PATCH
var %cb_data = $2-
}
elseif (%cb_cmd == MODSITESECTION) {
var %cb_urn = /sites/ $+ $1 $+ /sections/ $+ $2
var %cb_method = PATCH
var %cb_data = $3-
}
elseif ($regex(%cb_cmd,/^(FILELIST|INCOMPLETES)/)) {
var %cb_urn = /filelist?site= $+ $1 $+ &path= $+ $iif($2,$v1,/) $+ $iif($3,&timeout= $+ $v1)
var %cb_method = GET
}
elseif (%cb_cmd == INFO) {
var %cb_urn = /info
var %cb_method = GET
}
else {
echo -ast CBCMD: ERROR > unsupported command
return
}
set %cb_gotres 0
;; Call 'JSONFormIRC' to get data from API
JSONClose -w cbftp*
var %cb_hn = cbftp $+ $rand(1,10000)
JSONOpen -uwi %cb_hn $CbUri(%cb_url, %cb_urn)
CbHttpHeaders %cb_hn
JSONHttpMethod %cb_hn %cb_method
JSONHttpFetch %cb_hn %cb_data
if (%cb_debug >= 2) {
echo $CbHttpDebug(%cb_url, %cb_urn, %cb_hn, %cb_method, %cb_data)
echo $JSON(%cb_hn).debug
}
if (%cb_debug >= 4) {
echo -ast DEBUG: HttpResponse $json(%cb_hn).HttpResponse
echo -ast DEBUG: HttpStatus $json(%cb_hn).HttpStatus
echo -ast DEBUG: HttpStatusText $json(%cb_hn).HttpStatusText
echo -ast DEBUG: HttpHeaders $json(%cb_hn).HttpHeaders
echo -ast DEBUG: HttpHeader $json(%cb_hn).HttpHeader
echo -ast DEBUG: HttpBody $json(%cb_hn).HttpBody
}
;; Check JSON for error and handle results
if (!$JSONError) {
if (%cb_debug >= 1) {
echo -ast DEBUG: CbCmd OK, no JSONError
}
;; HTTP Status 201+ succesful, 4xx: client error and 5xx: server error
if ($regex($JSON(%cb_hn).HttpStatus,/^2(0[1-9]|[1-9][0-9])$/)) {
echo -as CBAPI: OK > $JSON(%cb_hn).HttpStatus $JSON(%cb_hn).HttpStatusText
JSONClose %cb_hn
return
}
if ($regex($JSON(%cb_hn).HttpStatus,/^[4-5]\d\d$)) {
echo -ast CBAPI: ERROR > $JSON(%cb_hn).HttpStatus $JSON(%cb_hn).HttpStatusText $JSON(%cb_hn).HttpBody
JSONClose %cb_hn
return
}
;; DELSITE/DELSECTION: HTTP Status 2xx and a non-empty reponse
elseif ($regex(%cb_cmd,/^DEL(SITE|SECTION)/)) {
if ($regex($json(%cb_hn).HttpStatus,/^2/)) {
if ($json(%cb_hn).length > 0) {
set %cb_gotres 1
}
}
}
;; GETSECTIONS: Show array with all sections names
elseif (%cb_cmd == GETSECTIONS) {
if ($regex($json(%cb_hn).HttpStatus,/^2/)) {
if ($json(%cb_hn).length > 0) {
set %cb_gotres 1
if ($1) {
noop $regsubex(res,$json(%cb_hn).HttpBody,/[{}]/g,,%cb_sec_details)
echo -as CBSITES: SECTION > $+ $1 $+ $+ %cb_sec_details
}
else {
var %i = 0
var %sections = $null
while (%i < $json(%cb_hn).length) {
var %sections = $json(%cb_hn,%i,name).String %sections
inc %i
}
set %cb_getsections $replace(%sections,",)
if (%cb_isid == $true) {
echo %cb_getsections
}
else {
echo -as CBSITES: ALL SECTIONS > %sections ( $+ $json(%cb_hn).length $+ sections)
}
}
}
}
}
;; DUPE/SEARCH: Walk JSON calling CbSiteItem for each site item
elseif ($regex(%cb_cmd,/^SITE (DUPE|SEARCH|STAT)$/)) {
echo -as $crlf
if (%cb_sites) {
var %cb_len = 0
if ($json(%cb_hn).length > 0) {
var %cb_len = $json(%cb_hn).length
}
if (%cb_debug >= 2) {
echo -ast DEBUG: JSONForEach walk (cb_len= $+ %cb_len $+ )
}
noop $JSONForEach(%cb_hn,CbSiteItem %cb_len).walk
}
}
;; FILELIST: Parse JSON and call CbFileItem or CbIncItem for each dir name
elseif (%cb_cmd == FILELIST) {
set -eu30 %cnt_inc 0
set -eu30 %cnt_nuked 0