Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- *Apokalypsys, Dimus
- *http://minecrafting.ru/
- *http://minecrafting.ru/forum/viewtopic.php?f=32&t=5331
- ]]
- local hex_char = "0123456789abcdef"
- local function getHex(seed)
- local str = ""
- for i = 0, 3 do
- local ind1, ind2 = bit.band(bit.brshift(seed, i * 8 + 4), 15) + 1, bit.band(bit.brshift(seed, i * 8), 15) + 1
- str = str..
- hex_char:sub(ind1, ind1)..
- hex_char:sub(ind2, ind2)
- end
- return str
- end
- local function string_to_blks(str)
- local nblk = bit.brshift((str:len() + 8), 6) + 1
- local blks = {}
- local len = str:len()
- for i = 0, nblk * 16 - 1 do
- blks[i] = 0
- end
- for i = 0, str:len() - 1 do
- blks[bit.brshift(i, 2)] =
- bit.bor(
- blks[bit.brshift(i, 2)],
- bit.blshift(
- str:byte(i+1),
- (((i) % 4) * 8)
- )
- )
- end
- blks[bit.brshift(len, 2)] =
- bit.bor(
- blks[bit.brshift(len, 2)],
- bit.blshift(
- 128,
- (((len) % 4) * 8)
- )
- )
- blks[nblk * 16 - 2] = len * 8
- return blks
- end
- local function add(x, y)
- return x + y > 4294967296 and x + y or x + y - 4294967296
- end
- local function rol(number, count)
- return bit.bor(bit.blshift(number, count), bit.blogic_rshift(number, (32 - count)))
- end
- local function X(a, b, c, x, s, t)
- return add(rol(add(add(b, a), add(x, t)), s), c)
- end
- local function F(a, b, c, d, x, s, t)
- return X(bit.bor(bit.band(b, c), bit.band(bit.bnot(b), d)), a, b, x, s, t)
- end
- local function G(a, b, c, d, x, s, t)
- return X(bit.bor(bit.band(b, d), bit.band(c, bit.bnot(d))), a, b, x, s, t)
- end
- local function H(a, b, c, d, x, s, t)
- return X(bit.bxor(bit.bxor(b, c), d), a, b, x, s, t)
- end
- local function I(a, b, c, d, x, s, t)
- return X(bit.bxor(c, bit.bor(b, bit.bnot(d))), a, b, x, s, t)
- end
- function md5(encoding_string)
- local blks = string_to_blks(encoding_string)
- local a = 1732584193
- local b = -271733879
- local c = -1732584194
- local d = 271733878
- for i = 0, #blks-1, 16 do
- local olda, oldb, oldc, oldd = a, b, c, d
- a = F(a, b, c, d, blks[i+ 0], 7, -680876936)
- d = F(d, a, b, c, blks[i+ 1], 12, -389564586)
- c = F(c, d, a, b, blks[i+ 2], 17, 606105819)
- b = F(b, c, d, a, blks[i+ 3], 22, -1044525330)
- a = F(a, b, c, d, blks[i+ 4], 7, -176418897)
- d = F(d, a, b, c, blks[i+ 5], 12, 1200080426)
- c = F(c, d, a, b, blks[i+ 6], 17, -1473231341)
- b = F(b, c, d, a, blks[i+ 7], 22, -45705983)
- a = F(a, b, c, d, blks[i+ 8], 7, 1770035416)
- d = F(d, a, b, c, blks[i+ 9], 12, -1958414417)
- c = F(c, d, a, b, blks[i+10], 17, -42063)
- b = F(b, c, d, a, blks[i+11], 22, -1990404162)
- a = F(a, b, c, d, blks[i+12], 7, 1804603682)
- d = F(d, a, b, c, blks[i+13], 12, -40341101)
- c = F(c, d, a, b, blks[i+14], 17, -1502002290)
- b = F(b, c, d, a, blks[i+15], 22, 1236535329)
- a = G(a, b, c, d, blks[i+ 1], 5, -165796510)
- d = G(d, a, b, c, blks[i+ 6], 9, -1069501632)
- c = G(c, d, a, b, blks[i+11], 14, 643717713)
- b = G(b, c, d, a, blks[i+ 0], 20, -373897302)
- a = G(a, b, c, d, blks[i+ 5], 5, -701558691)
- d = G(d, a, b, c, blks[i+10], 9, 38016083)
- c = G(c, d, a, b, blks[i+15], 14, -660478335)
- b = G(b, c, d, a, blks[i+ 4], 20, -405537848)
- a = G(a, b, c, d, blks[i+ 9], 5, 568446438)
- d = G(d, a, b, c, blks[i+14], 9, -1019803690)
- c = G(c, d, a, b, blks[i+ 3], 14, -187363961)
- b = G(b, c, d, a, blks[i+ 8], 20, 1163531501)
- a = G(a, b, c, d, blks[i+13], 5, -1444681467)
- d = G(d, a, b, c, blks[i+ 2], 9, -51403784)
- c = G(c, d, a, b, blks[i+ 7], 14, 1735328473)
- b = G(b, c, d, a, blks[i+12], 20, -1926607734)
- a = H(a, b, c, d, blks[i+ 5], 4, -378558)
- d = H(d, a, b, c, blks[i+ 8], 11, -2022574463)
- c = H(c, d, a, b, blks[i+11], 16, 1839030562)
- b = H(b, c, d, a, blks[i+14], 23, -35309556)
- a = H(a, b, c, d, blks[i+ 1], 4, -1530992060)
- d = H(d, a, b, c, blks[i+ 4], 11, 1272893353)
- c = H(c, d, a, b, blks[i+ 7], 16, -155497632)
- b = H(b, c, d, a, blks[i+10], 23, -1094730640)
- a = H(a, b, c, d, blks[i+13], 4, 681279174)
- d = H(d, a, b, c, blks[i+ 0], 11, -358537222)
- c = H(c, d, a, b, blks[i+ 3], 16, -722521979)
- b = H(b, c, d, a, blks[i+ 6], 23, 76029189)
- a = H(a, b, c, d, blks[i+ 9], 4, -640364487)
- d = H(d, a, b, c, blks[i+12], 11, -421815835)
- c = H(c, d, a, b, blks[i+15], 16, 530742520)
- b = H(b, c, d, a, blks[i+ 2], 23, -995338651)
- a = I(a, b, c, d, blks[i+ 0], 6, -198630844)
- d = I(d, a, b, c, blks[i+ 7], 10, 1126891415)
- c = I(c, d, a, b, blks[i+14], 15, -1416354905)
- b = I(b, c, d, a, blks[i+ 5], 21, -57434055)
- a = I(a, b, c, d, blks[i+12], 6, 1700485571)
- d = I(d, a, b, c, blks[i+ 3], 10, -1894986606)
- c = I(c, d, a, b, blks[i+10], 15, -1051523)
- b = I(b, c, d, a, blks[i+ 1], 21, -2054922799)
- a = I(a, b, c, d, blks[i+ 8], 6, 1873313359)
- d = I(d, a, b, c, blks[i+15], 10, -30611744)
- c = I(c, d, a, b, blks[i+ 6], 15, -1560198380)
- b = I(b, c, d, a, blks[i+13], 21, 1309151649)
- a = I(a, b, c, d, blks[i+ 4], 6, -145523070)
- d = I(d, a, b, c, blks[i+11], 10, -1120210379)
- c = I(c, d, a, b, blks[i+ 2], 15, 718787259)
- b = I(b, c, d, a, blks[i+ 9], 21, -343485551)
- a = add(a, olda)
- b = add(b, oldb)
- c = add(c, oldc)
- d = add(d, oldd)
- end
- return getHex(a)..getHex(b)..getHex(c)..getHex(d)
- end
- --[[
- *End of MD5 encoding
- ]]
- function open()
- local bOpen, sFreeSide = false, nil
- for n,sSide in pairs(rs.getSides()) do
- if peripheral.getType( sSide ) == "modem" then
- sFreeSide = sSide
- if rednet.isOpen( sSide ) then
- bOpen = true
- break
- end
- end
- end
- if not bOpen then
- if sFreeSide then
- print( "No modem active. Opening "..sFreeSide.." modem" )
- rednet.open( sFreeSide )
- return true
- else
- print( "No modem attached" )
- return false
- end
- end
- return true
- end
- ActiveUser={}
- AllUsers={}
- eventKey={}
- eventRednet={}
- pwdfilename=shell.resolve('passwd')
- messdir=shell.dir()..'/messages/'
- readdir=shell.dir()..'/readed/'
- newsdir=shell.dir()..'/news/'
- eventRednet.ask=function (dat,id)
- if ActiveUser[id] then
- return 'You are logined as '..ActiveUser[id].name
- else
- return 'You are not logined'
- end
- end
- eventRednet.adduser=function (dat,id)
- local user,pwd=dat:match('user=([%w_]+)%s+pwd=(%S+)')
- if not user then return 'Incorrect login or password' end
- if AllUsers[user] then return 'User '..user..' already exists' end
- file=fs.open(pwdfilename,'a')
- file.write(user..' '..md5(pwd)..'\r\n')
- file:close()
- AllUsers[user]=md5(pwd)
- ActiveUser[id]={}
- ActiveUser[id].name=user
- ActiveUser[id].mess={}
- print ('Create user '..user..' id '..id)
- return 'User '..user..' created'
- end
- eventRednet.login=function (dat,id)
- local user,pwd=dat:match('user=([%w_]+)%s+pwd=(%S+)')
- if not user then
- return 'Incorrect login or password'
- end
- if AllUsers[user]==md5(pwd) then
- ActiveUser[id]={}
- ActiveUser[id].name=user
- ActiveUser[id].mess={}
- print ('Login user '..user..' id '..id)
- local file=fs.open(readdir..user,'r')
- if file then
- local line=file.readLine()
- while line do
- table.insert( ActiveUser[id].mess, line )
- line=file.readLine()
- end
- file:close()
- end
- return 'User '..user..' connected'
- else
- print ('Unsuccessful login user '..user..' id '..id)
- return 'Incorrect login or password'
- end
- end
- eventRednet.logout=function (dat,id)
- if ActiveUser[id] then
- local name=ActiveUser[id].name
- ActiveUser[id]=nil
- print ('User '..name..' disconnected id '..id)
- return 'User '..name..' disconnected'
- end
- end
- eventRednet.checkmess=function (dat,id)
- if ActiveUser[id] then
- local file=fs.open(messdir..ActiveUser[id].name,'r')
- if file then
- local n=0
- local line=file.readLine()
- while line do
- n=n+1
- table.insert( ActiveUser[id].mess, line )
- line=file.readLine()
- end
- file:close()
- if n>0 then
- file=fs.open(readdir..ActiveUser[id].name,'w')
- if file then
- for i,mess in ipairs(ActiveUser[id].mess) do
- file.write(ActiveUser[id].mess[i]..'\r\n')
- end
- file:close()
- fs.delete(messdir..ActiveUser[id].name)
- end
- return 'You have '..n..' new message(s)'
- end
- end
- return 'You have not new messages'
- end
- return 'You need login'
- end
- eventRednet.readmess=function (dat,id)
- if ActiveUser[id] then
- if ActiveUser[id].mess and #ActiveUser[id].mess>ActiveUser[id].nummes then
- ActiveUser[id].nummes=ActiveUser[id].nummes+1
- return ActiveUser[id].mess[ActiveUser[id].nummes]
- else
- return 'You have not more messages.'
- end
- end
- return 'You need login'
- end
- eventRednet.readfirst=function (dat,id)
- if ActiveUser[id] then
- ActiveUser[id].nummes=0
- return eventRednet.readmess(dat,id)
- end
- return 'You need login'
- end
- eventRednet.delmess=function (dat,id)
- if ActiveUser[id] then
- if ActiveUser[id].mess and #ActiveUser[id].mess>ActiveUser[id].nummes then
- table.remove (ActiveUser[id].mess , ActiveUser[id].nummes)
- ActiveUser[id].nummes=ActiveUser[id].nummes-1
- file=fs.open(readdir..ActiveUser[id].name,'w')
- if file then
- for i,mess in ipairs(ActiveUser[id].mess) do
- file.write(ActiveUser[id].mess[i]..'\r\n')
- end
- file:close()
- end
- end
- return eventRednet.readmess(dat,id)
- end
- return 'You need login'
- end
- eventRednet.sendmess=function (dat,id)
- if ActiveUser[id] then
- local user,mess=dat:match('user=(%S+)(.*)')
- if not AllUsers[user] then return 'Incorrect user name' end
- file=fs.open(messdir..user,'a')
- if not file then
- return 'Cannt open message file'
- end
- file.write('<'..ActiveUser[id].name..'> '..mess..'\r\n')
- file:close()
- local k=next(ActiveUser)
- while k do
- if user==ActiveUser[k].name then
- rednet.send(k,'You have new message')
- end
- k=next(ActiveUser,k)
- end
- return 'Message is sent successful'
- end
- return 'You need login'
- end
- eventRednet.users=function(dat,id)
- local users=''
- for name in pairs(AllUsers) do
- users=users..name..' '
- end
- return users
- end
- eventRednet.news=function(dat,id)
- if ActiveUser[id] then
- if not ActiveUser[id].news or #ActiveUser[id].news==0 then
- ActiveUser[id].news={}
- for n,file in pairs(fs.list(newsdir)) do
- if not fs.isDir(newsdir..file) then
- table.insert( ActiveUser[id].news, file )
- end
- end
- end
- -- print('News ',#ActiveUser[id].news)
- if #ActiveUser[id].news==0 then
- return 'No more news'
- else
- local filename = table.remove (ActiveUser[id].news , 1)
- local text=''
- local file=fs.open(newsdir..filename,'r')
- if file then
- local line=file.readLine()
- while line do
- text=text..line..'\n'
- line=file.readLine()
- end
- file:close()
- end
- return text
- end
- end
- return 'You need login'
- end
- work=true
- eventKey[keys.a]=function ()
- local k=next(ActiveUser)
- if not k then
- print('No active users')
- return
- end
- print('Active users:')
- while k do
- print(k,' ',ActiveUser[k].name)
- k=next(ActiveUser,k)
- end
- end
- eventKey[keys.q]= function ()
- work=false
- end
- eventKey[keys.u]= function ()
- print('All users:')
- print(eventRednet.users())
- end
- if not fs.exists(pwdfilename) then
- local file=fs.open(pwdfilename,'w')
- file:close()
- end
- if not fs.exists(messdir) then fs.makeDir(messdir) end
- if not fs.exists(readdir) then fs.makeDir(readdir) end
- if not fs.exists(newsdir) then fs.makeDir(newsdir) end
- local file=fs.open(pwdfilename,'r')
- local line=file.readLine()
- while line do
- local name,md5pwd=line:match('(%S+)%s+(%S+)')
- if name then
- AllUsers[name]=md5pwd
- end
- line=file.readLine()
- end
- file.close()
- if open() then
- while work do
- local event,p1,p2=os.pullEvent()
- if event=='rednet_message' then
- local func,dat=p2:match('(%S+)(.*)')
- if eventRednet[func]~=nil then
- -- print(func,' ',dat)
- p2=eventRednet[func](dat,p1)
- else
- p2='Incorrect function "'..func..'"'
- end
- rednet.send(p1,p2)
- elseif event=='key' then
- if eventKey[p1]~=nil then
- eventKey[p1]()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement