xXm0dzXx

NeXuS IRC v2.0 - Rewrite

Sep 29th, 2012
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.35 KB | None | 0 0
  1. --This program is owned and copyrighted by the NeXuS Coperation (c)
  2. --Program mainly coded by xXm0dzXx
  3. --Legal Notice:
  4. --                   DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  5. --                                   Version 2, December 2004
  6. --Copyright (C) 2004 Sam Hocevar <[email protected]>
  7. --Everyone is permitted to copy and distribute verbatim or modified
  8. --copies of this license document, and changing it is allowed as long
  9. --as the name is changed.
  10. --                   DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  11. --  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  12. --0. You just DO WHAT THE FUCK YOU WANT TO.
  13.  
  14. local messages = {};
  15. local serverHost = false;
  16. local channelOP = 0;
  17. local programName = shell.getRunningProgram()
  18. local getVersionIRC = "N-IRC v2.0"
  19. local x,y = term.getSize()
  20. local serverOPs = {}
  21. local banList = {}
  22. local function cPrint(text)
  23.    local x,y = term.getSize()
  24.    x2,y2 = term.getCursorPos()
  25.    term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  26.    write(text.. "\n")
  27. end
  28.  
  29. function newIRCREAD( _sReplaceChar, _tHistory )
  30.     term.setCursorBlink( true )
  31.  
  32.     local sLine = ""
  33.     local nHistoryPos = nil
  34.     local nPos = 0
  35.     if _sReplaceChar then
  36.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  37.     end
  38.    
  39.     local w, h = term.getSize()
  40.     local sx, sy = term.getCursorPos() 
  41.     local function redraw()
  42.         local nScroll = 0
  43.         if sx + nPos >= w then
  44.             nScroll = (sx + nPos) - w
  45.         end
  46.            
  47.         term.setCursorPos( sx, sy )
  48.         term.write( string.rep(" ", w - sx + 1) )
  49.         term.setCursorPos( sx, sy )
  50.         if _sReplaceChar then
  51.             term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  52.         else
  53.             term.write( string.sub( sLine, nScroll + 1 ) )
  54.         end
  55.         term.setCursorPos( sx + nPos - nScroll, h )
  56.     end
  57.    
  58.     while true do
  59.         local sEvent, param, message = os.pullEvent()
  60.         if sEvent == "char" then
  61.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  62.             nPos = nPos + 1
  63.             redraw()
  64.            
  65.         elseif sEvent == "key" then
  66.             if param == keys.enter then
  67.                 -- Enter
  68.                 break
  69.                
  70.             elseif param == keys.left then
  71.                 -- Left
  72.                 if nPos > 0 then
  73.                     nPos = nPos - 1
  74.                     redraw()
  75.                 end
  76.                
  77.             elseif param == keys.right then
  78.                 -- Right               
  79.                 if nPos < string.len(sLine) then
  80.                     nPos = nPos + 1
  81.                     redraw()
  82.                 end
  83.            
  84.             elseif param == keys.up or param == keys.down then
  85.                 -- Up or down
  86.                 if _tHistory then
  87.                     if param == keys.up then
  88.                         -- Up
  89.                         if nHistoryPos == nil then
  90.                             if #_tHistory > 0 then
  91.                                 nHistoryPos = #_tHistory
  92.                             end
  93.                         elseif nHistoryPos > 1 then
  94.                             nHistoryPos = nHistoryPos - 1
  95.                         end
  96.                     else
  97.                         -- Down
  98.                         if nHistoryPos == #_tHistory then
  99.                             nHistoryPos = nil
  100.                         elseif nHistoryPos ~= nil then
  101.                             nHistoryPos = nHistoryPos + 1
  102.                         end                    
  103.                     end
  104.                    
  105.                     if nHistoryPos then
  106.                         sLine = _tHistory[nHistoryPos]
  107.                         nPos = string.len( sLine )
  108.                     else
  109.                         sLine = ""
  110.                         nPos = 0
  111.                     end
  112.                     redraw()
  113.                 end
  114.             elseif param == keys.backspace then
  115.                 -- Backspace
  116.                 if nPos > 0 then
  117.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  118.                     nPos = nPos - 1                
  119.                     redraw()
  120.                 end
  121.             end
  122.         elseif sEvent == "rednet_message" then
  123.             local id = param
  124.             local _messageWords1 = split(message, ":")
  125.             local _messageWords2 = split(message, "<!:>")
  126.             local _messageLength2 = string.len("IRC_MESSAGE<!:>" .._tChannel.. "<!:>");
  127.             local _messageLength3 = string.len("IRC_COMMAND:" .._tChannel.. ":");
  128.             if serverHost and message == ("IRC_OPS:" .._tChannel) then
  129.                 rednet.send(id, textutils.serialize(serverOPs))
  130.             elseif serverHost and message == ("IRC_BAN:" .._tChannel) then
  131.                 rednet.send(id, textutils.serialize(banList))
  132.             elseif message == ("IRC_OP:" .._tChannel.. ":" .._tUser) then
  133.                 addMSG("You are now OP!")
  134.             elseif _messageWords1[1] == ("IRC_JOIN") and _messageWords1[2] == (_tChannel) then
  135.                 if isBanned( _messageWords1[3] ) ~= true then
  136.                     addMSG(_messageWords1[3].. " has joined the server!")
  137.                 end
  138.             elseif _messageWords2[1] == ("IRC_MESSAGE") and _messageWords2[2] == (_tChannel) then
  139.                 if isBanned( _messageWords2[3] ) ~= true then
  140.                     if isOP(_messageWords2[3],serverOPs) then
  141.                         addMSG("[" .._messageWords2[3].. "] " .._messageWords2[4])
  142.                     else
  143.                         addMSG("<" .._messageWords2[3].. "> " .._messageWords2[4])
  144.                     end
  145.                 end
  146.             elseif message == ("ServerPing" .._tChannel) and serverHost then
  147.                 rednet.send(id, ("Ping_Received_" .._tChannel.. "_IRC"))
  148.             end
  149.            
  150.             local x11,y11 = term.getSize()
  151.             local x12,y12 = term.getCursorPos()
  152.             term.clear()
  153.             term.setCursorPos(1,1)
  154.             print(_tUser.. " has joined the server!")
  155.             for i=1,#messages do
  156.                 print(messages[i])
  157.             end
  158.            
  159.             print()
  160.             term.setCursorPos(1,y11)
  161.             write("> " ..sLine)
  162.         end
  163.     end
  164.    
  165.     term.setCursorBlink( false )
  166.    
  167.     return sLine
  168. end
  169.  
  170. function isOP( username )
  171.     if serverHost ~= true then
  172.         rednet.send(channelOP, "IRC_OPS:" .._tChannel)
  173.         repeat
  174.             id, message2 = rednet.receive()
  175.         until id == channelOP
  176.         serverOPs = textutils.unserialize(message2)
  177.     end
  178.     for i=1,#serverOPs do
  179.         if serverOPs[i] == username then
  180.             return true
  181.         end
  182.     end
  183.     return false
  184. end
  185.  
  186. function isBanned( username )
  187.     if serverHost ~= true then
  188.         rednet.send(channelOP, "IRC_BAN:" .._tChannel)
  189.         repeat
  190.             id, message2 = rednet.receive()
  191.         until id == channelOP
  192.         banList = textutils.unserialize(message2)
  193.     end
  194.     for i=1,#banList do
  195.         if banList[i] == username then
  196.             return true
  197.         end
  198.     end
  199.     return false
  200. end
  201.  
  202. function split( sLine, sCode )
  203.     local tWords = {}
  204.     for match in string.gmatch(sLine, "[^" ..sCode.. "\t]+") do
  205.         table.insert( tWords, match )
  206.     end
  207.     return tWords
  208. end
  209.  
  210. function startProgram()
  211.     term.clear()
  212.     term.setCursorPos(1,1)
  213.     textutils.slowPrint("Opening modems...")
  214.     rednet.open("top")
  215.     textutils.slowPrint("Pinging server: " .._tChannel)
  216.     local timer = os.startTimer(1)
  217.     while true do
  218.         rednet.broadcast("ServerPing" .._tChannel)
  219.         local event, id, message = os.pullEvent()
  220.         if event == "rednet_message" and message == "Ping_Received_" .._tChannel.. "_IRC" then
  221.             channelOP = id;
  222.             serverHost = false;
  223.             textutils.slowPrint("Server found! Joining...")
  224.             sleep(0.5)
  225.             break
  226.         elseif event == "timer" and id == timer then
  227.             textutils.slowPrint("Server not detected. Hosting...")
  228.             serverHost = true;
  229.             serverOPs[1] = _tUser
  230.             sleep(0.5)
  231.             break
  232.         end
  233.     end
  234.     term.clear()
  235.     term.setCursorPos(1,1)
  236.     sleep(0)
  237.     function addMSG( messagez )
  238.         messages[#messages + 1] = messagez
  239.     end
  240.    
  241.     rednet.broadcast("IRC_JOIN:" .._tChannel.. ":" .._tUser)
  242.    
  243.     function b()
  244.         while true do
  245.             local x1,y1 = term.getCursorPos()
  246.             local x,y = term.getSize()
  247.             term.setCursorPos(1,1)
  248.             print(_tUser.. " has joined the server!")
  249.            
  250.             term.setCursorPos(1, y)
  251.             write("> ")
  252.             local messageaa = newIRCREAD()
  253.             term.setCursorPos(1,y-1)
  254.             term.clearLine()
  255.             term.setCursorPos(1,y)
  256.             term.clearLine()
  257.             term.setCursorPos(x1,y1)
  258.            
  259.             if string.sub(messageaa, 1, 1) == "/" then
  260.                 if string.sub(messageaa, 1, 3) == "/op" then
  261.                     if isOP(_tUser, serverOPs) then
  262.                         addMSG("(Console) Op'ing " ..string.sub(messageaa, 5, string.len(messageaa)))
  263.                         serverOPs[#serverOPs+1] = string.sub(messageaa, 5, string.len(messageaa))
  264.                         rednet.broadcast("IRC_OP:" .._tChannel.. ":" ..string.sub(messageaa, 5, string.len(messageaa)))
  265.                     else
  266.                         addMSG("No permission")
  267.                     end
  268.                 elseif string.sub(messageaa, 1, 5) == "/mute" then
  269.                     if isOP(_tUser, serverOPs) then
  270.                         addMSG(string.sub(messageaa, 7, string.len(messageaa)).. " has been muted.")
  271.                         banList[#banList+1] = string.sub(messageaa, 7, string.len(messageaa))
  272.                     else
  273.                         addMSG("No permission")
  274.                     end
  275.                 else
  276.                     addMSG("Unknown command.")
  277.                 end
  278.             else
  279.                 if isBanned( _tUser ) then
  280.                     addMSG("You have been muted.")
  281.                 else
  282.                     if isOP(_tUser, serverOPs) then
  283.                         addMSG("[" .._tUser.. "] " ..messageaa)
  284.                     else
  285.                         addMSG("<" .._tUser.. "> " ..messageaa)
  286.                     end
  287.                     rednet.broadcast("IRC_MESSAGE<!:>" .._tChannel.. "<!:>" .._tUser.. "<!:>" ..messageaa)
  288.                 end
  289.             end
  290.            
  291.             local x11,y11 = term.getSize()
  292.             local x12,y12 = term.getCursorPos()
  293.             term.clear()
  294.             term.setCursorPos(1,1)
  295.             print(_tUser.. " has joined the server!")
  296.             for i=1,#messages do
  297.                 print(messages[i])
  298.             end
  299.            
  300.             print()
  301.         end
  302.     end
  303.    
  304.     b()
  305. end
  306.  
  307. function mainMenu( showAdministrator )
  308.     term.clear()
  309.     term.setCursorPos(1,4)
  310.     cPrint("+--------------------+")
  311.     cPrint("|     " ..getVersionIRC.. "     |")
  312.     cPrint("|     ----------     |")
  313.     cPrint("| User:              |")
  314.     cPrint("| Channel: #         |")
  315.     if showAdministrator then
  316.         cPrint("|     ----------     |")
  317.         cPrint("| Admin:             |")
  318.         cPrint("| Enter administrator|")
  319.         cPrint("| code to manage IRC.|")
  320.     end
  321.     cPrint("+--------------------+")
  322.  
  323.     if showAdministrator then
  324.         term.setCursorPos(23,7)
  325.         write(_tUser)
  326.     else
  327.         repeat
  328.             term.setCursorPos(23,7)
  329.             _tUser = read()
  330.         until _tUser ~= ""
  331.     end
  332.  
  333.     repeat
  334.         term.setCursorPos(27,8)
  335.         _tChannel = read()
  336.     until _tChannel ~= ""
  337.    
  338.     if _tChannel == "ShowAdmin" then --OoOoOo! A easter egg :D Too bad you can't use it :/
  339.         mainMenu( true )
  340.     end
  341.    
  342.     if showAdministrator then
  343.         term.setCursorPos(24,10)
  344.         _tAdminCode = read()
  345.         -- Doesn't do anything yet...
  346.     end
  347. end
  348.  
  349. function autoUpdate()
  350.     term.clear()
  351.     term.setCursorPos(1,6)
  352.     cPrint("+--------------------+")
  353.     cPrint("|     Loading...     |")
  354.     cPrint("| ||||||             |") --Fake loading bar FTW
  355.     cPrint("+--------------------+")
  356.     while true do
  357.         local downloadedFile = http.request("http://pastebin.com/raw.php?i=6QfbvrBW")
  358.         local event, url, body = os.pullEvent()
  359.         if event == "http_success" then
  360.             _tBody = body.readAll()
  361.             break
  362.         elseif event == "http_failed" then
  363.             break
  364.         end
  365.     end
  366.     term.clear()
  367.     term.setCursorPos(1,6)
  368.     cPrint("+--------------------+")
  369.     cPrint("|     Loading...     |")
  370.     cPrint("| ||||||||||||       |")
  371.     cPrint("+--------------------+")
  372.     file = fs.open(programName, "r")
  373.     _fBody = file.readAll()
  374.     file.close()
  375.     sleep(0.5)
  376.     term.clear()
  377.     term.setCursorPos(1,6)
  378.     cPrint("+--------------------+")
  379.     cPrint("|     Loading...     |")
  380.     cPrint("| |||||||||||||||||| |")
  381.     cPrint("+--------------------+")
  382.     sleep(0.5)
  383.     local selection = 1
  384.     if _fBody ~= _tBody then
  385.         while true do
  386.             if selection == 1 then
  387.                 term.clear()
  388.                 term.setCursorPos(1,6)
  389.                 cPrint("+--------------------+")
  390.                 cPrint("| Install new update |")
  391.                 cPrint("| [Yes]    ?     No  |")
  392.                 cPrint("+--------------------+")
  393.             else
  394.                 term.clear()
  395.                 term.setCursorPos(1,6)
  396.                 cPrint("+--------------------+")
  397.                 cPrint("| Install new update |")
  398.                 cPrint("|  Yes     ?    [No] |")
  399.                 cPrint("+--------------------+")
  400.             end
  401.             local event, key = os.pullEvent("key")
  402.             if key == keys.left then
  403.                 selection = 1
  404.             elseif key == keys.right then
  405.                 selection = 2
  406.             elseif key == keys.enter then
  407.                 if selection == 1 then
  408.                     fs.delete(programName)
  409.                     file = fs.open(programName, "w")
  410.                     file.write(_tBody)
  411.                     file.close()
  412.                     shell.run(programName)
  413.                     error()
  414.                 else
  415.                     break
  416.                 end
  417.             end
  418.         end
  419.     end
  420. end
  421.  
  422. autoUpdate()
  423. mainMenu( false )
  424. startProgram()
Advertisement
Add Comment
Please, Sign In to add comment