mirror of
https://github.com/minetest-mods/irc.git
synced 2025-06-28 14:16:09 +02:00
initial commit
This commit is contained in:
50
etc/luasocket-samples/README
Normal file
50
etc/luasocket-samples/README
Normal file
@ -0,0 +1,50 @@
|
||||
This directory contains some sample programs using
|
||||
LuaSocket. This code is not supported.
|
||||
|
||||
listener.lua -- socket to stdout
|
||||
talker.lua -- stdin to socket
|
||||
|
||||
listener.lua and talker.lua are about the simplest
|
||||
applications you can write using LuaSocket. Run
|
||||
|
||||
'lua listener.lua' and 'lua talker.lua'
|
||||
|
||||
on different terminals. Whatever you type on talk.lua will
|
||||
be printed by listen.lua.
|
||||
|
||||
lpr.lua -- lpr client
|
||||
|
||||
This is a cool program written by David Burgess to print
|
||||
files using the Line Printer Daemon protocol, widely used in
|
||||
Unix machines. It uses the lp.lua implementation, in the
|
||||
etc directory. Just run 'lua lpr.lua <filename>
|
||||
queue=<printername>' and the file will print!
|
||||
|
||||
cddb.lua -- CDDB client
|
||||
|
||||
This is the first try on a simple CDDB client. Not really
|
||||
useful, but one day it might become a module.
|
||||
|
||||
daytimeclnt.lua -- day time client
|
||||
|
||||
Just run the program to retrieve the hour and date in
|
||||
readable form from any server running an UDP daytime daemon.
|
||||
|
||||
echoclnt.lua -- UDP echo client
|
||||
echosrvr.lua -- UDP echo server
|
||||
|
||||
These are a UDP echo client/server pair. They work with
|
||||
other client and servers as well.
|
||||
|
||||
tinyirc.lua -- irc like broadcast server
|
||||
|
||||
This is a simple server that waits simultaneously on two
|
||||
server sockets for telnet connections. Everything it
|
||||
receives from the telnet clients is broadcasted to every
|
||||
other connected client. It tests the select function and
|
||||
shows how to create a simple server whith LuaSocket. Just
|
||||
run tinyirc.lua and then open as many telnet connections
|
||||
as you want to ports 8080 and 8081.
|
||||
|
||||
Good luck,
|
||||
Diego.
|
46
etc/luasocket-samples/cddb.lua
Normal file
46
etc/luasocket-samples/cddb.lua
Normal file
@ -0,0 +1,46 @@
|
||||
local socket = require("socket")
|
||||
local http = require("socket.http")
|
||||
|
||||
if not arg or not arg[1] or not arg[2] then
|
||||
print("luasocket cddb.lua <category> <disc-id> [<server>]")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local server = arg[3] or "http://freedb.freedb.org/~cddb/cddb.cgi"
|
||||
|
||||
function parse(body)
|
||||
local lines = string.gfind(body, "(.-)\r\n")
|
||||
local status = lines()
|
||||
local code, message = socket.skip(2, string.find(status, "(%d%d%d) (.*)"))
|
||||
if tonumber(code) ~= 210 then
|
||||
return nil, code, message
|
||||
end
|
||||
local data = {}
|
||||
for l in lines do
|
||||
local c = string.sub(l, 1, 1)
|
||||
if c ~= '#' and c ~= '.' then
|
||||
local key, value = socket.skip(2, string.find(l, "(.-)=(.*)"))
|
||||
value = string.gsub(value, "\\n", "\n")
|
||||
value = string.gsub(value, "\\\\", "\\")
|
||||
value = string.gsub(value, "\\t", "\t")
|
||||
data[key] = value
|
||||
end
|
||||
end
|
||||
return data, code, message
|
||||
end
|
||||
|
||||
local host = socket.dns.gethostname()
|
||||
local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6"
|
||||
local url = string.format(query, server, arg[1], arg[2], host)
|
||||
local body, headers, code = http.get(url)
|
||||
|
||||
if code == 200 then
|
||||
local data, code, error = parse(body)
|
||||
if not data then
|
||||
print(error or code)
|
||||
else
|
||||
for i,v in pairs(data) do
|
||||
io.write(i, ': ', v, '\n')
|
||||
end
|
||||
end
|
||||
else print(error) end
|
23
etc/luasocket-samples/daytimeclnt.lua
Normal file
23
etc/luasocket-samples/daytimeclnt.lua
Normal file
@ -0,0 +1,23 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- UDP sample: daytime protocol client
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: daytimeclnt.lua,v 1.11 2004/06/21 06:07:57 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require"socket"
|
||||
host = host or "127.0.0.1"
|
||||
port = port or 13
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
host = socket.dns.toip(host)
|
||||
udp = socket.udp()
|
||||
print("Using host '" ..host.. "' and port " ..port.. "...")
|
||||
udp:setpeername(host, port)
|
||||
udp:settimeout(3)
|
||||
sent, err = udp:send("anything")
|
||||
if err then print(err) os.exit() end
|
||||
dgram, err = udp:receive()
|
||||
if not dgram then print(err) os.exit() end
|
||||
io.write(dgram)
|
24
etc/luasocket-samples/echoclnt.lua
Normal file
24
etc/luasocket-samples/echoclnt.lua
Normal file
@ -0,0 +1,24 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- UDP sample: echo protocol client
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: echoclnt.lua,v 1.10 2005/01/02 22:44:00 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "localhost"
|
||||
port = port or 7
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
host = socket.dns.toip(host)
|
||||
udp = assert(socket.udp())
|
||||
assert(udp:setpeername(host, port))
|
||||
print("Using remote host '" ..host.. "' and port " .. port .. "...")
|
||||
while 1 do
|
||||
line = io.read()
|
||||
if not line or line == "" then os.exit() end
|
||||
assert(udp:send(line))
|
||||
dgram = assert(udp:receive())
|
||||
print(dgram)
|
||||
end
|
29
etc/luasocket-samples/echosrvr.lua
Normal file
29
etc/luasocket-samples/echosrvr.lua
Normal file
@ -0,0 +1,29 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- UDP sample: echo protocol server
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: echosrvr.lua,v 1.12 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "127.0.0.1"
|
||||
port = port or 7
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
print("Binding to host '" ..host.. "' and port " ..port.. "...")
|
||||
udp = assert(socket.udp())
|
||||
assert(udp:setsockname(host, port))
|
||||
assert(udp:settimeout(5))
|
||||
ip, port = udp:getsockname()
|
||||
assert(ip, port)
|
||||
print("Waiting packets on " .. ip .. ":" .. port .. "...")
|
||||
while 1 do
|
||||
dgram, ip, port = udp:receivefrom()
|
||||
if dgram then
|
||||
print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
|
||||
udp:sendto(dgram, ip, port)
|
||||
else
|
||||
print(ip)
|
||||
end
|
||||
end
|
26
etc/luasocket-samples/listener.lua
Normal file
26
etc/luasocket-samples/listener.lua
Normal file
@ -0,0 +1,26 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- TCP sample: Little program to dump lines received at a given port
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: listener.lua,v 1.11 2005/01/02 22:44:00 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "*"
|
||||
port = port or 8080
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
print("Binding to host '" ..host.. "' and port " ..port.. "...")
|
||||
s = assert(socket.bind(host, port))
|
||||
i, p = s:getsockname()
|
||||
assert(i, p)
|
||||
print("Waiting connection from talker on " .. i .. ":" .. p .. "...")
|
||||
c = assert(s:accept())
|
||||
print("Connected. Here is the stuff:")
|
||||
l, e = c:receive()
|
||||
while not e do
|
||||
print(l)
|
||||
l, e = c:receive()
|
||||
end
|
||||
print(e)
|
51
etc/luasocket-samples/lpr.lua
Normal file
51
etc/luasocket-samples/lpr.lua
Normal file
@ -0,0 +1,51 @@
|
||||
local lp = require("socket.lp")
|
||||
|
||||
local function usage()
|
||||
print('\nUsage: lua lpr.lua [filename] [keyword=val...]\n')
|
||||
print('Valid keywords are :')
|
||||
print(
|
||||
' host=remote host or IP address (default "localhost")\n' ..
|
||||
' queue=remote queue or printer name (default "printer")\n' ..
|
||||
' port=remote port number (default 515)\n' ..
|
||||
' user=sending user name\n' ..
|
||||
' format=["binary" | "text" | "ps" | "pr" | "fortran"] (default "binary")\n' ..
|
||||
' banner=true|false\n' ..
|
||||
' indent=number of columns to indent\n' ..
|
||||
' mail=email of address to notify when print is complete\n' ..
|
||||
' title=title to use for "pr" format\n' ..
|
||||
' width=width for "text" or "pr" formats\n' ..
|
||||
' class=\n' ..
|
||||
' job=\n' ..
|
||||
' name=\n' ..
|
||||
' localbind=true|false\n'
|
||||
)
|
||||
return nil
|
||||
end
|
||||
|
||||
if not arg or not arg[1] then
|
||||
return usage()
|
||||
end
|
||||
|
||||
do
|
||||
local opt = {}
|
||||
local pat = "[%s%c%p]*([%w]*)=([\"]?[%w%s_!@#$%%^&*()<>:;]+[\"]\?\.?)"
|
||||
for i = 2, table.getn(arg), 1 do
|
||||
string.gsub(arg[i], pat, function(name, value) opt[name] = value end)
|
||||
end
|
||||
if not arg[2] then
|
||||
return usage()
|
||||
end
|
||||
if arg[1] ~= "query" then
|
||||
opt.file = arg[1]
|
||||
r,e=lp.send(opt)
|
||||
io.stdout:write(tostring(r or e),'\n')
|
||||
else
|
||||
r,e=lp.query(opt)
|
||||
io.stdout:write(tostring(r or e), '\n')
|
||||
end
|
||||
end
|
||||
|
||||
-- trivial tests
|
||||
--lua lp.lua lp.lua queue=default host=localhost
|
||||
--lua lp.lua lp.lua queue=default host=localhost format=binary localbind=1
|
||||
--lua lp.lua query queue=default host=localhost
|
21
etc/luasocket-samples/talker.lua
Normal file
21
etc/luasocket-samples/talker.lua
Normal file
@ -0,0 +1,21 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- TCP sample: Little program to send text lines to a given host/port
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: talker.lua,v 1.9 2005/01/02 22:44:00 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "localhost"
|
||||
port = port or 8080
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
print("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
|
||||
c = assert(socket.connect(host, port))
|
||||
print("Connected! Please type stuff (empty line to stop):")
|
||||
l = io.read()
|
||||
while l and l ~= "" and not e do
|
||||
assert(c:send(l .. "\n"))
|
||||
l = io.read()
|
||||
end
|
90
etc/luasocket-samples/tinyirc.lua
Normal file
90
etc/luasocket-samples/tinyirc.lua
Normal file
@ -0,0 +1,90 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Select sample: simple text line server
|
||||
-- LuaSocket sample files.
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: tinyirc.lua,v 1.14 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "*"
|
||||
port1 = port1 or 8080
|
||||
port2 = port2 or 8181
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port1 = arg[2] or port1
|
||||
port2 = arg[3] or port2
|
||||
end
|
||||
|
||||
server1 = assert(socket.bind(host, port1))
|
||||
server2 = assert(socket.bind(host, port2))
|
||||
server1:settimeout(1) -- make sure we don't block in accept
|
||||
server2:settimeout(1)
|
||||
|
||||
io.write("Servers bound\n")
|
||||
|
||||
-- simple set implementation
|
||||
-- the select function doesn't care about what is passed to it as long as
|
||||
-- it behaves like a table
|
||||
-- creates a new set data structure
|
||||
function newset()
|
||||
local reverse = {}
|
||||
local set = {}
|
||||
return setmetatable(set, {__index = {
|
||||
insert = function(set, value)
|
||||
if not reverse[value] then
|
||||
table.insert(set, value)
|
||||
reverse[value] = table.getn(set)
|
||||
end
|
||||
end,
|
||||
remove = function(set, value)
|
||||
local index = reverse[value]
|
||||
if index then
|
||||
reverse[value] = nil
|
||||
local top = table.remove(set)
|
||||
if top ~= value then
|
||||
reverse[top] = index
|
||||
set[index] = top
|
||||
end
|
||||
end
|
||||
end
|
||||
}})
|
||||
end
|
||||
|
||||
set = newset()
|
||||
|
||||
io.write("Inserting servers in set\n")
|
||||
set:insert(server1)
|
||||
set:insert(server2)
|
||||
|
||||
while 1 do
|
||||
local readable, _, error = socket.select(set, nil)
|
||||
for _, input in ipairs(readable) do
|
||||
-- is it a server socket?
|
||||
if input == server1 or input == server2 then
|
||||
io.write("Waiting for clients\n")
|
||||
local new = input:accept()
|
||||
if new then
|
||||
new:settimeout(1)
|
||||
io.write("Inserting client in set\n")
|
||||
set:insert(new)
|
||||
end
|
||||
-- it is a client socket
|
||||
else
|
||||
local line, error = input:receive()
|
||||
if error then
|
||||
input:close()
|
||||
io.write("Removing client from set\n")
|
||||
set:remove(input)
|
||||
else
|
||||
io.write("Broadcasting line '", line, "'\n")
|
||||
writable, error = socket.skip(1, socket.select(nil, set, 1))
|
||||
if not error then
|
||||
for __, output in ipairs(writable) do
|
||||
if output ~= input then
|
||||
output:send(line .. "\n")
|
||||
end
|
||||
end
|
||||
else io.write("No client ready to receive!!!\n") end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user