-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
1449 lines (1364 loc) · 36.2 KB
/
logic.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 (
"bb/addons"
bot "bb/urlparse"
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil" //deprecated will need to update this soon
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/fatih/color" //to add colour to output
"mvdan.cc/xurls/v2" //to parse URLs
"github.com/pterm/pterm" //to make terminal pretty
)
var (
//Everything below does not need to be configured
ismod bool
clear map[string]func() // create a map for storing clear funcs
username string // store the username of the individual currently using BB
homefilepath string // the path for where the bb config files are
snapfilepath string // the path for where the bb snapshot file is
masterfilepath = "/home/" + admin + "/.bbmod/" // mods will be added to this file. Will also store the anonymous file.
modfilepath string
aa Snap // Snapshot object
bb BB // BB object
mm Mod // Moderator object
an Anon // Anon object
ll Last // Last board object
pin Pin // Pin board object
per Personal //Personal variables object
snapname = "bbsn4p.json"
lastname = "bbl4st.json"
anonname = "bban0n.json"
pinname = "bbp1n.json"
pername = "bbp3r.json"
back int //board scroll logic
maximum int //board scroll logic
minimum int //board scroll logic
length = 20
helpstring = `
===BB HELP===
for INDEX section:
new - create a new board i.e 'new topictitle'
del - delete a board by index.
If nobody else has accessed it - you can delete it.
Otherwise, you need superuser permission.
fil - filter index by search string e.g YYYY-MM or Title
pin+ - pin a board by index
pin- - unpin a board by index
q - to quit, or use ctrl-c
r - refresh the index section
w - scroll up the index
s - scroll down the index
b - choose gemini client (default=amfora)
for CHAT section:
q - exits back to index section
r - refresh the board you are on
fil - filter chat by specific string e.g YYYY-MM or substring
w - scroll up the board
s - scroll down the board
l - visit a gemini url via client
anon - make message anonymous
rev - reverses your text
anything else - types text to board
nothing - also exits back to index section
ctrl-c to quit
FYI:
- For gemini client functionality you need to run bb inside tmux
- Boards glow cyan when new content is posted
- New boards glow green.
- You can comment other people via @ sign i.e @person
they will see message highlighted
- If you are on a board and new content is posted on another board,
you'll see '^new' beside author name
PRESS ENTER TO CONTINUE...
`
)
//GENERAL FUNCTIONS////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func init() { //runs at start pre-main to initialize some things.
pterm.EnableOutput()
clear = make(map[string]func()) //Initialize it
clear["linux"] = func() {
cmd := exec.Command("clear") //Linux - maybe one day we'll add windows functionality. who knows.
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
}
}
func check(username string) bool {
if username == admin {
return true
}
if _, err := os.Stat("/home/" + admin + "/.bb"); os.IsNotExist(err) {
fmt.Printf("the admin: '%s' has not yet ran / initiated bb.\n", admin)
return false
}
return true
}
//Clear screen
func callclear() {
value, ok := clear[runtime.GOOS] //runtime.GOOS -> linux, windows, darwin etc.
if ok { //if we defined a clear func for that platform:
value() //we execute it
}
//No need to panic.
}
func intmux(link, client string) {
muxcmd := exec.Command("tmux", "split", "-h", client, link) //Linux
err := muxcmd.Run()
if err != nil {
fmt.Println(err)
}
}
//Grab a timestamp.
func timestamp() string {
t := time.Now()
var x = t.Format("2006-01-02 15:04:05")
return x
}
//create a new board for the global BB variable.
func newboard(title string, bb BB) {
for index := range bb.B {
if bb.B[index].Title == title {
fmt.Println("Please use unique title. Press enter to continue")
fmt.Scanln()
return
}
}
board := Board{}
board.Title = title
board.Owner = username
t := time.Now()
var x = t.Format("2006-01-02")
board.Date = x
board.Save(title)
fmt.Println("saved " + title)
}
//remove a board from BB at a specific index
func remove(slice []Board, s int) []Board {
if len(slice) <= 1 {
return []Board{}
}
return append(slice[:s], slice[s+1:]...)
}
func modremove(s []string, index int) []string {
ret := make([]string, 0)
ret = append(ret, s[:index]...)
return append(ret, s[index+1:]...)
}
//Does 2D string array already have a specific string in it?
func alreadyhas(s [][]string, st string) bool {
for index := range s {
for index2 := range s[index] {
if s[index][index2] == st {
return true
}
}
}
return false
}
func grabgeminiurl(input string) string {
if strings.Contains(input, "gemini://") {
rxRelaxed := xurls.Relaxed()
astring := rxRelaxed.FindString(input)
if astring != "" { //--------------------------GEM grap input print out a fancy Title
return "gemi" + astring
}
}
return ""
}
//Add a slice to a 2d slice
func add2slice(ax *[][]string, b []string) [][]string {
a := *ax
a = append(a, b)
*ax = a
return a
}
//Grab a list folders in home dir (to grab the Usernames)
func ufolderlist() []string {
output := []string{}
files, _ := ioutil.ReadDir("/home/")
for _, f := range files {
output = append(output, f.Name())
}
return output
}
//Refresh the BB data with any new inputs.
func rehash() {
pin.Save()
an.Save()
bb = BB{} //Clear BB
bb.Load() //Reload BB
}
//Personal METHODS//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Personal struct {
Browser string
}
//Save file
func (p Personal) Save() {
Base := &p
output, err := json.MarshalIndent(Base, "", "\t")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(snapfilepath+pername, output, 0666)
if err != nil {
fmt.Println(err)
}
}
//Load file
func (p *Personal) Load() {
item := *p
jsonFile, _ := ioutil.ReadFile(snapfilepath + pername)
_ = json.Unmarshal([]byte(jsonFile), &item)
if item.Browser == "" {
item.Browser = "amfora"
}
*p = item
}
///Pin board METHODS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Pin struct {
Title []string
Date []string
}
func (P *Pin) Add(ix int) {
p := *P
titletosave := ""
datetosave := ""
exists := false
for index2 := range bb.B {
if index2 == ix {
titletosave = bb.B[index2].Title
datetosave = bb.B[index2].Date
}
}
for index := range p.Title {
if p.Title[index] == titletosave && p.Date[index] == datetosave {
exists = true
}
}
if !exists {
p.Title = append(p.Title, titletosave)
p.Date = append(p.Date, datetosave)
}
*P = p
P.Save()
}
func (P *Pin) Remove(ix int) {
p := *P
titletodel := ""
datetodel := ""
for index2 := range bb.B {
if index2 == ix {
titletodel = bb.B[index2].Title
datetodel = bb.B[index2].Date
break
}
}
for index := range p.Title {
if p.Title[index] == titletodel && p.Date[index] == datetodel {
p.Title = modremove(p.Title, index)
p.Date = modremove(p.Date, index)
break
}
}
*P = p
P.Save()
}
//Save mod file
func (p Pin) Save() {
Base := &p
output, err := json.MarshalIndent(Base, "", "\t")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(masterfilepath+pinname, output, 0666)
if err != nil {
fmt.Println(err)
}
}
//Load pins
func (p *Pin) Load() {
item := *p
jsonFile, _ := ioutil.ReadFile(masterfilepath + pinname)
_ = json.Unmarshal([]byte(jsonFile), &item)
*p = item
}
///Lastfile & Last METHODS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Last struct {
Title string
Date string
}
//Save last file
func (l Last) Save() {
Base := &l
output, err := json.MarshalIndent(Base, "", "\t")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(snapfilepath+lastname, output, 0644)
if err != nil {
fmt.Println(err)
}
}
//Load mods
func (l *Last) Load() {
item := *l
jsonFile, _ := ioutil.ReadFile(snapfilepath + lastname)
_ = json.Unmarshal([]byte(jsonFile), &item)
*l = item
}
///Moderator & Mod METHODS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Only the administrator can be a moderator at the moment
type Mod struct {
Name []string
Boardarchive []string
Datearchive []string
}
//Save mod file
func (m Mod) Save() {
if ismod {
Base := &m
output, err := json.MarshalIndent(Base, "", "\t")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(modfilepath+"mod.json", output, 0644)
if err != nil {
fmt.Println(err)
}
}
}
//Load mods
func (m *Mod) Load() {
item := *m
jsonFile, _ := ioutil.ReadFile(masterfilepath + "mod.json") //no need to worry about error
_ = json.Unmarshal([]byte(jsonFile), &item) //but leaving it empty in case one day... i need to worry
item.Collect()
*m = item
}
func (m *Mod) Collect() {
list := ufolderlist()
for index := range list {
m.collect(list[index])
}
}
func (m *Mod) collect(homeuser string) {
if m.IsUserMod(homeuser) {
modp := "/home/" + homeuser + "/.bbmod/"
learnfolder, err := ioutil.ReadDir(modp)
if err == nil {
for _, learnfile := range learnfolder {
if learnfile.Name() == "mod.json" {
mm := Mod{}
jsonFile, _ := ioutil.ReadFile(modp + "mod.json") //no need to worry about error
_ = json.Unmarshal([]byte(jsonFile), &mm) //but leaving it empty in case one day... i need to worry
for index := range mm.Boardarchive {
exists := false
for index2 := range m.Boardarchive {
if m.Boardarchive[index2] == mm.Boardarchive[index] && m.Datearchive[index2] == mm.Datearchive[index] {
exists = true
}
}
if !exists {
m.Boardarchive = append(m.Boardarchive, mm.Boardarchive[index])
m.Datearchive = append(m.Datearchive, mm.Datearchive[index])
}
}
}
}
}
}
}
//Check if a board is on the archive list - this is alternative to delete. If a board is on board archive it won't load
func (m Mod) Check(b Board) bool {
for index := range m.Boardarchive {
if m.Boardarchive[index] == b.Title && m.Datearchive[index] == b.Date {
return true
}
}
return false
}
//Checks if current user is a mod
func (m Mod) IsMod() {
for index := range m.Name {
if m.Name[index] == username {
ismod = true
}
}
}
//Checks if current user is a mod
func (m Mod) IsUserMod(uname string) bool {
for index := range m.Name {
if m.Name[index] == uname {
return true
}
}
return false
}
//Lets moderators archive a board by indoex
func (m *Mod) Archive(item int) {
for index := range bb.B {
if index == item {
m.Boardarchive = append(m.Boardarchive, bb.B[index].Title)
m.Datearchive = append(m.Datearchive, bb.B[index].Date)
fmt.Println(bb.B[index].Title + " archived")
return
}
}
}
//Lets you add a mod if you are admin
func (m *Mod) AddMod(user string) {
for index := range m.Name {
if m.Name[index] == user {
return
}
}
m.Name = append(m.Name, user)
}
//Lets you add a mod if you are admin
func (m *Mod) RemoveMod(user string) {
for index := range m.Name {
if m.Name[index] == user {
m.Name = modremove(m.Name, index)
break
}
}
}
///Anon & Anon METHODS////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type Anon struct {
Title []string
Date []string
Board []Anonboard
}
type Anonboard struct {
Contents [][]string
}
//Save mod file
func (a Anon) Save() {
Base := &a
output, err := json.MarshalIndent(Base, "", "\t")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(masterfilepath+anonname, output, 0666)
if err != nil {
fmt.Println(err)
}
}
//Load mods
func (a *Anon) Load() {
item := *a
jsonFile, _ := ioutil.ReadFile(masterfilepath + anonname)
_ = json.Unmarshal([]byte(jsonFile), &item)
*a = item
}
func (a *Anon) Add(title, date string, contents []string) {
A := *a
exists := false
for index := range A.Title {
if A.Title[index] == title && A.Date[index] == date {
exists = true
a.Board[index].Contents = append(a.Board[index].Contents, contents)
}
}
if !exists {
A.Title = append(A.Title, title)
A.Date = append(A.Date, date)
board := Anonboard{}
board.Contents = append(board.Contents, contents)
A.Board = append(A.Board, board)
}
*a = A
}
///Snapshot & Snapshot METHODS////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Snap is struct for storing snapshot of every board in BB
type Snap struct {
Owner []string
Title []string
Date []string
Length []int
Checked []bool
}
//Save snapshot to json
func (s Snap) Save() {
Base := &s
output, err := json.MarshalIndent(Base, "", "\t")
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(snapfilepath+snapname, output, 0644)
if err != nil {
fmt.Println(err)
}
}
//Load snapshot to json
func (s *Snap) Load() {
item := *s
jsonFile, _ := ioutil.ReadFile(snapfilepath + snapname)
_ = json.Unmarshal([]byte(jsonFile), &item)
*s = item
}
//If we've just read a board, we want to switch that board from 'not checked' to 'checked'.
func (s *Snap) Switch(title, date string) {
S := *s
for index := range S.Title {
if S.Title[index] == title && S.Date[index] == date {
S.Checked[index] = true
}
}
*s = S
}
func (s Snap) Exists(title, date string) bool {
for index := range s.Title {
if s.Title[index] == title && s.Date[index] == date {
return true
}
}
return false
}
func (s Snap) Whatsnew() []string {
news := []string{}
for index := range s.Title {
for index2 := range bb.B {
if bb.B[index2].Title == s.Title[index] && bb.B[index2].Date == s.Date[index] && len(bb.B[index2].Contents) != s.Length[index] {
news = append(news, bb.B[index2].Title)
}
}
}
return news
}
///BB & BB METHODS////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//BB is the main struct holding all boards.
type BB struct {
B []Board
}
//Save a snapshot of the global BB obj to global Snap obj
func (b BB) saveSnapshot() Snap {
s := Snap{}
for index := range b.B {
s.Title = append(s.Title, b.B[index].Title)
s.Owner = append(s.Owner, b.B[index].Owner)
s.Length = append(s.Length, len(b.B[index].Contents))
s.Date = append(s.Date, b.B[index].Date)
s.Checked = append(s.Checked, false)
}
return s
}
//Load all the boards to BB
func (b *BB) Load() {
an.Load()
list := ufolderlist()
for index := range list {
b.collect(list[index])
}
b.anoncollect()
}
//Iterate through snapshot to check if something has changed (at BB level)
func (b BB) snapcheck(s Snap) bool {
for index := range b.B {
for index2 := range s.Title {
if b.B[index].Title == s.Title[index2] && b.B[index].Date == s.Date[index2] {
if len(b.B[index].Contents) != s.Length[index2] && !s.Checked[index2] {
return true
}
}
}
}
return false
}
func (b BB) loadpin(s Snap) []int {
indexlist := []int{}
for index := range b.B {
for indexp := range pin.Title {
if pin.Title[indexp] == b.B[index].Title && pin.Date[indexp] == b.B[index].Date {
indexlist = append(indexlist, index)
strindex := strconv.Itoa(index)
repeat := false
for index2 := range s.Title {
if b.B[index].Title == s.Title[index2] && len(b.B[index].Contents) != s.Length[index2] && !s.Checked[index2] {
color.Cyan(strindex + ") " + b.B[index].Title + " | author: " + b.B[index].Owner + " | " + b.B[index].Date + " **PINNED**")
repeat = true
break
}
if !s.Exists(b.B[index].Title, b.B[index].Date) {
color.Green(strindex + ") " + b.B[index].Title + " | author: " + b.B[index].Owner + " | " + b.B[index].Date + " **PINNED**")
repeat = true
break
}
}
if !repeat {
fmt.Println(strindex + ") " + b.B[index].Title + " | author: " + b.B[index].Owner + " | " + b.B[index].Date + " **PINNED**")
}
} else {
continue
}
}
}
fmt.Println("")
return indexlist
}
func checkindexlist(list []int, index int) bool {
for ix := range list {
if list[ix] == index {
return true
}
}
return false
}
//Load all contents of BB to the screen.
func (b BB) loadall(s Snap, searchstring string) {
header := pterm.DefaultHeader.WithBackgroundStyle(pterm.NewStyle(pterm.BgBlue))
change := bb.snapcheck(aa)
var truemin int
var truemax int
if change {
pterm.Println(header.Sprint("-- " + boardtitle + " -- [NEW]"))
} else {
pterm.Println(header.Sprint("-- " + boardtitle + " --"))
}
indexlist := b.loadpin(s)
if len(b.B) <= length {
minimum = 0
maximum = len(b.B)
} else {
minimum = len(b.B) - length
maximum = len(b.B)
}
if searchstring != "" {
truemin = 0
truemax = len(b.B)
} else {
truemin = minimum - back
if truemin < 0 {
truemin = 0
}
truemax = maximum - back
}
for index := len(b.B) - 1; index >= 0; index-- {
if index >= truemin && index <= truemax && !checkindexlist(indexlist, index) && b.B[index].Date != "" {
strindex := strconv.Itoa(index)
repeat := false
searching := false
for index2 := range s.Title {
if searchstring != "" {
if !strings.Contains(b.B[index].Title, searchstring) && !strings.Contains(b.B[index].Date, searchstring) {
searching = true
break
}
}
if b.B[index].Title == s.Title[index2] && len(b.B[index].Contents) != s.Length[index2] && !s.Checked[index2] {
color.Cyan(strindex + ") " + b.B[index].Title + " | author: " + b.B[index].Owner + " | " + b.B[index].Date)
repeat = true
break
}
if !s.Exists(b.B[index].Title, b.B[index].Date) {
color.Green(strindex + ") " + b.B[index].Title + " | author: " + b.B[index].Owner + " | " + b.B[index].Date)
repeat = true
break
}
}
if !repeat && !searching {
fmt.Println(strindex + ") " + b.B[index].Title + " | author: " + b.B[index].Owner + " | " + b.B[index].Date)
}
} else {
continue
}
}
if len(b.B) == 0 {
fmt.Println("there are no boards")
} else {
fmt.Println("")
fmt.Printf("BB Length: %d, range: %d to %d", len(b.B), truemin, truemax)
fmt.Printf("\n\n")
}
}
func (b BB) loadgem(ix, urlindex int) string {
var link string
for index := range b.B {
if index == ix {
for index2 := range b.B[index].Contents {
if index2 == urlindex {
if len(b.B[index].Contents[index2]) == 3 {
link = grabgeminiurl(b.B[index].Contents[index2][1])
break
}
}
}
}
}
return link
}
func (b BB) viewurl(ix int) bool {
change := bb.snapcheck(aa)
var real bool
var truemin int
var truemax int
for index := range b.B {
if index == ix && b.B[index].Owner != "" && b.B[index].Date != "" {
aa.Switch(b.B[index].Title, b.B[index].Date)
sort.Slice(b.B[index].Contents, func(i, j int) bool { return b.B[index].Contents[i][0] < b.B[index].Contents[j][0] })
real = true
ll.Title = b.B[index].Title
ll.Date = b.B[index].Date
ll.Save()
if !change {
pterm.DefaultSection.Println(b.B[index].Title + " | " + b.B[index].Owner + " ^new")
} else {
pterm.DefaultSection.Println(b.B[index].Title + " | " + b.B[index].Owner)
}
fmt.Println("")
if len(b.B[index].Contents) <= length {
minimum = 0
maximum = len(b.B[index].Contents)
} else {
minimum = len(b.B[index].Contents) - length
maximum = len(b.B[index].Contents)
}
truemin = minimum - back
if truemin < 0 {
truemin = 0
}
truemax = maximum - back
for index2 := range b.B[index].Contents {
if index2 >= truemin && index2 <= truemax {
urlindex := strconv.Itoa(index2)
if len(b.B[index].Contents[index2]) == 3 && grabgeminiurl(b.B[index].Contents[index2][1]) != "" {
if strings.Contains(b.B[index].Contents[index2][1], "@"+username) {
color.Cyan(urlindex + ") " + grabgeminiurl(b.B[index].Contents[index2][1]))
} else {
fmt.Println(urlindex + ") " + grabgeminiurl(b.B[index].Contents[index2][1]))
}
}
} else {
continue
}
}
}
}
if !real {
fmt.Println("invalid index")
return false
} else {
fmt.Println("")
fmt.Printf("Board length: %d, range: %d to %d", len(b.B[ix].Contents), truemin, truemax)
fmt.Printf("\n\n")
}
return true
}
//Load specific board up.
func (b *BB) loadboard(ix int, searchstring string) bool {
btp := &pterm.BasicTextPrinter{}
bgp := btp.WithStyle(&pterm.ThemeDefault.SuccessMessageStyle)
change := bb.snapcheck(aa)
var real bool
var truemin int
var truemax int
for index := range b.B {
if index == ix && b.B[index].Owner != "" && b.B[index].Date != "" {
aa.Switch(b.B[index].Title, b.B[index].Date)
sort.Slice(b.B[index].Contents, func(i, j int) bool { return b.B[index].Contents[i][0] < b.B[index].Contents[j][0] })
real = true
ll.Title = b.B[index].Title
ll.Date = b.B[index].Date
ll.Save()
if change {
pterm.DefaultSection.Println(b.B[index].Title + " | " + b.B[index].Owner + " ^new")
} else {
pterm.DefaultSection.Println(b.B[index].Title + " | " + b.B[index].Owner)
}
fmt.Println("")
if len(b.B[index].Contents) <= length {
minimum = 0
maximum = len(b.B[index].Contents)
} else {
minimum = len(b.B[index].Contents) - length
maximum = len(b.B[index].Contents)
}
if searchstring != "" {
truemin = 0
truemax = len(b.B[index].Contents)
} else {
truemin = minimum - back
if truemin < 0 {
truemin = 0
}
truemax = maximum - back
}
for index2 := range b.B[index].Contents {
if index2 >= truemin && index2 <= truemax {
if len(b.B[index].Contents[index2]) == 3 {
if searchstring != "" && !(strings.Contains(b.B[index].Contents[index2][1], searchstring)) && !(strings.Contains(b.B[index].Contents[index2][0], searchstring)) {
continue
}
if strings.Contains(b.B[index].Contents[index2][1], "@"+username) {
bgp.Println(b.B[index].Contents[index2][0] + " <" + b.B[index].Contents[index2][2] + "> " + b.B[index].Contents[index2][1])
} else {
btp.Println(b.B[index].Contents[index2][0] + " <" + b.B[index].Contents[index2][2] + "> " + b.B[index].Contents[index2][1])
}
}
} else {
continue
}
}
}
}
if !real {
fmt.Println("invalid index")
return false
} else {
fmt.Println("")
fmt.Printf("Board length: %d, range: %d to %d", len(b.B[ix].Contents), truemin, truemax)
fmt.Printf("\n\n")
}
return true
}
//Delete a board in BB. Only works if nobody else has viewed the board. Only sudo / root can delete regardless of this.
func (b *BB) delboard(i int) {
var real bool
for index := range b.B {
if index == i && b.B[index].Owner == username {
real = true
b.B[index].Delete(b.B[index].Title)
b.B = remove(b.B, index)
}
}
if !real {
index := strconv.Itoa(i)
fmt.Println("If index " + index + " exists, you are not owner of topic. Ask a mod to archive if needed.")
fmt.Println("Press ENTER to continue.")
fmt.Scanln()
}
}
func (b *BB) anoncollect() {
B := *b
for index := range B.B {
for index2 := range an.Title {
if B.B[index].Title == an.Title[index2] {
for index3 := range an.Board[index2].Contents {
B.B[index].Contents = append(B.B[index].Contents, an.Board[index2].Contents[index3])
}
}
}
}
*b = B
}
//Collect all data from every user to load into BB - making sure to order boards by date created.
func (b *BB) collect(homeuser string) {
ownercount := make(map[string][]string)
homepath := "/home/" + homeuser + "/.bb/"
B := *b
learnfolder, err := ioutil.ReadDir(homepath)
if err == nil {
for _, learnfile := range learnfolder {
Ex := filepath.Ext(learnfile.Name())
if Ex == ".json" {
x := Board{}
x.Load(homepath + learnfile.Name())
archiveboard := mm.Check(x)
if archiveboard {
continue
}
chk := false
for index := range B.B {
if B.B[index].Title == x.Title && B.B[index].Date == x.Date {
ownercount[x.Title] = append(ownercount[x.Title], x.Owner)
chk = true
for index2 := range x.Contents {
if !alreadyhas(B.B[index].Contents, x.Contents[index2][1]) {
B.B[index].Contents = add2slice(&B.B[index].Contents, x.Contents[index2])
}
}
}
}
for key, element := range ownercount {
if len(element) > 1 {
var users string
for index := range element {
if index != len(element) {
users += element[index] + ","
} else {
users += element[index]
}
}
fmt.Printf("Topic '%s' ownership has been tampered with %d times, potential owners are: %s\n", key, len(element)-1, users) //You could add a log here
fmt.Scanln()
}
}
if !chk {
B.B = append(B.B, x)
}
}
}
}
sort.SliceStable(B.B, func(i, j int) bool {
return B.B[i].Date < B.B[j].Date
})
*b = B
}
// BB ADD TO BOARD AND 'BOT' LOGIC - i.e BB can interpret input and add additional content / do actions on back of it.
func (b *BB) addtoboard(input, title, date string, anon bool) {
botindex := 0
for index := range b.B {
if b.B[index].Title == title && b.B[index].Date == date {
botindex = index
break
}