Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public_quieve = {}
- private_quieve = {}
- sizeX, sizeY = term.getSize()
- message_display_area = 5
- text_input_area = 1
- notification_area = 3
- login = nil
- password = nil
- current_chat = "public_chat"
- master_id = 2414
- login_server_id = 2414
- message_server_id = 2426
- message_refresh_interval = 2
- --helper func
- function detectPeripheral(name)
- local sides = {"top" , "bottom" , "front" , "left" , "right" , "back"}
- for i = 1, 6 do
- if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
- return sides[i]
- end
- end
- return nil
- end
- function getID()
- local id = tostring(os.getComputerID())
- return(string.rep("0", 6 - string.len(id))..id)
- end
- --formating functions
- function notify(notification)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, notification_area)
- term.clearLine()
- term.write(notification)
- end
- function warn(warning)
- term.setBackgroundColor(colors.red)
- term.setCursorPos(1, notification_area)
- term.clearLine()
- term.write(warning)
- end
- function getInput()
- term.setCursorPos(1, text_input_area)
- term.clearLine()
- local input = read()
- return input
- end
- function clearChat()
- for i=message_display_area - 1,sizeY do --KOSTYL, in case of trouble
- term.setCursorPos(1, i)
- term.clearLine()
- end
- term.setCursorPos(1, text_input_area)
- end
- --handler functions
- function manageRedentMessage()
- local id, message = rednet.receive()
- --notify(" Receiving message "..message.."ID "..id.."MID "..master_id)
- if id == message_server_id then
- --manageRequest(message)
- table.insert(public_quieve, message)
- end
- end
- function manageRequest(request)
- local sender = nil
- local message = nil
- local start = 1
- local separator = string.find(request, ":")
- print("starting function")
- sender = string.sub(request, start, separator)
- message = string.sub(request, separator)
- print("adding message")
- if string.sub(request, 1, 3) == "PRV" then
- start = 4
- message = string.sub(request, separator)
- notify("adding message to priv_quieve")
- table.insert(public_quieve, sender.." : "..message)
- else
- notify("adding message to private_quieve")
- table.insert(private_quieve, sender.." : "..message)
- end
- end
- function printChat()
- while true do
- if current_chat == "public_chat" then
- printMessages(public_quieve)
- elseif current_chat == "private_chat" then
- printMessages(private_quieve)
- end
- term.setCursorPos(1, text_input_area) -- more KOSTYL for KOSTYL GOD!
- sleep(message_refresh_interval)
- end
- end
- function printMessages(chat_quieve)
- clearChat()
- local first_message = 1
- local screen_capacity = sizeY - message_display_area
- table_size = table.maxn(chat_quieve)
- if table_size > screen_capacity then
- first_message = table_size - screen_capacity
- end
- if table_size == 0 then
- term.setCursorPos(1, message_display_area)
- print("no messages")
- else
- term.setCursorPos(1, message_display_area)
- for i= first_message, table_size do
- print(chat_quieve[i])
- end
- end
- term.setCursorPos(1, message_display_area - 1)
- for i = 1, sizeY do
- term.write("-")
- end
- end
- function receive_input()
- while true do
- term.setCursorPos(1, text_input_area)
- local message = read()
- rednet.send(2414, "CHP BRC "..login.." "..message.." "..getID())
- term.setCursorPos(1, text_input_area)
- term.clearLine()
- end
- end
- function send_message(msg)
- rednet.send()
- end
- function manage_authentification(auth_data, login, password)
- if auth_data == "login" then
- rednet.send(login_server_id, "LGP LOG "..login.." "..password.." "..getID())
- while true do
- local sender_id, msg = rednet.receive(10)
- if not sender_id == nil then
- print("ID ".. sender_id )
- end
- if sender_id == login_server_id then
- if msg == "SRV 200" then
- notify("OK")
- return true
- elseif msg == "SRV 202" then
- warn("No such user")
- return false
- elseif msg == "SRV 203" then
- warn("Wrong password")
- return false
- elseif msg == "SRV 204" then
- notify("Allready logged in")
- return true
- else
- warn("SASI Error")
- return false
- end
- elseif sender_id == "nil" then
- warn("Service Unavaiable")
- return false
- end
- end
- elseif auth_data == "register" then
- rednet.send(login_server_id, "LGP REG "..login.." "..password.." "..getID())
- while true do
- local sender_id, msg = rednet.receive(10)
- if not sender_id == nil then
- print("ID ".. sender_id )
- end
- if sender_id == login_server_id then
- if msg == "SRV 200" then
- notify("OK")
- return true
- elseif msg == "SRV 201" then
- warn("login is unavaiable")
- return false
- else
- warn("SASI Error")
- return false
- end
- elseif sender_id == nil then
- warn("Service is unavaiable")
- return false
- end
- end
- end
- end
- rednet.open(detectPeripheral("modem"))
- --authentificaton loop
- authentificated = false
- while not authentificated do
- term.setBackgroundColor(colors.black)
- term.clear()
- notify("Hello. Type register to register and login to login")
- print("Warring! Don't use password your account")
- auth_data = getInput()
- if auth_data == "login" then
- notify("Enter your login")
- login = getInput()
- --Check if allreay logged in
- rednet.send(login_server_id, "LGP CHK "..login.." "..getID().." "..getID())
- sender_id, msg = rednet.receive(10)
- notify("Enter your password")
- password = getInput("*")
- authentificated = manage_authentification(auth_data, login, password)
- sleep(2)
- elseif auth_data == "register" then
- notify("Enter desired login")
- login = getInput()
- notify("Enter desired password")
- term.setCursorPos(1, text_input_area)
- password = getInput()
- authentificated = manage_authentification(auth_data, login, password)
- sleep(2)
- end
- end
- term.clear()
- while true do
- parallel.waitForAny(receive_input, manageRedentMessage, printChat) -- print messages and receive status
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement