100 lines
2.8 KiB
Lua
100 lines
2.8 KiB
Lua
local socket = require 'socket'
|
|
local tcp = socket.bind('*', 8080)
|
|
tcp:settimeout(0)
|
|
|
|
motd = {
|
|
' __ ' ,
|
|
' ___________/__\\___________ ',
|
|
' / _ _ -__ - ___ - \\',
|
|
' \\__________________________/',
|
|
' | | ' ,
|
|
' | | ' ,
|
|
' _____| |______ ' ,
|
|
' ___| | | |___ ' ,
|
|
' | | | | | | ' ,
|
|
'___|___|____/____\\_____|___|___',
|
|
'',
|
|
' Welcome to Er2Chat!',
|
|
' You need to login to continue',
|
|
}
|
|
|
|
local cls = {}
|
|
local tmp = {}
|
|
|
|
local function get(v)
|
|
v.buf = v.buf or ''
|
|
local l, err
|
|
while not err do
|
|
l, err = v.cli:receive(1)
|
|
if l == '\r' or l == '\n' then
|
|
if v.le then v.cli:send '\r\n' end
|
|
l, v.buf = v.buf, nil
|
|
break
|
|
elseif l == '\127'
|
|
then v.cli:send '\008'
|
|
v.buf = v.buf:sub(1, -2)
|
|
elseif l then
|
|
v.buf = v.buf.. l
|
|
if v.le then v.cli:send(l) end
|
|
end
|
|
end
|
|
return l, err
|
|
end
|
|
|
|
while true do
|
|
local cli = tcp:accept()
|
|
if cli then
|
|
cli:settimeout(0)
|
|
if (cli:receive() or ''):match 'GET /.* HTTP.*'
|
|
then cli:send('HTTP/1.1 200 OK\r\n\r\nPlease use telnet to use this service.')
|
|
cli:close()
|
|
else cli:send(table.concat(motd, '\r\n')..'\r\n')
|
|
table.insert(tmp, {
|
|
cli = cli,
|
|
st = 0,
|
|
})
|
|
end
|
|
end
|
|
for k, v in pairs(tmp) do
|
|
if v.reads then -- nothing
|
|
elseif v.st == 0 then v.cli:send('Do you need input echo? [YNQ] ')
|
|
elseif v.st == 1 then v.cli:send('Username: ')
|
|
elseif v.st == 2 then v.cli:send('Password: ')
|
|
elseif v.st == 3 then v.cli:send('Welcome, '.. v.user ..'!\r\n')
|
|
for _, w in pairs(cls) do
|
|
w.cli:send('\r\n-- '.. v.user ..' joined --\r\n')
|
|
end
|
|
v.st, v.reads = nil, nil
|
|
table.remove(tmp, k)
|
|
table.insert(cls, v)
|
|
end
|
|
if v.st then v.reads = true
|
|
local l, err = get(v)
|
|
if err == 'timeout' then -- nothing
|
|
elseif err then table.remove(tmp, k)
|
|
elseif (l or ''):match '^%s*$' then v.reads = false
|
|
elseif v.st == 0 then v.st, v.reads = 1, false
|
|
if l:match 'y'
|
|
then v.le = true; v.cli:send(l.. '\r\n')
|
|
elseif l:match 'q'
|
|
then v.cli:close(); table.remove(tmp, k)
|
|
end
|
|
elseif v.st == 1 then v.st, v.user, v.reads = 3, l:match '^%S+', false
|
|
elseif v.st == 2 then v.st, v.reads = 1, false
|
|
end
|
|
end
|
|
end
|
|
for k, v in pairs(cls) do
|
|
local l, err = get(v)
|
|
if l == '/leave' then v.cli:close(); err = 'closed'
|
|
end
|
|
if err and err ~= 'timeout' then table.remove(cls, k) end
|
|
for j, w in pairs(cls) do
|
|
if err and err ~= 'timeout'
|
|
then w.cli:send('\r\n-- '.. v.user ..' leaved --\r\n')
|
|
elseif (l or ''):match '^%s*$' then -- nothing
|
|
elseif k ~= j then w.cli:send('\r\n'.. v.user ..': '.. l ..'\r\n')
|
|
end
|
|
end
|
|
end
|
|
end
|