LBPHacker

RedStuff chat client v0.16 by LBPHacker

Sep 19th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.21 KB | None | 0 0
  1. -- One program from LBPHacker's RedStuff collection
  2. -- Chat client for RS Chat server (http://pastebin.com/8cgaw0Sm)
  3. -- For ComputerCraft 1.3 or above
  4.  
  5. function trim(stringT)
  6.     local i=1
  7.     while (string.sub(stringT, i, i)==" ") do
  8.         i=i+1
  9.     end
  10.     local startT=i
  11.     i=string.len(stringT)
  12.     while (string.sub(stringT, i, i)==" ") do
  13.         i=i-1
  14.     end
  15.     local endT=i
  16.     return string.sub(stringT, startT, endT)
  17. end
  18.  
  19. function toboolean(sBool)
  20.     if (string.find(sBool, "true")) then
  21.         return true
  22.     else
  23.         return false
  24.     end
  25. end
  26.  
  27. function readLimited(limit, default, printx, printy)
  28.     local input=default
  29.     local inputsize=string.len(input)
  30.     local entered=false
  31.     local preconf=(string.len(default)>0)
  32.     while (not entered) do
  33.         term.setCursorPos(printx, printy)
  34.         term.write("["..input..string.rep(" ", 16-string.len(input)).."]")
  35.         event, arg1=os.pullEvent()
  36.         if (event=="char" and inputsize<16 and string.byte(arg1)>31 and string.byte(arg1)<128) then
  37.             input=input..arg1
  38.             inputsize=inputsize+1
  39.         end
  40.         if (event=="key" and arg1==14 and inputsize>0) then
  41.             if (preconf) then
  42.                 input=""
  43.                 inputsize=0
  44.                 preconf=false
  45.             else
  46.                 input=string.sub(input, 1, string.len(input)-1)
  47.                 inputsize=inputsize-1
  48.             end
  49.         end
  50.         if (event=="key" and arg1==28 and inputsize>0) then
  51.             entered=true
  52.         end
  53.     end
  54.     return input, inputsize
  55. end
  56.  
  57. function refreshServerList(username)
  58.     term.clear()
  59.     term.setCursorPos(1, 3)
  60.     term.write(string.rep("-", termWidth))
  61.     term.setCursorPos(1, termHeight-2)
  62.     term.write(string.rep("-", termWidth))
  63.     term.setCursorPos(math.floor(termWidth/2)-11, math.floor(termHeight/2)-1)
  64.     term.write("Searching for servers...")
  65.     term.setCursorPos(math.floor(termWidth/2)-15, math.floor(termHeight/2))
  66.     term.write("["..string.rep(" ", 30).."]")
  67.     term.setCursorPos(math.floor(termWidth/2)-13, math.floor(termHeight/2)+1)
  68.     term.write("Press X to close the program")
  69.     term.setCursorPos(math.floor(termWidth/2)-13, math.floor(termHeight/2)+2)
  70.     term.write("Username:"..string.rep(" ", 19-string.len(username))..username)
  71.     local scanning=true
  72.     local serverList={{}, {}}
  73.     local serverCounter=0
  74.     local timerCounter=0
  75.     local timer=os.startTimer(0.1)
  76.     rednet.broadcast("/rcsping")
  77.     while (scanning) do
  78.         event, arg1, arg2=os.pullEvent()
  79.         term.setCursorPos(math.floor(termWidth/2)-14+timerCounter, math.floor(termHeight/2))
  80.         term.write("#")
  81.         if (event=="timer" and arg1==timer) then
  82.             timerCounter=timerCounter+1
  83.             if (timerCounter==30) then
  84.                 scanning=false
  85.             else
  86.                 timer=os.startTimer(0.1)
  87.             end
  88.         end
  89.         if (event=="rednet_message" and string.sub(arg2, 1, 8)=="/rcsresp") then
  90.             serverCounter=serverCounter+1
  91.             serverList[1][serverCounter]=arg1
  92.             serverList[2][serverCounter]=string.sub(arg2, 10, string.len(arg2))
  93.         end
  94.         if (event=="char" and string.lower(arg1)=="x") then
  95.             return -1
  96.         end
  97.     end
  98.     sleep(0.1)
  99.     return serverCounter, serverList
  100. end
  101.  
  102. function chooseServer(serverCounter, serverList, default)
  103.     term.clear()
  104.     term.setCursorPos(math.floor(termWidth/2)-12, 1)
  105.     term.write("Choose a server to connect")
  106.     term.setCursorPos(math.floor(termWidth/2)-22, 2)
  107.     term.write("Server list will be refreshed after 90 seconds")
  108.     term.setCursorPos(1, 3)
  109.     term.write(string.rep("-", termWidth))
  110.     term.setCursorPos(1, termHeight-2)
  111.     term.write(string.rep("-", termWidth))
  112.     term.setCursorPos(math.floor(termWidth/2)-18, termHeight-1)
  113.     term.write("Navigate with arrow keys - Exit with X")
  114.     term.setCursorPos(math.floor(termWidth/2)-21, termHeight)
  115.     term.write("Press Enter to connect, or R to refresh list")
  116.     local selection=1
  117.     local viewPoint=0
  118.     local cursorUI=1
  119.     local fieldsUI=termHeight-6
  120.     for i=1, serverCounter do
  121.         if (serverList[i]==default) then
  122.             selection=i
  123.         end
  124.     end
  125.     local entered=false
  126.     local timer=os.startTimer(90)
  127.     while (not entered) do
  128.         if (serverCounter>0) then
  129.             for i=1, fieldsUI do
  130.                 term.setCursorPos(5, i+3)
  131.                 term.write(serverList[2][i+viewPoint])
  132.                 term.setCursorPos(2, i+3)
  133.                 if (cursorUI==i) then
  134.                     term.write("[")
  135.                 else
  136.                     term.write(" ")
  137.                 end
  138.                 term.setCursorPos(termWidth-1, i+3)
  139.                 if (cursorUI==i) then
  140.                     term.write("]")
  141.                 else
  142.                     term.write(" ")
  143.                 end
  144.             end
  145.         else
  146.             term.setCursorPos(math.floor(termWidth/2)-15, 4)
  147.             term.write("No active servers found in range")
  148.         end
  149.         event, arg1=os.pullEvent()
  150.         if (event=="key" and arg1==28 and serverCounter>0) then
  151.             entered=true
  152.         end
  153.         if ((event=="key" and arg1==19) or (event=="timer" and timer==arg1)) then
  154.             return 0
  155.         end
  156.         if (event=="char" and string.lower(arg1)=="x") then
  157.             return -1
  158.         end
  159.         if (event=="key" and arg1==200 and selection>1) then
  160.             selection=selection-1
  161.             if (cursorUI==1) then
  162.                 viewPoint=viewPoint-1
  163.             else
  164.                 cursorUI=cursorUI-1
  165.             end
  166.         end
  167.         if (event=="key" and arg1==208 and selection<serverCounter) then
  168.             selection=selection+1
  169.             if (cursorUI==fieldsUI) then
  170.                 viewPoint=viewPoint+1
  171.             else
  172.                 cursorUI=cursorUI+1
  173.             end
  174.         end
  175.     end
  176.     return selection
  177. end
  178.  
  179. function addToMessageUI(messageUI, message)
  180.     local lines={}
  181.     for i=1, messageSize do
  182.         lines[i]=""
  183.     end
  184.     local lineNum=1
  185.     local lineSize=0
  186.     local word=""
  187.     for i=1, string.len(message) do
  188.         if (string.sub(message, i, i)==" " or string.len(message)==i or lineSize==termWidth) then
  189.             if (string.len(message)==i) then
  190.                 word=word..string.sub(message, i, i)
  191.             end
  192.             if (lineSize+string.len(word)+1>termWidth) then
  193.                 lineNum=lineNum+1
  194.                 lines[lineNum]=word
  195.                 lineSize=string.len(word)
  196.             else
  197.                 lines[lineNum]=lines[lineNum].." "..word
  198.                 lineSize=lineSize+string.len(word)+1
  199.             end
  200.             word=""
  201.         else
  202.             word=word..string.sub(message, i, i)
  203.         end
  204.     end
  205.     for i=messageSize, 1+lineNum, -1 do
  206.         messageUI[i]=messageUI[i-lineNum]
  207.     end
  208.     for i=1, lineNum do
  209.         messageUI[i]=string.sub(lines[i], 2, string.len(lines[i]))
  210.     end
  211.     return messageUI
  212. end
  213.  
  214. termWidth, termHeight=term.getSize()
  215. local username, usernamesize, remember, dserver, serverCounter, serverList, cursorTS
  216. local config, selectedServer, selectedConnection, connClosed, messageTS, messageTSsize
  217. local exitprogram=false
  218. local messageUI={}
  219. messageSize=termHeight-4
  220. event=""
  221. arg1=0
  222. arg2=0
  223.  
  224. if (fs.exists("rsccConfig")) then
  225.     config=fs.open("rsccConfig", "r")
  226.     username=config.readLine()
  227.     usernamesize=string.len(username)
  228.     dserver=config.readLine()
  229.     config.close()
  230. else
  231.     username=""
  232.     usernamesize=0
  233.     dserver=1
  234. end
  235.  
  236. term.clear()
  237. term.setCursorPos(1, 1)
  238. local modemOnSide=0
  239. local modems=rs.getSides()
  240. for i=1, #modems do
  241.     if (peripheral.isPresent(modems[i]) and peripheral.getType(modems[i])=="modem") then
  242.         modemOnSide=i
  243.     end
  244. end
  245. if (modemOnSide==0) then
  246.     print("-- No modems found. Place a RedNet modem on one side of the computer, then restart this program.")
  247.     print("Press Enter to exit")
  248.     local key=0
  249.     local event=""
  250.     while (key~=28 or event~="key") do
  251.         event, key=os.pullEvent()
  252.     end
  253.     term.clear()
  254.     term.setCursorPos(1, 1)
  255.     return 1
  256. end
  257. rednet.open(modems[modemOnSide])
  258.  
  259. term.setCursorPos(math.floor(termWidth/2)-12, math.floor(termHeight/2)-2)
  260. term.write("RedStuff chat client v0.16")
  261. term.setCursorPos(math.floor(termWidth/2)-5, math.floor(termHeight/2)-1)
  262. term.write("by LBPHacker")
  263. term.setCursorPos(math.floor(termWidth/2)-16, math.floor(termHeight/2))
  264. term.write("Report bugs at lbphacker@gmail.com")
  265. term.setCursorPos(math.floor(termWidth/2)-4, math.floor(termHeight/2)+2)
  266. term.write("Username?:")
  267. term.setCursorPos(math.floor(termWidth/2)-8, math.floor(termHeight/2)+3)
  268. term.write("["..string.rep(" ", 16).."]")
  269. username, usernamesize=readLimited(16, username, math.floor(termWidth/2)-8, math.floor(termHeight/2)+3)
  270.  
  271. while (not exitprogram) do
  272.     selectedConnection=false
  273.     while (not selectedConnection) do
  274.         serverCounter, serverList=refreshServerList(username)
  275.         if (serverCounter<0) then
  276.             exitprogram=true
  277.             selectedServer=0
  278.             selectedConnection=true
  279.         else
  280.             selectedServer=chooseServer(serverCounter, serverList, dserver)
  281.             if (selectedServer>0) then
  282.                 selectedConnection=true
  283.             else
  284.                 if (selectedServer<0) then
  285.                     exitprogram=true
  286.                     selectedConnection=true
  287.                 end
  288.             end
  289.         end
  290.     end
  291.     if (selectedServer>0) then
  292.         messageTS=""
  293.         for i=1, messageSize do
  294.             messageUI[i]=""
  295.         end
  296.         messageTSsize=0
  297.         cursorTS=1
  298.         connClosed=false
  299.         rednet.send(serverList[1][selectedServer], username)
  300.         term.setCursorBlink(true)
  301.         while (not connClosed) do
  302.             event, arg1, arg2=os.pullEvent()
  303.             if (event=="rednet_message" and arg1==serverList[1][selectedServer]) then
  304.                 --if printable then
  305.                     messageUI=addToMessageUI(messageUI, arg2)
  306.                 --end
  307.             end
  308.             if (event=="char" and string.byte(arg1)>31 and string.byte(arg1)<128) then
  309.                 messageTS=messageTS..arg1
  310.                 messageTSsize=messageTSsize+1
  311.             end
  312.             if (event=="key" and arg1==14 and messageTSsize>0) then
  313.                 messageTS=string.sub(messageTS, 1, string.len(messageTS)-1)
  314.                 messageTSsize=messageTSsize-1
  315.             end
  316.             if (event=="key" and arg1==211) then
  317.                 messageTS=""
  318.                 messageTSsize=0
  319.             end
  320.             if (event=="key" and arg1==28 and messageTSsize>0) then
  321.                 messageTS=trim(messageTS)
  322.                 rednet.send(serverList[1][selectedServer], messageTS)
  323.                 if (messageTS=="/exit") then
  324.                     connClosed=true
  325.                 end
  326.                 messageTS=""
  327.                 messageTSsize=0
  328.             end
  329.             term.clear()
  330.             term.setCursorPos(1, 1)
  331.             term.write("Server:"..string.rep(" ", termWidth-7-string.len(serverList[2][selectedServer]))..serverList[2][selectedServer])
  332.             term.setCursorPos(1, 2)
  333.             term.write(string.rep("-", termWidth))
  334.             term.setCursorPos(1, termHeight-1)
  335.             term.write(string.rep("-", termWidth))
  336.             for i=1, messageSize do
  337.                 term.setCursorPos(1, i+2)
  338.                 term.write(messageUI[messageSize+1-i])
  339.             end
  340.             term.setCursorPos(1, termHeight)
  341.             if (string.len(messageTS)>termWidth-1) then
  342.                 term.write(string.sub(messageTS, string.len(messageTS)-termWidth+2, string.len(messageTS)))
  343.                 cursorTS=termWidth
  344.             else
  345.                 term.write(string.sub(messageTS, 1, string.len(messageTS)))
  346.                 cursorTS=string.len(messageTS)+1
  347.             end
  348.             term.setCursorPos(cursorTS, termHeight)
  349.         end
  350.         term.setCursorBlink(false)
  351.     end
  352. end
  353.  
  354. config=fs.open("rsccConfig", "w")
  355. config.writeLine(username)
  356. config.writeLine(dserver)
  357. config.close()
  358. rednet.close(modems[modemOnSide])
  359. term.clear()
  360. term.setCursorPos(1, 1)
Add Comment
Please, Sign In to add comment