-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient
More file actions
490 lines (445 loc) · 12.4 KB
/
Client
File metadata and controls
490 lines (445 loc) · 12.4 KB
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
--[[Changelog: 1.4: Added Notification sound.
1.5: Added Join/Leave message.
3.0 now on App Engine!
]]--
--Credit Jesusthekiller for color formatting
--Credit Mini for updating to latest cc
local split = function(str, pat) local t = {} local fpat = "(.-)"..pat local last_end = 1 local s, e, cap = str:find(fpat, 1) if not s then return {str} end while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end
local function jsh(str)
local hash = 0
local strn = string.len(str)
for i = 1, strn do
char = string.byte(string.sub(str, i,i))
hash = bit32.lshift(hash, 5) - hash + char
end
return hash
end
local tHex = {
[ colors.white ] = "0",
[ colors.orange ] = "1",
[ colors.magenta ] = "2",
[ colors.lightBlue ] = "3",
[ colors.yellow ] = "4",
[ colors.lime ] = "5",
[ colors.pink ] = "6",
[ colors.gray ] = "7",
[ colors.lightGray ] = "8",
[ colors.cyan ] = "9",
[ colors.purple ] = "a",
[ colors.blue ] = "b",
[ colors.brown ] = "c",
[ colors.green ] = "d",
[ colors.red ] = "e",
[ colors.black ] = "f",
}
local fHex = {}
for k,v in pairs(tHex) do
fHex[v] = k
end
isRestart=false
local channel = "main"
local trimColor=function(str)
str=string.gsub(str,"%#.","")
str=string.gsub(str,"%$.","")
return str
end
local history={}
local update,colorRead,play,clear,receive,send,processCommand,post,updateDisplay,getOld
local cprint,cwrite,toCode,cCode,clear,sb,st,sc
function sc(x, y)
term.setCursorPos(x, y)
end
function clear(move)
sb(colors.black)
term.clear()
if move ~= false then sc(1,1) end
end
function sb(color)
term.setBackgroundColor(color)
end
function st(color)
term.setTextColor(color)
end
function cCode(h)
if term.isColor() and term.isColor then
return (fHex[h] or 0)
else
if h == "f" then
return colors.black
else
return colors.white
end
end
end
function toCode(n)
return string.format('%x', n)
end
function cwrite(text)
text = tostring(text)
local i = 0
while true do
i = i + 1
if i > #text then break end
local c = text:sub(i, i)
if c == "\\" then
if text:sub(i+1, i+1) == "&" then
write("&")
i = i + 1
elseif text:sub(i+1, i+1) == "$" then
write("$")
i = i + 1
else
write(c)
end
elseif c == "#" then
st(cCode(text:sub(i+1, i+1)))
i = i + 1
elseif c == "$" then
sb(cCode(text:sub(i+1, i+1)))
i = i + 1
else
write(c)
end
end
return
end
function cprint(text)
return cwrite(tostring(text).."\n")
end
function colorRead( _sReplaceChar, _tHistory )
term.setCursorBlink( true )
local sLine = ""
local nHistoryPos = nil
local nPos = 0
if _sReplaceChar then
_sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
end
local w, h = term.getSize()
local sx, sy = term.getCursorPos()
local function redraw( _sCustomReplaceChar )
local nScroll = 0
if sx + nPos >= w then
nScroll = (sx + nPos) - w
end
term.setCursorPos( sx, sy )
local sReplace = _sCustomReplaceChar or _sReplaceChar
if sReplace then
cwrite( string.rep(sReplace, string.len(sLine) - nScroll) )
else
cwrite( string.sub( sLine, nScroll + 1 ) )
end
term.setCursorPos( sx + nPos - (#sLine-#trimColor(sLine)) - nScroll, sy )
end
while true do
local sEvent, param = os.pullEvent()
if sEvent == "char" then
sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
nPos = nPos + 1
redraw()
elseif sEvent == "key" then
if param == keys.enter then
-- Enter
break
elseif param == keys.left then
-- Left
if nPos > #sLine-#trimColor(sLine) then
nPos = nPos - 1
redraw()
end
elseif param == keys.right then
-- Right
if nPos < #sLine then
nPos = nPos + 1
redraw()
end
elseif param == keys.up or param == keys.down then
-- Up or down
if _tHistory then
redraw(" ");
if param == keys.up then
-- Up
if nHistoryPos == nil then
if #_tHistory > 0 then
nHistoryPos = #_tHistory
end
elseif nHistoryPos > 1 then
nHistoryPos = nHistoryPos - 1
end
else
-- Down
if nHistoryPos == #_tHistory then
nHistoryPos = nil
elseif nHistoryPos ~= nil then
nHistoryPos = nHistoryPos + 1
end
end
if nHistoryPos then
sLine = _tHistory[nHistoryPos]
nPos = string.len( sLine )
else
sLine = ""
nPos = 0
end
redraw()
end
elseif param == keys.backspace then
-- Backspace
if nPos > 0 then
redraw(" ");
sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
nPos = nPos - 1
redraw()
end
elseif param == keys.home then
-- Home
nPos = 0
redraw()
elseif param == keys.delete then
if nPos < string.len(sLine) then
redraw(" ");
sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
redraw()
end
elseif param == keys["end"] then
-- End
nPos = string.len(sLine)
redraw()
end
end
end
term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()
return sLine
end
local postLink,getLink
postLink="http://http-chat-serv.appspot.com/post"
getLink="http://http-chat-serv.appspot.com/get"
updateLink = "https://raw.github.com/MudkipTheEpic/chatHTTP/master/Client"
currentProgram = shell.getRunningProgram()
args = {...}
name = args[1]
if args[2] then
channel = args[2]
end
screenX, screenY = term.getSize()
screenY = screenY-2
version = "3.0"
data = {}
if not fs.exists("/.chatData") then
fs.makeDir("/.chatData")
end
if fs.exists("/.chatData/data") then
file = fs.open("/.chatData/data", "r")
data.file = file.readAll()
password = data.file
file.close()
else
password = "guest"
end
local function centerWrite(text, ny)
if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
else
local x, y = term.getCursorPos()
local w, h = term.getSize()
term.setCursorPos(w/2 - text:len()/2 + (#text % 2 == 0 and 1 or 0), ny or y)
cwrite(text)
end
end
function updateDisplay(txt)
term.setCursorPos(1, screenY)
term.clearLine()
centerWrite(txt)
end
local post=function(txt,txt2) txt=txt2 or txt return http.get(postLink,{usr = trimColor(name), data = txt, chan = channel, pass=password}) end
local cmdpost=function(txt,txt2) txt=txt2 or txt return http.get(postLink,{usr = trimColor(name), cmd=txt, pass=password}) end
local commandTable={
update=function() return update() end;
version=function()
return updateDisplay("Version: "..version)
end;
exit=function(...) local l if #{...}>0 then l=" ["..table.concat({...}," ").."]" else l="" end post(name.."#0$f left the room."..l) return error(nil,3) end;
me=function(...)
local str=table.concat({...}," ")
return post("*"..name.."#0$f "..str)
end;
nick=function(newNick)
if not newNick or newNick=="" then updateDisplay("Please supply a nickname.") return nil end
post(name.."#0$f has changed nick to "..newNick)
name=newNick
return newNick
end;
ping=function()
updateDisplay(split(cmdpost("ping").readAll(), "\n")[2])
end;
server=function(...)
local args = {...}
sendstr = table.concat(args, " ")
return updateDisplay(split(cmdpost(sendstr).readAll(), "\n")[2])
end;
swapServ=function(...)
local args = {...}
local sendstr = "mvserv "..table.concat(args, " ")
return updateDisplay(split(cmdpost(sendstr).readAll(), "\n")[2])
end;
swapChannel=function(channel)
local sendstr = "chanc "..channel
return updateDisplay(split(cmdpost(sendstr).readAll(), "\n")[2])
end;
setPass=function(pass)
password=pass
local fh = fs.open(".chatData/data", "w")
fh.write(jsh(pass))
fh.close()
end;
}
ServCmdTbl = {
mvserv = function(NewServ)
post(name.."#0$f has left this server")
postLink="http://"..NewServ[3]
getLink="http://"..NewServ[4]
post(name.."#0$f has joined this server")
end;
cc = function(ChannelData)
post(name.."#0$f has left this channel")
channel = ChannelData[3]
post(name.."#0$f has joined this channel")
end;
}
function ProcServCmd(str)
local data2 = split(str, "\n")
str2 = data2[#data2]
if str2:sub(1,10) == "servercmd;" then
local strT = split(str2, ";")
for k,v in pairs(ServCmdTbl) do
if "servercmd;"..k == strT[1]..";"..strT[2] then
cmd = strT[2]
ServCmdTbl[cmd](strT)
return false
end
end
return true
end
return true
end
function processCommand(str)
if str:sub(1,1)=="/" then
local strT=split(str," ")
for k,v in pairs(commandTable) do
if "/"..k==strT[1] then
return commandTable[strT[1]:sub(2)]
end
end
return 4
end
return false
end
--cake
function update()
updateContent = http.get(updateLink)
updateContent = updateContent.readAll()
file = fs.open(currentProgram, "r")
fileContent = file.readAll()
file.close()
if fileContent == updateContent then
term.setCursorPos(1, screenY)
write(currentProgram.." is up to date!")
else
term.setCursorPos(1, screenY)
write("New update available! Installing now...")
sleep(1)
file = fs.open(currentProgram, "w")
file.write(updateContent)
file.close()
term.setCursorPos(1, screenY)
term.clearLine()
write("Done! Restarting "..currentProgram.." now!")
isRestart=true
commandTable["exit"]("Updating client...")
end
end
--test
function play()
redstone.setOutput("back", true)
sleep(0.1)
redstone.setOutput("back", false)
sleep(0.3)
redstone.setOutput("back", true)
sleep(0.1)
redstone.setOutput("back",false)
sleep(0.05)
redstone.setOutput("back",true)
sleep(0.1)
redstone.setOutput("back",false)
end
function clearY(fromY, toY)
repeat
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(1, fromY)
term.clearLine()
fromY = fromY+1
until fromY >= toY
end
function receive()
while true do
get = http.get(getLink, {usr= name, slines=10, chan = channel, pass=password})
get = get.readAll()
get = string.gsub(get,"\\(.)","%1")
if get ~= getOld then
dop = ProcServCmd(get)
if dop == true then
xOld, yOld = term.getCursorPos()
clearY(1, screenY)
term.setCursorPos(1,1)
cprint(get)
end
getOld = get
term.setCursorPos(xOld, yOld)
play()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
sleep(1.25)
end
end
function send()
while true do
term.setCursorPos(1, screenY+1)
cwrite(name..": ")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
input = colorRead(nil,history)
table.insert(history,input)
l=processCommand(input)
if l==4 then updateDisplay("Unknown command.")
elseif l then l(select(2,unpack(split(input," "))))
else
if input ~= "" then
post(name.."#0$f: "..input)
end
end
clearY(screenY+1, screenY+2)
end
end
term.clear()
term.setCursorPos(1, screenY+1)
if not name then
write("Please enter a name: ")
nameInput = colorRead()
if nameInput == "" or nameInput == nil then
name = "User"..tostring(math.random(1, 999)
)else
name = nameInput
end
end
post(name.."#0$f joined the room!")
clearY(screenY+1, screenY+3)
write("/exit to exit, /update to update.")
get = http.get(getLink, {usr = name, slines=10, chan = channel})
get = get.readAll()
get = getOld
local ok,err=pcall(parallel.waitForAny,send, receive)
if isRestart then return shell.run(currentProgram,name)
elseif err=="Terminated" then post(name.."#0$f has left the room. [Terminated]")
end