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
|
package ctfc
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"gitlab.com/Syndamia/ctfc/go-src/ui"
)
func StartupWindow(...string) {
clearScreen()
ui.NormalBox(true, "Login", "Register")
input := ui.InputField("[L/R]")
command := strings.ToLower(input)
if command == "l" {
defer loginWindow()
} else if command == "r" {
defer registerWindow()
} else {
defer showError(invalidCommand, StartupWindow)
}
}
func loginWindow(values ...string) {
clearScreen()
ui.NormalBox(true, "Login")
var input string
switch len(values) {
case 0:
input = ui.InputField("Username")
case 1:
ui.InputFieldFilled("Username", values[0])
input = ui.InputField("Password")
}
if len(values) == 0 {
defer loginWindow(input)
} else if logInUser(values[0], input) {
defer chatsWindow()
} else {
defer showError(invalidCredentials, loginWindow, values...)
}
}
func registerWindow(values ...string) {
clearScreen()
ui.NormalBox(true, "Register")
var inputidationF func(string) bool
var fieldName, specification string
switch len(values) {
case 0:
inputidationF, fieldName, specification = stringValidUsername, "Username", "[A-z, 0-9, *, ., _, -; at least 1 letter]"
case 1:
ui.InputFieldFilled("Username", values[0])
inputidationF, fieldName, specification = stringValidPassword, "Password", "[5-40 characters; at least 1 number]"
case 2:
ui.InputFieldFilled("Username", values[0])
ui.InputFieldFilled("Password", values[1])
inputidationF, fieldName, specification = stringValidName, "Name", "[2-60 characters]"
}
input := ui.InputField(fieldName + " " + specification)
if inputidationF(input) {
values = append(values, input)
// When user has entered all required values
if len(values) == 3 {
createUser(values)
logInUser(values[0], values[1])
defer chatsWindow()
} else { // When there are still values to be given
defer registerWindow(values...)
}
} else {
defer showError(invalidValueFor(fieldName), registerWindow, values...)
}
}
func chatsWindow(values ...string) {
clearScreen()
ui.NormalBox(true, "Direct Messages", "Account", "Logout")
ui.PageField(0, 0)
ui.EmptyLine()
if len(values) > 0 {
if values[len(values)-1] == "ShowHelp" {
ui.TextField("Chats page options: [(D)irect messages/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(C) for create chat/(name) for go to chat room by name/(number) for go to chat room by number/(H)elp]")
}
}
input := ui.InputField("[D/A/L/</>/C/E/name/number/H]")
handled := handleInputActions(input, true,
inputAction{"H", chatsWindow, []string{"ShowHelp"}},
)
if !handled {
defer showError(invalidCommand, chatsWindow)
}
}
type inputAction struct {
inputue string
execute func(...string)
args []string
}
func handleInputActions(input string, handleNav bool, ia ...inputAction) bool {
if handleNav {
ia = append(ia,
inputAction{"C", chatsWindow, nil},
inputAction{"D", chatsWindow, nil},
inputAction{"A", chatsWindow, nil},
inputAction{"L", chatsWindow, nil},
)
}
for _, v := range ia {
if input == v.inputue {
defer v.execute(v.args...)
return true
}
}
return false
}
var clearNext = true
func showError(message string, callback func(...string), callbackData ...string) {
clearScreen()
ui.ErrorBox(message)
clearNext = false
defer callback(callbackData...)
}
func clearScreen() {
if !clearNext {
clearNext = true
return
}
if runtime.GOOS == "windows" {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
} else {
fmt.Println("\033[2J")
}
}
|