init
This commit is contained in:
		
						commit
						d4b070754a
					
				
					 2 changed files with 137 additions and 0 deletions
				
			
		
							
								
								
									
										100
									
								
								main.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								main.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,100 @@ | |||
| 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 | ||||
							
								
								
									
										37
									
								
								readme.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								readme.md
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,37 @@ | |||
| ``` | ||||
|               __              | ||||
|   ___________/__\___________  | ||||
|  /   _  _  -__ -   ___  -   \ | ||||
|  \__________________________/ | ||||
|              |  |             | ||||
|              |  |             | ||||
|         _____|  |______       | ||||
|     ___|     |  |      |___   | ||||
|    |   |     |  |      |   |  | ||||
| ___|___|____/____\_____|___|___ | ||||
| 
 | ||||
|      Welcome to Er2Chat! | ||||
|  You need to login to continue | ||||
| Do you need input echo? [YNQ] | ||||
| ``` | ||||
| 
 | ||||
| --- | ||||
| 
 | ||||
| This is yet another telnet chat. | ||||
| 
 | ||||
| This server supports both clients with \r, \n and \r\n newline. | ||||
| (This means it supports `nc/telnet` and Android `ConnectBot` client) | ||||
| 
 | ||||
| Server currently is very simple and stupid, but can be improved. | ||||
| 
 | ||||
| # Running | ||||
| 
 | ||||
| To run this server, you need: | ||||
| 
 | ||||
| - **Lua** (from 5.1, supports luajit) | ||||
| 
 | ||||
| - **LuaSocket** (or **LuaSec**). | ||||
|   `# luarocks install luasocket` | ||||
| 
 | ||||
| To start now, just type: | ||||
| `$ lua main.lua` | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue