Advertisement
LDDestroier

Tarnchat v1 Mirror

Sep 18th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.74 KB | None | 0 0
  1. --Encryption program by PixelToast! Thanks!
  2. local function zfill(N)
  3.     N=string.format("%X",N)
  4.     Zs=""
  5.     if #N==1 then
  6.         Zs="0"
  7.     end
  8.     return Zs..N
  9. end
  10.  
  11. local function serializeImpl(t)
  12.     local sType = type(t)
  13.     if sType == "table" then
  14.         local lstcnt=0
  15.         for k,v in pairs(t) do
  16.             lstcnt = lstcnt + 1
  17.         end
  18.         local result = "{"
  19.         local aset=1
  20.         for k,v in pairs(t) do
  21.             if k==aset then
  22.                 result = result..serializeImpl(v)..","
  23.                 aset=aset+1
  24.             else
  25.                 result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  26.             end
  27.         end
  28.         result = result.."}"
  29.         return result
  30.     elseif sType == "string" then
  31.         return string.format("%q",t)
  32.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  33.         return tostring(t)
  34.     elseif sType == "function" then
  35.         local status,data=pcall(string.dump,t)
  36.         if status then
  37.             return 'func('..string.format("%q",data)..')'
  38.         else
  39.             error()
  40.         end
  41.     else
  42.         error()
  43.     end
  44. end
  45.  
  46. local function split(T,func)
  47.     if func then
  48.         T=func(T)
  49.     end
  50.     local Out={}
  51.     if type(T)=="table" then
  52.         for k,v in pairs(T) do
  53.             Out[split(k)]=split(v)
  54.         end
  55.     else
  56.         Out=T
  57.     end
  58.     return Out
  59. end
  60.  
  61. local function serialize( t )
  62.     t=split(t)
  63.     return serializeImpl( t, tTracking )
  64. end
  65.  
  66. local function unserialize( s )
  67.     local func, e = loadstring( "return "..s, "serialize" )
  68.     local funcs={}
  69.     if not func then
  70.         return e
  71.     end
  72.     setfenv( func, {
  73.         func=function(S)
  74.             local new={}
  75.             funcs[new]=S
  76.             return new
  77.         end,
  78.     })
  79.     return split(func(),function(val)
  80.         if funcs[val] then
  81.             return loadstring(funcs[val])
  82.         else
  83.             return val
  84.         end
  85.     end)
  86. end
  87.  
  88. local function sure(N,n)
  89.     if (l2-n)<1 then N="0" end
  90.     return N
  91. end
  92.  
  93. local function splitnum(S)
  94.     Out=""
  95.     for l1=1,#S,2 do
  96.         l2=(#S-l1)+1
  97.         CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
  98.         Out=string.char(CNum)..Out
  99.     end
  100.     return Out
  101. end
  102.  
  103. local function wrap(N)
  104.     return N-(math.floor(N/256)*256)
  105. end
  106.  
  107. function checksum(S,num)
  108.     local sum=0
  109.     for char in string.gmatch(S,".") do
  110.         for l1=1,(num or 1) do
  111.             math.randomseed(string.byte(char)+sum)
  112.             sum=sum+math.random(0,9999)
  113.         end
  114.     end
  115.     math.randomseed(sum)
  116.     return sum
  117. end
  118.  
  119. local function genkey(len)
  120.     checksum(encKey)
  121.     local key={}
  122.     local tKeys={}
  123.     for l1=1,len do
  124.         local num=math.random(1,len)
  125.         while tKeys[num] do
  126.             num=math.random(1,len)
  127.         end
  128.         tKeys[num]=true
  129.         key[l1]={num,math.random(0,255)}
  130.     end
  131.     return key
  132. end
  133.  
  134. function encrypt(data)
  135.     data=serialize(data)
  136.     local chs=checksum(data)
  137.     local key=genkey(#data,encKey)
  138.     local out={}
  139.     local cnt=1
  140.     for char in string.gmatch(data,".") do
  141.         table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
  142.         cnt=cnt+1
  143.     end
  144.     return string.sub(serialize({chs,table.concat(out)}),2,-3)
  145. end
  146.  
  147. function decrypt(data)
  148.     local oData=data
  149.     data=unserialize("{"..data.."}")
  150.     if type(data)~="table" then
  151.         return oData
  152.     end
  153.     local chs=data[1]
  154.     data=data[2]
  155.     local key=genkey((#data)/2,encKey)
  156.     local sKey={}
  157.     for k,v in pairs(key) do
  158.         sKey[v[1]]={k,v[2]}
  159.     end
  160.     local str=splitnum(data)
  161.     local cnt=1
  162.     local out={}
  163.     for char in string.gmatch(str,".") do
  164.         table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
  165.         cnt=cnt+1
  166.     end
  167.     out=table.concat(out)
  168.     if checksum(out or "")==chs then
  169.         return unserialize(out)
  170.     end
  171.     return oData,out,chs
  172. end
  173.  
  174.  
  175.  
  176. term.clear()
  177. term.setCursorPos(1,1)
  178.  
  179. print("Enter encryption key:")
  180. write(">")
  181. encKey = tostring(read())
  182.  
  183. function LR()
  184. local n=1
  185. while true do
  186. local x, y=term.getCursorPos()
  187. term.clearLine()
  188. if n==1 then write(">LEFT<   RIGHT") else write (" LEFT   >RIGHT<") end
  189. term.setCursorPos(x, y)
  190. a, b=os.pullEventRaw()
  191. while a~="key" do a, b=os.pullEventRaw() end
  192. if b==203 and n==2 then n=1 end
  193. if b==205 and n==1 then n=2 end
  194. if b==28 then print("") break end
  195. end
  196. if n==1 then return true end
  197. if n==2 then return false end
  198. return false
  199. end
  200. local chatTable = { [1] = "Welcome to TarnChat! (/help for commands)" }
  201. local chatLine = 1
  202. local message = ""
  203. local user = "???"
  204. local compID = os.getComputerID()
  205. local toID = ""
  206. local msgMessage = ""
  207. local msgSent = ""
  208.  
  209. function drawLine()
  210. term.setCursorPos( 1, 17 )
  211. term.clearLine()
  212. for n=1,49 do
  213. io.write("=")
  214. end
  215. term.setCursorPos(1,18)
  216. term.clearLine()
  217. write(username)
  218. write(": ")
  219. write(message)
  220. end
  221. function addChat(str)
  222. if chatLine < 16 then
  223.   table.insert(chatTable, str)
  224.   chatLine = chatLine + 1
  225. else
  226.   for n=1,15 do
  227.    chatTable[n] = chatTable[n+1]
  228.   end
  229.    chatTable[16] = str
  230.   end
  231. end
  232. function quit()
  233. os.exit()
  234. end
  235. function showChat()
  236. term.setCursorPos( 1, 1 )
  237. for n=1,chatLine do
  238.   term.clearLine()
  239.   print(chatTable[n])
  240. end
  241. end
  242. print("Is your modem on the Left or the Right?")
  243. term.setCursorPos(1,18)
  244. if LR() == true then
  245. rednet.open("left")
  246. else
  247. rednet.open("right")
  248. end
  249. term.clear()
  250. repeat
  251. term.setCursorPos(1,1)
  252. print("Enter a username for yourself.")
  253. term.setCursorPos(1,18)
  254. user = read()
  255. if string.len(user) > 9 then
  256. term.setCursorPos(1,2)
  257. print("Name too long! 9 character maximum.")
  258. term.setCursorPos(1,18)
  259. elseif string.len(user) > 0 then
  260. write("Your username is ")
  261. write(user)
  262. print("")
  263. print("Entering Chat...")
  264. username = user
  265. end
  266. until string.len(user) ~=0
  267. sleep(2.0)
  268. term.clear()
  269. drawLine()
  270. rednet.broadcast(encrypt(""))
  271. rednet.broadcast(encrypt(username .. " enters the room!"))
  272. repeat
  273.  
  274. term.setCursorPos(1,1)
  275. write(chatTable[chatLine] or "")
  276. drawLine()
  277. showChat()
  278. term.setCursorPos((3 + string.len(username) + string.len(message)),18)
  279. term.setCursorBlink( true )
  280.  
  281. event, param = os.pullEventRaw()
  282. if event == "key" then
  283.   if param == 28 then
  284.    if string.sub(message,1,1) == "/" then
  285.         if string.sub(message,1,6) == "/name " then
  286.          if string.len(message) > 6 then
  287.           if string.len(message) < 16 then
  288.            user = username
  289.            username = string.sub(message,7,string.len(message))
  290.            message = "User '" .. user .. "' is now called '" .. username .. "'!"
  291.            rednet.broadcast(encrypt(""))
  292.            rednet.broadcast(encrypt(message))
  293.            addChat(message)
  294.            message = ""
  295.            drawLine()
  296.           else
  297.            addChat("<SYS> Name too long! Maximum 9 characters.")
  298.            message = ""
  299.           end
  300.          end
  301.         elseif string.sub(message,1,4) == "/me " then
  302.          local meMessage = string.sub(message,4,string.len(message))
  303.          message = "*" .. username .. meMessage
  304.          rednet.broadcast(encrypt(""))
  305.          rednet.broadcast(encrypt(message))
  306.          addChat(message)
  307.          message = ""
  308.          drawLine()
  309.         elseif string.sub(message,1,5) == "/list" then
  310.          rednet.broadcast(encrypt(""))
  311.          rednet.broadcast(encrypt(message))
  312.          message = ""
  313.         elseif string.sub(message,1,5) == "/exit" then
  314.          addChat("<SYS> Goodbye!")
  315.          rednet.broadcast(encrypt(""))
  316.          rednet.broadcast(encrypt("User '" .. username .. "' exits the room."))
  317.          sleep(1.0)
  318.          term.clear()
  319.          term.setCursorPos(1,1)
  320.          break
  321.         elseif string.sub(message,1,5) == "/quit" then
  322.          addChat("<SYS> Goodbye!")
  323.          rednet.broadcast(encrypt(""))
  324.          rednet.broadcast(encrypt("User '" .. username .. "' exits the room."))
  325.          sleep(1.0)
  326.          term.clear()
  327.          term.setCursorPos(1,1)
  328.          break
  329.         elseif string.sub(message,1,5) == "/help" then
  330.          addChat("/help - lists programs")
  331.          addChat("/name <name> - changes username")
  332.          addChat("/list - lists connected users, and console IDs")
  333.          addChat("/me - used as emote")
  334.          addChat("/clear - clears the screen")
  335.          addChat("/msg <ID> <message> - sends a private message")
  336.          addChat("/exit - exits the program")
  337.          message = ""
  338.         elseif string.sub(message,1,5) == "/msg " then
  339.          if string.len(message) > 5 then
  340.           if string.sub(message,7,7) == " " then
  341.            toID = string.sub(message,6,6)
  342.            msgMessage = string.sub(message,8,string.len(message))
  343.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  344.            message = username .. " whispers to you: " .. msgMessage
  345.            toID = tonumber(toID)
  346.            rednet.send(toID,encrypt(""))
  347.            rednet.send(toID,encrypt(message))
  348.            message = ""
  349.           elseif string.sub(message, 8,8) == " " then
  350.            toID = string.sub(message,6,7)
  351.            msgMessage = string.sub(message,9,string.len(message))
  352.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  353.            message = username .. " whispers to you: " .. msgMessage
  354.            toID = tonumber(toID)
  355.            rednet.send(toID,encrypt(""))
  356.            rednet.send(toID,encrypt(message))
  357.            message = ""
  358.           elseif string.sub(message, 9,9) == " " then
  359.            toID = string.sub(message,6,8)
  360.            msgMessage = string.sub(message,10,string.len(message))
  361.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  362.            message = username .. " whispers to you: " .. msgMessage
  363.            toID = tonumber(toID)
  364.            rednet.send(toID,encrypt(""))
  365.            rednet.send(toID,encrypt(message))
  366.            message = ""
  367.           elseif string.sub(message,10,10) == " " then
  368.            toID = string.sub(message,6,9)
  369.            msgMessage = string.sub(message,11,string.len(message))
  370.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  371.            message = username .. " whispers to you: " .. msgMessage
  372.            toID = tonumber(toID)
  373.            rednet.send(toID,encrypt(""))
  374.            rednet.send(toID,encrypt(message))
  375.            message = ""
  376.           end
  377.          end
  378.         elseif string.sub(message,1,3) == "/m " then
  379.          if string.len(message) > 3 then
  380.           if string.sub(message,5,5) == " " then
  381.            toID = string.sub(message,4,4)
  382.            msgMessage = string.sub(message,6,string.len(message))
  383.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  384.            message = username .. " whispers to you: " .. msgMessage
  385.            toID = tonumber(toID)
  386.            rednet.send(toID,encrypt(""))
  387.            rednet.send(toID,encrypt(message))
  388.            message = ""
  389.           elseif string.sub(message, 6,6) == " " then
  390.            toID = string.sub(message,4,5)
  391.            msgMessage = string.sub(message,7,string.len(message))
  392.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  393.            message = username .. " whispers to you: " .. msgMessage
  394.            toID = tonumber(toID)
  395.            rednet.send(toID,encrypt(""))
  396.            rednet.send(toID,encrypt(message))
  397.            message = ""
  398.           elseif string.sub(message, 7,7) == " " then
  399.            toID = string.sub(message,4,6)
  400.            msgMessage = string.sub(message,8,string.len(message))
  401.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  402.            message = username .. " whispers to you: " .. msgMessage
  403.            toID = tonumber(toID)
  404.            rednet.send(toID,encrypt(""))
  405.            rednet.send(toID,encrypt(message))
  406.            message = ""
  407.           elseif string.sub(message,8,8) == " " then
  408.            toID = string.sub(message,4,7)
  409.            msgMessage = string.sub(message,9,string.len(message))
  410.            addChat("You whisper to #" .. toID .. ": " .. msgMessage)
  411.            message = username .. " whispers to you: " .. msgMessage
  412.            toID = tonumber(toID)
  413.            rednet.send(toID,encrypt(""))
  414.            rednet.send(toID,encrypt(message))
  415.            message = ""
  416.           end
  417.          end
  418.         elseif string.sub(message,1,6) == "/clear" then
  419.          term.clear()
  420.          for n=1,16 do
  421.           addChat(" ")
  422.          end
  423.          chatTable = {}
  424.          chatLine = 1
  425.          message = ""
  426.         else
  427.          message = ""
  428.         end  
  429.    else
  430.         if string.len(message) > 0 then
  431.           username = tostring(username)
  432.           message = tostring(message)
  433.           message = "<" .. username .. "> " .. message
  434.           rednet.broadcast(encrypt(""))
  435.           rednet.broadcast(encrypt(message))
  436.           addChat(message)
  437.           message = ""
  438.           drawLine()
  439.         end
  440.    end
  441.   elseif param == 14 then
  442.    message = string.sub(message,1,string.len(message)-1)
  443.    drawLine()
  444.   end
  445. end
  446.  
  447. if event == "char" then
  448.   if string.len(message) < 40 then
  449.         message = message .. param
  450.   end
  451.   drawLine()
  452. end
  453.  
  454. if event == "rednet_message" then
  455.   id, incoming = rednet.receive(0.5)
  456.   incoming = decrypt(incoming)
  457.  
  458.   if incoming == "/list" then
  459.    rednet.send(id,encrypt(""))
  460.    rednet.send(id,encrypt("User '" .. username .. "' is connected at terminal #" .. compID))
  461.   else
  462.    addChat(incoming)
  463.   end
  464. end
  465.  
  466. until message == "override"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement