-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
310 lines (262 loc) · 5.34 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"sync"
ccbot "github.com/ciphercord/gophercord/bot"
ccmsg "github.com/ciphercord/gophercord/message"
"github.com/eiannone/keyboard"
"golang.org/x/term"
)
const (
ps1 string = ":"
)
var (
state *term.State
wg sync.WaitGroup
key, room, name string = "MyPrivateKey", "CipherCord", "NoNickname"
input []byte
cursorPos int = 0
)
func main() {
flag.StringVar(&key, "key", key, "The encryption key")
flag.StringVar(&room, "room", room, "The message space")
flag.StringVar(&name, "name", name, "The nickname")
flag.Usage = func() {
fmt.Println("Usage: ciphercord-cli [options...]")
fmt.Println(" -key The encryption key (Default: MyPrivateKey)")
fmt.Println(" -room The message space (Default: CipherCord)")
fmt.Println(" -name The nickname (Default: NoNickname)")
}
flag.Parse()
if err := keyboard.Open(); err != nil {
fmt.Println(err)
return
}
defer keyboard.Close()
if err := ccbot.Init(); err != nil {
fmt.Println(err)
return
}
var err error
state, err = term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
fmt.Println(err)
return
}
defer term.Restore(int(os.Stdin.Fd()), state)
go receiving()
wg.Add(1)
go chatBar()
wg.Wait()
}
func read() {
read:
for {
prompt()
// FIXME: this is horribly broken (half of the keys on the keyboard dont register). someone choose a different package.
// also why r == 0 is space (because its broken)
r, key, err := keyboard.GetKey()
if err != nil {
clsl()
println(err)
}
switch key {
case '\r':
break read
case keyboard.KeyArrowDown, keyboard.KeyArrowUp:
case keyboard.KeyArrowLeft:
if cursorPos > 0 {
cursorPos--
}
case keyboard.KeyArrowRight:
if cursorPos < len(input) {
cursorPos++
}
case keyboard.KeyBackspace, keyboard.KeyBackspace2:
if len(input) > 0 && cursorPos > 0 {
var inputBuf []byte
for i, r := range input {
if i != cursorPos-1 {
inputBuf = append(inputBuf, r)
}
}
input = inputBuf
cursorPos--
}
case keyboard.KeyDelete:
if len(input) > 0 {
var inputBuf []byte
for i, r := range input {
if i != cursorPos {
inputBuf = append(inputBuf, r)
}
}
input = inputBuf
}
case keyboard.KeyCtrlA:
cursorPos = 0
case keyboard.KeyCtrlE:
cursorPos = len(input)
case keyboard.KeyCtrlU:
var cursorPosBuf = cursorPos
var inputBuf []byte
for i, r := range input {
if i >= cursorPosBuf {
inputBuf = append(inputBuf, r)
} else {
cursorPos--
}
}
input = inputBuf
case keyboard.KeyCtrlK:
var inputBuf []byte
for i, r := range input {
if i < cursorPos {
inputBuf = append(inputBuf, r)
}
}
input = inputBuf
case keyboard.KeyCtrlW:
var from int
var to int = cursorPos
from = to
if cursorPos == 0 {
continue
}
for {
from--
if from == 0 || input[from-1] == ' ' {
break
}
}
var inputBuf []byte
for i, r := range input {
if i < from || i >= to {
inputBuf = append(inputBuf, r)
}
}
input = inputBuf
cursorPos = from
case keyboard.KeyCtrlL:
fmt.Print("\x1b[2J")
fmt.Print("\x1b[H")
case keyboard.KeyCtrlC, keyboard.KeyCtrlD:
exit()
default:
if key == keyboard.KeySpace || r == 0 {
r = ' '
}
inputBuf := append(input, 0)
copy(inputBuf[cursorPos+1:], inputBuf[cursorPos:])
inputBuf[cursorPos] = byte(r)
input = inputBuf
cursorPos++
}
}
}
func prompt() {
clsl()
fmt.Print(ps1)
fmt.Print(string(input))
move(cursorPos + len(ps1) + 1)
}
func chatBar() {
defer wg.Done()
for {
read()
if len(input) == 0 {
continue
}
s := string(input)
input = []byte{}
cursorPos = 0
prompt()
if strings.HasPrefix(s, "/") {
command(s)
continue
}
var umsg ccmsg.UnencryptedMessage
umsg.Key = key
umsg.Room = room
umsg.Content = s
umsg.Author = name
data, err := ccmsg.Package(umsg)
if err != nil {
clsl()
println(err)
prompt()
continue
}
if err := ccbot.Send(data); err != nil {
clsl()
println(err)
prompt()
continue
}
}
}
func command(s string) {
if strings.HasPrefix(s, "/help") {
clsl()
println("/help Show this menu")
println("/name <name> Set your nickname")
println("/room <room> Move rooms")
println("/key <key> Change encryption key")
println("/exit Quit the program")
} else if str, found := strings.CutPrefix(s, "/name "); found {
name = str
} else if str, found := strings.CutPrefix(s, "/room "); found {
room = str
} else if str, found := strings.CutPrefix(s, "/key "); found {
key = str
} else if strings.HasPrefix(s, "/exit") {
exit()
} else {
clsl()
println("Unknown command.")
}
}
func receiving() {
for {
data := <-ccbot.Messages
emsg, err := ccmsg.Decode(data)
if err != nil {
clsl()
println(err)
prompt()
continue
}
if emsg.Room != room {
continue
}
umsg, err := ccmsg.DecryptMessage(emsg, key)
if err == ccmsg.ErrUnmatched {
continue
} else if err != nil {
clsl()
println(err)
prompt()
continue
}
clsl()
fmt.Printf("%s: %s\r\n", umsg.Author, umsg.Content)
prompt()
}
}
func exit() {
// FIXME: Make this better:
wg.Done()
}
func clsl() { // clear line
fmt.Print("\033[2K\r")
}
func println(a ...any) {
fmt.Print(a...)
fmt.Print("\r\n")
}
func move(i int) {
fmt.Printf("\033[%dG", i)
}