Advertisement
Xemiru

xIRC_Client

Nov 26th, 2012
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.58 KB | None | 0 0
  1. term.clear()
  2. version = "v0.2"
  3. revision = "rev160"
  4.  
  5. -- Startup variables/functions.
  6.  
  7. tx,ty = term.getSize()
  8. local cfgname = ""
  9. local autoafk = false
  10.  
  11. local function process(message)
  12.  local returned = {}
  13.  message:gsub("{(.-)}", function(word)
  14.   --Format: "{actiontype} {input}"
  15.   returned[#returned+1] = word
  16.  end)
  17.  
  18.  return unpack(returned)
  19. end
  20.  
  21. local function setData(sName, bAutoafk)
  22.  h = fs.open("ircsettings", "w")
  23.  h.writeLine("name="..sName)
  24.  h.writeLine("autoafk="..bAutoafk)
  25.  h.close()
  26. end
  27.  
  28. local function getData()
  29.  returned = {}
  30.  h = fs.open("ircsettings", "r")
  31.  returned[#returned + 1] = h.readLine()
  32.  returned[#returned + 1] = h.readLine()
  33.  h.close()
  34.  
  35.  return unpack(returned)
  36. end
  37.  
  38. local function printHeader(headerstring)
  39.  term.clear()
  40.  term.setCursorPos(1,1)
  41.  local tx,ty = term.getSize()
  42.  print(headerstring)
  43.  write(" ")
  44.  repeat
  45.   cx,cy = term.getCursorPos()
  46.   write("-")
  47.  until cx == tx - 1
  48.  term.setCursorPos(1,3)
  49. end
  50.  
  51. -- Startup.
  52.  
  53. printHeader("xIRC Client "..version.." "..revision.." - Setup")
  54. write("Searching for a modem.")
  55. textutils.slowWrite("...", 1)
  56. sleep(1)
  57. term.setCursorPos(1,4)
  58.  
  59. for k,v in ipairs(rs.getSides()) do
  60.  if peripheral.getType(v) == "modem" then
  61.   print("Found a modem. Enabling rednet.")
  62.   modemside = v
  63.   sleep(0.3)
  64.   break
  65.  end
  66. end
  67.  
  68. if modemside == nil then
  69.  print("Failed to find a modem.")
  70.  print("This requires a modem to function correctly.")
  71.  print("Terminating.")
  72.  sleep(3)
  73.  term.clear()
  74.  term.setCursorPos(1,1)
  75.  susuccess = false
  76.  return
  77. else
  78.  rednet.open(modemside)
  79. end
  80.  
  81. if not fs.exists("ircsettings") then
  82.  printHeader("xIRC Client "..version.." "..revision.." - Setup")
  83.  print("You don't have any user data on this computer.")
  84.  print("Create user data?")
  85.  print("[Enter] = Yes; [Backspace] = No;")
  86.  while true do
  87.   event, key = os.pullEvent("key")
  88.   if key == 28 then --enter
  89.    local afkinput = ""
  90.    local nameinput = ""
  91.    print("Enter your name for this computer.")
  92.    cx,cy = term.getCursorPos()
  93.    write(">> ")
  94.    nameinput = io.read()
  95.    term.setCursorPos(1,cy + 1)
  96.    print("Allow AutoAFK?")
  97.    print("[Enter] = Yes; [Backspace] = No;")
  98.    while true do
  99.     event, key = os.pullEvent("key")
  100.     if key == 28 then
  101.      print("AutoAFK is enabled.")
  102.      afkinput = "true"
  103.      autoafk = true
  104.      break
  105.     elseif key == 14 then
  106.      print("AutoAFK is disabled.")
  107.      afkinput = "false"
  108.      autoafk = true
  109.      break
  110.     end
  111.    end
  112.    cfgname = nameinput
  113.    sleep(0.3)
  114.    setData(nameinput,afkinput)
  115.    break
  116.   elseif key == 14 then --backspace
  117.    print("Skipping data creation.")
  118.    print("You still need a name, though.")
  119.    cx,cy = term.getCursorPos()
  120.    write(">> ")
  121.    cfgname = io.read()
  122.    autoafk = true
  123.    term.setCursorPos(1,cy + 1)
  124.    sleep(0.3)
  125.    break
  126.   end
  127.  end
  128. else
  129.  name,afk = getData()
  130.  cfgname = string.sub(name, 6)
  131.  if string.sub(afk, 9) == "true" then
  132.   autoafk = true
  133.  else
  134.   autoafk = false
  135.  end
  136. end
  137.  
  138. printHeader("xIRC Client "..version.." "..revision.." - Setup")
  139. print("Finding you a server...")
  140. local timeout = os.startTimer(1)
  141. rednet.broadcast("{finder} {}")
  142. local servers = {}
  143. local ids = {}
  144. while true do
  145.  event, sender, message = os.pullEvent()
  146.  if event == "timer" and sender == timeout then
  147.   break
  148.  elseif event == "rednet_message" then
  149.   actiontype, ainput = process(message)
  150.   ids["["..tostring(sender).."]"] = {}
  151.   ids["["..tostring(sender).."]"].name = actiontype
  152.   ids["["..tostring(sender).."]"].isHidden = ainput
  153.  end
  154. end
  155.  
  156. term.clear()
  157. term.setCursorPos(1,1)
  158.  
  159. local count = 0
  160. for k,v in pairs(ids) do
  161.  if v.isHidden == "false" then
  162.   count = count + 1
  163.   if count >= ty - 4 then
  164.    term.setCursorPos(tx / 2, (k - ty) + 1)
  165.    write(k.." "..v.name)
  166.   else
  167.    print(k.." "..v.name)
  168.   end
  169.  end
  170. end
  171.  
  172. term.setCursorPos(1,ty - 3)
  173. print("Enter the ID of the server to join; \"exit\" to leave; Reload by restarting the program.")
  174. write(">> ")
  175. while true do
  176.  input2 = read()
  177.  input2 = "["..input2.."]"
  178.  term.setCursorPos(1, ty - 1)
  179.  term.clearLine()
  180.  write(">> ")
  181.  if input2 == "[exit]" then
  182.   print("Exited selection.")
  183.   return
  184.  elseif ids[input2] ~= nil then
  185.   break
  186.  end
  187. end
  188.  
  189. local failed = false
  190. term.clear()
  191. term.setCursorPos(1,1)
  192. write("Attempting to join "..ids[input2].name..".")
  193. textutils.slowWrite("...", 1)
  194. sleep(3)
  195. local input = 0
  196. input2:gsub("[(.-)]", function(word)
  197.   input = word
  198.  end)
  199. rednet.send(tonumber(input), "{join} {"..cfgname.."}")
  200. while true do
  201.  sender,message = rednet.receive(3)
  202.  if sender == nil or sender == "" then
  203.   print("[Sys> Failed to connect. Read timed out.")
  204.   failed = true
  205.   break
  206.  else
  207.   action, ainput = process(message)
  208.   if action == "result" then
  209.    if ainput == "j-success" then
  210.     failed = false
  211.     break
  212.    elseif ainput == "nameconflict" then
  213.     print("[Sys> Someone is online with your name.")
  214.     break
  215.    elseif ainput == "idconflict" then
  216.     print("[Sys> The ID of this computer is already connected.")
  217.     break
  218.    elseif ainput == "banned" then
  219.     print("[Sys> This computer, or this name, is banned from this server.")
  220.     break
  221.    end
  222.   end
  223.  end
  224. end
  225.  
  226. if failed == true then
  227.  return
  228. end
  229.  
  230. local host = tonumber(input)
  231.  
  232. -- Interior variables/functions.
  233.  
  234. local lastmessage = ""
  235. local centered = 0
  236. local entered = ""
  237. local messages = 1
  238.  
  239. local function redrawInput()
  240.  term.setCursorPos(1, ty)
  241.  term.clearLine()
  242.  write(">> "..entered)
  243.  term.setCursorPos(string.len(entered) + 4, ty)
  244. end
  245.  
  246. --[[v1 = 1 + (tx * lines)
  247. v2 = (tx * lines) + tx]]--
  248.  
  249. local function redrawScreen(lastmsg)
  250.  messagelines = string.len(input) / tx
  251.  lines = math.floor(messagelines)
  252.  n1 = string.len(lastmsg) / tx
  253.  n2 = math.floor(n1) + 1
  254.  messages = messages + n2
  255.  cx,cy = term.getCursorPos()
  256.  term.setCursorPos(1,ty)
  257.  term.clearLine()
  258.  term.setCursorPos(1,cy)
  259.  
  260.  print(lastmsg)
  261.  redrawInput()
  262. end
  263.  
  264. -- Interior.
  265.  
  266. term.setCursorBlink(true)
  267. redrawInput()
  268. redrawScreen("[Sys> Successfully joined.")
  269.  
  270. while true do
  271.  event, p1, p2 = os.pullEventRaw()
  272.  if event == "char" then
  273.   cx,cy = term.getCursorPos()
  274.   if cx ~= tx then
  275.    entered = entered..p1
  276.    centered = centered + 1
  277.   end
  278.   redrawInput()
  279.  elseif event == "key" then
  280.   if p1 == 28 then --enter
  281.    local invalid = false
  282.    local testinput = ""
  283.    
  284.    for word in string.gmatch(entered, "[^%s]+") do
  285.     testinput = testinput..word
  286.    end
  287.      
  288.    if testinput == "" or testinput == nil then
  289.     invalid = true
  290.    end
  291.    
  292.    if invalid == false then
  293.     term.setCursorPos(1, messages + 1)
  294.     term.clearLine()
  295.     if string.sub(entered, 1, 1) == "/" then
  296.      commandinput = string.sub(entered, 2, string.len(entered))
  297.      if string.lower(string.sub(commandinput, 1, 5)) == "leave" then
  298.       rednet.send(tonumber(host), "{leave} {}")
  299.       term.clear()
  300.       term.setCursorPos(1,1)
  301.       print("[Sys> Exited server.")
  302.       return
  303.      end
  304.      
  305.      success = rednet.send(tonumber(host), "{command} {"..commandinput.."}")
  306.     else
  307.      success = rednet.send(tonumber(host), "{message} {"..entered.."}")
  308.     end
  309.    end
  310.    
  311.    term.setCursorPos(1, ty)
  312.    term.clearLine()
  313.    write(">> ")
  314.    centered = 0
  315.    entered = ""
  316.    
  317.    if success == false then
  318.     redrawScreen("[Sys> Lost connection to the server.")
  319.    end
  320.   elseif p1 == 14 then --backspace
  321.    if entered ~= "" then
  322.     centered = centered - 1
  323.     entered = entered:sub(1, string.len(entered) - 1)
  324.     redrawInput()
  325.    end
  326.   end
  327.  elseif event == "rednet_message" then
  328.   local sender = p1
  329.   local message = p2
  330.   if sender == host then
  331.    actiontype,ainput = process(message)
  332.    if actiontype == "message" then
  333.     if lastmessage ~= ainput then
  334.      lastmessage = ainput
  335.      messages = messages + 1
  336.      redrawScreen(ainput)
  337.     end
  338.    elseif actiontype == "kick" then
  339.     redrawScreen("[Sys> You have been kicked out of the server.")
  340.     redrawScreen("[Sys> Reason: "..ainput)
  341.     term.setCursorPos(1,ty)
  342.     term.clearLine()
  343.     return
  344.    elseif actiontype == "ban" then
  345.     redrawScreen("[Sys> You have been banned from the server.")
  346.     redrawScreen("[Sys> Reason: "..ainput)
  347.     term.setCursorPos(1,ty)
  348.     term.clearLine()
  349.     return
  350.    elseif actiontype == "terminate" then
  351.     redrawScreen("[Sys> Server shutting down.")
  352.     sleep(0.3)
  353.     term.clear()
  354.     term.setCursorPos(1,1)
  355.     print("[Sys> Lost connection to the server.")
  356.     rednet.close(modemside)
  357.     return
  358.    end
  359.   end
  360.  elseif event == "terminate" then
  361.   rednet.send(tonumber(host), "{leave} {}")
  362.   rednet.close(modemside)
  363.   term.clear()
  364.   term.setCursorPos(1,1)
  365.   print("[Sys> Exited server.")
  366.   return
  367.  end
  368. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement