Advertisement
LDDestroier

FakeChat (server)

Aug 1st, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ...use with responsibility.
  2. --
  3. -- Get with
  4. -- pastebin get ugMfh2wc fc_server
  5. -- pastebin get ugMfh2wc startup
  6. -- std ld fakechat_server
  7.  
  8. local tArg = {...}
  9.  
  10. if not commands then
  11.   error("This requires a command computer.")
  12. end
  13.  
  14. local channel = 1251  --default 1251
  15.  
  16. local sHistory = {}
  17. local scr_x, scr_y = term.getSize()
  18. local modems
  19. local setModems = function()
  20.     modems = {peripheral.find("modem")}
  21.     for a = 1, #modems do
  22.         if modems[a].isWireless() then
  23.             modem = modems[a]
  24.             modem.open(channel)
  25.             return
  26.         end
  27.     end
  28.     modem = nil
  29. end
  30.  
  31. setModems()
  32.  
  33. local prefix = ""
  34. local suffix = ""
  35.  
  36. local npos = 1
  37. local dnb = function(phunk, ...)
  38.     local t,b = term.getTextColor(), term.getBackgroundColor()
  39.     local x,y = term.getCursorPos()
  40.     local output = {phunk(...)}
  41.     term.setCursorPos(x,y)
  42.     term.setTextColor(t)
  43.     term.setBackgroundColor(b)
  44.     return unpack(output)
  45. end
  46.  
  47. local prevnames = {}
  48.  
  49. local colnames = {
  50.     ["0"] = "black",
  51.     ["1"] = "dark_blue",
  52.     ["2"] = "dark_green",
  53.     ["3"] = "dark_aqua",
  54.     ["4"] = "dark_red",
  55.     ["5"] = "dark_purple",
  56.     ["6"] = "gold",
  57.     ["7"] = "gray",
  58.     ["8"] = "dark_gray",
  59.     ["9"] = "blue",
  60.     ["a"] = "green",
  61.     ["b"] = "aqua",
  62.     ["c"] = "red",
  63.     ["d"] = "light_purple",
  64.     ["e"] = "yellow",
  65.     ["f"] = "white",
  66. }
  67.  
  68. local colors_names = { --for use with colors api, you see
  69.     ["0"] = colors.black,
  70.     ["1"] = colors.blue,
  71.     ["2"] = colors.green,
  72.     ["3"] = colors.cyan,
  73.     ["4"] = colors.red,
  74.     ["5"] = colors.purple,
  75.     ["6"] = colors.orange,
  76.     ["7"] = colors.lightGray,
  77.     ["8"] = colors.gray,
  78.     ["9"] = colors.blue, --they don't translate perfectly, okay??
  79.     ["a"] = colors.lime,
  80.     ["b"] = colors.lightBlue,
  81.     ["c"] = colors.red,
  82.     ["d"] = colors.magenta,
  83.     ["e"] = colors.yellow,
  84.     ["f"] = colors.white,
  85. }
  86. local codeNames = { --just for checking
  87.     ["k"] = "obfuscate",
  88.     ["o"] = "italic",
  89.     ["l"] = "bold",
  90.     ["m"] = "strikethrough",
  91.     ["n"] = "underline",
  92.     ["r"] = "reset",
  93. }
  94.  
  95. filterColors = function(str,doprint)
  96.     local p = 1
  97.     local output = ""
  98.     local code = "&"
  99.     local col = "f"
  100.     while p <= #str do
  101.         if str:sub(p,p) == code then
  102.             if colors_names[str:sub(p+1,p+1)] then
  103.                 col = str:sub(p+1,p+1)
  104.                 p = p + 1
  105.             elseif codeNames[str:sub(p+1,p+1)] then
  106.                 if str:sub(p+1,p+1) == "r" then
  107.                     col = "f"
  108.                 end
  109.                 p = p + 1
  110.             else
  111.                 if doprint then
  112.                     term.setTextColor(colors_names[col])
  113.                     write(str:sub(p,p))
  114.                 end
  115.             end
  116.             p = p + 1
  117.         else
  118.             output = output..str:sub(p,p)
  119.             if doprint then
  120.                 term.setTextColor(colors_names[col])
  121.                 write(str:sub(p,p))
  122.             end
  123.             p = p + 1
  124.         end
  125.     end
  126.     return output
  127. end
  128.  
  129. colorFormat = function(str)
  130.     local color = "f"
  131.     local obfuscated = false
  132.     local bold = false
  133.     local strikethrough = false
  134.     local underline = false
  135.     local italic = false
  136.    
  137.     local code = "&" --ONE CHARACTER
  138.     local pos = 1
  139.     local opos = 1
  140.     local output = {}
  141.    
  142.     while pos <= #str do
  143.         output[opos] = {}
  144.         if str:sub(pos,pos) == code and pos < #str then
  145.             local changed = false
  146.             if colnames[str:sub(pos+1,pos+1)] then
  147.                 color = str:sub(pos+1,pos+1)
  148.                 changed = true
  149.             else
  150.                 if str:sub(pos+1,pos+1) == "r" then
  151.                     color = "f"
  152.                     obfuscated = false
  153.                     bold = false
  154.                     strikethrough = false
  155.                     underline = false
  156.                     italic = false
  157.                     changed = true
  158.                 end
  159.                 if str:sub(pos+1,pos+1) == "k" then
  160.                     obfuscated = true
  161.                     changed = true
  162.                 end
  163.                 if str:sub(pos+1,pos+1) == "l" then
  164.                     bold = true
  165.                     changed = true
  166.                 end
  167.                 if str:sub(pos+1,pos+1) == "m" then
  168.                     strikethrough = true
  169.                     changed = true
  170.                 end
  171.                 if str:sub(pos+1,pos+1) == "n" then
  172.                     underline = true
  173.                     changed = true
  174.                 end
  175.                 if str:sub(pos+1,pos+1) == "o" then
  176.                     italic = true
  177.                     changed = true
  178.                 end
  179.             end
  180.             if changed then
  181.                 output[opos].text = ""
  182.                 pos = pos + 2
  183.             else
  184.                 output[opos].text = str:sub(pos,pos)
  185.                 pos = pos + 1
  186.             end
  187.         else
  188.             output[opos].text = str:sub(pos,pos)
  189.             pos = pos + 1
  190.         end
  191.         output[opos].color = colnames[color]
  192.         output[opos].obfuscated = obfuscated
  193.         output[opos].bold = bold
  194.         output[opos].strikethrough = strikethrough
  195.         output[opos].underline = underline
  196.         output[opos].italic = italic
  197.         opos = opos + 1
  198.     end
  199.     return textutils.serialiseJSON(output)
  200. end
  201.  
  202. local players = { --Add playernames here if you want to filter who receives messages
  203.   "dan200",
  204. }
  205.  
  206. local doFilter = false --If true, filters according to 'players' table
  207.  
  208. local send = function(name,msg,np,ns,realname,doprint)
  209.   local compiled --compiled message, not compiled program
  210.   local filtmsg = filterColors(msg)
  211.   if filtmsg:sub(1,3) == "___" then --should this be a client option? hmm...
  212.     if filtmsg == "___join" then
  213.         compiled = "&e"..filterColors(realname).." joined the game."
  214.     elseif filtmsg == "___leave" then
  215.         compiled = "&e"..filterColors(realname).." left the game."
  216.     else
  217.         compiled = msg:sub(4)
  218.     end
  219.   else
  220.     compiled = prefix.."&r"..np.."&r"..name.."&r"..ns.."&r"..suffix.."&r"..msg
  221.   end
  222.   compiled = colorFormat(compiled)
  223.   if doFilter then
  224.     for a = 1, #players do
  225.       commands.tellraw(players[a],compiled)
  226.     end
  227.   else
  228.     commands.tellraw("@a",compiled)
  229.   end
  230. end
  231.  
  232. local netsend = function(n,m,p,s,mp,rn) --name, message, prefix, suffix
  233.     local data = {
  234.         name = n,
  235.         realname = rn or n,
  236.         msg = mp..m,
  237.         prefix = p or "<",
  238.         suffix = s or "> ",
  239.     }
  240.     modem.transmit(channel,channel,data)
  241. end
  242.  
  243. term.setBackgroundColor(colors.black)
  244. term.clear()
  245. term.setCursorPos(1,1)
  246. local name
  247. if tArg[1] then
  248.     name = tArg[1]
  249.     if name:gsub(" ","") ~= "" and prevnames[#prevnames] ~= name then
  250.       table.insert(prevnames,name)
  251.     end
  252. else
  253.     print("Enter your name:")
  254.     write(">")
  255.     name = read(nil,prevnames)
  256.     if name:gsub(" ","") ~= "" and prevnames[#prevnames] ~= name then
  257.       table.insert(prevnames,name)
  258.     end
  259. end
  260.  
  261. local names = { --Add names to quickly switch between using UP and DOWN
  262.     {
  263.         name = name,
  264.         prefix = "<",
  265.         suffix = "> ",
  266.         realname = name
  267.     },
  268.     {
  269.         name = "&o&6Jesus",
  270.         prefix = "<",
  271.         suffix = "> ",
  272.     },
  273.     {
  274.         name = "&dServer",
  275.         prefix = "&d[",
  276.         suffix = "&d] ",
  277.         msgprefix = "&d",
  278.         realname = "But nobody"
  279.     },
  280.     {
  281.         name = "dan200",
  282.         prefix = "<",
  283.         suffix = "> ",
  284.     },
  285. }
  286.  
  287. local insend = function(n,m,p,s,mp,rn) --name, message, prefix, suffix
  288.     local data = {
  289.         name = n,
  290.         realname = rn or n,
  291.         msg = mp..m,
  292.         prefix = p or "<",
  293.         suffix = s or "> ",
  294.     }
  295.     os.queueEvent("modem_message","top",channel,channel,data,0)
  296. end
  297.  
  298. local function doRead()
  299.     while true do
  300.         term.setBackgroundColor(colors.black)
  301.         term.clear()
  302.         term.setCursorPos(1,1)
  303.         term.setCursorPos(1,scr_y-1)
  304.         term.setBackgroundColor(colors.gray)
  305.         term.clearLine()
  306.         term.setTextColor(colors.white)
  307.         write(">")
  308.         local msg = read()
  309.         if msg:gsub(" ","") == "/exit" then
  310.             return
  311.         end
  312.         if msg:gsub(" ","") ~= "" then
  313.             local sdata = {
  314.                 names[npos].name,
  315.                 msg,
  316.                 prefix..names[npos].prefix or "<",
  317.                 suffix..names[npos].suffix or "> ",
  318.                 names[npos].msgprefix or "",
  319.             }
  320.             if type(names[npos].realname) == "string" then
  321.                 table.insert(sdata,names[npos].realname)
  322.             end
  323.             insend(unpack(sdata))
  324.         end
  325.     end
  326. end
  327.  
  328. local renderHistory = function()
  329.     local y = scr_y-2
  330.     local r = "&r"
  331.     term.setBackgroundColor(colors.black)
  332.     for a = #sHistory, #sHistory-(scr_y-2), -1 do
  333.         if not sHistory[a] then break end
  334.         term.setCursorPos(1,y)
  335.         term.clearLine()
  336.         filterColors(sHistory[a].p..r..sHistory[a].n..r..sHistory[a].s..r..sHistory[a].m,true)
  337.         y = y - 1
  338.     end
  339. end
  340.  
  341. local server = function()
  342.   while true do
  343.     local evt = {os.pullEvent("modem_message")}
  344.     if type(evt[5]) == "table" then
  345.       local i = evt[5]
  346.       if type(i.name) == "string" and type(i.msg) == "string" and type(i.prefix) == "string" and type(i.suffix) == "string" and type(i.realname) == "string" then
  347.         table.insert(sHistory,{n=i.name, m=i.msg, p=i.prefix, s=i.suffix})
  348.         send( i.name, i.msg, i.prefix, i.suffix, i.realname, true)
  349.         dnb(function() return renderHistory() end)
  350.       end
  351.     end
  352.   end
  353. end
  354.  
  355. local client = function()
  356.     doRead()
  357. end
  358.  
  359. moreModems = function()
  360.     while true do
  361.         setModems()
  362.         sleep(0)
  363.     end
  364. end
  365.  
  366. local funclist = {
  367.     client,
  368.     moreModems,
  369.     server,
  370. }
  371.  
  372. parallel.waitForAny(unpack(funclist))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement