forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_demo.go
2458 lines (2137 loc) · 59.5 KB
/
cli_demo.go
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
package main
import (
"bufio"
"errors"
"fmt"
"io"
//"io/ioutil"
"encoding/json"
"log"
"os"
"reflect"
"regexp"
"strconv"
"strings"
pubnub "github.com/crimsonred/go/v5"
"net/http"
_ "net/http/pprof"
)
var config *pubnub.Config
var pn *pubnub.PubNub
var quitSubscribe = false
const outputPrefix = "\x1b[32;1m Example >>>> \x1b[0m"
const outputSuffix = "\x1b[32;2m Example <<<< \x1b[0m"
func main() {
connect()
// go pubnub.NewPubNub(pubnub.NewConfig())
// pubnub.NewPubNub(pubnub.NewConfig())
}
func connect() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
config = pubnub.NewConfig()
config.UseHTTP2 = false
config.PNReconnectionPolicy = pubnub.PNExponentialPolicy
var infoLogger *log.Logger
logfileName := "pubnubMessaging.log"
f, err := os.OpenFile(logfileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("error opening file: ", err.Error())
fmt.Println("Logging disabled")
} else {
fmt.Println("Logging enabled writing to ", logfileName)
infoLogger = log.New(f, "", log.Ldate|log.Ltime|log.Lshortfile)
}
config.Log = infoLogger
config.Log.SetPrefix("PubNub :-> ")
config.PublishKey = "demo"
config.SubscribeKey = "demo"
config.CipherKey = "enigma"
config.UseRandomInitializationVector = true
pn = pubnub.NewPubNub(config)
// for subscribe event
listener := pubnub.NewListener()
go func() {
for {
select {
case status := <-listener.Status:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- STATUS: ")
fmt.Println(fmt.Sprintf("%s status.Error %s", outputPrefix, status.Error))
fmt.Println(fmt.Sprintf("%s status.Category %s", outputPrefix, status.Category))
fmt.Println(fmt.Sprintf("%s status.Operation %s", outputPrefix, status.Operation))
fmt.Println(fmt.Sprintf("%s status.StatusCode %d", outputPrefix, status.StatusCode))
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, status.ErrorData))
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, status.ClientRequest))
fmt.Println("")
fmt.Println(fmt.Sprintf("%s", outputSuffix))
case msg := <-listener.Message:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- MESSAGE: ")
fmt.Println(fmt.Sprintf("%s msg.Channel: %s", outputPrefix, msg.Channel))
fmt.Println(fmt.Sprintf("%s msg.Message: %s", outputPrefix, msg.Message))
fmt.Println(fmt.Sprintf("%s msg.SubscribedChannel: %s", outputPrefix, msg.SubscribedChannel))
fmt.Println(fmt.Sprintf("%s msg.Timetoken: %d", outputPrefix, msg.Timetoken))
fmt.Println("")
fmt.Println(fmt.Sprintf("%s", outputSuffix))
case msg := <-listener.Signal:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- SIGNAL: ")
fmt.Println(fmt.Sprintf("%s msg.Channel: %s", outputPrefix, msg.Channel))
fmt.Println(fmt.Sprintf("%s msg.Message: %s", outputPrefix, msg.Message))
fmt.Println(fmt.Sprintf("%s msg.SubscribedChannel: %s", outputPrefix, msg.SubscribedChannel))
fmt.Println(fmt.Sprintf("%s msg.Timetoken: %d", outputPrefix, msg.Timetoken))
fmt.Println("")
fmt.Println(fmt.Sprintf("%s", outputSuffix))
case presence := <-listener.Presence:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- PRESENCE: ")
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, presence))
fmt.Println("")
fmt.Println(fmt.Sprintf("%s", outputSuffix))
case uuidEvent := <-listener.UUIDEvent:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- UUIDEvent: ")
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, uuidEvent))
fmt.Println(fmt.Sprintf("%s uuidEvent.Channel: %s", outputPrefix, uuidEvent.Channel))
fmt.Println(fmt.Sprintf("%s uuidEvent.SubscribedChannel: %s", outputPrefix, uuidEvent.SubscribedChannel))
fmt.Println(fmt.Sprintf("%s uuidEvent.Event: %s", outputPrefix, uuidEvent.Event))
fmt.Println(fmt.Sprintf("%s uuidEvent.UUID: %s", outputPrefix, uuidEvent.UUID))
fmt.Println(fmt.Sprintf("%s uuidEvent.Description: %s", outputPrefix, uuidEvent.Description))
fmt.Println(fmt.Sprintf("%s uuidEvent.Timestamp: %s", outputPrefix, uuidEvent.Timestamp))
fmt.Println(fmt.Sprintf("%s uuidEvent.Name: %s", outputPrefix, uuidEvent.Name))
fmt.Println(fmt.Sprintf("%s uuidEvent.ExternalID: %s", outputPrefix, uuidEvent.ExternalID))
fmt.Println(fmt.Sprintf("%s uuidEvent.ProfileURL: %s", outputPrefix, uuidEvent.ProfileURL))
fmt.Println(fmt.Sprintf("%s uuidEvent.Email: %s", outputPrefix, uuidEvent.Email))
// fmt.Println(fmt.Sprintf("%s uuidEvent.Created: %s", outputPrefix, uuidEvent.Created))
fmt.Println(fmt.Sprintf("%s uuidEvent.Updated: %s", outputPrefix, uuidEvent.Updated))
fmt.Println(fmt.Sprintf("%s uuidEvent.ETag: %s", outputPrefix, uuidEvent.ETag))
fmt.Println(fmt.Sprintf("%s uuidEvent.Custom: %v", outputPrefix, uuidEvent.Custom))
case channelEvent := <-listener.ChannelEvent:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- ChannelEvent: ")
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, channelEvent))
fmt.Println(fmt.Sprintf("%s channelEvent.Channel: %s", outputPrefix, channelEvent.Channel))
fmt.Println(fmt.Sprintf("%s channelEvent.SubscribedChannel: %s", outputPrefix, channelEvent.SubscribedChannel))
fmt.Println(fmt.Sprintf("%s channelEvent.Event: %s", outputPrefix, channelEvent.Event))
fmt.Println(fmt.Sprintf("%s channelEvent.Channel: %s", outputPrefix, channelEvent.Channel))
fmt.Println(fmt.Sprintf("%s channelEvent.Description: %s", outputPrefix, channelEvent.Description))
fmt.Println(fmt.Sprintf("%s channelEvent.Timestamp: %s", outputPrefix, channelEvent.Timestamp))
// fmt.Println(fmt.Sprintf("%s channelEvent.Created: %s", outputPrefix, channelEvent.Created))
fmt.Println(fmt.Sprintf("%s channelEvent.Updated: %s", outputPrefix, channelEvent.Updated))
fmt.Println(fmt.Sprintf("%s channelEvent.ETag: %s", outputPrefix, channelEvent.ETag))
fmt.Println(fmt.Sprintf("%s channelEvent.Custom: %v", outputPrefix, channelEvent.Custom))
case membershipEvent := <-listener.MembershipEvent:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- MembershipEvent: ")
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, membershipEvent))
fmt.Println(fmt.Sprintf("%s membershipEvent.Channel: %s", outputPrefix, membershipEvent.Channel))
fmt.Println(fmt.Sprintf("%s membershipEvent.SubscribedChannel: %s", outputPrefix, membershipEvent.SubscribedChannel))
fmt.Println(fmt.Sprintf("%s membershipEvent.Event: %s", outputPrefix, membershipEvent.Event))
fmt.Println(fmt.Sprintf("%s membershipEvent.Channel: %s", outputPrefix, membershipEvent.Channel))
fmt.Println(fmt.Sprintf("%s membershipEvent.UUID: %s", outputPrefix, membershipEvent.UUID))
fmt.Println(fmt.Sprintf("%s membershipEvent.Description: %s", outputPrefix, membershipEvent.Description))
fmt.Println(fmt.Sprintf("%s membershipEvent.Timestamp: %s", outputPrefix, membershipEvent.Timestamp))
fmt.Println(fmt.Sprintf("%s membershipEvent.Custom: %v", outputPrefix, membershipEvent.Custom))
case messageActionsEvent := <-listener.MessageActionsEvent:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- MessageActionsEvent: ")
fmt.Println(fmt.Sprintf("%s %s", outputPrefix, messageActionsEvent))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.Channel: %s", outputPrefix, messageActionsEvent.Channel))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.SubscribedChannel: %s", outputPrefix, messageActionsEvent.SubscribedChannel))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.Event: %s", outputPrefix, messageActionsEvent.Event))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.Data.ActionType: %s", outputPrefix, messageActionsEvent.Data.ActionType))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.Data.ActionValue: %s", outputPrefix, messageActionsEvent.Data.ActionValue))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.Data.ActionTimetoken: %s", outputPrefix, messageActionsEvent.Data.ActionTimetoken))
fmt.Println(fmt.Sprintf("%s messageActionsEvent.Data.MessageTimetoken: %s", outputPrefix, messageActionsEvent.Data.MessageTimetoken))
case file := <-listener.File:
fmt.Print(fmt.Sprintf("%s Subscribe Response:", outputPrefix))
fmt.Println(" --- MessageActionsEvent: ")
fmt.Println(fmt.Sprintf("file.File.PNMessage.Text: %s", file.File.PNMessage.Text))
fmt.Println(fmt.Sprintf("file.File.PNFile.Name: %s", file.File.PNFile.Name))
fmt.Println(fmt.Sprintf("file.File.PNFile.ID: %s", file.File.PNFile.ID))
fmt.Println(fmt.Sprintf("file.File.PNFile.URL: %s", file.File.PNFile.URL))
fmt.Println(fmt.Sprintf("file.Channel: %s", file.Channel))
fmt.Println(fmt.Sprintf("file.Timetoken: %d", file.Timetoken))
fmt.Println(fmt.Sprintf("file.SubscribedChannel: %s", file.SubscribedChannel))
fmt.Println(fmt.Sprintf("file.Publisher: %s", file.Publisher))
out, _ := os.Create("out.txt")
resDLFile, statusDLFile, errDLFile := pn.DownloadFile().Channel("demo-channel").CipherKey("enigma").ID(file.File.PNFile.ID).Name(file.File.PNFile.Name).Execute()
fmt.Println(statusDLFile, errDLFile)
if resDLFile != nil {
_, err := io.Copy(out, resDLFile.File)
if err != nil {
fmt.Println(err)
}
}
}
}
}()
pn.AddListener(listener)
showHelp()
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print(fmt.Sprintf("%s ", outputPrefix))
text, _ := reader.ReadString('\n')
text = text[:len(text)-1]
if len(text) != 0 {
readCommand(text)
}
fmt.Println("")
}
}
func showErr(err string) {
fmt.Println(fmt.Sprintf("%s \x1b[31;1m %s \x1b[0m", outputPrefix, errors.New(err)))
}
func showHelp() {
fmt.Println("")
fmt.Println("============")
fmt.Println(" || HELP ||")
fmt.Println("============")
fmt.Println("")
showPublishHelp()
showSubscribeHelp()
showTimeHelp()
showHereNowHelp()
showHistoryHelp()
showDelMessagesHelp()
showWhereNowHelp()
showUnsubscribeHelp()
showFetchHelp()
showFireHelp()
showSetStateHelp()
showGetStateHelp()
showAddToCgHelp()
showRemFromCgHelp()
showListAllChOfCgHelp()
showDelCgHelp()
showGrantHelp()
showGrantTokenHelp()
showSubscribeWithStateHelp()
showPresenceTimeoutHelp()
showPresenceHelp()
showMessageCountsHelp()
showSignalHelp()
showSetUUIDMetadataHelp()
showGetAllUUIDMetadataHelp()
showEditMembershipsHelp()
showUpdateMembersHelp()
showGetSpaceMembershipsHelp()
showGetMembersHelp()
showGetAllChannelMetadataHelp()
showUpdateChannelMetadataHelp()
showDeleteSpaceHelp()
showCreateSpaceHelp()
showGetSpaceHelp()
showDeleteUserHelp()
showUpdateUUIDMetadataHelp()
showGetUserHelp()
showAddActionHelp()
showGetActionsHelp()
showDeleteActionHelp()
fmt.Println("")
fmt.Println("================")
fmt.Println(" || COMMANDS ||")
fmt.Println("================")
fmt.Println("")
fmt.Println(" UNSUBSCRIBE ALL \n\tq ")
fmt.Println(" QUIT \n\tctrl+c ")
}
func showAddActionHelp() {
fmt.Println(" AddAction EXAMPLE: ")
fmt.Println(" addaction channel timetoken actiontype actionval")
fmt.Println(" addaction my-channel 15210190573608384 reaction smiley_face")
}
func showGetActionsHelp() {
fmt.Println(" GetActions EXAMPLE: ")
fmt.Println(" getactions channel start end limit")
fmt.Println(" getactions my-channel 15692395344923130 15210190573608384 10")
}
func showDeleteActionHelp() {
fmt.Println(" DeleteAction EXAMPLE: ")
fmt.Println(" remaction channel messagetTimetoken actionTimetoken")
fmt.Println(" remaction my-channel 15210190573608384 15692395344923130 ")
}
func showEditMembershipsHelp() {
fmt.Println(" EditMemberships EXAMPLE: ")
fmt.Println(" managememberships channelMetadataid id a/u/r limit count")
fmt.Println(" managememberships id0 id1 a 100 true")
}
func showUpdateMembersHelp() {
fmt.Println(" UpdateMembers EXAMPLE: ")
fmt.Println(" managemem memebers id a/u/r limit count")
fmt.Println(" managemem id0 id0 a 100 true")
}
func showGetSpaceMembershipsHelp() {
fmt.Println(" GetChannelMetadata EXAMPLE: ")
fmt.Println(" getchannelmetadata channelMetadataid limit count start")
fmt.Println(" getchannelmetadata id0 100 true Mymx")
}
func showGetMembersHelp() {
fmt.Println(" GetMembers EXAMPLE: ")
fmt.Println(" getmem userid limit count start")
fmt.Println(" getmem id0 100 true Mymx")
}
func showGetAllChannelMetadataHelp() {
fmt.Println(" GetAllChannelMetadata EXAMPLE: ")
fmt.Println(" getallchannelmetadata limit count start")
fmt.Println(" getallchannelmetadata 100 true MjWn")
}
func showUpdateChannelMetadataHelp() {
fmt.Println(" UpdateChannelMetadata EXAMPLE: ")
fmt.Println(" updatechannelmetadata id name desc")
fmt.Println(" updatechannelmetadata id0 name desc")
}
func showDeleteSpaceHelp() {
fmt.Println(" DeleteChannelMetadata EXAMPLE: ")
fmt.Println(" delchannelmetadata id")
fmt.Println(" delchannelmetadata id0")
}
func showCreateSpaceHelp() {
fmt.Println(" SetChannelMetadata EXAMPLE: ")
fmt.Println(" setchannelmetadata id name desc")
fmt.Println(" setchannelmetadata id0 name desc")
}
func showGetSpaceHelp() {
fmt.Println(" GetChannelMetadata EXAMPLE: ")
fmt.Println(" getchannelmetadata id")
fmt.Println(" getchannelmetadata id0")
}
func showDeleteUserHelp() {
fmt.Println(" DeleteUUIDMetadata EXAMPLE: ")
fmt.Println(" deleteuuidmetadata id")
fmt.Println(" deleteuuidmetadata id0")
}
func showUpdateUUIDMetadataHelp() {
fmt.Println(" UpdateUUIDMetadata EXAMPLE: ")
fmt.Println(" updateuuidmetadata id name extid url email")
fmt.Println(" updateuuidmetadata id0 name extid purl email")
}
func showGetUserHelp() {
fmt.Println(" GetMetadata EXAMPLE: ")
fmt.Println(" getuuidmetadata id")
fmt.Println(" getuuidmetadata id0")
}
func showMessageCountsHelp() {
fmt.Println(" MessageCounts EXAMPLE: ")
fmt.Println(" messagecounts Channel(s) timetoken1,timetoken2")
fmt.Println(" messagecounts my-channel,my-channel1 15210190573608384,15211140747622125")
}
func showGetAllUUIDMetadataHelp() {
fmt.Println(" GetAllUUIDMetadata EXAMPLE: ")
fmt.Println(" getalluuidmetadata limit count start")
fmt.Println(" getalluuidmetadata 100 true MjWn")
}
func showSetUUIDMetadataHelp() {
fmt.Println(" SetUUIDMetadata EXAMPLE: ")
fmt.Println(" setuuidmetadata id name extid url email")
fmt.Println(" setuuidmetadata id0 name extid purl email")
}
func showSignalHelp() {
fmt.Println(" Signal EXAMPLE: ")
fmt.Println(" signal Channel Message")
fmt.Println(" signal my-channel \"my-signal\"")
}
func showGetStateHelp() {
fmt.Println(" GET STATE EXAMPLE: ")
fmt.Println(" getstate Channel ")
fmt.Println(" getstate my-channel ")
}
func showSetStateHelp() {
fmt.Println(" SET STATE EXAMPLE: ")
fmt.Println(" setstate Channel state ")
fmt.Println(" setstate my-channel {\"k\":\"v\"} ")
}
func showFetchHelp() {
fmt.Println(" FETCH EXAMPLE: ")
fmt.Println(" fetch Channel Reverse Max Start End ")
fmt.Println(" fetch my-channel,test true 10 15210190573608384 15211140747622125 ")
}
func showFireHelp() {
fmt.Println(" FIRE EXAMPLE: ")
fmt.Println(" fire usePost \"my-message\" my-channel")
fmt.Println(" fire false \"my-message\" my-channel")
}
func showPublishHelp() {
fmt.Println(" PUBLISH EXAMPLE: ")
fmt.Println(" pub usePost store noreplicate \"my-message\" my-channel")
fmt.Println(" pub false true false \"my-message\" my-channel")
}
func showTimeHelp() {
fmt.Println(" TIME EXAMPLE: ")
fmt.Println(" time")
}
func showHereNowHelp() {
fmt.Println(" HERENOW EXAMPLE: ")
fmt.Println(" herenow includeState includeUUIDs channel channel-group")
fmt.Println(" herenow false false my-channel my-channel-group")
}
func showSubscribeHelp() {
fmt.Println(" SUBSCRIBE EXAMPLE: ")
fmt.Println(" sub withPresence channels channelGroups")
fmt.Println(" sub true my-channel,my-another-channel my-channelgroup,my-another-channel-group")
}
func showSubscribeWithStateHelp() {
fmt.Println(" SUBSCRIBE With State EXAMPLE: ")
fmt.Println(" subs withPresence channels channelGroups state")
fmt.Println(" subs true my-channel,my-another-channel my-channelgroup,my-another-channel-group {\"k2\":\"v2\"}")
}
func showHistoryHelp() {
fmt.Println(" HISTORY EXAMPLE: ")
fmt.Println(" hist Channel IncludeTimetoken Reverse Count Start End ")
fmt.Println(" hist my-channel true true 10 15210190573608384 15211140747622125 ")
}
func showDelMessagesHelp() {
fmt.Println(" Delete Messages EXAMPLE: ")
fmt.Println(" delmessages Channel Start End ")
fmt.Println(" delmessages my-channel 15210190573608384 15211140747622125 ")
}
func showWhereNowHelp() {
fmt.Println(" WHERENOW EXAMPLE: ")
fmt.Println(" wherenow uuid ")
fmt.Println(" wherenow \"uuidToCheck\"")
}
func showUnsubscribeHelp() {
fmt.Println(" UNSUBSCRIBE EXAMPLE: ")
fmt.Println(" unsub channels channelGroups")
fmt.Println(" unsub my-channel,my-another-channel my-channelgroup,my-another-channel-group")
}
func showAddToCgHelp() {
fmt.Println(" Add Channels to Channel Group EXAMPLE: ")
fmt.Println(" addcg Channel ChannelGroup")
fmt.Println(" addcg my-channel1,my-channel2 cg")
}
func showRemFromCgHelp() {
fmt.Println(" Remove Channels from Channel Group EXAMPLE: ")
fmt.Println(" remcg Channel ChannelGroup ")
fmt.Println(" remcg my-channel1 cg")
}
func showListAllChOfCgHelp() {
fmt.Println(" List Channels of Channel Group EXAMPLE: ")
fmt.Println(" listcg ChannelGroup")
fmt.Println(" listcg cg ")
}
func showDelCgHelp() {
fmt.Println(" Delete Channel Group EXAMPLE: ")
fmt.Println(" delcg ChannelGroup ")
fmt.Println(" delcg cg ")
}
func showGrantTokenHelp() {
fmt.Println(" GRANTTOKEN EXAMPLE: ")
fmt.Println(" granttoken Channels ChannelGroups Users Spaces ttl ")
fmt.Println(" granttoken ch1,ch2 cg1,cg2 u1,u2 s1,s2 ^ch-[0-9a-f]*$ ^:cg-[0-9a-f]*$ ^u-[0-9a-f]*$ ^s-[0-9a-f]*$ 10")
}
func showGrantHelp() {
fmt.Println(" GRANT2 EXAMPLE: ")
fmt.Println(" grant Channels ChannelGroups manage read write ttl ")
fmt.Println(" grant my-channel cg false false false 10")
}
func showPresenceTimeoutHelp() {
fmt.Println(" Presence Timeout: ")
fmt.Println(" setpto presenceTimeout presenceHeartbeatInterval ")
fmt.Println(" setpto 120 59")
}
func showPresenceHelp() {
fmt.Println(" Presence: ")
fmt.Println(" presence Connected Channels ChannelGroups")
fmt.Println(" presence true my-channel,my-another-channel my-channelgroup,my-another-channel-group")
}
func readCommand(cmd string) {
command := strings.Split(cmd, " ")
switch w := command[0]; w {
case "pub":
publishRequest(command[1:])
case "fire":
fireRequest(command[1:])
case "sub":
subscribeRequest(command[1:])
case "time":
timeRequest()
case "herenow":
hereNowRequest(command[1:])
case "hist":
historyRequest(command[1:])
case "wherenow":
whereNowRequest(command[1:])
case "unsub":
unsubscribeRequest(command[1:])
case "fetch":
fetchRequest(command[1:])
case "delmessages":
delMessageRequest(command[1:])
case "subs":
subscribeRequest(command[1:])
case "setstate":
setStateRequest(command[1:])
case "getstate":
getStateRequest(command[1:])
case "addcg":
addToChannelGroup(command[1:])
case "remcg":
removeFromChannelGroup(command[1:])
case "listcg":
listChannelsOfChannelGroup(command[1:])
case "delcg":
delChannelGroup(command[1:])
case "grant":
grant(command[1:])
case "granttoken":
granttoken(command[1:])
case "help":
showHelp()
case "pt":
publishTest()
case "setpto":
setPresenceTimeout(command[1:])
case "presence":
runPresenceRequest(command[1:])
case "messagecounts":
messageCounts(command[1:])
case "signal":
signal(command[1:])
case "setuuidmetadata":
setUUIDMetadata(command[1:])
case "getalluuidmetadata":
getAllUUIDMetadata(command[1:])
case "getuuidmetadata":
getUUIDMetadata(command[1:])
case "updateuuidmetadata":
updateUUIDMetadata(command[1:])
case "deleteuuidmetadata":
deleteUser(command[1:])
case "getallchannelmetadata":
getSpaces(command[1:])
case "setchannelmetadata":
createChannelMetadata(command[1:])
case "delchannelmetadata":
deleteChannelMetadata(command[1:])
case "updatechannelmetadata":
updateChannelMetadata(command[1:])
case "getchannelmetadata":
getChannelMetadata(command[1:])
case "getmemberships":
getSpaceMemberships(command[1:])
case "getmem":
getMembers(command[1:])
case "managememberships":
manageMemberships(command[1:])
case "managemem":
manageMembers(command[1:])
case "settoken":
setToken(command[1:])
case "settokens":
setTokens(command[1:])
case "gettoken":
getToken(command[1:])
case "gettokens":
getTokens(command[1:])
case "gettokenres":
getTokenRes(command[1:])
case "addaction":
addMessageAction(command[1:])
case "addactions":
addMessageActions(command[1:])
case "getactions":
getMessageActions(command[1:])
case "getactionsrec":
getMessageActionsRec(command[1:])
case "getactionsrec2":
getMessageActionsRec(command[1:])
case "uploadfile":
uploadFile(command[1:])
case "delfile":
delFile(command[1:])
case "listfiles":
listFiles(command[1:])
case "getfileurl":
getFileURL(command[1:])
case "downloadfile":
downloadFile(command[1:])
case "q":
pn.UnsubscribeAll()
case "d":
pn.Destroy()
fmt.Println("after Destroy")
default:
showHelp()
}
}
func uploadFile(args []string) {
channel := args[0]
message := args[1]
name := args[2]
filepath := args[3]
file, err := os.Open(filepath)
defer file.Close()
cipherKey := args[4]
res, status, err := pn.SendFile().Channel(channel).Message(message).CipherKey(cipherKey).Name(name).File(file).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
func delFile(args []string) {
ch := args[0]
id := args[1]
name := args[2]
res, status, err := pn.DeleteFile().Channel(ch).ID(id).Name(name).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
func listFiles(args []string) {
ch := args[0]
res, status, err := pn.ListFiles().Channel(ch).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
func getFileURL(args []string) {
ch := args[0]
id := args[1]
name := args[2]
res, status, err := pn.GetFileURL().Channel(ch).ID(id).Name(name).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
func downloadFile(args []string) {
ch := args[0]
id := args[1]
name := args[2]
resDLFile, status, err := pn.DownloadFile().Channel(ch).ID(id).Name(name).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", resDLFile)
if resDLFile != nil {
out, _ := os.Create("out.txt")
defer out.Close()
_, err := io.Copy(out, resDLFile.File)
if err != nil {
fmt.Println(err)
}
}
}
func getMessageActionsRec2(args []string) {
channel := args[0]
getMessageActionsRecursive(channel, "", false, 0)
}
func getMessageActionsRec(args []string) {
channel := args[0]
getMessageActionsRecursive(channel, "", true, 0)
}
func getMessageActionsRecursive(channel string, start string, more bool, counter int) {
var res *pubnub.PNGetMessageActionsResponse
if start == "" {
res, _, _ = pn.GetMessageActions().Channel(channel).Execute()
} else {
res, _, _ = pn.GetMessageActions().Channel(channel).Start(start).Execute()
}
if (res != nil) && (len(res.Data) > 0) {
printMessageActions(res, counter+1)
if more {
if res.More.Start != "" {
getMessageActionsRecursive(channel, res.More.Start, more, len(res.Data))
}
} else {
if len(res.Data) > 0 {
getMessageActionsRecursive(channel, res.Data[0].ActionTimetoken, more, len(res.Data))
}
}
}
}
func removeMessageActions(args []string) {
if len(args) < 3 {
showDeleteActionHelp()
return
}
channel := args[0]
tt := args[1]
att := args[2]
res, status, err := pn.RemoveMessageAction().Channel(channel).MessageTimetoken(tt).ActionTimetoken(att).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
func getMessageActions(args []string) {
if len(args) < 1 {
showGetActionsHelp()
return
}
channel := args[0]
if len(args) == 4 {
var limit int
n, err := strconv.ParseInt(args[3], 10, 64)
if err == nil {
limit = int(n)
}
res, status, err := pn.GetMessageActions().Channel(channel).Start(args[1]).End(args[2]).Limit(limit).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
printMessageActions(res, 0)
} else if len(args) == 3 {
res, status, err := pn.GetMessageActions().Channel(channel).Start(args[1]).End(args[2]).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
printMessageActions(res, 0)
} else if len(args) == 2 {
res, status, err := pn.GetMessageActions().Channel(channel).Start(args[1]).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
printMessageActions(res, 0)
} else {
res, status, err := pn.GetMessageActions().Channel(channel).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
printMessageActions(res, 0)
}
}
func printMessageActions(res *pubnub.PNGetMessageActionsResponse, counter int) {
if res != nil {
for i, k := range res.Data {
fmt.Println(fmt.Sprintf("No: %d, Val: %s", i+counter, k))
}
fmt.Println("More:", res.More)
}
}
func addMessageActions(args []string) {
if len(args) < 5 {
showAddActionHelp()
return
}
// addaction my-channel 15210190573608384 reaction smiley_face
channel := args[0]
tt := args[1]
actionType := args[2]
actionVal := args[3]
var count int
n, err := strconv.ParseInt(args[4], 10, 64)
if err == nil {
count = int(n)
}
for i := 0; i < count; i++ {
ma := pubnub.MessageAction{
ActionType: actionType,
ActionValue: actionVal + "_" + strconv.Itoa(i),
}
res, status, err := pn.AddMessageAction().Channel(channel).MessageTimetoken(tt).Action(ma).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
}
func addMessageAction(args []string) {
if len(args) < 4 {
showAddActionHelp()
return
}
// addaction my-channel 15210190573608384 reaction smiley_face
channel := args[0]
tt := args[1]
actionType := args[2]
actionVal := args[3]
ma := pubnub.MessageAction{
ActionType: actionType,
ActionValue: actionVal,
}
res, status, err := pn.AddMessageAction().Channel(channel).MessageTimetoken(tt).Action(ma).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
func setToken(args []string) {
pn.SetToken(args[0])
}
func setTokens(args []string) {
var tokens []string
tokens = strings.Split(args[0], ",")
pn.SetTokens(tokens)
}
func getToken(args []string) {
res := pn.GetTokens()
fmt.Println(res)
}
func getTokens(args []string) {
res := pn.GetTokens()
fmt.Println(res)
}
func getTokenRes(args []string) {
res := pn.GetTokens()
fmt.Println(res)
}
func manageMembers(args []string) {
if len(args) < 5 {
showEditMembershipsHelp()
return
}
channelMetadataID := args[0]
id0 := args[1]
//id1 := args[2]
//id2 := args[3]
action := args[2]
var limit int
n, err := strconv.ParseInt(args[3], 10, 64)
if err == nil {
limit = int(n)
}
count, _ := strconv.ParseBool(args[4])
var start string
if len(args) > 5 {
start = args[5]
}
incl := []pubnub.PNChannelMembersInclude{
pubnub.PNChannelMembersIncludeCustom,
pubnub.PNChannelMembersIncludeUUID,
pubnub.PNChannelMembersIncludeUUIDCustom,
}
custom := make(map[string]interface{})
custom["a1"] = "b1"
custom["c1"] = "d1"
uuid := pubnub.PNChannelMembersUUID{
ID: id0,
}
in := pubnub.PNChannelMembersSet{
UUID: uuid,
Custom: custom,
}
custom2 := make(map[string]interface{})
custom2["a2"] = "b2"
custom2["c2"] = "d2"
up := pubnub.PNChannelMembersSet{
UUID: uuid,
Custom: custom2,
}
inArr := []pubnub.PNChannelMembersSet{
in,
up,
}
re := pubnub.PNChannelMembersRemove{
UUID: uuid,
}
reArr := []pubnub.PNChannelMembersRemove{
re,
}
if action == "a" {
reArr = []pubnub.PNChannelMembersRemove{}
inArr = []pubnub.PNChannelMembersSet{}
} else if action == "u" {
reArr = []pubnub.PNChannelMembersRemove{}
inArr = []pubnub.PNChannelMembersSet{}
} else if action == "r" {
inArr = []pubnub.PNChannelMembersSet{}
}
if start != "" {
res, status, err := pn.ManageChannelMembers().Channel(channelMetadataID).Set(inArr).Remove(reArr).Include(incl).Limit(limit).Count(count).Start(start).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
} else {
res, status, err := pn.ManageChannelMembers().Channel(channelMetadataID).Set(inArr).Remove(reArr).Include(incl).Limit(limit).Count(count).Execute()
fmt.Println("status", status)
fmt.Println("err", err)
fmt.Println("res", res)
}
}
func manageMemberships(args []string) {
if len(args) < 5 {
showUpdateMembersHelp()
return
}
userID := args[0]
id0 := args[1]
//id1 := args[2]
//id2 := args[3]
action := args[2]
var limit int
n, err := strconv.ParseInt(args[3], 10, 64)
if err == nil {
limit = int(n)
}
count, _ := strconv.ParseBool(args[4])
var start string
if len(args) > 5 {
start = args[5]
}
incl := []pubnub.PNMembershipsInclude{
pubnub.PNMembershipsIncludeCustom,
pubnub.PNMembershipsIncludeChannel,
pubnub.PNMembershipsIncludeChannelCustom,
}
custom3 := make(map[string]interface{})
custom3["a3"] = "b3"
custom3["c3"] = "d3"
channel := pubnub.PNMembershipsChannel{
ID: id0,
}
in := pubnub.PNMembershipsSet{
Channel: channel,
Custom: custom3,