Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local compName = os.getComputerID()
- -- Defining the compName var thats default is the computer's ID
- version = "1.1"
- -- Defining the version for my updating program
- rednet.open("back")
- -- Opening the modem so computer can use rednet
- local chatLog = {}
- -- Defining the chatLog table
- shell.run("clear")
- function initMessage()
- shell.run("clear")
- term.setTextColor(colors.blue)
- print("Welcome to the messaging application!\n\nWhile using the application at any time you can see available commands by using /help.\n")
- -- Printing the first 2 messages in blue
- term.setTextColor(colors.lightBlue)
- print("Type in the channel you would like to connect to:")
- -- Printing the third message in light blue
- term.setTextColor(colors.white)
- prot = read()
- -- Defining the prot variable
- shell.run("clear")
- -- Clearing the screen the messaging can begin
- end
- -- The messages that appear once you run the application
- function wordSplit(string)
- local out = {}
- for word in string:gmatch("%S+") do
- table.insert(out, word)
- end
- return out
- end
- -- Function for splitting a string into a table at the spaces
- function toScreen(msg)
- table.insert(chatLog,#chatLog+1,msg)
- -- Inserting the latest message into the chatLog table
- local maxX,maxY = term.getSize()
- -- Defining the maximum dimensions for use in while loop
- while #chatLog > maxY-1 do
- table.remove(chatLog,1)
- end
- -- Removing the old chat messages
- local maxX,maxY = term.getSize()
- local curX,_ = term.getCursorPos()
- -- Getting the maximum size of the screen
- for y=1,maxY-1 do
- for x=1,maxX do
- term.setCursorPos(x,y)
- term.write(" ")
- end
- end
- -- Clearing the screen except for the last line (the chat box)
- term.setCursorPos(1,1)
- for i=1,#chatLog do
- print(chatLog[i])
- end
- -- Printing the chatLog
- term.setCursorPos(curX,maxY)
- -- Resetting the cursor's coordinates after print
- end
- -- Function that will log the messages to a file and will print the messages once done
- function receive()
- while true do
- local id, msg = rednet.receive(prot)
- if msg[1] and msg[1] == "msgAPP" and msg[2] and tostring(msg[2]) then
- if msg[3] and tostring(msg[3]) then
- toScreen("<" .. msg[3] .. "> " .. msg[2])
- -- Receiving a message on the connected channel (protocol) and then displaying it using the toScreen() function
- else
- toScreen("<" .. id .. "> " .. msg[2])
- -- Defaulting the computer name to their ID
- end
- local _, height = term.getSize()
- term.setCursorPos(1,height)
- -- Resetting the cursor
- end
- end
- end
- -- Function that receives any rednet messages on the same protocol and will run in unison with the input() function
- function input()
- while true do
- local _, height = term.getSize()
- term.setCursorPos(1,height)
- local inp = read()
- -- First moving the cursor to the bottom left of the screen and then getting a input from the user
- if inp:sub(1,1) == "/" then
- -- Seeing if the user has entered a command or not
- local inpTab = wordSplit(inp)
- -- Splitting the command into a table
- if inpTab[1]:lower() == "/help" then
- toScreen("<console> /exit - will exit the messaging application\n/nick - change your nickname")
- -- First checking if the command is help and then displaying the help message if it is
- elseif inpTab[1]:lower() == "/exit" then
- rednet.broadcast({"msgAPP","Has disconnected.",compName})
- break
- -- First checking if the command is exit and then breaking the initial while loop so that the entire program will stop.
- elseif inpTab[1]:lower() == "/nick" then
- if inpTab[2] and #inpTab[2] <= 15 then
- compName = inpTab[2]
- toScreen("<console> I have changed my name to " .. inpTab[2])
- rednet.broadcast({"msgAPP","Is the new name of " .. os.getComputerID(),compName},prot)
- -- Broadcasting when you update your nickname
- elseif inpTab[2] and #inpTab[2] >= 16 then
- toScreen("<console> Nick too long. Has to be 15 characters or less.")
- -- If the nick is too long then it will be caught here
- else
- toScreen("<console> Usage: /nick *new nickname*")
- -- Printing the usage of the nick command
- end
- else
- toScreen('<console> Unknown command. Type "/help" for options.')
- -- Catching any other commands that aren't recognised and displaying an error message
- end
- elseif inp ~= "" then
- toScreen("<me> " .. inp)
- rednet.broadcast({"msgAPP",inp,compName},prot)
- -- If the input isn't a command it will get broadcasted across the same channel (protocol) to other connected computers
- else
- toScreen()
- end
- end
- end
- -- Function that will get the user input and detect if its a command or not
- initMessage()
- -- Running the initMessage() function so the channel gets defined
- if prot ~= "" then
- toScreen("Connected to: " .. prot)
- else
- toScreen("Connected to the global channel.")
- end
- -- Seeing if the channel has been entered or not and giving the appropriate message
- parallel.waitForAny(input,receive)
- -- Running both input() and receive() at the same time
Advertisement
Add Comment
Please, Sign In to add comment