luasocket/etc/lp.lua

325 lines
11 KiB
Lua
Raw Permalink Normal View History

2004-07-01 07:35:35 +02:00
-----------------------------------------------------------------------------
-- LPD support for the Lua language
-- LuaSocket toolkit.
-- Author: David Burgess
-- Modified by Diego Nehab, but David is in charge
-- RCS ID: $Id$
-----------------------------------------------------------------------------
2004-07-01 05:39:56 +02:00
--[[
2004-07-01 07:35:35 +02:00
if you have any questions: RFC 1179
2004-07-01 05:39:56 +02:00
]]
2004-06-18 23:41:44 +02:00
-- make sure LuaSocket is loaded
2004-11-27 08:58:04 +01:00
local io = require("io")
2005-06-14 06:29:23 +02:00
local base = _G
local os = require("os")
local math = require("math")
2004-11-27 08:58:04 +01:00
local string = require("string")
2004-06-18 23:41:44 +02:00
local socket = require("socket")
local ltn12 = require("ltn12")
2004-12-23 23:32:12 +01:00
module("socket.lp")
2004-06-18 23:41:44 +02:00
-- default port
PORT = 515
SERVER = os.getenv("SERVER_NAME") or os.getenv("COMPUTERNAME") or "localhost"
PRINTER = os.getenv("PRINTER") or "printer"
2004-07-01 05:39:56 +02:00
local function connect(localhost, option)
local host = option.host or SERVER
local port = option.port or PORT
local skt
local try = socket.newtry(function() if skt then skt:close() end end)
if option.localbind then
-- bind to a local port (if we can)
local localport = 721
2004-07-03 01:47:52 +02:00
local done, err
2004-07-01 05:39:56 +02:00
repeat
2004-11-27 08:58:04 +01:00
skt = socket.try(socket.tcp())
2004-07-01 05:39:56 +02:00
try(skt:settimeout(30))
2004-07-03 01:47:52 +02:00
done, err = skt:bind(localhost, localport)
2004-07-01 05:39:56 +02:00
if not done then
localport = localport + 1
skt:close()
skt = nil
else break end
until localport > 731
2004-11-27 08:58:04 +01:00
socket.try(skt, err)
else skt = socket.try(socket.tcp()) end
2004-07-01 05:39:56 +02:00
try(skt:connect(host, port))
return { skt = skt, try = try }
end
2004-06-18 23:41:44 +02:00
--[[
RFC 1179
5.3 03 - Send queue state (short)
+----+-------+----+------+----+
| 03 | Queue | SP | List | LF |
+----+-------+----+------+----+
Command code - 3
Operand 1 - Printer queue name
Other operands - User names or job numbers
If the user names or job numbers or both are supplied then only those
jobs for those users or with those numbers will be sent.
The response is an ASCII stream which describes the printer queue.
The stream continues until the connection closes. Ends of lines are
indicated with ASCII LF control characters. The lines may also
contain ASCII HT control characters.
5.4 04 - Send queue state (long)
+----+-------+----+------+----+
| 04 | Queue | SP | List | LF |
+----+-------+----+------+----+
Command code - 4
Operand 1 - Printer queue name
Other operands - User names or job numbers
If the user names or job numbers or both are supplied then only those
jobs for those users or with those numbers will be sent.
The response is an ASCII stream which describes the printer queue.
The stream continues until the connection closes. Ends of lines are
indicated with ASCII LF control characters. The lines may also
contain ASCII HT control characters.
]]
-- gets server acknowledement
2004-07-01 05:39:56 +02:00
local function recv_ack(con)
local ack = con.skt:receive(1)
2004-07-01 05:47:22 +02:00
con.try(string.char(0) == ack, "failed to receive server acknowledgement")
2004-06-18 23:41:44 +02:00
end
-- sends client acknowledement
2004-07-01 05:39:56 +02:00
local function send_ack(con)
local sent = con.skt:send(string.char(0))
con.try(sent == 1, "failed to send acknowledgement")
2004-06-18 23:41:44 +02:00
end
-- sends queue request
-- 5.2 02 - Receive a printer job
--
-- +----+-------+----+
-- | 02 | Queue | LF |
-- +----+-------+----+
-- Command code - 2
-- Operand - Printer queue name
--
-- Receiving a job is controlled by a second level of commands. The
-- daemon is given commands by sending them over the same connection.
-- The commands are described in the next section (6).
--
-- After this command is sent, the client must read an acknowledgement
-- octet from the daemon. A positive acknowledgement is an octet of
-- zero bits. A negative acknowledgement is an octet of any other
-- pattern.
2004-07-01 05:39:56 +02:00
local function send_queue(con, queue)
queue = queue or PRINTER
local str = string.format("\2%s\10", queue)
local sent = con.skt:send(str)
con.try(sent == string.len(str), "failed to send print request")
recv_ack(con)
2004-06-18 23:41:44 +02:00
end
-- sends control file
-- 6.2 02 - Receive control file
--
-- +----+-------+----+------+----+
-- | 02 | Count | SP | Name | LF |
-- +----+-------+----+------+----+
-- Command code - 2
-- Operand 1 - Number of bytes in control file
-- Operand 2 - Name of control file
--
-- The control file must be an ASCII stream with the ends of lines
-- indicated by ASCII LF. The total number of bytes in the stream is
-- sent as the first operand. The name of the control file is sent as
-- the second. It should start with ASCII "cfA", followed by a three
-- digit job number, followed by the host name which has constructed the
-- control file. Acknowledgement processing must occur as usual after
-- the command is sent.
--
-- The next "Operand 1" octets over the same TCP connection are the
-- intended contents of the control file. Once all of the contents have
-- been delivered, an octet of zero bits is sent as an indication that
-- the file being sent is complete. A second level of acknowledgement
-- processing must occur at this point.
-- sends data file
-- 6.3 03 - Receive data file
--
-- +----+-------+----+------+----+
-- | 03 | Count | SP | Name | LF |
-- +----+-------+----+------+----+
-- Command code - 3
-- Operand 1 - Number of bytes in data file
-- Operand 2 - Name of data file
--
-- The data file may contain any 8 bit values at all. The total number
-- of bytes in the stream may be sent as the first operand, otherwise
-- the field should be cleared to 0. The name of the data file should
-- start with ASCII "dfA". This should be followed by a three digit job
-- number. The job number should be followed by the host name which has
-- constructed the data file. Interpretation of the contents of the
-- data file is determined by the contents of the corresponding control
-- file. If a data file length has been specified, the next "Operand 1"
-- octets over the same TCP connection are the intended contents of the
-- data file. In this case, once all of the contents have been
-- delivered, an octet of zero bits is sent as an indication that the
-- file being sent is complete. A second level of acknowledgement
-- processing must occur at this point.
2004-07-01 05:39:56 +02:00
local function send_hdr(con, control)
local sent = con.skt:send(control)
con.try(sent and sent >= 1 , "failed to send header file")
recv_ack(con)
2004-06-18 23:41:44 +02:00
end
2004-07-01 05:39:56 +02:00
local function send_control(con, control)
2004-07-01 05:47:22 +02:00
local sent = con.skt:send(control)
2004-07-01 05:39:56 +02:00
con.try(sent and sent >= 1, "failed to send control file")
send_ack(con)
2004-06-18 23:41:44 +02:00
end
2004-07-01 05:39:56 +02:00
local function send_data(con,fh,size)
local buf
2004-06-18 23:41:44 +02:00
while size > 0 do
buf,message = fh:read(8192)
if buf then
2004-07-01 05:47:22 +02:00
st = con.try(con.skt:send(buf))
2004-06-18 23:41:44 +02:00
size = size - st
else
2004-07-01 05:39:56 +02:00
con.try(size == 0, "file size mismatch")
2004-06-18 23:41:44 +02:00
end
end
recv_ack(con) -- note the double acknowledgement
2004-07-01 05:39:56 +02:00
send_ack(con)
recv_ack(con)
return size
2004-06-18 23:41:44 +02:00
end
--[[
local control_dflt = {
"H"..string.sub(socket.hostname,1,31).."\10", -- host
"C"..string.sub(socket.hostname,1,31).."\10", -- class
"J"..string.sub(filename,1,99).."\10", -- jobname
"L"..string.sub(user,1,31).."\10", -- print banner page
"I"..tonumber(indent).."\10", -- indent column count ('f' only)
"M"..string.sub(mail,1,128).."\10", -- mail when printed user@host
"N"..string.sub(filename,1,131).."\10", -- name of source file
"P"..string.sub(user,1,31).."\10", -- user name
"T"..string.sub(title,1,79).."\10", -- title for banner ('p' only)
"W"..tonumber(width or 132).."\10", -- width of print f,l,p only
"f"..file.."\10", -- formatted print (remove control chars)
"l"..file.."\10", -- print
"o"..file.."\10", -- postscript
"p"..file.."\10", -- pr format - requires T, L
"r"..file.."\10", -- fortran format
"U"..file.."\10", -- Unlink (data file only)
}
]]
-- generate a varying job number
2004-07-01 05:39:56 +02:00
local seq = 0
local function newjob(connection)
seq = seq + 1
return math.floor(socket.gettime() * 1000 + seq)%1000
2004-06-18 23:41:44 +02:00
end
local format_codes = {
binary = 'l',
text = 'f',
ps = 'o',
pr = 'p',
fortran = 'r',
l = 'l',
r = 'r',
o = 'o',
p = 'p',
f = 'f'
}
-- lp.send{option}
-- requires option.file
2004-07-01 05:39:56 +02:00
send = socket.protect(function(option)
2004-11-27 08:58:04 +01:00
socket.try(option and base.type(option) == "table", "invalid options")
local file = option.file
socket.try(file, "invalid file name")
2004-11-27 08:58:04 +01:00
local fh = socket.try(io.open(file,"rb"))
2004-07-01 05:39:56 +02:00
local datafile_size = fh:seek("end") -- get total size
fh:seek("set") -- go back to start of file
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
2004-07-01 05:39:56 +02:00
or "localhost"
local con = connect(localhost, option)
2004-06-18 23:41:44 +02:00
-- format the control file
2004-07-01 05:39:56 +02:00
local jobno = newjob()
2004-06-18 23:41:44 +02:00
local localip = socket.dns.toip(localhost)
localhost = string.sub(localhost,1,31)
local user = string.sub(option.user or os.getenv("LPRUSER") or
2004-07-01 05:39:56 +02:00
os.getenv("USERNAME") or os.getenv("USER") or "anonymous", 1,31)
2004-06-18 23:41:44 +02:00
local lpfile = string.format("dfA%3.3d%-s", jobno, localhost);
local fmt = format_codes[option.format] or 'l'
local class = string.sub(option.class or localip or localhost,1,31)
local _,_,ctlfn = string.find(file,".*[%/%\\](.*)")
ctlfn = string.sub(ctlfn or file,1,131)
local cfile =
string.format("H%-s\nC%-s\nJ%-s\nP%-s\n%.1s%-s\nU%-s\nN%-s\n",
localhost,
class,
2004-07-01 05:39:56 +02:00
option.job or "LuaSocket",
2004-06-18 23:41:44 +02:00
user,
fmt, lpfile,
lpfile,
ctlfn); -- mandatory part of ctl file
if (option.banner) then cfile = cfile .. 'L'..user..'\10' end
2004-11-27 08:58:04 +01:00
if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end
2004-06-18 23:41:44 +02:00
if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end
if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end
if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then
2004-11-27 08:58:04 +01:00
cfile = cfile .. 'W'..base.tonumber(option,width)..'\10'
2004-06-18 23:41:44 +02:00
end
2004-07-01 05:39:56 +02:00
con.skt:settimeout(option.timeout or 65)
2004-06-18 23:41:44 +02:00
-- send the queue header
2004-07-01 05:39:56 +02:00
send_queue(con, option.queue)
2004-06-18 23:41:44 +02:00
-- send the control file header
local cfilecmd = string.format("\2%d cfA%3.3d%-s\n",string.len(cfile), jobno, localhost);
2004-07-01 05:39:56 +02:00
send_hdr(con,cfilecmd)
2004-06-18 23:41:44 +02:00
-- send the control file
2004-07-01 05:39:56 +02:00
send_control(con,cfile)
2004-06-18 23:41:44 +02:00
-- send the data file header
local dfilecmd = string.format("\3%d dfA%3.3d%-s\n",datafile_size, jobno, localhost);
2004-07-01 05:39:56 +02:00
send_hdr(con,dfilecmd)
2004-06-18 23:41:44 +02:00
-- send the data file
2004-07-01 05:39:56 +02:00
send_data(con,fh,datafile_size)
2004-06-18 23:41:44 +02:00
fh:close()
2004-07-01 05:39:56 +02:00
con.skt:close();
return jobno, datafile_size
2004-06-18 23:41:44 +02:00
end)
2004-07-01 05:39:56 +02:00
--
-- lp.query({host=,queue=printer|'*', format='l'|'s', list=})
--
query = socket.protect(function(p)
p = p or {}
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
2004-07-01 05:39:56 +02:00
or "localhost"
local con = connect(localhost,p)
local fmt
2004-06-18 23:41:44 +02:00
if string.sub(p.format or 's',1,1) == 's' then fmt = 3 else fmt = 4 end
con.try(con.skt:send(string.format("%c%s %s\n", fmt, p.queue or "*",
2004-07-01 05:39:56 +02:00
p.list or "")))
2004-07-01 05:47:22 +02:00
local data = con.try(con.skt:receive("*a"))
2004-07-01 05:39:56 +02:00
con.skt:close()
return data
2004-06-18 23:41:44 +02:00
end)