CryptekCoding

IRC|Client

Mar 31st, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.90 KB | None | 0 0
  1. --[[ Coded by LoneWolf with some help ]]--
  2. --[[ Chat PC ]]--
  3.  
  4. shell.run("clear")
  5. if fs.exists("ChatSettings/Settings") then
  6.   dofile("ChatSettings/Settings")
  7. else
  8.   write("Your screen name: ")
  9.   Person = io.read()
  10.   fs.makeDir("ChatSettings")
  11.   local WriteSettings = io.open("/ChatSettings/Settings","w")
  12.   WriteSettings:write("\nPerson = "..[["]]..Person..[["]])
  13.   WriteSettings:close()
  14.   shell.run("clear")
  15. end
  16. X,Y = term.getSize()
  17. Host = "a"
  18. function openRednet()
  19.   for _,side in ipairs(rs.getSides()) do
  20.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  21.       rednet.open(side)
  22.       return side
  23.     end
  24.   end
  25.   error("No modem attached")
  26. end
  27. openRednet()
  28. function tag(tag)
  29.   return "["..tag.."]"
  30. end
  31. function tagView(msg)
  32.   local ReturnMsg = msg
  33.   local ReturnTag = {}
  34.   if string.find(ReturnMsg, "%[") and string.find(ReturnMsg, "%]") then
  35.     while true do
  36.       first = string.find(ReturnMsg, "%[", 1)
  37.       last = string.find(ReturnMsg, "%]", 2)
  38.       if first and last then ReturnTag[#ReturnTag+1] = string.sub(ReturnMsg, first+1, last-1)
  39.         ReturnMsg = string.sub(ReturnMsg, last+1, string.len(msg))
  40.       else
  41.         break
  42.       end
  43.     end
  44.     ReturnTag[#ReturnTag+1] = ReturnMsg
  45.     return unpack(ReturnTag)
  46.   else
  47.     return false
  48.   end
  49. end
  50.  
  51. local ChatInput = ""
  52. function Clear()
  53.   term.setCursorPos(1,1)
  54.   term.clear()
  55. end
  56. Clear()
  57. print("Now searching for a IRC network")
  58. rednet.broadcast("Zero Zero Two Nine'r looking for network. Over")
  59. End = os.startTimer(1)
  60. Servers = {}
  61. n = 0
  62. function Same(para)
  63.   for n=1,#Servers do
  64.     if tagView(Servers[n]) == para then
  65.       return true
  66.     end
  67.   end
  68.   return false
  69. end
  70.  
  71.  
  72. while true do
  73.   event,param1,param2 = os.pullEvent()
  74.   if param2 then Name, Confirm = tagView(param2) end
  75.   if not Confirm then
  76.     Confirm = ""
  77.   end
  78.   if event == "rednet_message" and Confirm == "We receive and we confirm. Over" then
  79.     if not Same(Name) then
  80.       Servers[#Servers+1] = tag(Name)..param1
  81.       n = n+1
  82.       term.setCursorPos(1,2)
  83.       print(n.." servers has been found")
  84.     end
  85.   elseif event == "timer" and param1 == End then
  86.     break
  87.   end
  88. end
  89. n=nil
  90.  
  91. if Servers[1] then
  92.   Clear()
  93.   for n=1,#Servers do
  94.     local x,y = term.getCursorPos()
  95.     if y == Y then
  96.       print("Press spacebar")
  97.       while true do
  98.         event,param1 = os.pullEvent()
  99.         if event == "key" and param1 == 57 then
  100.           term.clearLine()
  101.           local Name, ID = Servers[n]
  102.           print("Server: "..Name)
  103.           break
  104.         end
  105.       end
  106.     else
  107.       local Name, ID = tagView(Servers[n])
  108.       print("Server: "..Name)
  109.     end
  110.   end
  111.   write("Choose server: ")
  112.   ServerName = read()
  113.   for n=1,#Servers do
  114.     local Name, ID = tagView(Servers[n])
  115.     if Name == ServerName then
  116.       Host = tonumber(ID)
  117.     elseif string.lower(Name) == string.lower(ServerName) then
  118.       Host = tonumber(ID)
  119.     end
  120.   end
  121.   if Host == "a" then
  122.     Clear()
  123.     print("Server name isnt found!")
  124.     error()
  125.   else
  126.     rednet.send(Host, tag("You are the chosen one")..Person)
  127.   end
  128.   else
  129.     print("Network cannot be found")
  130.     error()
  131. end
  132. typex = 1
  133. Clear()
  134. print("** You are running LibChat 1.0")
  135. curx, cury = 1, 2
  136. term.setCursorPos(1, Y)
  137. term.setCursorBlink(true)
  138. Returned = true
  139. function Ping()
  140.   if Returned == true then
  141.     rednet.send(Host, "Ping")
  142.     Returned = false
  143.     Pingo = os.startTimer(1.5)
  144.     return true
  145.   else
  146.     return false
  147.   end
  148. end
  149.  
  150. Ping()
  151.  
  152. function Commands(commanda)
  153.   command = string.lower(commanda)
  154.   if command == "/ping" then
  155.     rednet.send(Host, "Pinger!")
  156.   elseif command == "/players" then
  157.     rednet.send(Host, "Players?")
  158.   elseif command == "/quit" then
  159.     Clear()
  160.     print("You have quit IRC")
  161.     error()
  162.   elseif string.sub(command, 1, 4) == "/me " then
  163.     rednet.send(Host, tag(os.clock()..os.getComputerID())..tag("LibChatServer").."* "..Person.." "..string.sub(commanda, 5))
  164.   elseif string.sub(command, 1, 6) == "/nick " and string.gsub(string.sub(commanda, 7), " ", "") ~= "" and string.len(string.gsub(string.sub(commanda, 7), " ", "")) < 15  then
  165.     rednet.send(Host, tag(os.time()..os.getComputerID())..tag("NickChange")..tag(Person)..string.sub(commanda, 7))
  166.     Person = string.sub(commanda, 7)
  167.     local WriteSettings = io.open("/ChatSettings/Settings","w")
  168.     WriteSettings:write("\nPerson = "..[["]]..Person..[["]])
  169.     WriteSettings:close()
  170.   elseif command == "/help" then
  171.     Print("Commands: /nick <nick>, /quit, /ping, /players, /help")
  172.   else
  173.     Print("** Unknown command")
  174.   end
  175. end
  176. function Print(thing)
  177.   term.setCursorPos(curx, cury)
  178.   thing = string.gsub(thing, "!LeftBracket!", "%]")
  179.   thing = string.gsub(thing, "!RightBracket!", "%[")
  180.   local x = "Derp"
  181.   if cury <= Y then
  182.     print(thing)
  183.     x,cury = term.getCursorPos()
  184.   else
  185.     term.scroll(1)
  186.     cury = cury-1
  187.     term.setCursorPos(curx,cury)
  188.     print(thing)
  189.   end
  190.   local x,y = term.getCursorPos()
  191.   term.setCursorPos(typex,Y)
  192. end
  193.  
  194. while true do
  195.   event, param1, param2 = os.pullEvent()
  196.   if event == "rednet_message" and param1 == Host then
  197.     RedOne, RedTwo = tagView(param2)
  198.     if RedOne == "LibChat" then -- Display MSG time!
  199.       Print(RedTwo)
  200.     elseif param2 == "Ping" then
  201.       rednet.send(Host, "Pong")
  202.     elseif param2 == "Pong" and param1 == Host then
  203.       Returned = true
  204.     end
  205.   elseif event == "char" then -- Typing
  206.     if string.len(ChatInput) < X-1 then
  207.       local x,y = term.getCursorPos()
  208.       term.setCursorPos(1,Y)
  209.       ChatInput = ChatInput..param1
  210.       term.clearLine()
  211.       write(ChatInput)
  212.       if typex < X then
  213.         typex = typex+1
  214.       end
  215.       term.setCursorPos(typex,Y)
  216.     end
  217.   elseif event == "key" and param1 == 28 then -- Enter
  218.     if string.gsub(ChatInput, " ", "") ~= "" then
  219.       if string.sub(ChatInput, 1, 1) == "/" then
  220.         Commands(ChatInput)
  221.       else
  222.         ChatInput = string.gsub(ChatInput, "%]", "!LeftBracket!")
  223.         ChatInput = string.gsub(ChatInput, "%[", "!RightBracket!")
  224.         rednet.send(Host, tag(os.clock()..os.getComputerID())..tag("LibChatServer")..Person..": "..ChatInput)
  225.       end
  226.       term.setCursorPos(curx, cury)
  227.       local x,y = term.getCursorPos()
  228.       term.setCursorPos(1,Y)
  229.       term.clearLine()
  230.       typex = 1
  231.       ChatInput = ""
  232.       term.setCursorPos(typex,Y)
  233.     end
  234.   elseif event == "key" and param1 == 14 then -- Backspace
  235.     term.setCursorPos(curx, cury)
  236.     ChatInput = string.sub(ChatInput, 1, -2)
  237.     local x,y = term.getCursorPos()
  238.     term.setCursorPos(1,Y)
  239.     term.clearLine()
  240.     write(ChatInput)
  241.     if typex > 1 then
  242.       typex = typex-1
  243.     end
  244.     term.setCursorPos(typex,Y)
  245.   elseif event == "timer" and param1 == Pingo then
  246.     if not Ping() then
  247.       term.setCursorPos(curx, cury)
  248.       print("Client has lost connection to the server")
  249.       break
  250.     end
  251.   end
  252. end
Add Comment
Please, Sign In to add comment