Advertisement
LDDestroier

RogerBot + Enchat 2.1

Apr 11th, 2017
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  pastebin get XP2UPWgZ dischat
  3.  std pb XP2UPWgZ dischat
  4.  
  5. 'Enchat' derivative for posting to Discord (RogerBot)
  6.  
  7. --]]
  8.  
  9. local log,_log,chatlog
  10.  
  11. local timedivisor = 2 --Must be same as other enchat clients. Can be used to limit scrambling due to using os.time
  12.  
  13. local tArg = {...}
  14. local yourName = tArg[1]
  15.  
  16. log = {}         --Log formatted for view
  17. _log = {}        --Log with all information
  18. chatlog = {}   --Log with all pre-formatted information
  19. local commandTable   --Contains all commands
  20. local scr_x, scr_y = term.getSize()
  21. local channel = 0 --Do not modify, it's changed anyway
  22. local modem, scroll, doScroll
  23. local endMode = 0 --0 is exit, 1 is restart
  24. local cfile --Current file. Defined later.
  25. local hasKrazy = false
  26.  
  27. local enchatSettings = {
  28.     fancyMsg = false, --Whether or not to colorize as you type
  29.     doColorize = true, --Whether or not to use color formatting at all
  30.     reverseScroll = false, --Reverses scrolling direction. But why would you want to do that?
  31. }
  32.  
  33. local palate = {
  34.     bg = colors.gray, --Default chat background color
  35.     txt = colors.white, --Default chat text color
  36.     chatbg = colors.white, --Chat prompt background color
  37.     chattxt = colors.black, --Chat prompt text color
  38. }
  39.  
  40. local colors_names = { --this has been modified to use the Paint colors rather than minecraft formatting
  41.     ["0"] = colors.white,
  42.     ["1"] = colors.orange,
  43.     ["2"] = colors.magenta,
  44.     ["3"] = colors.lightBlue,
  45.     ["4"] = colors.yellow,
  46.     ["5"] = colors.lime,
  47.     ["6"] = colors.pink,
  48.     ["7"] = colors.gray,
  49.     ["8"] = colors.lightGray,
  50.     ["9"] = colors.cyan,
  51.     ["a"] = colors.purple,
  52.     ["b"] = colors.blue,
  53.     ["c"] = colors.brown,
  54.     ["d"] = colors.green,
  55.     ["e"] = colors.red,
  56.     ["f"] = colors.black,
  57. }  
  58.  
  59. --going to add compatibility for ComputerCaft 1.63
  60. local _currentcolors = {
  61.     txt = colors.white,
  62.     bg = colors.black,
  63. }
  64. local termsetTextColor,termsetBackgroundColor = term.setTextColor,term.setBackgroundColor
  65. local termsetCursorPos,termclear,termclearLine,termwrite,termgetSize,termsetCursorBlink,termisColor = term.setCursorPos,term.clear,term.clearLine,term.write,term.getSize,term.setCursorBlink,term.isColor
  66. local tableinsert,tableconcat,tableunpack = table.insert,table.concat,table.unpack
  67. local termblit,termgetTextColor,termgetBackgroundColor,termgetCursorPos
  68. local parallelwaitForAny = parallel.waitForAny
  69. local termsetVisible
  70. if term.current then
  71.     --termcurrent = term.current
  72. end
  73.  
  74. local oldsettext = term.setTextColor
  75. local oldsetbg = term.setBackgroundColor
  76. if not term.blit then
  77.     termsetTextColor = function(col)
  78.         oldsettext(col)
  79.         _currentcolors.txt = col
  80.     end
  81.     termsetBackgroundColor = function(col)
  82.         oldsetbg(col)
  83.         _currentcolors.bg = col
  84.     end
  85.     termgetTextColor = function()
  86.         return _currentcolors.txt
  87.     end
  88.     termgetBackgroundColor = function()
  89.         return _currentcolors.bg
  90.     end
  91.     termblit = function(txt,tx,bg)
  92.         local pt,pb = _currentcolors.bg,_currentcolors.txt
  93.         if type(txt) ~= "string" or type(tx) ~= "string" or type(bg) ~= "string" then
  94.             error("expected 3 strings, got "..tableconcat({type(txt),type(tx),type(bg)},", "))
  95.         end
  96.         if not ((#txt == #tx) and #tx == #bg) then
  97.             error("all three arguments must be of same length")
  98.         end
  99.         for p = 1, #txt do
  100.             oldsettext(colors_names[tx:sub(p,p)])
  101.             oldsetbg(colors_names[bg:sub(p,p)])
  102.             termwrite(txt:sub(p,p))
  103.         end
  104.         oldsettext(pt)
  105.         oldsettext(pb)
  106.     end
  107. else
  108.     termblit = term.blit
  109.     termgetTextColor = term.getTextColor
  110.     termgetBackgroundColor = term.getBackgroundColor
  111.     termgetCursorPos = term.getCursorPos
  112. end
  113.  
  114. local tableConcat = function(tbl,between)
  115.     local output = ""
  116.     for k,v in pairs(tbl) do
  117.         output = output..k..between
  118.     end
  119.     return output:sub(1,-2)
  120. end
  121.  
  122. local encode = function(txt) --converts string into a table of each character's byte code
  123.     if type(txt) ~= "string" then return false, "requires string" end
  124.     return {txt:byte(1,-1)}
  125. end
  126.  
  127. local decode = function(tbl) --converts an encoded string into something useful.
  128.     if type(tbl) ~= "table" then return false, "requires table" end
  129.     return string.char(tableunpack(tbl))
  130. end
  131.  
  132. local strcapsule = function(txt)
  133.     return "\""..tostring(txt).."\""
  134. end
  135.  
  136. local cwrite = function(txt,setY,doClearLine)
  137.     local scr_x, scr_y = termgetSize()
  138.     local x,y = termgetCursorPos()
  139.     termsetCursorPos((scr_x/2)-(#txt/2),setY or y)
  140.     if doClearLine then termclearLine() end
  141.     write(txt)
  142. end
  143.  
  144. local waitForModem = function()
  145.     local mod
  146.     while true do
  147.         sleep(0.2)
  148.         mod = peripheral.find("modem")
  149.         if mod then
  150.             return mod
  151.         end
  152.     end
  153. end
  154.  
  155. if not peripheral.find("modem") then
  156.     termsetBackgroundColor(colors.gray)
  157.     termsetTextColor(colors.white)
  158.     termclear()
  159.     cwrite("Enchat requires a modem.",3)
  160.     cwrite("Add one, or press a key.",4)
  161.     sleep(0.1)
  162.     local outcome = parallelwaitForAny(function() os.pullEvent("key") end, waitForModem)
  163.     modem = peripheral.find("modem")
  164.     if not modem then
  165.         termsetBackgroundColor(colors.black)
  166.         termsetCursorPos(1,scr_y)
  167.         termclearLine()
  168.         sleep(0)
  169.         return false
  170.     end
  171. end
  172.  
  173. local tsv = function(visible)
  174.     if termcurrent then
  175.         if termcurrent().setVisible then
  176.             termcurrent().setVisible(visible)
  177.             return true
  178.         else
  179.             return false
  180.         end
  181.     else
  182.         return false
  183.     end
  184. end
  185.  
  186. local logadd = function(name,msg,newline,stopFormatting)
  187.     chatlog[#chatlog+1] = {name,msg,newline,stopFormatting or false}
  188. end
  189.  
  190. local deepCopy = function(tbl)
  191.     local output = {}
  192.     for k,v in pairs(tbl) do
  193.         output[k] = v
  194.     end
  195.     return output
  196. end
  197.  
  198. local explode = function(div,str)
  199.     if (div=='') then return false end
  200.     local pos,arr = 0,{}
  201.     for st,sp in function() return string.find(str,div,pos,false) end do
  202.         tableinsert(arr,string.sub(str,pos,st-1))
  203.         pos = sp + 1
  204.     end
  205.     tableinsert(arr,string.sub(str,pos))
  206.     return arr
  207. end
  208.  
  209. local colors_strnames = { --primarily for use when coloring palate
  210.     ["white"] = colors.white,
  211.     ["orange"] = colors.orange,
  212.     ["magenta"] = colors.magenta,
  213.     ["lightpurple"] = colors.magenta,
  214.     ["light purple"] = colors.magenta,
  215.     ["lightblue"] = colors.lightBlue,
  216.     ["light blue"] = colors.lightBlue,
  217.     ["yellow"] = colors.yellow,
  218.     ["piss"] = colors.yellow,
  219.     ["lemon"] = colors.yellow,
  220.     ["lime"] = colors.lime,
  221.     ["lightgreen"] = colors.lime,
  222.     ["light green"] = colors.lime,
  223.     ["pink"] = colors.pink,
  224.     ["lightish red"] = colors.pink,
  225.     ["gray"] = colors.gray,
  226.     ["grey"] = colors.gray,
  227.     ["lightgray"] = colors.lightGray,
  228.     ["lightgrey"] = colors.lightGray,
  229.     ["light gray"] = colors.lightGray,
  230.     ["light grey"] = colors.lightGray,
  231.     ["cyan"] = colors.cyan,
  232.     ["seawater"] = colors.cyan,
  233.     ["purple"] = colors.purple,
  234.     ["purble"] = colors.purple,
  235.     ["blue"] = colors.blue,
  236.     ["blu"] = colors.blue,
  237.     ["brown"] = colors.brown,
  238.     ["shit"] = colors.brown,
  239.     ["green"] = colors.green,
  240.     ["grass"] = colors.green,
  241.     ["red"] = colors.red,
  242.     ["blood"] = colors.red,
  243.     ["black"] = colors.black,
  244. }
  245.  
  246. for k,v in pairs(colors_names) do
  247.     colors_strnames[k] = v
  248. end
  249.  
  250. local function alterXY()
  251.     local cx,cy = termgetCursorPos()
  252.     if cx == scr_x then
  253.         termsetCursorPos(1,cy+1)
  254.     end
  255. end
  256.  
  257. local blit_names = {}
  258. for k,v in pairs(colors_names) do
  259.     blit_names[v] = k
  260. end
  261.  
  262. local safeColor = function(col) --I could've done much better, but whatever
  263.     if type(col) ~= "number" then
  264.         return false
  265.     else
  266.         if col > 2^15 then
  267.             return false
  268.         else
  269.             if not termisColor() then
  270.                 if (col ~= colors.white) and (col ~= colors.lightGray) and (col ~= colors.gray) and (col ~= colors.black) then
  271.                     return false
  272.                 end
  273.             end
  274.         end
  275.     end
  276.     return true
  277. end
  278.  
  279. local blitWrap = function(text,txt,bg,noWrite)
  280.     local wordNo = 1
  281.     local words = explode(" ",text)
  282.     local lines = 0
  283.     local scr_x, scr_y = termgetSize()
  284.     local cx,cy
  285.     local startX,startY = termgetCursorPos()
  286.     for a = 1, #text do
  287.         cx,cy = termgetCursorPos()
  288.         if text:sub(a,a) == " " and text:sub(a-1,a-1) ~= " " and a > 1 then
  289.             wordNo = wordNo + 1
  290.             if cx + #words[wordNo] > scr_x then
  291.                 termsetCursorPos(1,cy+1)
  292.                 lines = lines + 1
  293.             end
  294.         end
  295.         cx,cy = termgetCursorPos()
  296.         if text:sub(a,a) == "\n" then
  297.             termsetCursorPos(1,cy+1)
  298.             lines = lines + 1
  299.         elseif not (cx == 1 and text:sub(a,a) == " ") then
  300.             if noWrite == true then
  301.                 termsetCursorPos(cx+1,cy)
  302.             else
  303.                 if safeColor(colors_names[txt:sub(a,a)]) then
  304.                     termblit(text:sub(a,a),txt:sub(a,a),bg:sub(a,a))
  305.                 else
  306.                     termwrite(text:sub(a,a))
  307.                 end
  308.             end
  309.         end
  310.         if cx == scr_x then
  311.             termsetCursorPos(1,cy+1)
  312.             lines = lines + 1
  313.         end
  314.     end
  315.     if noWrite == true then
  316.         termsetCursorPos(startX,startY)
  317.     end
  318.     return lines
  319. end
  320.  
  321. local tablefind = function(tbl,str)
  322.     for a = 1, #tbl do
  323.         if tbl[a] == str then
  324.             return a
  325.         end
  326.     end
  327. end
  328.  
  329. local codeNames = {
  330.     ["r"] = "reset",    -- Sets either the text (&) or background (~) colors to their original color.
  331.     ["{"] = "stopFormatting",   --Toggles formatting text off
  332.     ["}"] = "startFormatting",  --Toggles formatting text on
  333.     ["k"] = "krazy" --Makes the font krazy!
  334. }
  335.  
  336. local kraziez = {
  337.     ["l"] = {
  338.         "!",
  339.         "l",
  340.         "1",
  341.         "|",
  342.         "i",
  343.         "I",
  344.         ":",
  345.         ";",
  346.     },
  347.     ["m"] = {
  348.         "M",
  349.         "W",
  350.         "w",
  351.         "m",
  352.         "X",
  353.         "N",
  354.         "_",
  355.         "%",
  356.         "@",
  357.     },
  358.     ["all"] = {}
  359. }
  360. for a = 1, #kraziez["l"] do
  361.     kraziez[kraziez["l"][a]] = kraziez["l"]
  362. end
  363. for k,v in pairs(kraziez) do
  364.     for a = 1, #v do
  365.         kraziez[kraziez[k][a]] = v
  366.     end
  367. end
  368. if _VERSION then
  369.     for a = 1, 255 do
  370.         if (a ~= 32) and (a ~= 13) and (a ~= 10) then
  371.             kraziez["all"][#kraziez["all"]+1] = string.char(a)
  372.         end
  373.     end
  374. else
  375.     for a = 33, 126 do
  376.         kraziez["all"][#kraziez["all"]+1] = string.char(a)
  377.     end
  378. end
  379.  
  380.  
  381. local textToBlit = function(str) --returns output for term.blit, or blitWrap, with formatting codes for color selection. Modified for use specifically with Enchat.
  382.     local p = 1
  383.     local output = ""
  384.     local txcolorout = ""
  385.     local bgcolorout = ""
  386.     local txcode = "&"
  387.     local bgcode = "~"
  388.     local isKrazy = false
  389.     local doFormatting = true
  390.     local usedformats = {}
  391.     local txcol,bgcol = blit_names[termgetTextColor()], blit_names[termgetBackgroundColor()]
  392.     local origTX,origBG = blit_names[termgetTextColor()], blit_names[termgetBackgroundColor()]
  393.     local cx,cy
  394.     local moveOn = function(tx,bg)
  395.         if isKrazy and (str:sub(p,p) ~= " ") and doFormatting then
  396.             if kraziez[str:sub(p,p)] then
  397.                 output = output..kraziez[str:sub(p,p)][math.random(1,#kraziez[str:sub(p,p)])]
  398.             else
  399.                 output = output..kraziez.all[math.random(1,#kraziez.all)]
  400.             end
  401.         else
  402.             output = output..str:sub(p,p)
  403.         end
  404.         txcolorout = txcolorout..(doFormatting and tx or origTX)
  405.         bgcolorout = bgcolorout..(doFormatting and bg or origBG)
  406.     end
  407.     while p <= #str do
  408.         if str:sub(p,p) == txcode then
  409.             if colors_names[str:sub(p+1,p+1)] and doFormatting then
  410.                 txcol = str:sub(p+1,p+1)
  411.                 usedformats.txcol = true
  412.                 p = p + 1
  413.             elseif codeNames[str:sub(p+1,p+1)] then
  414.                 if str:sub(p+1,p+1) == "r" and doFormatting then
  415.                     txcol = blit_names[termgetTextColor()]
  416.                     isKrazy = false
  417.                     p = p + 1
  418.                 elseif str:sub(p+1,p+1) == "{" and doFormatting then
  419.                     doFormatting = false
  420.                     p = p + 1
  421.                 elseif str:sub(p+1,p+1) == "}" and (not doFormatting) then
  422.                     doFormatting = true
  423.                     p = p + 1
  424.                 elseif str:sub(p+1,p+1) == "k" and doFormatting then
  425.                     isKrazy = true
  426.                     usedformats.krazy = true
  427.                     p = p + 1
  428.                 else
  429.                     moveOn(txcol,bgcol)
  430.                 end
  431.             else
  432.                 moveOn(txcol,bgcol)
  433.             end
  434.             p = p + 1
  435.         elseif str:sub(p,p) == bgcode then
  436.             if colors_names[str:sub(p+1,p+1)] and doFormatting then
  437.                 bgcol = str:sub(p+1,p+1)
  438.                 usedformats.bgcol = true
  439.                 p = p + 1
  440.             elseif codeNames[str:sub(p+1,p+1)] and (str:sub(p+1,p+1) == "r") and doFormatting then
  441.                 bgcol = blit_names[termgetBackgroundColor()]
  442.                 p = p + 1
  443.             elseif str:sub(p+1,p+1) == "k" and doFormatting then
  444.                 isKrazy = false
  445.                 p = p + 1
  446.             else
  447.                 moveOn(txcol,bgcol)
  448.             end
  449.             p = p + 1
  450.         else
  451.             moveOn(txcol,bgcol)
  452.             p = p + 1
  453.         end
  454.     end
  455.     return output, txcolorout, bgcolorout, usedformats
  456. end
  457.  
  458. local funcread = function(repchar,rHistory,doFunc,noNewLine,writeFunc,cursorAdjFunc,doFuncEvent)
  459.     local scr_x,scr_y = termgetSize()
  460.     local sx,sy = termgetCursorPos()
  461.     local cursor = 1
  462.     local rCursor = #rHistory+1
  463.     local output = ""
  464.     termsetCursorBlink(true)
  465.     local rite = writeFunc or termwrite
  466.     while true do
  467.         local evt,key = os.pullEvent()
  468.         if evt == doFuncEvent then
  469.             pleaseDoFunc = true
  470.         elseif evt == "key" then
  471.             if key == keys.enter then
  472.                 if not noNewLine then
  473.                     write("\n")
  474.                 end
  475.                 termsetCursorBlink(false)
  476.                 return output
  477.             elseif key == keys.left then
  478.                 if cursor-1 >= 1 then
  479.                     cursor = cursor - 1
  480.                 end
  481.             elseif key == keys.right then
  482.                 if cursor <= #output then
  483.                     cursor = cursor + 1
  484.                 end
  485.             elseif key == keys.up then
  486.                 if rCursor > 1 then
  487.                     rCursor = rCursor - 1
  488.                     termsetCursorPos(sx,sy)
  489.                     rite((" "):rep(#output))
  490.                     output = rHistory[rCursor] or ""
  491.                     cursor = #output+1
  492.                     pleaseDoFunc = true
  493.                 end
  494.             elseif key == keys.down then
  495.                 termsetCursorPos(sx,sy)
  496.                 rite((" "):rep(#output))
  497.                 if rCursor < #rHistory then
  498.                     rCursor = rCursor + 1
  499.                     output = rHistory[rCursor] or ""
  500.                     cursor = #output+1
  501.                     pleaseDoFunc = true
  502.                 else
  503.                     rCursor = #rHistory+1
  504.                     output = ""
  505.                     cursor = 1
  506.                 end
  507.             elseif key == keys.backspace then
  508.                 if cursor > 1 and #output > 0 then
  509.                     output = output:sub(1,cursor-2)..output:sub(cursor)
  510.                     cursor = cursor - 1
  511.                     pleaseDoFunc = true
  512.                 end
  513.             elseif key == keys.delete then
  514.                 if #output:sub(cursor,cursor) == 1 then
  515.                     output = output:sub(1,cursor-1)..output:sub(cursor+1)
  516.                     pleaseDoFunc = true
  517.                 end
  518.             end
  519.         elseif evt == "char" or evt == "paste" then
  520.             output = output:sub(1,cursor-1)..key..output:sub(cursor+(#key-1))
  521.             cursor = cursor + #key
  522.             pleaseDoFunc = true
  523.         end
  524.         local pOut = (output or ""):sub(math.max( 1,(#textToBlit(output)+sx)-scr_x) )
  525.         if pleaseDoFunc then
  526.             pleaseDoFunc = false
  527.             if type(doFunc) == "function" then
  528.                 doFunc(output)
  529.             end
  530.             termsetCursorPos(sx,sy)
  531.             if repchar then
  532.                 rite(repchar:sub(1,1):rep(#pOut))
  533.             else
  534.                 rite(pOut)
  535.             end
  536.             termwrite(" ")
  537.         end
  538.         termsetCursorPos(sx+cursorAdjFunc(pOut)+cursor-math.max( 1,(#textToBlit(output)+sx)-scr_x),sy)
  539.     end
  540. end
  541.  
  542. if yourName then
  543.     if textToBlit(yourName) == "con" or textToBlit(yourName) == "*" then return printError("Not that name!") end
  544. end
  545.  
  546. local writef = function(txt,noWrite)
  547.     if enchatSettings.doColorize then
  548.         local text, textCol, bgCol, usedformats = textToBlit(txt)
  549.         local out = blitWrap(text,textCol,bgCol,noWrite)
  550.         return out, #text, usedformats
  551.     else
  552.         return write(txt), #txt, {}
  553.     end
  554. end
  555.  
  556. local _ftlen = function(text)
  557.     return #textToBlit(text)-#text
  558. end
  559.  
  560. local clearLines = function(top, bottom)
  561.     for a = top, bottom do
  562.         termsetCursorPos(1,a)
  563.         termclearLine()
  564.     end
  565. end
  566.  
  567. scroll = 1
  568. doScroll = true
  569.  
  570. local urkrazy,_ = false
  571. local redrawScreen = function() --renders the chat and other things that change when scrolling.
  572.     tsv(false)
  573.     local prevX, prevY = termgetCursorPos()
  574.     local _logold = _log
  575.     _log,log = {},{}
  576.     for a = 1, #chatlog do
  577.         if not chatlog[a][3] then
  578.             _log[#_log+1] = {"",false}
  579.         end
  580.         if chatlog[a][4] == true then
  581.             _log[#_log+1] = {"<"..chatlog[a][1].."> "..chatlog[a][2],false}
  582.         else
  583.             _log[#_log+1] = {"<"..chatlog[a][1].."> "..chatlog[a][2],true}
  584.         end
  585.        
  586.     end
  587.     if not doScroll then
  588.         scroll = scroll + (#_log - #_logold)
  589.     end
  590.     for a = 1, #_log-(scroll-1) do
  591.         log[#log+1] = _log[a]
  592.     end
  593.     termsetCursorPos(1,1)
  594.     termsetBackgroundColor(palate.bg)
  595.     termsetTextColor(palate.txt)
  596.     termwrite(string.rep(" ",scr_x*(scr_y-2)))
  597.     local midPoint = {
  598.         scr_x / 2,
  599.         scr_y / 2,
  600.     }
  601.     local yoffset = 0
  602.     clearLines(1,scr_y-2)
  603.     termsetCursorPos(1,scr_y)
  604.     termclearLine()
  605.     local indent = 1 --in case any line is greater than the length of the screen
  606.     local indentIn = 0 --in the same case, mid writing
  607.     for a = 1, #log do
  608.         if log[a][2] then
  609.             indent = indent + writef(log[a][1],true)
  610.         else
  611.             indent = indent + math.floor(#log[a][1]/scr_x)
  612.         end
  613.     end
  614.     hasKrazy = false
  615.     for a = 1, #log do
  616.         termsetCursorPos(1,((((scr_y-1)+a-#log))-indent)+indentIn)
  617.         if log[a][2] then
  618.             indentIn = indentIn + writef(log[a][1],true)
  619.             _,_,urkrazy = writef(log[a][1],false)
  620.             hasKrazy = hasKrazy or (urkrazy.krazy or false)
  621.         else
  622.             indentIn = indentIn + math.floor((#log[a][1])/scr_x)
  623.             termwrite(log[a][1])
  624.         end
  625.     end
  626.     termsetCursorPos(scr_x,scr_y)
  627.     if doScroll then writef("&8.") else termwrite(" ") end
  628.     termsetCursorPos(prevX,prevY)
  629.     tsv(true)
  630. end
  631.  
  632. local handleCommand = function(commie)
  633.     commie = commie or {}
  634.     local command = commie[1] or ""
  635.     local argument = commie
  636.     if #commie > 0 then
  637.         table.remove(argument,1)
  638.         argument = tableconcat(argument," ") or ""
  639.     else
  640.         argument = ""
  641.     end
  642.     local commandTable
  643.     commandTable = {
  644.         ["exit"] = function() --why would you want to leave??
  645.             endMode = 0
  646.             return "exit"
  647.         end,
  648.         ["heil"] = function() --please don't tell me that you're offended, or I'll get offended
  649.             local heilTable = {
  650.                 "Cobra",
  651.                 "this",
  652.                 "LDD",
  653.                 "unto me",
  654.                 "dan200",
  655.                 "myself",
  656.                 "!",
  657.                 "oeed",
  658.                 "Exerro",
  659.                 "Kepler",
  660.                 "Danny",
  661.                 "Bagel",
  662.                 "Roger",
  663.                 "King Porky",
  664.             }
  665.             local hailer
  666.             if argument ~= "" then hailer = argument else hailer = heilTable[math.random(1,#heilTable)] end
  667.             local mess = "Heil "..hailer.."!"
  668.             os.queueEvent("enchat_send", yourName, mess)
  669.             logadd("con","You heiled "..hailer.."!",false)
  670.             redrawScreen()
  671.         end,
  672.         ["help"] = function() --I would call it "man", but there's only so much you can write about a two-argument function
  673.             local a = false
  674.             for k,v in pairs(commandTable) do
  675.                 strtime = tostring(math.floor(os.time()/timedivisor))
  676.                 logadd("*","&0/&4"..k,a)
  677.                 a = true
  678.             end
  679.             redrawScreen()
  680.         end,
  681.         ["clear"] = function() --*accidentally clears inventory* FUCK
  682.             _log = {}
  683.             log = {}
  684.             chatlog = {}
  685.             redrawScreen()
  686.         end,
  687.         ["ping"] = function() --what do you mean this command is useless
  688.             logadd("*",(argument ~= "") and argument or "Pong!",false)
  689.             redrawScreen()
  690.         end,
  691.         ["update"] = function()
  692.             local url
  693.             url = "https://pastebin.com/raw/XP2UPWgZ"
  694.             if shell then
  695.                 cfile = shell.getRunningProgram()
  696.             else
  697.                 logadd("con","&4Download where?",true)
  698.                 scroll = 1
  699.                 redrawScreen()
  700.                 termsetCursorPos(1,scr_y-1)
  701.                 termsetBackgroundColor(colors.lightGray)
  702.                 termsetTextColor(palate.chattxt)
  703.                 termclearLine()
  704.                 write(":")
  705.                 termsetBackgroundColor(palate.chatbg)
  706.                 cfile = read()
  707.             end
  708.             if fs.isReadOnly(cfile) then
  709.                 logadd("*","Unable to update to &4read-only directory.&r",true)
  710.                 redrawScreen()
  711.                 return
  712.             end
  713.             if not http then
  714.                 logadd("*","&4HTTP is disabled.&r Ask an admin to enable it.",true)
  715.                 redrawScreen()
  716.                 return
  717.             else
  718.                 if not http.checkURL(url) then
  719.                     logadd("*","&4It appears the download URL is whitelisted.&r &cPlease slap your admin in their stupid face.",true)
  720.                     redrawScreen()
  721.                     return
  722.                 end
  723.             end
  724.             --termsetBackgroundColor(palate.bg)
  725.             --termsetTextColor((palate.txt ~= colors.yellow) and colors.yellow or colors.black)
  726.             --cwrite("Downloading...",scr_y,true)
  727.             logadd("con","&4Downloading...",true)
  728.             scroll = 1
  729.             redrawScreen()
  730.             local data = http.get(url)
  731.             if not data then
  732.                 logadd("*","&4Couldn't connect to Pastebin.&r Sorry.",true)
  733.                 redrawScreen()
  734.                 return
  735.             else
  736.                 local file = fs.open(cfile,"w")
  737.                 file.write(data.readAll())
  738.                 file.close()
  739.                 --cwrite("Yay! Relaunching.",scr_y,true)
  740.                 logadd("con","&4Yay! Relaunching.",true)
  741.                 scroll = 1
  742.                 redrawScreen()
  743.                 sleep(1)
  744.                 --os.queueEvent("enchat_send", "con", yourName.."&r~r redownloaded the program.")
  745.                 sleep(0)
  746.                 endMode = 1
  747.                 return "restart"
  748.             end
  749.         end,
  750.         ["whoami"] = function()
  751.             if argument:lower() == "now" then
  752.                 logadd("*","You are still \""..yourName.."&r~r\"!",true)
  753.             else
  754.                 logadd("*","You are \""..yourName.."&r~r\"!",true)
  755.             end
  756.             redrawScreen()
  757.         end,
  758.         ["nick"] = function()
  759.             if argument ~= "" then
  760.                 if argument ~= yourName then
  761.                     if #textToBlit(argument) < 20 then
  762.                         --os.queueEvent("enchat_send", "con", tostring(yourName.."&r~r is now known as "..strcapsule(argument)))
  763.                         logadd("*",yourName.."&r~r is now known as "..argument,false)
  764.                         yourName = tostring(argument)
  765.                     else
  766.                         logadd("*","&4Maximum 20 non-formatting characters!",false)
  767.                     end
  768.                 else
  769.                     logadd("*","&4That's already your name.",false)
  770.                 end
  771.             else
  772.                 logadd("*","&4/nick <newname>",true)
  773.             end
  774.             redrawScreen()
  775.         end,
  776.         ["palate"] = function()
  777.             if argument:gsub("%s","") == "" then
  778.                 logadd("*","&4/palate "..tableConcat(palate,"/").." <color code>",false)
  779.             else
  780.                 argument = explode(" ",argument)
  781.                 if #argument == 1 then
  782.                     if argument[1]:gsub("%s",""):lower() == "reset" then
  783.                         palate = {
  784.                             bg = colors.gray,
  785.                             txt = colors.white,
  786.                             chatbg = colors.white,
  787.                             chattxt = colors.black,
  788.                         }
  789.                         logadd("*","&4You cleansed your palate.",false)
  790.                     else
  791.                         logadd("*","&4Give me a color code next time.",false)
  792.                     end
  793.                 else
  794.                     if #argument > 2 then
  795.                         argument = {argument[1], tableconcat(argument," ",2)}
  796.                     end
  797.                     argument[1] = argument[1]:lower()
  798.                     local newcol = argument[2]:lower()
  799.                     if not palate[argument[1]] then
  800.                         logadd("*","&4That's not a valid palate choice.")
  801.                     else
  802.                         if not colors_strnames[newcol] then
  803.                             logadd("*","&4That isn't a valid color code. (0-f)")
  804.                         else
  805.                             palate[argument[1]] = colors_strnames[newcol]
  806.                             logadd("*","You have such a sense of style.",false)
  807.                         end
  808.                     end
  809.                 end
  810.             end
  811.             redrawScreen()
  812.         end,
  813.     }
  814.     if commandTable[command] then
  815.         return commandTable[command]()
  816.     else
  817.         return false
  818.     end
  819. end
  820.  
  821. local doAndBack = function(func)
  822.     local backColor = termgetBackgroundColor()
  823.     local textColor = termgetTextColor()
  824.     local curX, curY = termgetCursorPos()
  825.     local results = {func()}
  826.     termsetBackgroundColor(backColor)
  827.     termsetTextColor(textColor)
  828.     termsetCursorPos(curX,curY)
  829.     return tableunpack(results)
  830. end
  831.  
  832. local renderAll = function()
  833.     termsetBackgroundColor(palate.bg)
  834.     termsetTextColor(palate.txt)
  835.     termclear()
  836.     local mHistory = {""}
  837.     local msg = nil
  838.     while true do
  839.         repeat
  840.             local msgPrompt = function()
  841.                 termsetCursorPos(1,scr_y-1)
  842.                 termsetBackgroundColor(palate.chatbg)
  843.                 termsetTextColor(colors.lightGray)
  844.                 termclearLine()
  845.                 write(">")
  846.                 termsetTextColor(palate.chattxt)
  847.             end
  848.             msgPrompt()
  849.             msg = enchatSettings.fancyMsg and funcread(nil,mHistory,msgPrompt,true,function(text)
  850.                 termsetBackgroundColor(palate.chatbg)
  851.                 local _,_,u = writef(text)
  852.                 hasKrazy=hasKrazy or (u.krazy or false)
  853.                 termsetCursorPos(1,scr_y)
  854.                 termsetBackgroundColor(palate.bg)
  855.                 termwrite((" "):rep(scr_x-2))
  856.             end,_ftlen,"redrawprompt") or read(nil,mHistory)
  857.             if msg ~= mHistory[#mHistory] and msg:gsub("%s","") ~= "" then
  858.                 tableinsert(mHistory,msg)
  859.             end
  860.         until string.gsub(msg,"%s","") ~= ""
  861.         if string.sub(msg,1,1) == "/" then
  862.             local comm = explode("%s",tostring(msg):sub(2))
  863.             local result = handleCommand(comm)
  864.             if result == "exit" then
  865.                 --os.queueEvent("enchat_send", "con", yourName.."&r~r has left.")
  866.                 sleep(0)
  867.                 return "exit"
  868.             elseif result == "restart" then
  869.                 sleep(0)
  870.                 return "restart"
  871.             elseif result == false then
  872.                 logadd("con","No such command.",false)
  873.                 redrawScreen()
  874.             end
  875.         else
  876.             local strtime = tostring(math.floor(os.time()/timedivisor))
  877.             logadd(yourName,msg,false)
  878.             redrawScreen()
  879.             os.queueEvent("enchat_send", yourName, msg)
  880.         end
  881.     end
  882. end
  883.  
  884. local httppost = function(tUrl,tForm,tHeader)
  885.   httphandle = http.post("http://smaller.hol.es/proxy.php?url="..tUrl,tForm,tHeader)
  886.   resp = httphandle.readAll()  
  887.   httphandle.close()
  888.   return resp
  889. end  
  890.  
  891. local sendMessages = function()
  892.     while true do
  893.         local event, name, message = os.pullEvent("enchat_send",key)
  894.         local str
  895.         if #name > 0 then
  896.             str = "**"..name.."**: "..message
  897.         else
  898.             str = message
  899.         end
  900.         header = {
  901.             ['ContentType'] = "application/json",
  902.             ['User-Agent'] = "DiscordBot (KrakenBot)",
  903.         }
  904.         httppost("https://discordapp.com/api/webhooks/300730487968759809/p7L4gBv51CAioatwMCTANd42bgDmb6QcL3rbD1-r1sYORqXFDMkb8SX85KN6GMcNocW3","content="..str,header)
  905.     end
  906. end
  907.  
  908. if termisColor() then
  909.     colormode = true
  910.     grayAllowed = true
  911. else
  912.     colormode = false
  913.     if _VERSION then
  914.         grayAllowed = true
  915.     else
  916.         grayAllowed = false
  917.     end
  918. end
  919.  
  920. termsetBackgroundColor(colors.gray)
  921. termclear()
  922. termsetCursorPos(1,1)
  923.  
  924. if not yourName then
  925.     write("\n")
  926.     local posX, posY = termgetCursorPos()
  927.     local prevX,prevY = 1,1
  928.     local namePrompt = function()
  929.         termsetCursorPos(prevX,prevY)
  930.         termsetBackgroundColor(colors.lightGray)
  931.         termsetTextColor(colors.black)
  932.         termclearLine()
  933.         write(">")
  934.     end
  935.     repeat
  936.         termsetBackgroundColor(colors.gray)
  937.         termsetTextColor(colors.white)
  938.         termsetCursorPos(posX,posY)
  939.         termclearLine()
  940.         cwrite("Enter your name:\n",posY)
  941.         prevX,prevY = termgetCursorPos()
  942.         termsetTextColor(colors.lightGray)
  943.         --cwrite("Color codes:",scr_y-3)
  944.         --cwrite("(&text,~backg.)",scr_y-2)
  945.         termsetCursorPos((scr_x/2)-8,scr_y-1)
  946.         termsetBackgroundColor(colors.gray)
  947.         termclearLine()
  948.         --blitWrap(textToBlit("~0&f0~1&01~22~33~44~55~66~77~88~99~aa~bb~cc~dd~ee~ff"))
  949.         termsetCursorPos(prevX,prevY)
  950.         termsetBackgroundColor(colors.lightGray)
  951.         termsetTextColor(colors.black)
  952.         termclearLine()
  953.         write(">")
  954.         yourName = enchatSettings.fancyMsg and funcread(nil,{},namePrompt,false,writef,_ftlen) or read()
  955.     until true
  956. end
  957. termsetBackgroundColor(colors.gray)
  958. termsetTextColor(colors.white)
  959.  
  960. local getScrolling = function()
  961.     local oldScroll
  962.     scroll = 1
  963.     doScroll = true
  964.     while true do
  965.         local _,dir,x,y = os.pullEvent("mouse_scroll")
  966.         oldScroll = scroll
  967.         if dir == (enchatSettings.reverseScroll and 1 or -1) then
  968.             if scroll < #_log-1 then
  969.                 scroll = scroll + 1
  970.                 doScroll = false
  971.             end
  972.         elseif dir == (enchatSettings.reverseScroll and -1 or 1) then
  973.             if scroll > 1 then
  974.                 scroll = scroll - 1
  975.                 if scroll == 1 then
  976.                     doScroll = true
  977.                 end
  978.             end
  979.         end
  980.         if oldScroll ~= scroll then
  981.             os.queueEvent("didScroll",scroll)
  982.         end
  983.     end
  984. end
  985.  
  986. --os.queueEvent("enchat_send", "con", yourName.."&r~r moseyed on over.")
  987.  
  988. local funcList = {
  989.     sendMessages,
  990.     renderAll,
  991.     getScrolling,
  992. }
  993. parallelwaitForAny(tableunpack(funcList))
  994. if endMode == 1 then
  995.     if shell then
  996.         shell.run(strcapsule(cfile),strcapsule(encKey),strcapsule(yourName))
  997.     else
  998.         loadfile(cfile)(encKey,yourName)
  999.     end
  1000. else
  1001.     if modem then modem.close(channel) end
  1002.     termsetCursorPos(1,scr_y)
  1003.     termsetBackgroundColor(colors.black)
  1004.     termclearLine()
  1005. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement