SHARE
TWEET

mess_serv

ZNZNCOOP May 5th, 2014 251 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.    *Apokalypsys, Dimus
  3.    *http://minecrafting.ru/
  4.    *http://minecrafting.ru/forum/viewtopic.php?f=32&t=5331
  5. ]]
  6.  
  7. local hex_char = "0123456789abcdef"
  8.  
  9. local function getHex(seed)
  10.    local str = ""
  11.    for i = 0, 3 do
  12.       local ind1, ind2 = bit.band(bit.brshift(seed, i * 8 + 4), 15) + 1, bit.band(bit.brshift(seed, i * 8), 15) + 1
  13.       str = str..
  14.       hex_char:sub(ind1, ind1)..
  15.       hex_char:sub(ind2, ind2)
  16.    end
  17.    return str
  18. end
  19.  
  20. local function string_to_blks(str)
  21.    local nblk = bit.brshift((str:len() + 8), 6) + 1
  22.    local blks = {}
  23.    local len = str:len()
  24.    for i = 0, nblk * 16 - 1 do
  25.       blks[i] = 0
  26.    end
  27.    for i = 0, str:len() - 1 do
  28.       blks[bit.brshift(i, 2)] =
  29.       bit.bor(
  30.          blks[bit.brshift(i, 2)],
  31.          bit.blshift(
  32.             str:byte(i+1),
  33.              (((i) % 4) * 8)
  34.           )
  35.       )
  36.    end
  37.       blks[bit.brshift(len, 2)] =
  38.       bit.bor(
  39.          blks[bit.brshift(len, 2)],
  40.          bit.blshift(
  41.             128,
  42.             (((len) % 4) * 8)
  43.          )
  44.       )
  45.       blks[nblk * 16 - 2] = len * 8
  46.    return blks
  47. end
  48.  
  49. local function add(x, y)
  50.    return x + y > 4294967296 and x + y or x + y - 4294967296
  51. end
  52.  
  53. local function rol(number, count)
  54.    return bit.bor(bit.blshift(number, count), bit.blogic_rshift(number, (32 - count)))
  55. end
  56.  
  57. local function X(a, b, c, x, s, t)
  58.    return add(rol(add(add(b, a), add(x, t)), s), c)
  59. end
  60.  
  61. local function F(a, b, c, d, x, s, t)
  62.    return X(bit.bor(bit.band(b, c), bit.band(bit.bnot(b), d)), a, b, x, s, t)
  63. end
  64.  
  65. local function G(a, b, c, d, x, s, t)
  66.    return X(bit.bor(bit.band(b, d), bit.band(c, bit.bnot(d))), a, b, x, s, t)
  67. end
  68.  
  69. local function H(a, b, c, d, x, s, t)
  70.    return X(bit.bxor(bit.bxor(b, c), d), a, b, x, s, t)
  71. end
  72.  
  73. local function I(a, b, c, d, x, s, t)
  74.    return X(bit.bxor(c, bit.bor(b, bit.bnot(d))), a, b, x, s, t)
  75. end
  76.  
  77. function md5(encoding_string)
  78.    local blks = string_to_blks(encoding_string)
  79.  
  80.    local a = 1732584193
  81.    local b = -271733879
  82.    local c = -1732584194
  83.    local d = 271733878
  84.  
  85.    for i = 0, #blks-1, 16 do
  86.       local olda, oldb, oldc, oldd = a, b, c, d
  87.  
  88.       a = F(a, b, c, d, blks[i+ 0], 7, -680876936)
  89.       d = F(d, a, b, c, blks[i+ 1], 12, -389564586)
  90.       c = F(c, d, a, b, blks[i+ 2], 17, 606105819)
  91.       b = F(b, c, d, a, blks[i+ 3], 22, -1044525330)
  92.       a = F(a, b, c, d, blks[i+ 4], 7, -176418897)
  93.       d = F(d, a, b, c, blks[i+ 5], 12, 1200080426)
  94.       c = F(c, d, a, b, blks[i+ 6], 17, -1473231341)
  95.       b = F(b, c, d, a, blks[i+ 7], 22, -45705983)
  96.       a = F(a, b, c, d, blks[i+ 8], 7, 1770035416)
  97.       d = F(d, a, b, c, blks[i+ 9], 12, -1958414417)
  98.       c = F(c, d, a, b, blks[i+10], 17, -42063)
  99.       b = F(b, c, d, a, blks[i+11], 22, -1990404162)
  100.       a = F(a, b, c, d, blks[i+12], 7, 1804603682)
  101.       d = F(d, a, b, c, blks[i+13], 12, -40341101)
  102.       c = F(c, d, a, b, blks[i+14], 17, -1502002290)
  103.       b = F(b, c, d, a, blks[i+15], 22, 1236535329)
  104.  
  105.       a = G(a, b, c, d, blks[i+ 1], 5, -165796510)
  106.       d = G(d, a, b, c, blks[i+ 6], 9, -1069501632)
  107.       c = G(c, d, a, b, blks[i+11], 14, 643717713)
  108.       b = G(b, c, d, a, blks[i+ 0], 20, -373897302)
  109.       a = G(a, b, c, d, blks[i+ 5], 5, -701558691)
  110.       d = G(d, a, b, c, blks[i+10], 9, 38016083)
  111.       c = G(c, d, a, b, blks[i+15], 14, -660478335)
  112.       b = G(b, c, d, a, blks[i+ 4], 20, -405537848)
  113.       a = G(a, b, c, d, blks[i+ 9], 5, 568446438)
  114.       d = G(d, a, b, c, blks[i+14], 9, -1019803690)
  115.       c = G(c, d, a, b, blks[i+ 3], 14, -187363961)
  116.       b = G(b, c, d, a, blks[i+ 8], 20, 1163531501)
  117.       a = G(a, b, c, d, blks[i+13], 5, -1444681467)
  118.       d = G(d, a, b, c, blks[i+ 2], 9, -51403784)
  119.       c = G(c, d, a, b, blks[i+ 7], 14, 1735328473)
  120.       b = G(b, c, d, a, blks[i+12], 20, -1926607734)
  121.  
  122.       a = H(a, b, c, d, blks[i+ 5], 4, -378558)
  123.       d = H(d, a, b, c, blks[i+ 8], 11, -2022574463)
  124.       c = H(c, d, a, b, blks[i+11], 16, 1839030562)
  125.       b = H(b, c, d, a, blks[i+14], 23, -35309556)
  126.       a = H(a, b, c, d, blks[i+ 1], 4, -1530992060)
  127.       d = H(d, a, b, c, blks[i+ 4], 11, 1272893353)
  128.       c = H(c, d, a, b, blks[i+ 7], 16, -155497632)
  129.       b = H(b, c, d, a, blks[i+10], 23, -1094730640)
  130.       a = H(a, b, c, d, blks[i+13], 4, 681279174)
  131.       d = H(d, a, b, c, blks[i+ 0], 11, -358537222)
  132.       c = H(c, d, a, b, blks[i+ 3], 16, -722521979)
  133.       b = H(b, c, d, a, blks[i+ 6], 23, 76029189)
  134.       a = H(a, b, c, d, blks[i+ 9], 4, -640364487)
  135.       d = H(d, a, b, c, blks[i+12], 11, -421815835)
  136.       c = H(c, d, a, b, blks[i+15], 16, 530742520)
  137.       b = H(b, c, d, a, blks[i+ 2], 23, -995338651)
  138.  
  139.       a = I(a, b, c, d, blks[i+ 0], 6, -198630844)
  140.       d = I(d, a, b, c, blks[i+ 7], 10, 1126891415)
  141.       c = I(c, d, a, b, blks[i+14], 15, -1416354905)
  142.       b = I(b, c, d, a, blks[i+ 5], 21, -57434055)
  143.       a = I(a, b, c, d, blks[i+12], 6, 1700485571)
  144.       d = I(d, a, b, c, blks[i+ 3], 10, -1894986606)
  145.       c = I(c, d, a, b, blks[i+10], 15, -1051523)
  146.       b = I(b, c, d, a, blks[i+ 1], 21, -2054922799)
  147.       a = I(a, b, c, d, blks[i+ 8], 6, 1873313359)
  148.       d = I(d, a, b, c, blks[i+15], 10, -30611744)
  149.       c = I(c, d, a, b, blks[i+ 6], 15, -1560198380)
  150.       b = I(b, c, d, a, blks[i+13], 21, 1309151649)
  151.       a = I(a, b, c, d, blks[i+ 4], 6, -145523070)
  152.       d = I(d, a, b, c, blks[i+11], 10, -1120210379)
  153.       c = I(c, d, a, b, blks[i+ 2], 15, 718787259)
  154.       b = I(b, c, d, a, blks[i+ 9], 21, -343485551)
  155.  
  156.       a = add(a, olda)
  157.       b = add(b, oldb)
  158.       c = add(c, oldc)
  159.       d = add(d, oldd)
  160.    end
  161.    return getHex(a)..getHex(b)..getHex(c)..getHex(d)
  162. end
  163. --[[
  164.    *End of MD5 encoding
  165. ]]
  166. function open()
  167.   local bOpen, sFreeSide = false, nil
  168.   for n,sSide in pairs(rs.getSides()) do
  169.     if peripheral.getType( sSide ) == "modem" then
  170.       sFreeSide = sSide
  171.       if rednet.isOpen( sSide ) then
  172.         bOpen = true
  173.         break
  174.       end
  175.     end
  176.   end
  177.   if not bOpen then
  178.     if sFreeSide then
  179.       print( "No modem active. Opening "..sFreeSide.." modem" )
  180.       rednet.open( sFreeSide )
  181.       return true
  182.     else
  183.       print( "No modem attached" )
  184.       return false
  185.     end
  186.   end
  187.   return true
  188. end
  189.  
  190. ActiveUser={}
  191. AllUsers={}
  192. eventKey={}
  193. eventRednet={}
  194. pwdfilename=shell.resolve('passwd')
  195. messdir=shell.dir()..'/messages/'
  196. readdir=shell.dir()..'/readed/'
  197. newsdir=shell.dir()..'/news/'
  198.  
  199. eventRednet.ask=function (dat,id)
  200.   if ActiveUser[id] then
  201.     return 'You are logined as '..ActiveUser[id].name
  202.   else
  203.     return 'You are not logined'
  204.   end
  205. end
  206.  
  207. eventRednet.adduser=function (dat,id)
  208.   local user,pwd=dat:match('user=([%w_]+)%s+pwd=(%S+)')
  209.   if not user then return 'Incorrect login or password' end
  210.   if AllUsers[user] then return 'User '..user..' already exists' end
  211.   file=fs.open(pwdfilename,'a')
  212.   file.write(user..' '..md5(pwd)..'\r\n')  
  213.   file:close()
  214.   AllUsers[user]=md5(pwd)
  215.   ActiveUser[id]={}
  216.   ActiveUser[id].name=user
  217.   ActiveUser[id].mess={}
  218.   print ('Create user '..user..' id '..id)
  219.   return 'User '..user..' created'
  220. end
  221.  
  222. eventRednet.login=function (dat,id)
  223.   local user,pwd=dat:match('user=([%w_]+)%s+pwd=(%S+)')
  224.   if not user then
  225.     return 'Incorrect login or password'
  226.   end
  227.   if AllUsers[user]==md5(pwd) then
  228.     ActiveUser[id]={}
  229.     ActiveUser[id].name=user
  230.     ActiveUser[id].mess={}
  231.     print ('Login user '..user..' id '..id)
  232.     local file=fs.open(readdir..user,'r')
  233.     if file then
  234.       local line=file.readLine()
  235.       while line do
  236.         table.insert( ActiveUser[id].mess, line )
  237.         line=file.readLine()
  238.       end
  239.       file:close()
  240.     end
  241.     return 'User '..user..' connected'
  242.   else
  243.     print ('Unsuccessful login user '..user..' id '..id)
  244.     return 'Incorrect login or password'
  245.   end
  246. end
  247.  
  248. eventRednet.logout=function (dat,id)
  249.   if ActiveUser[id] then
  250.     local name=ActiveUser[id].name
  251.     ActiveUser[id]=nil
  252.     print ('User '..name..' disconnected id '..id)
  253.     return 'User '..name..' disconnected'
  254.   end
  255. end
  256.  
  257. eventRednet.checkmess=function (dat,id)
  258.   if ActiveUser[id] then
  259.     local file=fs.open(messdir..ActiveUser[id].name,'r')
  260.     if file then
  261.       local n=0
  262.       local line=file.readLine()
  263.       while line do
  264.         n=n+1
  265.         table.insert( ActiveUser[id].mess, line )
  266.         line=file.readLine()
  267.       end
  268.       file:close()
  269.       if n>0 then
  270.         file=fs.open(readdir..ActiveUser[id].name,'w')
  271.         if file then
  272.           for i,mess in ipairs(ActiveUser[id].mess) do
  273.             file.write(ActiveUser[id].mess[i]..'\r\n')
  274.           end
  275.           file:close()
  276.           fs.delete(messdir..ActiveUser[id].name)
  277.         end
  278.         return 'You have '..n..' new message(s)'
  279.       end
  280.     end
  281.     return 'You have not new messages'
  282.   end
  283.   return 'You need login'
  284. end
  285.  
  286. eventRednet.readmess=function (dat,id)
  287.   if ActiveUser[id] then
  288.     if ActiveUser[id].mess and #ActiveUser[id].mess>ActiveUser[id].nummes then
  289.       ActiveUser[id].nummes=ActiveUser[id].nummes+1
  290.       return ActiveUser[id].mess[ActiveUser[id].nummes]
  291.     else
  292.       return 'You have not more messages.'
  293.     end
  294.   end
  295.   return 'You need login'
  296. end
  297.  
  298. eventRednet.readfirst=function (dat,id)
  299.   if ActiveUser[id] then
  300.     ActiveUser[id].nummes=0
  301.     return eventRednet.readmess(dat,id)
  302.   end
  303.   return 'You need login'
  304. end
  305.  
  306. eventRednet.delmess=function (dat,id)
  307.   if ActiveUser[id] then
  308.     if ActiveUser[id].mess and #ActiveUser[id].mess>ActiveUser[id].nummes then
  309.     table.remove (ActiveUser[id].mess , ActiveUser[id].nummes)
  310.     ActiveUser[id].nummes=ActiveUser[id].nummes-1
  311.     file=fs.open(readdir..ActiveUser[id].name,'w')
  312.     if file then
  313.       for i,mess in ipairs(ActiveUser[id].mess) do
  314.         file.write(ActiveUser[id].mess[i]..'\r\n')
  315.       end
  316.       file:close()
  317.     end
  318.     end
  319.     return eventRednet.readmess(dat,id)
  320.   end
  321.   return 'You need login'
  322. end
  323.  
  324. eventRednet.sendmess=function (dat,id)
  325.   if ActiveUser[id] then
  326.     local user,mess=dat:match('user=(%S+)(.*)')
  327.     if not AllUsers[user] then return 'Incorrect user name' end
  328.     file=fs.open(messdir..user,'a')
  329.     if not file then
  330.       return 'Cannt open message file'
  331.     end
  332.     file.write('<'..ActiveUser[id].name..'> '..mess..'\r\n')
  333.     file:close()
  334.     local k=next(ActiveUser)
  335.     while k do
  336.       if user==ActiveUser[k].name then
  337.         rednet.send(k,'You have new message')
  338.       end
  339.       k=next(ActiveUser,k)
  340.     end
  341.     return 'Message is sent successful'
  342.   end
  343.   return 'You need login'
  344. end
  345.  
  346. eventRednet.users=function(dat,id)
  347.   local users=''
  348.   for name in pairs(AllUsers) do
  349.     users=users..name..' '
  350.   end
  351.   return users
  352. end
  353.  
  354. eventRednet.news=function(dat,id)
  355.   if ActiveUser[id] then
  356.     if not ActiveUser[id].news or #ActiveUser[id].news==0 then
  357.       ActiveUser[id].news={}
  358.       for n,file in pairs(fs.list(newsdir)) do
  359.         if not fs.isDir(newsdir..file) then
  360.                 table.insert( ActiveUser[id].news, file )
  361.         end
  362.       end
  363.     end
  364. --    print('News ',#ActiveUser[id].news)
  365.     if #ActiveUser[id].news==0 then
  366.       return 'No more news'
  367.     else
  368.       local filename = table.remove (ActiveUser[id].news , 1)
  369.       local text=''
  370.       local file=fs.open(newsdir..filename,'r')
  371.       if file then
  372.         local line=file.readLine()
  373.         while line do
  374.           text=text..line..'\n'
  375.           line=file.readLine()
  376.         end
  377.         file:close()
  378.       end
  379.       return text
  380.     end
  381.   end
  382.   return 'You need login'
  383. end
  384.  
  385. work=true
  386.  
  387. eventKey[keys.a]=function ()
  388.   local k=next(ActiveUser)
  389.   if not k then
  390.     print('No active users')
  391.     return
  392.   end
  393.   print('Active users:')
  394.   while k do
  395.     print(k,' ',ActiveUser[k].name)
  396.     k=next(ActiveUser,k)
  397.   end
  398. end
  399.  
  400. eventKey[keys.q]= function ()
  401.   work=false
  402. end
  403.  
  404. eventKey[keys.u]= function ()
  405.   print('All users:')
  406.   print(eventRednet.users())
  407. end
  408.  
  409. if not fs.exists(pwdfilename) then
  410.   local file=fs.open(pwdfilename,'w')
  411.   file:close()
  412. end
  413. if not fs.exists(messdir) then fs.makeDir(messdir) end
  414. if not fs.exists(readdir) then fs.makeDir(readdir) end
  415. if not fs.exists(newsdir) then fs.makeDir(newsdir) end
  416.  
  417. local file=fs.open(pwdfilename,'r')
  418. local line=file.readLine()
  419. while line do
  420.   local name,md5pwd=line:match('(%S+)%s+(%S+)')
  421.   if name then
  422.     AllUsers[name]=md5pwd
  423.   end
  424.   line=file.readLine()
  425. end
  426. file.close()
  427.  
  428. if open() then
  429.   while work do
  430.     local event,p1,p2=os.pullEvent()
  431.     if event=='rednet_message' then
  432.       local func,dat=p2:match('(%S+)(.*)')
  433.       if eventRednet[func]~=nil then
  434. --        print(func,' ',dat)
  435.         p2=eventRednet[func](dat,p1)
  436.       else
  437.         p2='Incorrect function "'..func..'"'
  438.       end
  439.       rednet.send(p1,p2)
  440.     elseif event=='key' then
  441.       if eventKey[p1]~=nil then
  442.         eventKey[p1]()
  443.       end
  444.     end
  445.   end
  446. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top