Advertisement
margeoComputerCraft

[ccraft]Factionet

Nov 29th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 53.44 KB | None | 0 0
  1. --  Title: Factionet
  2. --
  3. --  Version: 1.1
  4. --
  5. --  Description: A fairly advanced messaging tool for
  6. --  ComputerCraft. Is both a client and sever rolled into
  7. --  one. Neat GUI and other features.
  8. --
  9. --  Author: margeobur
  10.  
  11. --test for non-advanced computer:
  12. if not term.isColor() then
  13.     print("Sorry, this program must be run on an advanced")
  14.     print("computer. Press any key to continue...")
  15.     os.pullEvent("key")
  16.     return false
  17. end
  18.  
  19. -- *** Global Variables (but local to the program) ***
  20. local rootDir = shell.dir()
  21. local hosting = false
  22. local serverName = ""
  23. local running = false
  24.  
  25. local chatList = {}         --complete history of chat lines
  26. local chatScreenList = {}   --list of lines of chat on the screen
  27. local lastLine = 0          --indice of the last line in the complete history of chat
  28. local lastScreenLine = 0    --indice of the last line in the list of chat lines on screen
  29. local scrollAmount = 0
  30. local chatExistsAlready = false
  31.  
  32. local userList = {}
  33. local userScreenList = {}
  34. local lastULline = 0
  35. local lastULScreenLine = 0
  36. local ULscrollAmount = 0
  37. local usersAlreadyOn = false
  38.  
  39. local hostID, myID = nil, os.getComputerID()
  40. local channelOne = nil
  41. local channelTwo = nil
  42. local modem = nil
  43. local modemSide = ""
  44.  
  45. --hosting variables
  46. local sessionList = {}
  47. local noSessions = 0
  48. local newSession = nil
  49. local sFilePath = nil
  50. local sPassword = ""
  51. local userList = {}
  52.  
  53. --client variables
  54. local myuser = ""
  55.  
  56. -- **************** Functions ********************
  57.  
  58. --[[
  59. eWrite():
  60.     Boy, has this saved space!
  61. ]]
  62. local function eWrite(pstring, xCoord, yCoord)
  63.     term.setCursorPos(xCoord,yCoord)
  64.     term.write(pstring)
  65. end
  66.  
  67. --[[
  68. getLine() and writeLine():
  69.     Well, I was going to use these functions
  70.     but I didn't end up needing them...
  71. ]]
  72. local function getLine(filePath,lineNumber)
  73.     if not fs.exists(filePath) then
  74.         return false
  75.     end
  76.     local text = ""
  77.     local fileHandle = fs.open(filePath, "r")
  78.     for i=1,lineNumber do
  79.         text = fileHandle.readLine()
  80.         if not text then
  81.             break
  82.         end
  83.     end
  84.     fileHandle.close()
  85.     if not text then
  86.         return false
  87.     else
  88.         return text
  89.     end
  90. end
  91.  
  92. local function writeLine(filePath,lineNumber,newText)
  93.     if not fs.exists(filePath) then
  94.         return false
  95.     end
  96.     if type(newText) ~= "string" then
  97.         return false
  98.     end
  99.     local beforeLine, afterLine = "", ""
  100.     local currentLine, i, j = 1, 0, 0           -- i will be the indice of
  101.     local fileHandle = fs.open(filePath, "r")   -- our wanted newline and j
  102.     local wholeFile = fileHandle.readAll()      -- will be the indice of the
  103.     fileHandle.close()                          -- next newline
  104.     for w in string.gmatch(wholeFile, "%a") do
  105.         if w == "\n" then
  106.             currentLine = currentLine + 1
  107.         end
  108.         if currentLine <= lineNumber then
  109.             i = i + 1
  110.         end
  111.         if currentLine <= lineNumber + 1 then
  112.             j = j + 1
  113.         end
  114.     end
  115.     if currentLine < lineNumber then    -- i.e. if lineNumber is past the file
  116.         return nil
  117.     else
  118.         beforeLine = string.sub(wholeFile, 1 , i)
  119.         afterLine = string.sub(wholeFile, j)
  120.         wholeFile = beforeLine .. newText .. afterLine
  121.         fileHandle = fs.open(filePath, "w")
  122.         fileHandle.write(wholeFile)
  123.         fileHandle.close()
  124.         return true -- returns true on success
  125.     end
  126. end
  127.  
  128.  
  129.  
  130.  
  131.  
  132. -- ******* Menu Software ******
  133.  
  134. --[[
  135. displayHomeScreen():
  136.     This displays the FactioNET logo in the menu.
  137.     I looked at the paintutils api to see how files
  138.     were loaded so that I could just 'make' the image
  139.     in-program instead of having a file with the image.
  140. ]]
  141. local function displayHomeScreen()
  142.     local image = {}
  143.     image[1] = "                         dd   d ddd ddddd"
  144.     image[2] = "dddd ddd ddd ddd ddd ddd d d  d d     d"
  145.     image[3] = "d    d d d    d   d  d d d  d d ddd   d"
  146.     image[4] = "ddd  ddd d    d   d  d d d   dd d     d"
  147.     image[5] = "d    d d ddd  d  ddd ddd d    d ddd   d"
  148.  
  149.     term.setBackgroundColour(colours.white)
  150.     term.setTextColour(colours.black)
  151.     term.clear()
  152.  
  153.     --** coppied direct from paintutils **
  154.     local tColourLookup = {}
  155.     for n=1,16 do
  156.         tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
  157.     end
  158.  
  159.     local tImage= {}
  160.     local i = 1
  161.     local sLine = image[i]
  162.     while sLine do
  163.         local tLine = {}
  164.         for x=1,sLine:len() do
  165.             tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
  166.         end
  167.         table.insert( tImage, tLine )
  168.         i = i + 1
  169.         sLine = image[i]
  170.     end
  171.  
  172.     --**
  173.  
  174.     paintutils.drawImage(tImage, 6,3)
  175.     term.setBackgroundColour(colours.white)
  176.     term.setTextColour(colours.red)
  177.     eWrite("by margeobur", 40,8)
  178. end
  179.  
  180. --[[
  181. displayExit():
  182.     ditto
  183. ]]
  184. local function displayExit()
  185.     term.setBackgroundColour(colours.white)
  186.     term.setTextColour(colours.black)
  187.     term.clear()
  188.  
  189.     local image = {}
  190.     image[1] = "eee   e   e  eeee"
  191.     image[2] = "e  e   e e   e   "
  192.     image[3] = "eee     e    eeee"
  193.     image[4] = "e  e    e    e   "
  194.     image[5] = "eee     e    eeee"
  195.  
  196.     local tColourLookup = {}
  197.     for n=1,16 do
  198.         tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
  199.     end
  200.  
  201.     local tImage= {}
  202.     local i = 1
  203.     local sLine = image[i]
  204.     while sLine do
  205.         local tLine = {}
  206.         for x=1,sLine:len() do
  207.             tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
  208.         end
  209.         table.insert( tImage, tLine )
  210.         i = i + 1
  211.         sLine = image[i]
  212.     end
  213.  
  214.     paintutils.drawImage(tImage, 17,8)
  215. end
  216.  
  217. --[[
  218. homeMenu():
  219.     Determines what the user wants to do.
  220.     it returns values to determine what happens
  221.     in the main loop. I created this first, so it
  222.     is rather disorganised, but it gets the job
  223.     done.
  224. ]]
  225. local function homeMenu()
  226.     while true do
  227.         displayHomeScreen()
  228.         for i=11, 17 do
  229.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  230.         end
  231.         term.setTextColour(colours.black)
  232.         paintutils.drawLine(16,12,35,12,colours.white)
  233.         eWrite("Host",24,12)
  234.         paintutils.drawLine(16,14,35,14,colours.white)
  235.         eWrite("Join",24,14)
  236.         paintutils.drawLine(16,16,35,16,colours.white)
  237.         eWrite("Exit",24,16)
  238.  
  239.         local event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  240.  
  241.         if xCoord > 15 and xCoord < 36 and yCoord == 12 then    --Host
  242.  
  243.             while true do
  244.                 for i=11, 17 do
  245.                     paintutils.drawLine(14,i,37,i,colours.lightGrey)
  246.                 end
  247.                 paintutils.drawLine(16,12,35,12,colours.white)
  248.                 paintutils.drawLine(16,14,35,14,colours.white)
  249.                 paintutils.drawLine(16,16,35,16,colours.white)
  250.                 eWrite("New Server",21,12)
  251.                 eWrite("Restore a Server",18,14)
  252.                 eWrite("Back",24,16)
  253.                 eWrite("                          ",14,18)
  254.  
  255.                 event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  256.  
  257.                 if xCoord > 15 and xCoord < 36 and yCoord == 12 then    --New Server
  258.                     for i=11, 17 do
  259.                         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  260.                     end
  261.                     eWrite("   Enter your chat    ",16,12)
  262.                     eWrite("   server's name*     ",16,13)
  263.                     paintutils.drawLine(16,14,35,14,colours.white)
  264.                     paintutils.drawLine(16,16,25,16,colours.white)
  265.                     eWrite("Back",19,16)
  266.                     paintutils.drawLine(27,16,35,16,colours.white)
  267.                     eWrite("Confirm",28,16)
  268.                     eWrite("*This can only be set once",14,18)
  269.                     while true do
  270.                         event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  271.                         if xCoord > 15 and xCoord < 36 and yCoord == 14 then    -- enter server name
  272.                             paintutils.drawLine(16,14,35,14,colours.white)
  273.                             term.setCursorPos(16,14)
  274.                             serverName = ""
  275.                             serverName = read()
  276.                         elseif xCoord > 26 and xCoord < 36 and yCoord == 16 and serverName then --confirm
  277.                             for i=11, 17 do
  278.                                 paintutils.drawLine(14,i,37,i,colours.lightGrey)
  279.                             end
  280.                             term.setBackgroundColour(colours.lightGrey)
  281.                             eWrite("   Enter your chat    ",16,12)
  282.                             eWrite("  server's password   ",16,13)
  283.                             term.setBackgroundColour(colours.white)
  284.                             eWrite("                          ",14,18)
  285.                             paintutils.drawLine(16,14,35,14,colours.white)
  286.                             paintutils.drawLine(16,16,25,16,colours.white)
  287.                             eWrite("Back",19,16)
  288.                             paintutils.drawLine(27,16,35,16,colours.white)
  289.                             eWrite("Start!",29,16)
  290.                             while true do
  291.                                 event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  292.                                 if xCoord > 15 and xCoord < 36 and yCoord == 14 then    --enter server password
  293.                                     paintutils.drawLine(16,14,35,14,colours.white)
  294.                                     term.setCursorPos(16,14)
  295.                                     sPassword = read()
  296.                                 elseif xCoord > 26 and xCoord < 36 and yCoord == 16 then    --start the server
  297.                                     sFilePath = rootDir .. "/sessions/" .. serverName
  298.                                     newSession = true
  299.                                     while true do
  300.                                         for i=11, 17 do
  301.                                             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  302.                                         end
  303.                                         hostID = myID
  304.                                         eWrite("The ID to connect to",16,12)
  305.                                         eWrite("is ".. hostID,23,13)
  306.                                         eWrite("Click to continue...",16,15)
  307.                                         os.pullEvent("mouse_click")
  308.                                         return 1
  309.                                     end
  310.                                 elseif xCoord > 15 and xCoord < 26 and yCoord == 16 then    --back
  311.                                     break
  312.                                 end
  313.                             end
  314.                         elseif xCoord > 15 and xCoord < 26 and yCoord == 16 then    --back
  315.                             break
  316.                         end
  317.                     end
  318.  
  319.                 elseif xCoord > 15 and xCoord < 36 and yCoord == 14 then    --Restore a Server
  320.                     local selectedSession = nil
  321.                     local posOne, posTwo, posThree = nil, nil, nil
  322.  
  323.                     if not fs.exists(rootDir .. "/sessions") then
  324.                         fs.makeDir(rootDir .. "/sessions")
  325.                     end
  326.                     for i=10, 18 do
  327.                         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  328.                     end
  329.                     for i=11,13 do
  330.                         paintutils.drawLine(16,i,34,i,colours.white)
  331.                     end
  332.                     paintutils.drawLine(16,15,25,15,colours.white)
  333.                     eWrite("Delete",18,15)
  334.                     paintutils.drawLine(27,15,35,15,colours.white)
  335.                     eWrite("Select",28,15)
  336.                     paintutils.drawLine(16,17,35,17,colours.white)
  337.                     eWrite("Back",24,17)
  338.                     paintutils.drawLine(35,11,35,13,colours.lightBlue)
  339.                     term.setTextColour(colours.blue)
  340.                     eWrite("^",35,11)
  341.                     eWrite("v",35,13)
  342.                     term.setTextColour(colours.black)
  343.                     term.setBackgroundColour(colours.white)
  344.  
  345.                     sessionList = fs.list(rootDir .. "/sessions")
  346.                     noSessions = 0
  347.                     for i, v in ipairs(sessionList) do
  348.                         noSessions = noSessions + 1
  349.                     end
  350.                     if noSessions > 0 then
  351.                         eWrite(sessionList[1],16,11)
  352.                     end
  353.                     if noSessions > 1 then
  354.                         eWrite(sessionList[2],16,12)
  355.                     end
  356.                     if noSessions > 2 then
  357.                         eWrite(sessionList[3],16,13)
  358.                     end
  359.                     posOne = 1
  360.                     posTwo = 2
  361.                     posThree = 3
  362.  
  363.                     while true do
  364.                         event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  365.                         if xCoord == 35 and yCoord == 13 then       -- scroll down
  366.                             if noSessions > 3 and posThree ~= noSessions then
  367.                                 eWrite("                   ",16,11)
  368.                                 eWrite(sessionList[posTwo],16,11)
  369.                                 posOne = posOne + 1
  370.                                 eWrite("                   ",16,12)
  371.                                 eWrite(sessionList[posThree],16,12)
  372.                                 posTwo = posTwo + 1
  373.                                 eWrite("                   ",16,13)
  374.                                 eWrite(sessionList[posThree + 1],16,13)
  375.                                 posThree = posThree + 1
  376.                             end
  377.                         elseif xCoord == 35 and yCoord == 11 then       -- scroll up
  378.                             if noSessions > 3 and posOne ~= 1 then
  379.                                 eWrite("                   ",16,11)
  380.                                 eWrite(sessionList[posOne - 1],16,11)
  381.                                 posOne = posOne - 1
  382.                                 eWrite("                   ",16,12)
  383.                                 eWrite(sessionList[posTwo - 1],16,12)
  384.                                 posTwo = posTwo - 1
  385.                                 eWrite("                   ",16,13)
  386.                                 eWrite(sessionList[posThree - 1],16,13)
  387.                                 posThree = posThree - 1
  388.                             end
  389.                         elseif xCoord > 15 and xCoord < 36 and yCoord == 11 then    --select first list item
  390.                             paintutils.drawLine(16,11,34,11,colours.lime)
  391.                             paintutils.drawLine(16,12,34,12,colours.white)
  392.                             paintutils.drawLine(16,13,34,13,colours.white)
  393.                             term.setBackgroundColour(colours.lime)
  394.                             eWrite(sessionList[posOne],16,11)
  395.                             term.setBackgroundColour(colours.white)
  396.                             eWrite(sessionList[posTwo],16,12)
  397.                             eWrite(sessionList[posThree],16,13)
  398.                             selectedSession = sessionList[posOne]
  399.  
  400.                         elseif xCoord > 15 and xCoord < 36 and yCoord == 12 then    --select second list item
  401.                             paintutils.drawLine(16,11,34,11,colours.white)
  402.                             paintutils.drawLine(16,12,34,12,colours.lime)
  403.                             paintutils.drawLine(16,13,34,13,colours.white)
  404.                             term.setBackgroundColour(colours.white)
  405.                             eWrite(sessionList[posOne],16,11)
  406.                             term.setBackgroundColour(colours.lime)
  407.                             eWrite(sessionList[posTwo],16,12)
  408.                             term.setBackgroundColour(colours.white)
  409.                             eWrite(sessionList[posThree],16,13)
  410.                             selectedSession = sessionList[posTwo]
  411.  
  412.                         elseif xCoord > 15 and xCoord < 36 and yCoord == 13 then    --select third list item
  413.                             paintutils.drawLine(16,11,34,11,colours.white)
  414.                             paintutils.drawLine(16,12,34,12,colours.white)
  415.                             paintutils.drawLine(16,13,34,13,colours.lime)
  416.                             term.setBackgroundColour(colours.white)
  417.                             eWrite(sessionList[posOne],16,11)
  418.                             eWrite(sessionList[posTwo],16,12)
  419.                             term.setBackgroundColour(colours.lime)
  420.                             eWrite(sessionList[posThree],16,13)
  421.                             selectedSession = sessionList[posThree]
  422.                             term.setBackgroundColour(colours.white)
  423.  
  424.                         elseif xCoord > 15 and xCoord < 26 and yCoord == 15 and selectedSession then    --delete
  425.                             paintutils.drawLine(15,15,36,15,colours.lightGrey)
  426.                             eWrite("Are you sure you want",15,15)
  427.                             eWrite("to delete that save?",15,16)
  428.                             paintutils.drawPixel(26,17,colours.lightGrey)
  429.                             term.setBackgroundColour(colours.white)
  430.                             eWrite("Yes    ",19,17)
  431.                             eWrite("   No",27,17)
  432.  
  433.                             while true do
  434.                                 event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  435.                                 if xCoord > 15 and xCoord < 26 and yCoord == 17 then
  436.                                     fs.delete(rootDir .. "/sessions/" .. selectedSession)
  437.                                     sessionList = fs.list(rootDir .. "/sessions")
  438.                                     noSessions = 0
  439.                                     for i, v in ipairs(sessionList) do
  440.                                         noSessions = noSessions + 1
  441.                                     end
  442.                                     if noSessions > 0 then
  443.                                         eWrite("                   ",16,11)
  444.                                         eWrite(sessionList[1],16,11)
  445.                                     end
  446.                                     if noSessions > 1 then
  447.                                         eWrite("                   ",16,12)
  448.                                         eWrite(sessionList[2],16,12)
  449.                                     end
  450.                                     if noSessions > 2 then
  451.                                         eWrite("                   ",16,13)
  452.                                         eWrite(sessionList[3],16,13)
  453.                                     end
  454.                                     posOne = 1
  455.                                     posTwo = 2
  456.                                     posThree = 3
  457.                                     selectedSession = nil
  458.                                     break
  459.                                 elseif xCoord > 26 and xCoord < 36 and yCoord == 17 then
  460.                                     break
  461.                                 end
  462.                             end
  463.  
  464.  
  465.                             for i=15,16 do
  466.                                 paintutils.drawLine(15,i,36,i,colours.lightGrey)
  467.                             end
  468.                             paintutils.drawLine(16,15,25,15,colours.white)
  469.                             eWrite("Delete",18,15)
  470.                             paintutils.drawLine(27,15,35,15,colours.white)
  471.                             eWrite("Select",28,15)
  472.                             paintutils.drawLine(16,17,35,17,colours.white)
  473.                             eWrite("Back",24,17)
  474.  
  475.  
  476.                         elseif xCoord > 26 and xCoord < 36 and yCoord == 15  then   --confirm
  477.                             serverName = selectedSession
  478.                             sFilePath = rootDir .. "/sessions/" .. serverName
  479.                             newSession = false
  480.                             return 2
  481.  
  482.                         elseif xCoord > 15 and xCoord < 36 and yCoord == 17 then --back
  483.                             paintutils.drawLine(14,10,37,10,colours.white)
  484.                             paintutils.drawLine(14,18,37,10,colours.white)
  485.                             break
  486.                         end
  487.                     end
  488.  
  489.                 elseif xCoord > 15 and xCoord < 36 and yCoord == 16 then    --back
  490.                     break
  491.                 end
  492.             end
  493.  
  494.         elseif xCoord > 15 and xCoord < 36 and yCoord == 14 then    --Join
  495.             while true do
  496.                 for i=11, 17 do
  497.                     paintutils.drawLine(14,i,37,i,colours.lightGrey)
  498.                 end
  499.                 eWrite("Enter your host's",18,12)
  500.                 eWrite("ID.",25,13)
  501.                 paintutils.drawLine(16,14,35,14,colours.white)
  502.                 eWrite(hostID,16,14)
  503.                 paintutils.drawLine(16,16,25,16,colours.white)
  504.                 eWrite("Back",19,16)
  505.                 paintutils.drawLine(27,16,35,16,colours.white)
  506.                 eWrite("Connect",28,16)
  507.  
  508.                 event, button, xCoord, yCoord = os.pullEvent("mouse_click")
  509.                 if xCoord > 15 and xCoord < 35 and yCoord == 14 then    --enter ID
  510.                     hostID = -1
  511.                     while hostID < 0 do
  512.                         for i=11, 17 do
  513.                             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  514.                         end
  515.                         eWrite("Enter your host's",18,12)
  516.                         eWrite("ID.",25,13)
  517.                         paintutils.drawLine(16,14,35,14,colours.white)
  518.                         paintutils.drawLine(16,16,25,16,colours.white)
  519.                         eWrite("Back",19,16)
  520.                         paintutils.drawLine(27,16,35,16,colours.white)
  521.                         eWrite("Connect",28,16)
  522.  
  523.                         term.setCursorPos(16,14)
  524.                         local temp = read()
  525.                         hostID = tonumber(temp)
  526.                         if not hostID or hostID < 0 then
  527.                             for i=12, 16 do
  528.                                 paintutils.drawLine(14,i,37,i,colours.lightGrey)
  529.                             end
  530.                             eWrite("Error, the ID can't",16,13)
  531.                             eWrite(" be below 0 or have",16,14)
  532.                             eWrite("      letters      ",16,15)
  533.                             hostID = -1
  534.                             os.pullEvent("mouse_click")
  535.                         end
  536.                     end
  537.  
  538.                 elseif xCoord > 26 and xCoord < 36 and yCoord == 16 then    --connect
  539.                     return 3
  540.  
  541.                 elseif xCoord > 15 and xCoord < 26 and yCoord == 16 then    --back
  542.                     break
  543.                 end
  544.             end
  545.         elseif xCoord > 15 and xCoord < 36 and yCoord == 16 then    --Exit
  546.             break
  547.         end
  548.     end
  549.     return 0
  550. end
  551.  
  552. -- ***** General Software *****
  553. -- functions for both client and host
  554.  
  555. --[[ displayLoadScreen(): Dunno why I made this XD
  556.     I derped a bit (it is only used once) ]]
  557. local function displayLoadScreen()
  558.     term.setBackgroundColour(colours.white)
  559.     term.clear()
  560.     for i=9,12 do
  561.         paintutils.drawLine(18,i,33,i,colours.lightGrey)
  562.     end
  563.     eWrite("Starting",22,10)
  564.     eWrite("server ...",21,11)
  565. end
  566.  
  567. --[[
  568. setupGUI():
  569.     This function draws everything to the screen on
  570.     server startup/after connecting. If the host has
  571.     loaded a save and there is already chat, the chat
  572.     is printed. Likewise if there are already users on
  573.     or chat in the history, this will draw those for the
  574.     client.
  575. ]]
  576. local function setupGUI()
  577.     term.setBackgroundColour(colours.white)
  578.     term.clear()
  579.  
  580.     --HotBar
  581.     paintutils.drawLine(1,1,6,1,colours.orange)
  582.     eWrite("[Menu]",1,1)
  583.     paintutils.drawLine(7,1,51,1,colours.grey)
  584.     term.setTextColour(colours.green)
  585.     eWrite("FactioNET | Server:",20,1)
  586.     term.setTextColour(colours.orange)
  587.     eWrite(serverName,40,1)
  588.     term.setTextColour(colours.black)
  589.  
  590.     --Chat Window
  591.     paintutils.drawLine(1,2,51,2,colours.lightGrey)
  592.     eWrite("Messages:",1,2)
  593.     paintutils.drawLine(34,3,34,15,colours.lightBlue)
  594.     term.setTextColour(colours.blue)
  595.     eWrite("^",34,3)
  596.     eWrite("v",34,15)
  597.     term.setTextColour(colours.black)
  598.     paintutils.drawLine(35,2,35,19,colours.lightGrey)
  599.  
  600.     if chatExistsAlready then
  601.         term.setBackgroundColour(colours.white)
  602.         for i=1,lastScreenLine do
  603.             eWrite(chatScreenList[i],1,i + 2)
  604.         end
  605.     end
  606.  
  607.     --Message Composition Window
  608.     paintutils.drawLine(1,16,35,16,colours.lightGrey)
  609.     eWrite("Compose a message:",1,16)
  610.     paintutils.drawLine(29,17,29,19,colours.lightBlue)
  611.     term.setTextColour(colours.black)
  612.     term.setTextColour(colours.blue)
  613.     eWrite("^",29,17)
  614.     eWrite("v",29,19)
  615.     term.setTextColour(colours.black)
  616.     for i=17,19 do
  617.         paintutils.drawLine(30,i,34,i,colours.lightGrey)
  618.     end
  619.     term.setBackgroundColour(colours.orange)
  620.     eWrite("Send",31,18)
  621.  
  622.     --Player List
  623.     term.setBackgroundColour(colours.lightGrey)
  624.     eWrite("Players in Chat:",36,2)
  625.     paintutils.drawLine(51,3,51,19,colours.lightBlue)
  626.     term.setTextColour(colours.blue)
  627.     eWrite("^",51,3)
  628.     eWrite("v",51,19)
  629.     term.setTextColour(colours.black)
  630.  
  631.     if usersAlreadyOn then
  632.         term.setBackgroundColour(colours.white)
  633.         for i=1,lastULScreenLine do
  634.             eWrite(userScreenList[i],36,i + 2)
  635.         end
  636.     end
  637. end
  638.  
  639. --[[
  640. readMessage():
  641.     Custom read() to stop text from overflowing
  642.     in the message composing box.
  643. ]]
  644. local function readMessage()
  645.     term.setBackgroundColour(colours.white)
  646.     term.setTextColour(colours.black)
  647.     local event, param1, param2, param3 = nil,nil,nil,nil
  648.     local currentX, currentY = 1,17
  649.     local string2send = ""
  650.     term.setCursorBlink(true)
  651.     eWrite(" ",0,17)
  652.     while true do
  653.         event, param1, param2, param3 = os.pullEvent()
  654.         if event == "key" then
  655.             if param1 == keys.backspace then
  656.                 if currentX == 1 and currentY == 17 then
  657.                     -- do nothing because we can't backspace
  658.                 else
  659.                     --remove the last character in our string
  660.                     local temp = string.sub(string2send, 1 , -2)
  661.                     string2send = temp
  662.                 end
  663.                 if currentX > 1 then
  664.                     eWrite(" ",currentX - 1, currentY)
  665.                     currentX = currentX - 1
  666.                     term.setCursorPos(currentX, currentY)
  667.                 elseif currentX == 1 and currentY ~= 17 then
  668.                     eWrite(" ",28,currentY - 1)
  669.                     currentX = 28
  670.                     currentY = currentY - 1
  671.                     term.setCursorPos(currentX, currentY)
  672.                 end
  673.             elseif param1 == keys.enter then
  674.                 term.setCursorBlink(false)
  675.                 for i = 17,19 do
  676.                     paintutils.drawLine(1,i,28,i,colours.white)
  677.                 end
  678.                 return string2send
  679.             end
  680.         elseif event == "char" then
  681.             if currentX == 28 and currentY < 19 then
  682.                 eWrite(param1,currentX,currentY)
  683.                 currentX = 1
  684.                 currentY = currentY + 1
  685.                 term.setCursorPos(currentX, currentY)
  686.                 string2send = string2send .. param1
  687.             elseif currentX == 28 and currentY == 19 then
  688.             else
  689.                 eWrite(param1,currentX,currentY)
  690.                 currentX = currentX + 1
  691.                 string2send = string2send .. param1
  692.             end
  693.         elseif event == "mouse_click" then
  694.             if param2 > 30 and param2 < 35 and param3 == 18 then    --send
  695.                 term.setCursorBlink(false)
  696.                 for i = 17,19 do
  697.                     paintutils.drawLine(1,i,28,i,colours.white)
  698.                 end
  699.                 return string2send
  700.             end
  701.         end
  702.     end
  703. end
  704.  
  705. --[[
  706. hotBarMenu():
  707.     A shared menu for the host and client. It
  708.     currently only has one option, and that's 'logout'.
  709.     The function returns a value that either clientUI()
  710.     or hostUI() acts on.
  711. ]]
  712. local function hotBarMenu()
  713.     local event, param1, param2, param3 = nil,nil,nil,nil
  714.     for i=2,4 do
  715.         paintutils.drawLine(1,i,6,i, colours.orange)
  716.     end
  717.     eWrite("Logout",1,4)
  718.     while true do
  719.         event, param1, param2, param3 = os.pullEvent("mouse_click")
  720.         if param2 < 7 and param3 == 4 then
  721.             term.setBackgroundColour(colours.red)
  722.             eWrite("Logout",1,4)
  723.             sleep(0.2)
  724.             return 1
  725.         elseif param2 > 7 or param3 > 4 then
  726.             term.setTextColour(colours.black)
  727.             term.setBackgroundColour(colours.lightGrey)
  728.             eWrite("Messages:",1,2)
  729.             term.setBackgroundColour(colours.white)
  730.             eWrite("                                 ",1,3)
  731.             eWrite(chatScreenList[1],1,3)
  732.             eWrite("                                 ",1,4)
  733.             eWrite(chatScreenList[2],1,4)
  734.             return 0
  735.         end
  736.     end
  737. end
  738.  
  739. --[[
  740. scrollChatWindow:
  741.     This scrolls the contents of the chat
  742.     window, and changes chatScreenList and
  743.     chatList appropriately. First, however
  744.     it tests to see if we should be able to
  745.     scroll up or down or at all.
  746. ]]
  747. local function scrollChatWindow(amount)
  748.     local temp = 0
  749.     local bottomRef = 0
  750.     local topRef = 0
  751.  
  752.     if lastScreenLine < 13 then
  753.         return false
  754.     elseif chatScreenList[1] == chatList[1] and amount < 0 then
  755.         return false
  756.     elseif scrollAmount + 13 == lastLine
  757.     and amount > 0 then
  758.         return false
  759.     end
  760.  
  761.     if amount > 0 then
  762.         bottomRef = scrollAmount + 13
  763.         -- this is the position on chatList that is equivalent
  764.         -- to the current last line on the screen
  765.     elseif amount < 0 then
  766.         topRef = scrollAmount
  767.         -- this is the position on chatList that is equivalent
  768.         -- to the current first line on the screen
  769.     end
  770.  
  771.     if amount == 1 then --scroll down once
  772.         temp = 1
  773.         for j=1, 12 do
  774.             eWrite("                                 ",1,temp + 2)
  775.             eWrite(chatScreenList[temp + 1],1,temp + 2)
  776.             chatScreenList[j] = chatScreenList[j + 1]
  777.             temp = temp + 1
  778.         end
  779.         chatScreenList[13] = chatList[bottomRef + 1]
  780.         eWrite("                                 ",1,15)
  781.         eWrite(chatScreenList[13],1,15)
  782.         scrollAmount = scrollAmount + 1
  783.     end
  784.     if amount == -1 then    --scroll up once
  785.         temp = 13
  786.         for j=1, 12 do
  787.             eWrite("                                 ",1,temp + 2)
  788.             eWrite(chatScreenList[temp - 1],1,temp + 2)
  789.             chatScreenList[temp] = chatScreenList[temp - 1]
  790.             temp = temp - 1
  791.         end
  792.         chatScreenList[1] = chatList[topRef]
  793.         eWrite("                                 ",1,3)
  794.         eWrite(chatScreenList[1],1,3)
  795.         scrollAmount = scrollAmount - 1
  796.     end
  797.  
  798.     return true
  799. end
  800.  
  801. --[[
  802. printChatWindow():
  803.     OMG THIS WAS SO HARD.
  804.     This is what organises your raw chat message
  805.     into lines with complete words, but stops them from
  806.     overflowing. It then prints them in the chat box.
  807.     This is a handy function because you can use it
  808.     to print anything to the chat window.
  809. ]]
  810.  
  811. local function printChatWindow(wholeMessage)
  812.     local lines = {}
  813.     lines[1] = ""
  814.     lines[2] = ""
  815.     lines[3] = ""
  816.     local numbLines = 0
  817.     local messageLength = string.len(wholeMessage)
  818.     if messageLength > 99 then
  819.         wholeMessage = string.sub(wholeMessage,1,99)
  820.     end
  821.     local word, w = "", ""
  822.     local i,j = 1,1
  823.     local wrapOffset1 = 0
  824.     local wrapOffset2 = 0
  825.  
  826.     term.setBackgroundColour(colours.white)
  827.     term.setTextColour(colours.black)
  828.  
  829.     for w in string.gmatch(wholeMessage, ".") do
  830.         if w == " " then
  831.             if i == 33 or i == 66 then
  832.                 lines[j] = lines[j] .. word
  833.                 word = ""
  834.             else
  835.                 lines[j] = lines[j] .. word .. " "
  836.                 word = ""
  837.             end
  838.         else
  839.             word = word .. w
  840.         end
  841.  
  842.         if i == 33 then
  843.             -- if the 1st pixel of the 2nd line contains a
  844.             -- space and is the equivalent of the last character
  845.             -- in wholeMessage, do nothing because the second
  846.             -- line would just be blank otherwise
  847.             if messageLength == 34 and
  848.                 string.sub(wholeMessage,34,34) == " " then
  849.  
  850.             else
  851.                 j = 2
  852.                 i = 32 + string.len(word)
  853.                 wrapOffset1 = string.len(word)
  854.             end
  855.  
  856.         elseif i == 66 then
  857.             -- if the 1st pixel of the 3rd line contains a
  858.             -- space and is the equivalent of the last character
  859.             -- in wholeMessage, do nothing because the third
  860.             -- line would just be blank otherwise
  861.             if messageLength + wrapOffset1 == 67 and
  862.                 string.sub(wholeMessage,messageLength,messageLength) == " " then
  863.  
  864.             else
  865.                 if string.len(word) >= 33 then
  866.                     lines[j] = lines[j] .. string.sub(word,1,33)
  867.                     word = ""
  868.                 end
  869.                 j = 3
  870.                 i = 65 + string.len(word)
  871.                 wrapOffset2 = string.len(word)
  872.             end
  873.         end
  874.         i = i + 1
  875.     end
  876.     if string.len(word) > 33 then
  877.         word = string.sub(word,1,33)
  878.         lines[j] = lines[j] .. word
  879.         numbLines = j
  880.     else
  881.         lines[j] = lines[j] .. word
  882.         numbLines = j
  883.     end
  884.  
  885.     if lastLine < 13 then
  886.         for i=1,numbLines do
  887.             eWrite("                                 ",1,lastScreenLine + 3)
  888.             eWrite(lines[i],1,lastScreenLine + 3)
  889.             lastScreenLine = lastScreenLine + 1
  890.             chatScreenList[lastScreenLine] = lines[i]
  891.             lastLine = lastLine + 1
  892.             chatList[lastLine] = lines[i]
  893.             if lastScreenLine >= 13 then
  894.                 for j=i+1,numbLines do
  895.                     lastLine = lastLine + 1
  896.                     chatList[lastLine] = lines[j]
  897.                 end
  898.                 scrollChatWindow(numbLines - i)
  899.                 break
  900.             end
  901.         end
  902.     elseif lastLine == 100 then
  903.         for i=1,numbLines do
  904.             for j=1,99 do
  905.                 chatList[j] = chatList[j + 1]
  906.             end
  907.             chatList[100] = lines[i]
  908.  
  909.             for j=1, 13 do
  910.                 chatScreenList[j] = chatList[scrollAmount + j]
  911.                 eWrite("                                 ",1,j + 2)
  912.                 eWrite(chatScreenList[j],1,j + 2)
  913.             end
  914.         end
  915.     else
  916.         for i=1,numbLines do
  917.             lastLine = lastLine + 1
  918.             chatList[lastLine] = lines[i]
  919.             scrollChatWindow(1)
  920.         end
  921.     end
  922. end
  923.  
  924. --[[
  925. scrollUserList():
  926.     Like scrollChatWindow() but for the user list.
  927.     I'm not sure if it would work well... I haven't
  928.     tried having that many users on.
  929. ]]
  930. local function scrollUserList(amount)
  931.     local temp = 0
  932.     local bottomRef = 0
  933.     local topRef = 0
  934.  
  935.     if lastULScreenLine < 17 then
  936.         return false
  937.     elseif ULscrollAmount == 0 and amount < 0 then
  938.         return false
  939.     elseif ULscrollAmount + 17 == lastULline
  940.     and amount > 0 and lastULline > 17 then
  941.         return false
  942.     end
  943.  
  944.     if amount > 0 then
  945.         bottomRef = ULscrollAmount + 17
  946.         -- this is the position on userList that is equivalent
  947.         -- to the current last user displayed on the screen
  948.     elseif amount < 0 then
  949.         topRef = ULscrollAmount
  950.         -- this is the position on userList that is equivalent
  951.         -- to the current first user displayed on the screen
  952.     end
  953.  
  954.     term.setBackgroundColour(colours.white)
  955.  
  956.     if amount == 1 then --scroll down once
  957.         temp = 1
  958.         for j=1, 16 do
  959.             eWrite("               ",36,temp + 2)
  960.             eWrite(userScreenList[temp + 1],36,temp + 2)
  961.             userScreenList[j] = userScreenList[j + 1]
  962.             temp = temp + 1
  963.         end
  964.         userScreenList[17] = userList[bottomRef + 1]
  965.         eWrite("               ",36,19)
  966.         eWrite(userScreenList[17],36,19)
  967.         ULscrollAmount = ULscrollAmount + 1
  968.     end
  969.     if amount == -1 then    --scroll up once
  970.         temp = 17
  971.         for j=1, 16 do
  972.             eWrite("               ",36,temp + 2)
  973.             eWrite(userScreenList[temp - 1],36,temp + 2)
  974.             userScreenList[temp] = userScreenList[temp - 1]
  975.             temp = temp - 1
  976.         end
  977.         userScreenList[1] = userList[topRef]
  978.         eWrite("               ",36,3)
  979.         eWrite(userScreenList[1],36,3)
  980.         ULscrollAmount = ULscrollAmount - 1
  981.     end
  982. end
  983.  
  984. --[[
  985. addUser():
  986.     Adds a user to userList and prints it to the
  987.     screen accordingly.
  988. ]]
  989. local function addUser(username)
  990.     term.setBackgroundColour(colours.white)
  991.     if lastULline < 17 then
  992.         if string.len(username) > 15 then
  993.             temp = string.sub(username,1,15)
  994.             eWrite("               ",36,lastULScreenLine + 3)
  995.             eWrite(temp,36,lastULScreenLine + 3)
  996.         else
  997.             eWrite("               ",36,lastULScreenLine + 3)
  998.             eWrite(username,36,lastULScreenLine + 3)
  999.         end
  1000.  
  1001.         lastULScreenLine = lastULScreenLine + 1
  1002.         userScreenList[lastULScreenLine] = username
  1003.  
  1004.         lastULline = lastULline + 1
  1005.         userList[lastULline] = username
  1006.     else
  1007.         lastULline = lastULline + 1
  1008.         userList[lastULline] = username
  1009.         scrollUserList(1)
  1010.     end
  1011. end
  1012.  
  1013. --[[
  1014. removeUser():
  1015.     Removes a user from userList and does whatever
  1016.     it needs to do.
  1017. ]]
  1018. local function removeUser(username)
  1019.     if lastULline == 0 then
  1020.         return false
  1021.     end
  1022.  
  1023.     local userindice = 0
  1024.  
  1025.     for i=1,lastULline do
  1026.         if userList[i] == username then
  1027.             userindice = i
  1028.             break
  1029.         end
  1030.     end
  1031.  
  1032.     for i=userindice,lastULline do
  1033.         userList[i] = userList[i+1]
  1034.     end
  1035.     lastULline = lastULline - 1
  1036.  
  1037.     for i=1,17 do
  1038.         if userList[ULscrollAmount + i] then
  1039.             userScreenList[i] = userList[ULscrollAmount + i]
  1040.             eWrite("               ",36,i + 2)
  1041.             eWrite(userScreenList[i],36,i + 2)
  1042.         else
  1043.             eWrite("               ",36,i + 2)
  1044.         end
  1045.     end
  1046.     lastULScreenLine = lastULScreenLine - 1
  1047. end
  1048.  
  1049. -- ******* Host Software ******
  1050.  
  1051. --[[
  1052. saveServer():
  1053.     This function puts all of the necessary
  1054.     values and text that are needed to restore
  1055.     the server into a file with the name of the
  1056.     server in the folder called 'sessions'.
  1057. ]]
  1058. local function saveServer()
  1059.     if not fs.exists(rootDir .. "/sessions") then
  1060.         fs.makeDir(rootDir .. "/sessions")
  1061.     end
  1062.  
  1063.     local fileHandle = fs.open(sFilePath, "w")
  1064.     if not fileHandle then
  1065.         return false
  1066.     end
  1067.  
  1068.     fileHandle.writeLine("password((" .. sPassword .. "))end")
  1069.     fileHandle.writeLine("channel1((" .. channelOne .. "))end")
  1070.     fileHandle.writeLine("channel2((" .. channelTwo .. "))end")
  1071.  
  1072.     if lastLine > 1 then
  1073.         fileHandle.writeLine("((CHAT))")
  1074.         fileHandle.writeLine("")
  1075.  
  1076.         for i=1,lastLine do
  1077.             fileHandle.writeLine("chatLine" .. i .. "((" .. chatList[i] .. "))end")
  1078.         end
  1079.     end
  1080.  
  1081.     fileHandle.close()
  1082.     return true
  1083. end
  1084.  
  1085. --[[
  1086. restoreServer():
  1087.     Starts a previously made server by loading
  1088.     all the values and text from the file
  1089.     into the necessary variables.
  1090. ]]
  1091. local function restoreServer()
  1092.     local temp1, temp2 = 0,0
  1093.     local firstIndice, secondIndice = 0,0
  1094.  
  1095.     local fileHandle = fs.open(sFilePath, "r")
  1096.  
  1097.     temp = fileHandle.readLine()
  1098.     if not temp then
  1099.         return false
  1100.     elseif string.find(temp,"password((",1,true) and string.find(temp,"))end",1,true) then
  1101.         temp1, temp2 = string.find(temp,"password((",1,true)
  1102.         firstIndice = temp2 + 1
  1103.  
  1104.         temp1, temp2 = string.find(temp,"))end",1,true)
  1105.         secondIndice = temp1 - 1
  1106.  
  1107.         sPassword = string.sub(temp,firstIndice,secondIndice)
  1108.     end
  1109.  
  1110.     temp = fileHandle.readLine()
  1111.     if not temp then
  1112.         return false
  1113.     elseif string.find(temp,"channel1((",1,true) and string.find(temp,"))end",1,true) then
  1114.         temp1, temp2 = string.find(temp,"channel1((",1,true)
  1115.         firstIndice = temp2 + 1
  1116.  
  1117.         temp1, temp2 = string.find(temp,"))end",1,true)
  1118.         secondIndice = temp1 - 1
  1119.  
  1120.         channelOne = tonumber(string.sub(temp,firstIndice,secondIndice))
  1121.     end
  1122.  
  1123.     temp = fileHandle.readLine()
  1124.     if not temp then
  1125.         return false
  1126.     elseif string.find(temp,"channel2((",1,true) and string.find(temp,"))end",1,true) then
  1127.         temp1, temp2 = string.find(temp,"channel2((",1,true)
  1128.         firstIndice = temp2 + 1
  1129.  
  1130.         temp1, temp2 = string.find(temp,"))end",1,true)
  1131.         secondIndice = temp1 - 1
  1132.  
  1133.         channelTwo = tonumber(string.sub(temp,firstIndice,secondIndice))
  1134.     end
  1135.  
  1136.     temp = fileHandle.readLine()
  1137.     if temp == "((CHAT))" then
  1138.         chatExistsAlready = true
  1139.     elseif not temp then
  1140.         return false
  1141.     end
  1142.     temp = fileHandle.readLine()
  1143.  
  1144.     for i=1,100 do
  1145.         temp = fileHandle.readLine()
  1146.         if not temp then
  1147.             break
  1148.         end
  1149.  
  1150.         if string.find(temp,"chatLine" .. i .. "((",1,true) and string.find(temp,"))end",1,true) then
  1151.             temp1, temp2 = string.find(temp,"chatLine" .. i .. "((",1,true)
  1152.             firstIndice = temp2 + 1
  1153.  
  1154.             temp1, temp2 = string.find(temp,"))end",1,true)
  1155.             secondIndice = temp1 - 1
  1156.  
  1157.             chatList[i] = string.sub(temp,firstIndice,secondIndice)
  1158.             lastLine = lastLine + 1
  1159.  
  1160.             if i <= 13 then
  1161.                 chatScreenList[i] = chatList[i]
  1162.                 lastScreenLine = lastScreenLine + 1
  1163.             end
  1164.         end
  1165.     end
  1166.  
  1167.     return true
  1168. end
  1169.  
  1170. --[[
  1171. hostConnectionManager():
  1172.     This manages incoming requests to connect to
  1173.     or disconnect from the server. I decided to
  1174.     use rednet for this in the long run because
  1175.     it allows the connecting computer and the
  1176.     host to communicate privately without
  1177.     disturbing anything else.
  1178. ]]
  1179.  
  1180. local function hostConnectionManager()
  1181.     local currentConnectie = nil
  1182.     local firstIndice, secondIndice = 0,0
  1183.     local temp,temp1, temp2 = 0,0,0
  1184.     local username = ""
  1185.     local timeout, takenUser = false, false
  1186.     rednet.open(modemSide)
  1187.  
  1188.     while running == true do
  1189.         local tabletosend = {}
  1190.         local senderId, message, distance = rednet.receive(7.5)
  1191.         if senderId and running then
  1192.             if message == "connectme2tsp" then
  1193.                 currentConnectie = senderId
  1194.                 for i=1, 10 do              -- times out after 10 times
  1195.                     rednet.send(currentConnectie,"login")   -- (about 12 secs)
  1196.                     senderId, message, distance = rednet.receive(1.2)
  1197.                     if senderId == currentConnectie then
  1198.                         if message == sPassword then
  1199.                             timeout = false
  1200.                             break
  1201.                         else
  1202.                             rednet.send(currentConnectie,"badpass")
  1203.                         end
  1204.                     end
  1205.                     timeout = true
  1206.                 end
  1207.                 if timeout == false then
  1208.                     for i=1, 10 do
  1209.                         rednet.send(currentConnectie,"enteruser")
  1210.                         senderId, message, distance = rednet.receive(1.2)
  1211.                         if senderId == currentConnectie then
  1212.                             if string.find(message,"user((",1,true) then    --login
  1213.                                 temp1, temp2 = string.find(message,"user((",1,true) -- get username indices
  1214.                                 firstIndice = temp2 + 1
  1215.                                 temp1, temp2 = string.find(message,"))end",1,true)
  1216.                                 secondIndice = temp1 - 1
  1217.                                 username = string.sub(message,firstIndice, secondIndice)
  1218.                                 for key, name in ipairs(userList) do
  1219.                                     if username == name then
  1220.                                         takenUser = true
  1221.                                         break
  1222.                                     end
  1223.                                     local temp = key
  1224.                                 end
  1225.  
  1226.                                 if not takenUser then
  1227.                                     addUser(username)
  1228.  
  1229.                                     tabletosend[1] = "ch1((" .. channelOne .. "))end"
  1230.                                     tabletosend[2] = "ch2((" .. channelTwo .. "))end"
  1231.                                     tabletosend[3] = "servername((" .. serverName .. "))end"
  1232.  
  1233.                                     if lastLine > 0 then
  1234.                                         tabletosend[4] = "((CHAT))"
  1235.                                         tabletosend[5] = ""
  1236.                                         for i=1,lastLine do
  1237.                                             tabletosend[i+5] = "chatLine" .. i .. "((" .. chatList[i] .. "))end"
  1238.                                         end
  1239.                                     end
  1240.  
  1241.                                     if lastULline > 0 then
  1242.                                         tabletosend[lastLine + 6] = "((USERS))"
  1243.                                         tabletosend[lastLine + 7] = ""
  1244.                                         for i=1,lastULline do
  1245.                                             tabletosend[lastLine + 7 + i] = "user" .. i .. "((" .. userList[i] .. "))end"
  1246.                                         end
  1247.                                     end
  1248.  
  1249.                                     sTabletosend = textutils.serialize(tabletosend)
  1250.  
  1251.                                     modem.transmit(channelTwo,0,"userconnect((" .. username .. "))end")
  1252.                                     printChatWindow(username .. " joined the chat server.")
  1253.  
  1254.                                     for i=1,3 do-- just in case
  1255.                                         rednet.send(currentConnectie, "success((".. sTabletosend)
  1256.                                         sleep(0.5)
  1257.                                     end
  1258.  
  1259.                                     break
  1260.                                 else
  1261.                                     rednet.send(currentConnectie, "takenuser")
  1262.                                 end
  1263.                             else
  1264.                                 rednet.send(currentConnectie, "badmessage") --some other message has been received
  1265.                             end
  1266.                         end
  1267.                     end
  1268.                 end
  1269.             elseif string.find(message,"disconnectmeFtsp",1,true) then
  1270.                 temp1, temp2 = string.find(message,"disconnectmeFtsp((",1,true) -- get username indices
  1271.                 firstIndice = temp2 + 1
  1272.                 temp1, temp2 = string.find(message,"))end",1,true)
  1273.                 secondIndice = temp1 - 1
  1274.                 username = string.sub(message,firstIndice, secondIndice)
  1275.                 rednet.send(senderId,"disconnected")
  1276.  
  1277.                 modem.transmit(channelTwo,0,"userdisconnect((" .. username .. "))end")
  1278.                 printChatWindow(username .. " left the chat server.")
  1279.                 removeUser(username)
  1280.             end
  1281.         end
  1282.     end
  1283.     rednet.close(modemSide)
  1284. end
  1285.  
  1286. --[[
  1287. hostChatManager:
  1288.     receives messages from clients,
  1289.     distributes them and prints
  1290.     them on the host's own screen
  1291. ]]
  1292.  
  1293. local function hostChatManager()
  1294.     local user, chatMessage = ""
  1295.     local temp, temp2 = 0,0
  1296.     local firstIndice, secondIndice = 0,0
  1297.     local wholeOut
  1298.     local event, side, frequency, replyFreqency, message, distance
  1299.  
  1300.     modem.open(channelOne)
  1301.     while running == true do
  1302.         os.startTimer(5)
  1303.         event, side, frequency, replyFreqency, message, distance = os.pullEvent()
  1304.         if event == "modem_message" then
  1305.             if frequency == channelOne then
  1306.                 if string.find(message, "user((", 1, true) and string.find(message, "))message((", 1, true) then
  1307.                     temp, temp2 = string.find(message,"user((",1,true)  -- get username indices
  1308.                     firstIndice = temp2 + 1
  1309.                     temp, temp2 = string.find(message,"))message",1,true)
  1310.                     secondIndice = temp - 1
  1311.                     user = string.sub(message,firstIndice, secondIndice)
  1312.  
  1313.                     temp, temp2 = string.find(message,"message((",1,true)   -- get message indices
  1314.                     firstIndice = temp2 + 1
  1315.                     temp, temp2 = string.find(message,"))end",1,true)
  1316.                     secondIndice = temp - 1
  1317.                     chatMessage = string.sub(message,firstIndice, secondIndice) --load whole message
  1318.  
  1319.                     wholeOut = user.. ": " .. chatMessage
  1320.                     modem.transmit(channelTwo,channelOne,wholeOut)
  1321.  
  1322.                     printChatWindow(wholeOut)   --print on host machine
  1323.                 end
  1324.             end
  1325.         elseif event == "timer" then
  1326.             if running == false then
  1327.                 break
  1328.             end
  1329.         end
  1330.     end
  1331.     modem.close(channelOne)
  1332. end
  1333.  
  1334. --[[
  1335. hostHotBarMenu():
  1336.     This controls the Host menu, similar to
  1337.     hotBarMenu().
  1338. ]]
  1339. local function hostHotBarMenu()
  1340.     local event, param1, param2, param3 = nil,nil,nil,nil
  1341.     for i=2,4 do
  1342.         paintutils.drawLine(8,i,14,i, colours.orange)
  1343.     end
  1344.     eWrite("Save",8,2)
  1345.     eWrite("Shutdown",7,3)
  1346.     while true do
  1347.         event, param1, param2, param3 = os.pullEvent("mouse_click")
  1348.         if param2 > 7 and param2 < 15 and param3 == 2 then
  1349.             term.setBackgroundColour(colours.red)
  1350.             eWrite("Save",8,2)
  1351.             sleep(0.2)
  1352.             term.setTextColour(colours.black)
  1353.             term.setBackgroundColour(colours.lightGrey)
  1354.             paintutils.drawLine(1,2,20,2,colours.lightGrey)
  1355.             eWrite("Messages:",1,2)
  1356.             term.setBackgroundColour(colours.white)
  1357.             eWrite(chatScreenList[1],1,3)
  1358.             eWrite(chatScreenList[2],1,4)
  1359.             return 1
  1360.  
  1361.         elseif param2 > 7 and param2 < 15 and param3 == 3 then
  1362.             term.setBackgroundColour(colours.red)
  1363.             eWrite("Shutdown",7,3)
  1364.             sleep(0.2)
  1365.             term.setTextColour(colours.black)
  1366.             term.setBackgroundColour(colours.lightGrey)
  1367.             paintutils.drawLine(1,2,20,2,colours.lightGrey)
  1368.             eWrite("Messages:",1,2)
  1369.             term.setBackgroundColour(colours.white)
  1370.             eWrite(chatScreenList[1],1,3)
  1371.             eWrite(chatScreenList[2],1,4)
  1372.             return 2
  1373.  
  1374.         elseif param2 < 7 or param2 > 14 or param3 > 4 then
  1375.             term.setTextColour(colours.black)
  1376.             term.setBackgroundColour(colours.lightGrey)
  1377.             paintutils.drawLine(1,2,20,2,colours.lightGrey)
  1378.             eWrite("Messages:",1,2)
  1379.             term.setBackgroundColour(colours.white)
  1380.             eWrite("                                 ",1,3)
  1381.             eWrite(chatScreenList[1],1,3)
  1382.             eWrite("                                 ",1,4)
  1383.             eWrite(chatScreenList[2],1,4)
  1384.             return 0
  1385.         end
  1386.     end
  1387. end
  1388.  
  1389. --[[
  1390. hostUI():
  1391.     This function allows the host to interact with
  1392.     everything while the server is running.
  1393. ]]
  1394. local function hostUI()
  1395.     local action
  1396.     local chatString
  1397.     local wholeOut
  1398.     setupGUI()
  1399.     term.setBackgroundColour(colours.orange)
  1400.     eWrite("[Host]",8,1)
  1401.     term.setBackgroundColour(colours.white)
  1402.     while running == true do
  1403.         event, param1, param2, param3 = os.pullEvent()
  1404.         if event == "mouse_click" then
  1405.             if param2 < 28 and param3 > 16 then
  1406.                 chatString = readMessage()
  1407.  
  1408.                 wholeOut = "Host: " .. chatString
  1409.                 modem.transmit(channelTwo,channelOne,wholeOut)
  1410.                 printChatWindow(wholeOut)
  1411.             elseif param2 < 6 and param3 == 1 then
  1412.                 term.setBackgroundColour(colours.red)
  1413.                 eWrite("[Menu]",1,1)
  1414.                 sleep(0.2)
  1415.                 term.setBackgroundColour(colours.orange)
  1416.                 eWrite("[Menu]",1,1)
  1417.                 action = hotBarMenu()
  1418.                 if action == 1 then
  1419.                     printChatWindow("The server is shutting down.")
  1420.                     modem.transmit(channelTwo,channelOne,"lionelhuttXX((SERVERSHUTDOWN))Xx")
  1421.                     -- had to make it something no one would likely type XD
  1422.                     sleep(5)
  1423.                     running = false
  1424.                     break
  1425.                 end
  1426.             elseif param2 > 7 and param2 < 14 and param3 == 1 then
  1427.                 term.setBackgroundColour(colours.red)
  1428.                 eWrite("[Host]",8,1)
  1429.                 sleep(0.2)
  1430.                 term.setBackgroundColour(colours.orange)
  1431.                 eWrite("[Host]",8,1)
  1432.                 action = hostHotBarMenu()
  1433.                 if action == 1 then
  1434.                     if not saveServer() then
  1435.                         printChatWindow("save failed.")
  1436.                     end
  1437.                 elseif action == 2 then
  1438.                     printChatWindow("The server is shutting down.")
  1439.                     modem.transmit(channelTwo,channelOne,"lionelhuttXX((SERVERSHUTDOWN))Xx")
  1440.                     sleep(5)
  1441.                     running = false
  1442.                 end
  1443.             elseif param2 == 34 and param3 == 15 then
  1444.                 scrollChatWindow(1)
  1445.             elseif param2 == 34 and param3 == 3 then
  1446.                 scrollChatWindow(-1)
  1447.             end
  1448.         end
  1449.     end
  1450. end
  1451.  
  1452.  
  1453. -- ****** Client Software *****
  1454.  
  1455. --[[
  1456. loadInfo():
  1457.     This loads all the values and text received from the
  1458.     host into the necessary variables. It's a bit like
  1459.     restoreServer() except it's for clients when they
  1460.     connect.
  1461. ]]
  1462. local function loadInfo(serverdata)
  1463.     local temp, temp1, temp2 = "",0,0
  1464.     local firstIndice, secondIndice = 0,0
  1465.  
  1466.     if string.find(serverdata[1],"ch1((",1,true) then
  1467.         temp1, temp2 = string.find(serverdata[1],"ch1((",1,true)
  1468.         firstIndice = temp2 + 1
  1469.  
  1470.         temp1, temp2 = string.find(serverdata[1],"))end",1,true)
  1471.         secondIndice = temp1 - 1
  1472.  
  1473.         temp = string.sub(serverdata[1],firstIndice,secondIndice)
  1474.         channelOne = tonumber(temp)
  1475.     end
  1476.  
  1477.     if string.find(serverdata[2],"ch2((",1,true) then
  1478.         temp1, temp2 = string.find(serverdata[2],"ch2((",1,true)
  1479.         firstIndice = temp2 + 1
  1480.  
  1481.         temp1, temp2 = string.find(serverdata[2],"))end",1,true)
  1482.         secondIndice = temp1 - 1
  1483.  
  1484.         temp = string.sub(serverdata[2],firstIndice,secondIndice)
  1485.         channelTwo = tonumber(temp)
  1486.     end
  1487.  
  1488.     if string.find(serverdata[3],"servername((",1,true) then
  1489.         temp1, temp2 = string.find(serverdata[3],"servername((",1,true)
  1490.         firstIndice = temp2 + 1
  1491.  
  1492.         temp1, temp2 = string.find(serverdata[3],"))end",1,true)
  1493.         secondIndice = temp1 - 1
  1494.  
  1495.         temp = string.sub(serverdata[3],firstIndice,secondIndice)
  1496.         serverName = temp
  1497.     end
  1498.  
  1499.     if serverdata[4] == "((CHAT))" then
  1500.         chatExistsAlready = true
  1501.     elseif not serverdata[4] then
  1502.         return true
  1503.     end
  1504.  
  1505.     for i=1,100 do
  1506.         if not serverdata[i+5] then
  1507.             break
  1508.         end
  1509.  
  1510.         if string.find(serverdata[i+5],"chatLine" .. i .. "((",1,true) and string.find(serverdata[i+5],"))end",1,true) then
  1511.             temp1, temp2 = string.find(serverdata[i+5],"chatLine" .. i .. "((",1,true)
  1512.             firstIndice = temp2 + 1
  1513.  
  1514.             temp1, temp2 = string.find(serverdata[i+5],"))end",1,true)
  1515.             secondIndice = temp1 - 1
  1516.  
  1517.             chatList[i] = string.sub(serverdata[i+5],firstIndice,secondIndice)
  1518.             lastLine = lastLine + 1
  1519.  
  1520.             if i <= 13 then
  1521.                 chatScreenList[i] = chatList[i]
  1522.                 lastScreenLine = lastScreenLine + 1
  1523.             end
  1524.         end
  1525.     end
  1526.  
  1527.     if serverdata[lastLine + 6] == "((USERS))" then
  1528.         usersAlreadyOn = true
  1529.     elseif not serverdata[lastLine + 6] then
  1530.         return true
  1531.     end
  1532.  
  1533.     for i=1,100 do
  1534.         if not serverdata[lastLine + 7 + i] then
  1535.             break
  1536.         end
  1537.  
  1538.         if string.find(serverdata[lastLine + 7 + i],"user" .. i .. "((",1,true) and string.find(serverdata[lastLine + 7 + i],"))end",1,true) then
  1539.             temp1, temp2 = string.find(serverdata[lastLine + 7 + i],"user" .. i .. "((",1,true)
  1540.             firstIndice = temp2 + 1
  1541.  
  1542.             temp1, temp2 = string.find(serverdata[lastLine + 7 + i],"))end",1,true)
  1543.             secondIndice = temp1 - 1
  1544.  
  1545.             userList[i] = string.sub(serverdata[lastLine + 7 + i],firstIndice,secondIndice)
  1546.             lastULline = lastULline + 1
  1547.  
  1548.             if i <= 17 then
  1549.                 userScreenList[i] = userList[i]
  1550.                 lastULScreenLine = lastULScreenLine + 1
  1551.             end
  1552.         end
  1553.     end
  1554.  
  1555.     return true
  1556. end
  1557.  
  1558. --[[
  1559. login():
  1560.     Does exactly what its name implies. It does all
  1561.     the necessary communicating with the server to
  1562.     get connnected.
  1563. ]]
  1564. local function login()
  1565.     term.setBackgroundColour(colours.white)
  1566.     term.clear()
  1567.     for i=7,15 do
  1568.         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1569.     end
  1570.     eWrite("logging in...",20,11)
  1571.     local senderId, message, distance
  1572.     local timeout = false
  1573.     local temp1, temp2, firstIndice, secondIndice = 0,0,0,0
  1574.     local password, username = "",""
  1575.     local serverdata = {}
  1576.  
  1577.     rednet.open(modemSide)
  1578.     for i=1, 10 do
  1579.         rednet.send(hostID, "connectme2tsp")
  1580.         senderId, message, distance = rednet.receive(1.2)
  1581.         if senderId == hostID then
  1582.             if message == "login" then
  1583.                 timeout = false
  1584.                 break
  1585.             end
  1586.         end
  1587.         timeout = true
  1588.     end
  1589.     if timeout == true then
  1590.         for i=7,15 do
  1591.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1592.         end
  1593.         eWrite("Could not connect to",16,10)
  1594.         eWrite("server",24,11)
  1595.         os.pullEvent("mouse_click")
  1596.         return false
  1597.     end
  1598.     for i=1, 12 do
  1599.         for i=7,15 do
  1600.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1601.         end
  1602.         eWrite("Please enter the",19,8)
  1603.         eWrite("server's password.",17,9)
  1604.         eWrite("(do this quickly or",16,10)
  1605.         eWrite("you will time out)",17,11)
  1606.         paintutils.drawLine(16,13,35,13,colours.white)
  1607.         term.setCursorPos(16,13)
  1608.         password = read()
  1609.  
  1610.         rednet.send(hostID,password)
  1611.         senderId, message, distance = rednet.receive(1.2)
  1612.         if senderId == hostID then
  1613.             if message == "enteruser" then
  1614.                 timeout = false
  1615.                 break
  1616.             elseif message == "badpass" then
  1617.                 for i=7,15 do
  1618.                     paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1619.                 end
  1620.                 eWrite("Password was incorrect.",15,11)
  1621.                 os.pullEvent("mouse_click")
  1622.             end
  1623.         end
  1624.         timeout = true
  1625.     end
  1626.     if timeout == true then
  1627.         for i=7,15 do
  1628.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1629.         end
  1630.         eWrite("Lost connection to",19,10)
  1631.         eWrite("server",20,11)
  1632.         os.pullEvent("mouse_click")
  1633.         return false
  1634.     end
  1635.     for i=1, 12 do
  1636.         for i=7,15 do
  1637.             paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1638.         end
  1639.         eWrite("Please enter your",18,8)
  1640.         eWrite("username for the",19,9)
  1641.         eWrite("server",22,10)
  1642.         paintutils.drawLine(16,13,35,13,colours.white)
  1643.         term.setCursorPos(16,13)
  1644.         username = read()
  1645.  
  1646.         rednet.send(hostID, "user((".. username .. "))end")
  1647.         senderId, message, distance = rednet.receive(1.2)
  1648.         if senderId == hostID then
  1649.             if string.find(message, "success((",1,true)  then
  1650.                 myuser = username
  1651.  
  1652.                 local temp =  string.sub(message,10)
  1653.                 serverdata = textutils.unserialize(temp)
  1654.  
  1655.                 loadInfo(serverdata)
  1656.  
  1657.                 rednet.close(modemSide)
  1658.                 return true
  1659.             elseif message == "takenuser" then
  1660.                 for i=7,15 do
  1661.                     paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1662.                 end
  1663.                 eWrite("That username was",19,10)
  1664.                 eWrite("already taken",20,11)
  1665.                 os.pullEvent("mouse_click")
  1666.             end
  1667.         end
  1668.         timeout = true
  1669.     end
  1670.     return nil
  1671. end
  1672.  
  1673. --[[
  1674. logout():
  1675.     Oh boy, I wonder what this one does? :P
  1676. ]]
  1677. local function logout()
  1678.     term.setBackgroundColour(colours.white)
  1679.     term.clear()
  1680.     for i=9,11 do
  1681.         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1682.     end
  1683.     term.setBackgroundColour(colours.lightGrey)
  1684.     term.setTextColour(colours.black)
  1685.     eWrite("logging out...",18,10)
  1686.  
  1687.     rednet.open(modemSide)
  1688.     for i=1,12 do
  1689.         rednet.send(hostID,"disconnectmeFtsp((" .. myuser .. "))end")
  1690.         local senderId, message, distance = rednet.receive(2.5)
  1691.         if senderId then
  1692.             if message == "disconnected" then
  1693.                 eWrite("successful logout",16,10)
  1694.                 rednet.close(modemSide)
  1695.                 return true
  1696.             end
  1697.         end
  1698.     end
  1699.     rednet.open(modemSide)
  1700.     return false
  1701. end
  1702.  
  1703. --[[
  1704.     This guy watches out for incomming messages from
  1705.     the host and either calls printChatWindow() to print
  1706.     them or acts on them accordingly.
  1707. ]]
  1708. local function clientReceiver()
  1709.     local event, side, frequency, replyFreqency, message, distance
  1710.     local temp, temp1, temp2 = "",0,0
  1711.     local firstIndice, secondIndice = 0,0
  1712.     local username = ""
  1713.  
  1714.     modem.open(channelTwo)
  1715.     while running == true do
  1716.         os.startTimer(5)
  1717.         event, side, frequency, replyFreqency, message, distance = os.pullEvent()
  1718.         if event == "modem_message" then
  1719.             if frequency == channelTwo then
  1720.                 if string.find(message,"userdisconnect((",1,true)
  1721.                 and string.find(message,"))end",1,true) then
  1722.                     temp1, temp2 = string.find(message,"userdisconnect((",1,true)
  1723.                     firstIndice = temp2 + 1
  1724.  
  1725.                     temp1, temp2 = string.find(message,"))end",1,true)
  1726.                     secondIndice = temp1 - 1
  1727.  
  1728.                     username = string.sub(message,firstIndice,secondIndice)
  1729.  
  1730.                     printChatWindow(username .. " left the chat server.")
  1731.                     removeUser(username)
  1732.  
  1733.                 elseif string.find(message,"userconnect((",1,true)
  1734.                 and string.find(message,"))end",1,true) then
  1735.                     temp1, temp2 = string.find(message,"userconnect((",1,true)
  1736.                     firstIndice = temp2 + 1
  1737.  
  1738.                     temp1, temp2 = string.find(message,"))end",1,true)
  1739.                     secondIndice = temp1 - 1
  1740.  
  1741.                     username = string.sub(message,firstIndice,secondIndice)
  1742.  
  1743.                     printChatWindow(username .. " joined the chat server.")
  1744.                     addUser(username)
  1745.  
  1746.                 elseif string.find(message,"lionelhuttXX((SERVERSHUTDOWN))Xx",1,true) then
  1747.                     printChatWindow("The server is shutting down, please click somewhere on the screen.")
  1748.                     running = false
  1749.                     sleep(4)
  1750.                     break
  1751.                 else
  1752.                     --if scrollAmount < lastLine then
  1753.                     --  scrollChatWindow(lastLine - scrollAmount)
  1754.                     --end
  1755.                     printChatWindow(message)
  1756.                 end
  1757.             end
  1758.         elseif event == "timer" then
  1759.             if running == false then
  1760.                 break
  1761.             end
  1762.         end
  1763.     end
  1764.     modem.close(channelTwo)
  1765. end
  1766.  
  1767. --[[
  1768. clientUI:
  1769.     controls all the user input and interaction
  1770.     on the client end.
  1771. ]]
  1772. local function clientUI()
  1773.     local action
  1774.     local chatString
  1775.     setupGUI()
  1776.     while running == true do
  1777.         event, param1, param2, param3 = os.pullEvent()
  1778.         if event == "mouse_click" then
  1779.             if param2 < 28 and param3 > 16 then
  1780.                 chatString = readMessage()
  1781.                 modem.transmit(channelOne,channelTwo,"user((".. myuser .."))message((".. chatString .."))end")
  1782.             elseif param2 < 6 and param3 == 1 then
  1783.                 term.setBackgroundColour(colours.red)
  1784.                 eWrite("[Menu]",1,1)
  1785.                 sleep(0.2)
  1786.                 term.setBackgroundColour(colours.orange)
  1787.                 eWrite("[Menu]",1,1)
  1788.                 action = hotBarMenu()
  1789.                 if action == 1 then
  1790.                     running = false
  1791.                     local test = logout()
  1792.                     sleep(1)
  1793.                     break
  1794.                 end
  1795.             elseif param2 == 34 and param3 == 15 then
  1796.                 scrollChatWindow(1)
  1797.             elseif param2 == 34 and param3 == 3 then
  1798.                 scrollChatWindow(-1)
  1799.             end
  1800.         end
  1801.     end
  1802. end
  1803.  
  1804. -- ******** Program Functionality Begins *********
  1805.  
  1806. --[[ And here we have the actual stuff; where all the
  1807.     magic comes together! ]]
  1808.  
  1809. displayHomeScreen()
  1810.  
  1811. --Check for Modems
  1812. term.setTextColour(colours.black)
  1813.     for i=13, 15 do
  1814.         paintutils.drawLine(14,i,37,i,colours.lightGrey)
  1815.     end
  1816. term.setCursorPos(15,14)
  1817. term.write("Checking for modems...")
  1818. sleep(0.75)
  1819.  
  1820. if peripheral.getType("top") == "modem" then
  1821.     modem = peripheral.wrap("top")
  1822.     modemSide = "top"
  1823. elseif peripheral.getType("left") == "modem" then
  1824.     modem = peripheral.wrap("left")
  1825.     modemSide = "left"
  1826. elseif peripheral.getType("right") == "modem" then
  1827.     modem = peripheral.wrap("right")
  1828.     modemSide = "right"
  1829. elseif peripheral.getType("back") == "modem" then
  1830.     modem = peripheral.wrap("back")
  1831.     modemSide = "back"
  1832. else
  1833.     term.setBackgroundColour(colours.white)
  1834.     term.setTextColour(colours.lightBlue)
  1835.     term.clear()
  1836.     eWrite("No modems were found, please place",8,4)
  1837.     eWrite("a modem either on the top, right, left",8,5)
  1838.     eWrite("or back side.",10,6)
  1839.     eWrite("Click anywhere to exit",8,8)
  1840.     os.pullEvent("mouse_click")
  1841.     term.setCursorPos(1,4)
  1842.     term.setTextColour(colours.white)
  1843.     term.setBackgroundColour(colours.black)
  1844.     term.clear()
  1845.     return
  1846. end
  1847.  
  1848. -- ******** Main Loop *********
  1849.  
  1850. while true do
  1851.     displayHomeScreen()
  1852.     choice = homeMenu()
  1853.     if choice == 1 then --start new server
  1854.         hosting = true
  1855.         running = true
  1856.         displayLoadScreen()
  1857.         math.randomseed(myID)
  1858.         channelOne = math.random(0,65535)
  1859.         channelTwo = math.random(0,65535)       --is this an OK idea?
  1860.         parallel.waitForAll(hostUI,hostConnectionManager,hostChatManager)
  1861.     elseif choice == 2 then --restore server
  1862.         hosting = true
  1863.         running = true
  1864.         restoreServer()
  1865.         displayLoadScreen()
  1866.         parallel.waitForAll(hostUI,hostConnectionManager,hostChatManager)
  1867.     elseif choice == 3 then
  1868.         running = true
  1869.         local success = login()
  1870.         if success then
  1871.             parallel.waitForAll(clientUI,clientReceiver)
  1872.         end
  1873.     elseif choice == 0 then --quit
  1874.         displayExit()
  1875.         sleep(2)
  1876.         break
  1877.     end
  1878.  
  1879.     --reset all the necessary variables
  1880.  
  1881.     hosting = false
  1882.     serverName = ""
  1883.     running = false
  1884.     chatList = {}
  1885.     chatScreenList = {}
  1886.     lastLine = 0
  1887.     lastScreenLine = 0
  1888.     scrollAmount = 0
  1889.     chatExistsAlready = false
  1890.  
  1891.     userList = {}
  1892.     userScreenList = {}
  1893.     lastULline = 0
  1894.     lastULScreenLine = 0
  1895.     ULscrollAmount = 0
  1896.     usersAlreadyOn = false
  1897.  
  1898.     hostID, myID = nil, os.getComputerID()
  1899.     channelOne = nil
  1900.     channelTwo = nil
  1901.  
  1902.     sessionList = {}
  1903.     noSessions = 0
  1904.     newSession = nil
  1905.     sFilePath = nil
  1906.     sPassword = ""
  1907.  
  1908.     myuser = ""
  1909. end
  1910.  
  1911. -- clean up
  1912.  
  1913. term.setBackgroundColour(colours.black)
  1914. term.setCursorPos(1,1)
  1915. term.clear()
  1916.  
  1917. -- Wellp, there's my program! Thanks for reading. :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement