Advertisement
LDDestroier

Fakechat CLI/SRV (beta)

Dec 29th, 2016
1,497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.33 KB | None | 0 0
  1. -- ...use with responsibility.
  2. --
  3. -- Get with
  4. -- pastebin get n3K9Nt3D fakechat
  5. -- std PB n3K9Nt3D fakechat
  6.  
  7. local names = { --Add names to quickly switch between using PageUP and PageDOWN
  8.     {
  9.         name = "&o&6Jesus",
  10.         prefix = "<",
  11.         suffix = "> ",
  12.     },
  13.     {
  14.         name = "&dServer",
  15.         prefix = "&d[",
  16.         suffix = "&d] ",
  17.         msgprefix = "&d",
  18.         realname = "But nobody"
  19.     },
  20.     {
  21.         name = "&eBagel&6Bot",
  22.         prefix = "<",
  23.         suffix = "> ",
  24.     },
  25.     {
  26.         name = "Lr_",
  27.         prefix = "<",
  28.         suffix = "> ",
  29.     },
  30.     {
  31.         name = "dan200",
  32.         prefix = "<",
  33.         suffix = "> ",
  34.     },
  35. }
  36.  
  37. local sHistories = {}
  38.  
  39. local cname = 1
  40.  
  41. local tArg = {...}
  42.  
  43. local channel = 1251  --default 1251
  44.  
  45. local sHistory = {}
  46. local scr_x, scr_y = term.getSize()
  47. local modems
  48. local setModems = function()
  49.     modems = {peripheral.find("modem")}
  50.     for a = 1, #modems do
  51.         if modems[a].isWireless() then
  52.             modem = modems[a]
  53.             modem.open(channel)
  54.             return
  55.         end
  56.     end
  57.     modem = nil
  58. end
  59.  
  60. setModems()
  61. if (not commands) and (not modem) then
  62.     return printError("You need a modem! Or a command computer!")
  63. end
  64. if commands then isServer = true else isServer = false end
  65.  
  66. if (not commands) and (not modem) then
  67.   error("This requires a command computer.")
  68. end
  69.  
  70. local prefix = ""
  71. local suffix = ""
  72.  
  73. local dnb = function(phunk, ...)
  74.     local t,b = term.getTextColor(), term.getBackgroundColor()
  75.     local x,y = term.getCursorPos()
  76.     local output = {phunk(...)}
  77.     term.setCursorPos(x,y)
  78.     term.setTextColor(t)
  79.     term.setBackgroundColor(b)
  80.     return unpack(output)
  81. end
  82.  
  83. local prevnames = {}
  84.  
  85. local colnames = {
  86.     ["0"] = "black",
  87.     ["1"] = "dark_blue",
  88.     ["2"] = "dark_green",
  89.     ["3"] = "dark_aqua",
  90.     ["4"] = "dark_red",
  91.     ["5"] = "dark_purple",
  92.     ["6"] = "gold",
  93.     ["7"] = "gray",
  94.     ["8"] = "dark_gray",
  95.     ["9"] = "blue",
  96.     ["a"] = "green",
  97.     ["b"] = "aqua",
  98.     ["c"] = "red",
  99.     ["d"] = "light_purple",
  100.     ["e"] = "yellow",
  101.     ["f"] = "white",
  102. }
  103.  
  104. local colors_names = { --for use with colors api, you see
  105.     ["0"] = colors.black,
  106.     ["1"] = colors.blue,
  107.     ["2"] = colors.green,
  108.     ["3"] = colors.cyan,
  109.     ["4"] = colors.red,
  110.     ["5"] = colors.purple,
  111.     ["6"] = colors.orange,
  112.     ["7"] = colors.lightGray,
  113.     ["8"] = colors.gray,
  114.     ["9"] = colors.blue, --they don't translate perfectly, okay??
  115.     ["a"] = colors.lime,
  116.     ["b"] = colors.lightBlue,
  117.     ["c"] = colors.red,
  118.     ["d"] = colors.magenta,
  119.     ["e"] = colors.yellow,
  120.     ["f"] = colors.white,
  121. }
  122. local codeNames = { --just for checking
  123.     ["k"] = "obfuscate",
  124.     ["o"] = "italic",
  125.     ["l"] = "bold",
  126.     ["m"] = "strikethrough",
  127.     ["n"] = "underline",
  128.     ["r"] = "reset",
  129. }
  130.  
  131. local filterColors = function(str,doprint)
  132.     local p = 1
  133.     local output = ""
  134.     local code = "&"
  135.     local col = "f"
  136.     local prevT,prevB = term.getTextColor(), term.getBackgroundColor()
  137.     while p <= #str do
  138.         if str:sub(p,p) == code then
  139.             if colors_names[str:sub(p+1,p+1)] then
  140.                 col = str:sub(p+1,p+1)
  141.                 p = p + 1
  142.             elseif codeNames[str:sub(p+1,p+1)] then
  143.                 if str:sub(p+1,p+1) == "r" then
  144.                     col = "f"
  145.                 end
  146.                 p = p + 1
  147.             else
  148.                 if doprint then
  149.                     if term.isColor() then
  150.                         term.setTextColor(colors_names[col])
  151.                     end
  152.                     write(str:sub(p,p))
  153.                 end
  154.             end
  155.             p = p + 1
  156.         else
  157.             output = output..str:sub(p,p)
  158.             if doprint then
  159.                 if term.isColor() then
  160.                     term.setTextColor(colors_names[col])
  161.                 end
  162.                 write(str:sub(p,p))
  163.             end
  164.             p = p + 1
  165.         end
  166.     end
  167.     term.setTextColor(prevT)
  168.     term.setBackgroundColor(prevB)
  169.     return output
  170. end
  171.  
  172. local renderReadBar = function(doReturn)
  173.     local cx,cy = term.getCursorPos()
  174.     local cb,ct = term.getBackgroundColor(), term.getTextColor()
  175.     term.setCursorPos(1,scr_y)
  176.     term.setBackgroundColor(colors.black)
  177.     term.clearLine()
  178.     filterColors(prefix.."&r"..names[cname].prefix.."&r"..names[cname].name.."&r"..names[cname].suffix.."&r"..suffix,true)
  179.     term.setCursorPos(1,scr_y-1)
  180.     if not doReturn then
  181.         term.setBackgroundColor(colors.gray)
  182.         term.clearLine()
  183.         term.setTextColor(colors.white)
  184.     else
  185.         term.setCursorPos(cx,cy)
  186.         term.setBackgroundColor(cb)
  187.         term.setTextColor(ct)
  188.     end
  189. end
  190.  
  191. local colorFormat = function(str)
  192.     local color = "f"
  193.     local obfuscated = false
  194.     local bold = false
  195.     local strikethrough = false
  196.     local underline = false
  197.     local italic = false
  198.    
  199.     local code = "&" --ONE CHARACTER
  200.     local pos = 1
  201.     local opos = 1
  202.     local output = {}
  203.    
  204.     while pos <= #str do
  205.         output[opos] = {}
  206.         if str:sub(pos,pos) == code and pos < #str then
  207.             local changed = false
  208.             if colnames[str:sub(pos+1,pos+1)] then
  209.                 color = str:sub(pos+1,pos+1)
  210.                 changed = true
  211.             else
  212.                 if str:sub(pos+1,pos+1) == "r" then
  213.                     color = "f"
  214.                     obfuscated = false
  215.                     bold = false
  216.                     strikethrough = false
  217.                     underline = false
  218.                     italic = false
  219.                     changed = true
  220.                 end
  221.                 if str:sub(pos+1,pos+1) == "k" then
  222.                     obfuscated = true
  223.                     changed = true
  224.                 end
  225.                 if str:sub(pos+1,pos+1) == "l" then
  226.                     bold = true
  227.                     changed = true
  228.                 end
  229.                 if str:sub(pos+1,pos+1) == "m" then
  230.                     strikethrough = true
  231.                     changed = true
  232.                 end
  233.                 if str:sub(pos+1,pos+1) == "n" then
  234.                     underline = true
  235.                     changed = true
  236.                 end
  237.                 if str:sub(pos+1,pos+1) == "o" then
  238.                     italic = true
  239.                     changed = true
  240.                 end
  241.             end
  242.             if changed then
  243.                 output[opos].text = ""
  244.                 pos = pos + 2
  245.             else
  246.                 output[opos].text = str:sub(pos,pos)
  247.                 pos = pos + 1
  248.             end
  249.         else
  250.             output[opos].text = str:sub(pos,pos)
  251.             pos = pos + 1
  252.         end
  253.         output[opos].color = colnames[color]
  254.         output[opos].obfuscated = obfuscated
  255.         output[opos].bold = bold
  256.         output[opos].strikethrough = strikethrough
  257.         output[opos].underline = underline
  258.         output[opos].italic = italic
  259.         opos = opos + 1
  260.     end
  261.     return textutils.serialiseJSON(output)
  262. end
  263.  
  264. local players = { --Add playernames here if you want to filter who receives messages
  265.     "dan200",
  266.     "EldidiStroyrr",
  267. }
  268.  
  269. local doFilter = false --If true, filters according to 'players' table
  270.  
  271. local send = function(name,msg,np,ns,realname,doprint)
  272.     local compiled --compiled message, not compiled program
  273.     local filtmsg = filterColors(msg)
  274.     if filtmsg:sub(1,3) == "___" then --should this be a client option? hmm...
  275.         if filtmsg == "___join" then
  276.             compiled = "&e"..filterColors(realname).." joined the game."
  277.         elseif filtmsg == "___notjoin" then
  278.             compiled = "&e"..filterColors(realname).." hasn't joined the game."
  279.         elseif filtmsg == "___leave" then
  280.             compiled = "&e"..filterColors(realname).." left the game."
  281.         elseif filtmsg == "___notleave" then
  282.             compiled = "&e"..filterColors(realname).." hasn't left the game."
  283.         elseif filtmsg:sub(1,8) == "___medal" then
  284.             compiled = realname.."&r has made the achievement &a["..msg:sub(10).."&a]"
  285.         else
  286.             compiled = msg:sub(4)
  287.         end
  288.     else
  289.         compiled = prefix.."&r"..np.."&r"..name.."&r"..ns.."&r"..suffix.."&r"..msg
  290.     end
  291.     compiled = colorFormat(compiled)
  292.     if doFilter then
  293.         for a = 1, #players do
  294.             commands.tellraw(players[a],compiled)
  295.         end
  296.     else
  297.         commands.tellraw("@a",compiled)
  298.     end
  299. end
  300.  
  301. local netsend = function(n,m,p,s,mp,rn) --name, message, prefix, suffix
  302.     local data = {
  303.         name = n,
  304.         realname = rn or n,
  305.         msg = mp..m,
  306.         prefix = p or "<",
  307.         suffix = s or "> ",
  308.     }
  309.     if modem then modem.transmit(channel,channel,data) end
  310. end
  311.  
  312. term.setBackgroundColor(colors.black)
  313. term.clear()
  314. term.setCursorPos(1,1)
  315. local name
  316. if tArg[1] then
  317.     name = tArg[1]
  318.     if name:gsub(" ","") ~= "" and prevnames[#prevnames] ~= name then
  319.       table.insert(prevnames,name)
  320.     end
  321. else
  322.     print("Enter a name:")
  323.     write(">")
  324.     dnb(function() write("\n\nUse PageUP and PageDOWN to switch names.\nDo '/exit' to exit.\nDo '___join' or '___leave' to simulate doing either.\nPrefix any other message with '___' to do raw chat.\n\nUse responsibly") if term.isColor() then print(".") else print(", and it'd be preferable to use a golden computer. Not required, but...") end end)
  325.     name = read(nil,prevnames)
  326.     if name:gsub(" ","") ~= "" and prevnames[#prevnames] ~= name then
  327.       table.insert(prevnames,name)
  328.     end
  329. end
  330. if name:gsub(" ","") == "" then name = "Scabby cunt" end --enter in a fucking name
  331. table.insert(names,1,{
  332.     name = name,
  333.     prefix = "<",
  334.     suffix = "> ",
  335.     realname = name
  336. })
  337.  
  338. local insend = function(n,m,p,s,mp,rn) --name, message, prefix, suffix. Sends a message inwards to simulate receiving a message from a client
  339.     local data = {
  340.         name = n,
  341.         realname = rn or n,
  342.         msg = mp..m,
  343.         prefix = p or "<",
  344.         suffix = s or "> ",
  345.     }
  346.     os.queueEvent("modem_message","top",channel,channel,data,0)
  347. end
  348.  
  349. local client = function() --this is where you enter in your awesome messages! it also happens to be where *you* enter in your shitty messages
  350.     while true do
  351.         term.setBackgroundColor(colors.black) --dats rasist
  352.         term.clear()
  353.         renderReadBar()
  354.         write(">")
  355.         local msg = read(nil,sHistories[cname] or {"I like to eat pubes I find in urinals.","One of my hobbies is to kick small dogs and see how far they go.","Have you reddit","My child-sized skin suit it starting to smell."})
  356.         if msg:gsub(" ","") == "/exit" then
  357.             return
  358.         end
  359.         if msg:gsub(" ","") ~= "" then
  360.             if not sHistories[cname] then
  361.                 sHistories[cname] = {msg}
  362.             else
  363.                 if sHistories[cname][#sHistories[cname]] ~= msg then
  364.                     sHistories[cname][#sHistories[cname]+1] = msg
  365.                 end
  366.             end
  367.         end
  368.         if isServer then --if you are a command computer, fuck that netsend() function!!! that's for pussies!!
  369.             if msg:gsub(" ","") ~= "" then
  370.                 local sdata = {
  371.                     names[cname].name,
  372.                     msg,
  373.                     prefix..names[cname].prefix or "<",
  374.                     suffix..names[cname].suffix or "> ",
  375.                     names[cname].msgprefix or "",
  376.                 }
  377.                 if type(names[cname].realname) == "string" then
  378.                     table.insert(sdata,names[cname].realname)
  379.                 end
  380.                 insend(unpack(sdata))
  381.             end
  382.         else --if you're a client, studies have shown that you're the type of fuck who makes positive acronyms out of insults that are thrown your way
  383.             local sdata = {
  384.                 names[cname].name,
  385.                 msg,
  386.                 prefix..names[cname].prefix or "<",
  387.                 suffix..names[cname].suffix or "> ",
  388.                 names[cname].msgprefix or "",
  389.             }
  390.             if type(names[cname].realname) == "string" then
  391.                 table.insert(sdata,names[cname].realname)
  392.             end
  393.             netsend(unpack(sdata))
  394.         end
  395.     end
  396. end
  397.  
  398. local renderHistory = function() --this shows all the latest and greatest (optional) messages sent by fakechat clients!
  399.     local y = scr_y-2
  400.     local r = "&r"
  401.     term.setBackgroundColor(colors.black)
  402.     for a = #sHistory, #sHistory-(scr_y-2), -1 do
  403.         if not sHistory[a] then break end
  404.         term.setCursorPos(1,y)
  405.         term.clearLine()
  406.         filterColors(sHistory[a].p..r..sHistory[a].n..r..sHistory[a].s..r..sHistory[a].m,true)
  407.         y = y - 1
  408.     end
  409. end
  410.  
  411. local server = function() --now THIS is what I call a quality function! servers are where it's at! *it* being command computers, of course...
  412.   while true do
  413.     local evt = {os.pullEvent("modem_message")}
  414.     if type(evt[5]) == "table" then
  415.       local i = evt[5]
  416.       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
  417.         table.insert(sHistory,{n=i.name, m=i.msg, p=i.prefix, s=i.suffix})
  418.         send( i.name, i.msg, i.prefix, i.suffix, i.realname, true)
  419.         dnb(function() return renderHistory() end)
  420.       end
  421.     end
  422.   end
  423. end
  424.  
  425. moreModems = function() --keep them modems loaded!
  426.     while true do
  427.         setModems()
  428.         sleep(0)
  429.     end
  430. end
  431.  
  432. switchNames = function() --in case you happen to be named harry kuntz, and want to change that right away
  433.     local evt, key, rerender
  434.     while true do
  435.         rerender = false
  436.         evt, key = os.pullEvent("key")
  437.         if key == keys.pageUp then
  438.             cname = cname - 1
  439.             rerender = true
  440.         elseif key == keys.pageDown then
  441.             cname = cname + 1
  442.             rerender = true
  443.         end
  444.         if cname <= 0 then
  445.             cname = #names
  446.         elseif cname > #names then
  447.             cname = 1
  448.         end
  449.         if rerender then renderReadBar(true) end
  450.     end
  451. end
  452.  
  453. local funclist = { --this is the funciest list I've ever laid eyes upon
  454.     client,
  455.     moreModems,
  456.     switchNames,
  457. }
  458. if isServer then table.insert(funclist,server) end --clients are not servers, you thick idiot
  459.  
  460. parallel.waitForAny(unpack(funclist)) --execute the SHIT out of this!
  461.  
  462. term.setCursorPos(1,scr_y)
  463. term.setBackgroundColor(colors.black)
  464. term.clearLine() --the last line is always the hardest...*sniffle*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement