eniallator

Messaging Application

Feb 7th, 2016
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. local compName = os.getComputerID()
  2. -- Defining the compName var thats default is the computer's ID
  3.  
  4. version = "1.1"
  5. -- Defining the version for my updating program
  6.  
  7. rednet.open("back")
  8. -- Opening the modem so computer can use rednet
  9.  
  10. local chatLog = {}
  11. -- Defining the chatLog table
  12.  
  13. shell.run("clear")
  14.  
  15. function initMessage()
  16.  
  17.   shell.run("clear")
  18.   term.setTextColor(colors.blue)
  19.   print("Welcome to the messaging application!\n\nWhile using the application at any time you can see available commands by using /help.\n")
  20.   -- Printing the first 2 messages in blue
  21.  
  22.   term.setTextColor(colors.lightBlue)
  23.   print("Type in the channel you would like to connect to:")
  24.   -- Printing the third message in light blue
  25.  
  26.   term.setTextColor(colors.white)
  27.   prot = read()
  28.   -- Defining the prot variable
  29.  
  30.   shell.run("clear")
  31.   -- Clearing the screen the messaging can begin
  32. end
  33. -- The messages that appear once you run the application
  34.  
  35. function wordSplit(string)
  36.  
  37.    local out = {}
  38.  
  39.    for word in string:gmatch("%S+") do
  40.  
  41.       table.insert(out, word)
  42.    end
  43.  
  44.    return out
  45. end
  46. -- Function for splitting a string into a table at the spaces
  47.  
  48. function toScreen(msg)
  49.  
  50.   table.insert(chatLog,#chatLog+1,msg)
  51.   -- Inserting the latest message into the chatLog table
  52.  
  53.   local maxX,maxY = term.getSize()
  54.   -- Defining the maximum dimensions for use in while loop
  55.  
  56.   while #chatLog > maxY-1 do
  57.  
  58.     table.remove(chatLog,1)
  59.   end
  60.   -- Removing the old chat messages
  61.  
  62.   local maxX,maxY = term.getSize()
  63.   local curX,_ = term.getCursorPos()
  64.   -- Getting the maximum size of the screen
  65.  
  66.   for y=1,maxY-1 do
  67.     for x=1,maxX do
  68.  
  69.       term.setCursorPos(x,y)
  70.       term.write(" ")
  71.     end
  72.   end
  73.   -- Clearing the screen except for the last line (the chat box)
  74.  
  75.   term.setCursorPos(1,1)
  76.  
  77.   for i=1,#chatLog do
  78.  
  79.     print(chatLog[i])
  80.   end
  81.   -- Printing the chatLog
  82.  
  83.   term.setCursorPos(curX,maxY)
  84.   -- Resetting the cursor's coordinates after print
  85. end
  86. -- Function that will log the messages to a file and will print the messages once done
  87.  
  88. function receive()
  89.   while true do
  90.  
  91.     local id, msg = rednet.receive(prot)
  92.  
  93.     if msg[1] and msg[1] == "msgAPP" and msg[2] and tostring(msg[2]) then
  94.       if msg[3] and tostring(msg[3]) then
  95.  
  96.         toScreen("<" .. msg[3] .. "> " .. msg[2])
  97.         -- Receiving a message on the connected channel (protocol) and then displaying it using the toScreen() function
  98.       else
  99.  
  100.         toScreen("<" .. id .. "> " .. msg[2])
  101.         -- Defaulting the computer name to their ID
  102.       end
  103.  
  104.       local _, height = term.getSize()
  105.       term.setCursorPos(1,height)
  106.       -- Resetting the cursor
  107.     end
  108.   end
  109. end
  110. -- Function that receives any rednet messages on the same protocol and will run in unison with the input() function
  111.  
  112. function input()
  113.   while true do
  114.  
  115.     local _, height = term.getSize()
  116.     term.setCursorPos(1,height)
  117.     local inp = read()
  118.     -- First moving the cursor to the bottom left of the screen and then getting a input from the user
  119.  
  120.     if inp:sub(1,1) == "/" then
  121.       -- Seeing if the user has entered a command or not
  122.  
  123.       local inpTab = wordSplit(inp)
  124.       -- Splitting the command into a table
  125.  
  126.       if inpTab[1]:lower() == "/help" then
  127.  
  128.         toScreen("<console> /exit - will exit the messaging application\n/nick - change your nickname")
  129.         -- First checking if the command is help and then displaying the help message if it is
  130.  
  131.       elseif inpTab[1]:lower() == "/exit" then
  132.        
  133.         rednet.broadcast({"msgAPP","Has disconnected.",compName})
  134.         break
  135.         -- First checking if the command is exit and then breaking the initial while loop so that the entire program will stop.
  136.  
  137.       elseif inpTab[1]:lower() == "/nick" then
  138.         if inpTab[2] and #inpTab[2] <= 15 then
  139.          
  140.           compName = inpTab[2]
  141.           toScreen("<console> I have changed my name to " .. inpTab[2])
  142.           rednet.broadcast({"msgAPP","Is the new name of " .. os.getComputerID(),compName},prot)
  143.           -- Broadcasting when you update your nickname
  144.  
  145.         elseif inpTab[2] and #inpTab[2] >= 16 then
  146.  
  147.           toScreen("<console> Nick too long. Has to be 15 characters or less.")
  148.           -- If the nick is too long then it will be caught here
  149.         else
  150.  
  151.           toScreen("<console> Usage: /nick *new nickname*")
  152.           -- Printing the usage of the nick command
  153.         end
  154.       else
  155.  
  156.         toScreen('<console> Unknown command. Type "/help" for options.')
  157.         -- Catching any other commands that aren't recognised and displaying an error message
  158.       end
  159.     elseif inp ~= "" then
  160.  
  161.       toScreen("<me> " .. inp)
  162.       rednet.broadcast({"msgAPP",inp,compName},prot)
  163.       -- If the input isn't a command it will get broadcasted across the same channel (protocol) to other connected computers
  164.     else
  165.      
  166.       toScreen()
  167.     end
  168.   end
  169. end
  170. -- Function that will get the user input and detect if its a command or not
  171.  
  172. initMessage()
  173. -- Running the initMessage() function so the channel gets defined
  174.  
  175. if prot ~= "" then
  176.  
  177.   toScreen("Connected to: " .. prot)
  178. else
  179.  
  180.   toScreen("Connected to the global channel.")
  181. end
  182. -- Seeing if the channel has been entered or not and giving the appropriate message
  183.  
  184. parallel.waitForAny(input,receive)
  185. -- Running both input() and receive() at the same time
Advertisement
Add Comment
Please, Sign In to add comment