Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- One program from LBPHacker's RedStuff collection
- -- Chat client for RS Chat server (http://pastebin.com/8cgaw0Sm)
- -- For ComputerCraft 1.3 or above
- function trim(stringT)
- local i=1
- while (string.sub(stringT, i, i)==" ") do
- i=i+1
- end
- local startT=i
- i=string.len(stringT)
- while (string.sub(stringT, i, i)==" ") do
- i=i-1
- end
- local endT=i
- return string.sub(stringT, startT, endT)
- end
- function toboolean(sBool)
- if (string.find(sBool, "true")) then
- return true
- else
- return false
- end
- end
- function readLimited(limit, default, printx, printy)
- local input=default
- local inputsize=string.len(input)
- local entered=false
- local preconf=(string.len(default)>0)
- while (not entered) do
- term.setCursorPos(printx, printy)
- term.write("["..input..string.rep(" ", 16-string.len(input)).."]")
- event, arg1=os.pullEvent()
- if (event=="char" and inputsize<16 and string.byte(arg1)>31 and string.byte(arg1)<128) then
- input=input..arg1
- inputsize=inputsize+1
- end
- if (event=="key" and arg1==14 and inputsize>0) then
- if (preconf) then
- input=""
- inputsize=0
- preconf=false
- else
- input=string.sub(input, 1, string.len(input)-1)
- inputsize=inputsize-1
- end
- end
- if (event=="key" and arg1==28 and inputsize>0) then
- entered=true
- end
- end
- return input, inputsize
- end
- function refreshServerList(username)
- term.clear()
- term.setCursorPos(1, 3)
- term.write(string.rep("-", termWidth))
- term.setCursorPos(1, termHeight-2)
- term.write(string.rep("-", termWidth))
- term.setCursorPos(math.floor(termWidth/2)-11, math.floor(termHeight/2)-1)
- term.write("Searching for servers...")
- term.setCursorPos(math.floor(termWidth/2)-15, math.floor(termHeight/2))
- term.write("["..string.rep(" ", 30).."]")
- term.setCursorPos(math.floor(termWidth/2)-13, math.floor(termHeight/2)+1)
- term.write("Press X to close the program")
- term.setCursorPos(math.floor(termWidth/2)-13, math.floor(termHeight/2)+2)
- term.write("Username:"..string.rep(" ", 19-string.len(username))..username)
- local scanning=true
- local serverList={{}, {}}
- local serverCounter=0
- local timerCounter=0
- local timer=os.startTimer(0.1)
- rednet.broadcast("/rcsping")
- while (scanning) do
- event, arg1, arg2=os.pullEvent()
- term.setCursorPos(math.floor(termWidth/2)-14+timerCounter, math.floor(termHeight/2))
- term.write("#")
- if (event=="timer" and arg1==timer) then
- timerCounter=timerCounter+1
- if (timerCounter==30) then
- scanning=false
- else
- timer=os.startTimer(0.1)
- end
- end
- if (event=="rednet_message" and string.sub(arg2, 1, 8)=="/rcsresp") then
- serverCounter=serverCounter+1
- serverList[1][serverCounter]=arg1
- serverList[2][serverCounter]=string.sub(arg2, 10, string.len(arg2))
- end
- if (event=="char" and string.lower(arg1)=="x") then
- return -1
- end
- end
- sleep(0.1)
- return serverCounter, serverList
- end
- function chooseServer(serverCounter, serverList, default)
- term.clear()
- term.setCursorPos(math.floor(termWidth/2)-12, 1)
- term.write("Choose a server to connect")
- term.setCursorPos(math.floor(termWidth/2)-22, 2)
- term.write("Server list will be refreshed after 90 seconds")
- term.setCursorPos(1, 3)
- term.write(string.rep("-", termWidth))
- term.setCursorPos(1, termHeight-2)
- term.write(string.rep("-", termWidth))
- term.setCursorPos(math.floor(termWidth/2)-18, termHeight-1)
- term.write("Navigate with arrow keys - Exit with X")
- term.setCursorPos(math.floor(termWidth/2)-21, termHeight)
- term.write("Press Enter to connect, or R to refresh list")
- local selection=1
- local viewPoint=0
- local cursorUI=1
- local fieldsUI=termHeight-6
- for i=1, serverCounter do
- if (serverList[i]==default) then
- selection=i
- end
- end
- local entered=false
- local timer=os.startTimer(90)
- while (not entered) do
- if (serverCounter>0) then
- for i=1, fieldsUI do
- term.setCursorPos(5, i+3)
- term.write(serverList[2][i+viewPoint])
- term.setCursorPos(2, i+3)
- if (cursorUI==i) then
- term.write("[")
- else
- term.write(" ")
- end
- term.setCursorPos(termWidth-1, i+3)
- if (cursorUI==i) then
- term.write("]")
- else
- term.write(" ")
- end
- end
- else
- term.setCursorPos(math.floor(termWidth/2)-15, 4)
- term.write("No active servers found in range")
- end
- event, arg1=os.pullEvent()
- if (event=="key" and arg1==28 and serverCounter>0) then
- entered=true
- end
- if ((event=="key" and arg1==19) or (event=="timer" and timer==arg1)) then
- return 0
- end
- if (event=="char" and string.lower(arg1)=="x") then
- return -1
- end
- if (event=="key" and arg1==200 and selection>1) then
- selection=selection-1
- if (cursorUI==1) then
- viewPoint=viewPoint-1
- else
- cursorUI=cursorUI-1
- end
- end
- if (event=="key" and arg1==208 and selection<serverCounter) then
- selection=selection+1
- if (cursorUI==fieldsUI) then
- viewPoint=viewPoint+1
- else
- cursorUI=cursorUI+1
- end
- end
- end
- return selection
- end
- function addToMessageUI(messageUI, message)
- local lines={}
- for i=1, messageSize do
- lines[i]=""
- end
- local lineNum=1
- local lineSize=0
- local word=""
- for i=1, string.len(message) do
- if (string.sub(message, i, i)==" " or string.len(message)==i or lineSize==termWidth) then
- if (string.len(message)==i) then
- word=word..string.sub(message, i, i)
- end
- if (lineSize+string.len(word)+1>termWidth) then
- lineNum=lineNum+1
- lines[lineNum]=word
- lineSize=string.len(word)
- else
- lines[lineNum]=lines[lineNum].." "..word
- lineSize=lineSize+string.len(word)+1
- end
- word=""
- else
- word=word..string.sub(message, i, i)
- end
- end
- for i=messageSize, 1+lineNum, -1 do
- messageUI[i]=messageUI[i-lineNum]
- end
- for i=1, lineNum do
- messageUI[i]=string.sub(lines[i], 2, string.len(lines[i]))
- end
- return messageUI
- end
- termWidth, termHeight=term.getSize()
- local username, usernamesize, remember, dserver, serverCounter, serverList, cursorTS
- local config, selectedServer, selectedConnection, connClosed, messageTS, messageTSsize
- local exitprogram=false
- local messageUI={}
- messageSize=termHeight-4
- event=""
- arg1=0
- arg2=0
- if (fs.exists("rsccConfig")) then
- config=fs.open("rsccConfig", "r")
- username=config.readLine()
- usernamesize=string.len(username)
- dserver=config.readLine()
- config.close()
- else
- username=""
- usernamesize=0
- dserver=1
- end
- term.clear()
- term.setCursorPos(1, 1)
- local modemOnSide=0
- local modems=rs.getSides()
- for i=1, #modems do
- if (peripheral.isPresent(modems[i]) and peripheral.getType(modems[i])=="modem") then
- modemOnSide=i
- end
- end
- if (modemOnSide==0) then
- print("-- No modems found. Place a RedNet modem on one side of the computer, then restart this program.")
- print("Press Enter to exit")
- local key=0
- local event=""
- while (key~=28 or event~="key") do
- event, key=os.pullEvent()
- end
- term.clear()
- term.setCursorPos(1, 1)
- return 1
- end
- rednet.open(modems[modemOnSide])
- term.setCursorPos(math.floor(termWidth/2)-12, math.floor(termHeight/2)-2)
- term.write("RedStuff chat client v0.16")
- term.setCursorPos(math.floor(termWidth/2)-5, math.floor(termHeight/2)-1)
- term.write("by LBPHacker")
- term.setCursorPos(math.floor(termWidth/2)-16, math.floor(termHeight/2))
- term.write("Report bugs at lbphacker@gmail.com")
- term.setCursorPos(math.floor(termWidth/2)-4, math.floor(termHeight/2)+2)
- term.write("Username?:")
- term.setCursorPos(math.floor(termWidth/2)-8, math.floor(termHeight/2)+3)
- term.write("["..string.rep(" ", 16).."]")
- username, usernamesize=readLimited(16, username, math.floor(termWidth/2)-8, math.floor(termHeight/2)+3)
- while (not exitprogram) do
- selectedConnection=false
- while (not selectedConnection) do
- serverCounter, serverList=refreshServerList(username)
- if (serverCounter<0) then
- exitprogram=true
- selectedServer=0
- selectedConnection=true
- else
- selectedServer=chooseServer(serverCounter, serverList, dserver)
- if (selectedServer>0) then
- selectedConnection=true
- else
- if (selectedServer<0) then
- exitprogram=true
- selectedConnection=true
- end
- end
- end
- end
- if (selectedServer>0) then
- messageTS=""
- for i=1, messageSize do
- messageUI[i]=""
- end
- messageTSsize=0
- cursorTS=1
- connClosed=false
- rednet.send(serverList[1][selectedServer], username)
- term.setCursorBlink(true)
- while (not connClosed) do
- event, arg1, arg2=os.pullEvent()
- if (event=="rednet_message" and arg1==serverList[1][selectedServer]) then
- --if printable then
- messageUI=addToMessageUI(messageUI, arg2)
- --end
- end
- if (event=="char" and string.byte(arg1)>31 and string.byte(arg1)<128) then
- messageTS=messageTS..arg1
- messageTSsize=messageTSsize+1
- end
- if (event=="key" and arg1==14 and messageTSsize>0) then
- messageTS=string.sub(messageTS, 1, string.len(messageTS)-1)
- messageTSsize=messageTSsize-1
- end
- if (event=="key" and arg1==211) then
- messageTS=""
- messageTSsize=0
- end
- if (event=="key" and arg1==28 and messageTSsize>0) then
- messageTS=trim(messageTS)
- rednet.send(serverList[1][selectedServer], messageTS)
- if (messageTS=="/exit") then
- connClosed=true
- end
- messageTS=""
- messageTSsize=0
- end
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Server:"..string.rep(" ", termWidth-7-string.len(serverList[2][selectedServer]))..serverList[2][selectedServer])
- term.setCursorPos(1, 2)
- term.write(string.rep("-", termWidth))
- term.setCursorPos(1, termHeight-1)
- term.write(string.rep("-", termWidth))
- for i=1, messageSize do
- term.setCursorPos(1, i+2)
- term.write(messageUI[messageSize+1-i])
- end
- term.setCursorPos(1, termHeight)
- if (string.len(messageTS)>termWidth-1) then
- term.write(string.sub(messageTS, string.len(messageTS)-termWidth+2, string.len(messageTS)))
- cursorTS=termWidth
- else
- term.write(string.sub(messageTS, 1, string.len(messageTS)))
- cursorTS=string.len(messageTS)+1
- end
- term.setCursorPos(cursorTS, termHeight)
- end
- term.setCursorBlink(false)
- end
- end
- config=fs.open("rsccConfig", "w")
- config.writeLine(username)
- config.writeLine(dserver)
- config.close()
- rednet.close(modems[modemOnSide])
- term.clear()
- term.setCursorPos(1, 1)
Add Comment
Please, Sign In to add comment