Advertisement
giintv

KoproChat 0.001 Latest

Aug 19th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.02 KB | None | 0 0
  1. public_quieve = {}
  2. private_quieve = {}
  3. sizeX, sizeY = term.getSize()
  4. message_display_area = 5
  5. text_input_area = 1
  6. notification_area = 3
  7. login = nil
  8. password = nil
  9. current_chat = "public_chat"
  10. master_id = 2414
  11. login_server_id = 2414
  12. message_server_id = 2426
  13. message_refresh_interval = 2
  14.  
  15. --helper func
  16. function detectPeripheral(name)  
  17.     local sides = {"top" , "bottom" , "front" , "left" , "right" , "back"}    
  18.         for i = 1, 6 do
  19.             if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
  20.                 return sides[i]
  21.             end
  22.         end
  23.     return nil
  24. end
  25.  
  26. function getID()
  27.         local id = tostring(os.getComputerID())
  28.         return(string.rep("0", 6 - string.len(id))..id)
  29. end
  30.  
  31. --formating functions
  32.  
  33. function notify(notification)
  34.         term.setBackgroundColor(colors.black)
  35.         term.setCursorPos(1, notification_area)
  36.         term.clearLine()
  37.         term.write(notification)
  38. end
  39.  
  40. function warn(warning)
  41.         term.setBackgroundColor(colors.red)
  42.         term.setCursorPos(1, notification_area)
  43.         term.clearLine()
  44.         term.write(warning)
  45. end
  46.  
  47. function getInput()
  48.         term.setCursorPos(1, text_input_area)
  49.         term.clearLine()
  50.         local input = read()
  51.         return input
  52. end
  53.  
  54. function clearChat()
  55.     for i=message_display_area - 1,sizeY do --KOSTYL, in case of trouble
  56.         term.setCursorPos(1, i)
  57.         term.clearLine()
  58.     end
  59.     term.setCursorPos(1, text_input_area)
  60. end
  61.  
  62. --handler functions
  63. function manageRedentMessage()
  64.         local id, message  = rednet.receive()
  65.         --notify(" Receiving message "..message.."ID "..id.."MID "..master_id)
  66.         if id == message_server_id then
  67.                 --manageRequest(message)
  68.         table.insert(public_quieve, message)
  69.         end
  70. end
  71.  
  72.  
  73. function manageRequest(request)
  74.         local sender = nil
  75.         local message = nil
  76.         local start = 1
  77.         local separator = string.find(request, ":")
  78.     print("starting function")
  79.         sender = string.sub(request, start, separator)
  80.         message = string.sub(request, separator)
  81.     print("adding message")
  82.         if string.sub(request, 1, 3) == "PRV" then
  83.         start = 4
  84.         message = string.sub(request, separator)
  85.             notify("adding message to priv_quieve")
  86.                 table.insert(public_quieve, sender.." : "..message)
  87.         else       
  88.             notify("adding message to private_quieve")
  89.                 table.insert(private_quieve, sender.." : "..message)
  90.         end
  91. end
  92.  
  93. function printChat()
  94.     while true do
  95.         if current_chat == "public_chat" then
  96.                 printMessages(public_quieve)
  97.         elseif current_chat == "private_chat" then
  98.                 printMessages(private_quieve)
  99.         end
  100.     term.setCursorPos(1, text_input_area) -- more KOSTYL for KOSTYL GOD!
  101.     sleep(message_refresh_interval)
  102.     end    
  103. end
  104.  
  105.  
  106. function printMessages(chat_quieve)
  107.     clearChat()
  108.         local first_message = 1
  109.         local screen_capacity = sizeY - message_display_area
  110.         table_size = table.maxn(chat_quieve)
  111.         if table_size > screen_capacity then
  112.             first_message = table_size - screen_capacity
  113.         end
  114.         if table_size == 0 then
  115.                 term.setCursorPos(1, message_display_area)
  116.                 print("no messages")
  117.         else
  118.                 term.setCursorPos(1, message_display_area)
  119.                 for i= first_message, table_size do
  120.                         print(chat_quieve[i])
  121.                 end
  122.         end
  123.     term.setCursorPos(1, message_display_area - 1)
  124.     for i = 1, sizeY do
  125.         term.write("-")
  126.     end
  127.  
  128. end
  129.  
  130.  
  131. function receive_input()
  132.     while true do
  133.     term.setCursorPos(1, text_input_area)
  134.     local message = read()
  135.     rednet.send(2414, "CHP BRC "..login.." "..message.." "..getID())
  136.     term.setCursorPos(1, text_input_area)
  137.     term.clearLine()
  138.     end
  139. end
  140.  
  141.  
  142.  
  143. function send_message(msg)
  144.     rednet.send()
  145. end
  146.  
  147.  
  148. function manage_authentification(auth_data, login, password)
  149.         if auth_data == "login" then
  150.                 rednet.send(login_server_id, "LGP LOG "..login.." "..password.." "..getID())
  151.                 while true do
  152.                         local sender_id, msg = rednet.receive(10)
  153.                         if not sender_id == nil then
  154.                                 print("ID ".. sender_id )
  155.                         end
  156.                         if sender_id == login_server_id then
  157.                                 if msg == "SRV 200" then
  158.                                         notify("OK")
  159.                                         return true
  160.                                 elseif msg == "SRV 202" then
  161.                                         warn("No such user")
  162.                                         return false
  163.                                 elseif msg == "SRV 203" then
  164.                                         warn("Wrong password")
  165.                                         return false
  166.                                 elseif msg == "SRV 204" then
  167.                                         notify("Allready logged in")
  168.                                         return true
  169.                                 else
  170.                                         warn("SASI Error")
  171.                                         return false
  172.                                 end
  173.                         elseif sender_id == "nil" then
  174.                                 warn("Service Unavaiable")
  175.                                 return false
  176.  
  177.                         end
  178.                 end
  179.         elseif auth_data == "register" then
  180.                 rednet.send(login_server_id, "LGP REG "..login.." "..password.." "..getID())  
  181.                 while true do
  182.                         local sender_id, msg = rednet.receive(10)
  183.                         if not sender_id == nil then
  184.                                 print("ID ".. sender_id )
  185.                         end
  186.                         if sender_id == login_server_id then
  187.                                 if msg == "SRV 200" then
  188.                                         notify("OK")
  189.                                         return true
  190.                                 elseif msg == "SRV 201" then
  191.                                         warn("login is unavaiable")
  192.                                         return false
  193.                                 else
  194.                                         warn("SASI Error")
  195.                                         return false
  196.                                 end
  197.                         elseif sender_id == nil then
  198.                                 warn("Service is unavaiable")
  199.                                 return false
  200.                         end
  201.                 end
  202.         end
  203. end
  204.  
  205. rednet.open(detectPeripheral("modem"))
  206. --authentificaton loop
  207. authentificated = false
  208. while not authentificated  do
  209.         term.setBackgroundColor(colors.black)
  210.         term.clear()
  211.         notify("Hello. Type register to register and login to login")
  212.     print("Warring! Don't use password your account")
  213.         auth_data = getInput()
  214.         if auth_data == "login" then
  215.                 notify("Enter your login")
  216.                 login = getInput()
  217.                 --Check if allreay logged in
  218.                 rednet.send(login_server_id, "LGP CHK "..login.." "..getID().." "..getID())
  219.                 sender_id, msg = rednet.receive(10)
  220.                 notify("Enter your password")
  221.                 password = getInput("*")
  222.                 authentificated = manage_authentification(auth_data, login, password)
  223.                 sleep(2)
  224.         elseif auth_data == "register" then
  225.                 notify("Enter desired login")
  226.                 login = getInput()
  227.                 notify("Enter desired password")
  228.                 term.setCursorPos(1, text_input_area)
  229.                 password = getInput()
  230.                 authentificated = manage_authentification(auth_data, login, password)
  231.                 sleep(2)
  232.         end
  233. end
  234.  
  235. term.clear()
  236. while true do
  237.         parallel.waitForAny(receive_input, manageRedentMessage, printChat) -- print messages and receive status
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement