| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
local io = require("io") |
| 12 |
local base = _G |
| 13 |
local os = require("os") |
| 14 |
local math = require("math") |
| 15 |
local string = require("string") |
| 16 |
local socket = require("socket") |
| 17 |
local ltn12 = require("ltn12") |
| 18 |
module("socket.lp") |
| 19 |
|
| 20 |
|
| 21 |
PORT = 515 |
| 22 |
SERVER = os.getenv("SERVER_NAME") or os.getenv("COMPUTERNAME") or "localhost" |
| 23 |
PRINTER = os.getenv("PRINTER") or "printer" |
| 24 |
|
| 25 |
local function connect(localhost, option) |
| 26 |
local host = option.host or SERVER |
| 27 |
local port = option.port or PORT |
| 28 |
local skt |
| 29 |
local try = socket.newtry(function() if skt then skt:close() end end) |
| 30 |
if option.localbind then |
| 31 |
|
| 32 |
local localport = 721 |
| 33 |
local done, err |
| 34 |
repeat |
| 35 |
skt = socket.try(socket.tcp()) |
| 36 |
try(skt:settimeout(30)) |
| 37 |
done, err = skt:bind(localhost, localport) |
| 38 |
if not done then |
| 39 |
localport = localport + 1 |
| 40 |
skt:close() |
| 41 |
skt = nil |
| 42 |
else break end |
| 43 |
until localport > 731 |
| 44 |
socket.try(skt, err) |
| 45 |
else skt = socket.try(socket.tcp()) end |
| 46 |
try(skt:connect(host, port)) |
| 47 |
return { skt = skt, try = try } |
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
local function recv_ack(con) |
| 89 |
local ack = con.skt:receive(1) |
| 90 |
con.try(string.char(0) == ack, "failed to receive server acknowledgement") |
| 91 |
end |
| 92 |
|
| 93 |
|
| 94 |
local function send_ack(con) |
| 95 |
local sent = con.skt:send(string.char(0)) |
| 96 |
con.try(sent == 1, "failed to send acknowledgement") |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
local function send_queue(con, queue) |
| 117 |
queue = queue or PRINTER |
| 118 |
local str = string.format("\2%s\10", queue) |
| 119 |
local sent = con.skt:send(str) |
| 120 |
con.try(sent == string.len(str), "failed to send print request") |
| 121 |
recv_ack(con) |
| 122 |
end |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
local function send_hdr(con, control) |
| 174 |
local sent = con.skt:send(control) |
| 175 |
con.try(sent and sent >= 1 , "failed to send header file") |
| 176 |
recv_ack(con) |
| 177 |
end |
| 178 |
|
| 179 |
local function send_control(con, control) |
| 180 |
local sent = con.skt:send(control) |
| 181 |
con.try(sent and sent >= 1, "failed to send control file") |
| 182 |
send_ack(con) |
| 183 |
end |
| 184 |
|
| 185 |
local function send_data(con,fh,size) |
| 186 |
local buf |
| 187 |
while size > 0 do |
| 188 |
buf,message = fh:read(8192) |
| 189 |
if buf then |
| 190 |
st = con.try(con.skt:send(buf)) |
| 191 |
size = size - st |
| 192 |
else |
| 193 |
con.try(size == 0, "file size mismatch") |
| 194 |
end |
| 195 |
end |
| 196 |
recv_ack(con) |
| 197 |
send_ack(con) |
| 198 |
recv_ack(con) |
| 199 |
return size |
| 200 |
end |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
local seq = 0 |
| 227 |
local function newjob(connection) |
| 228 |
seq = seq + 1 |
| 229 |
return math.floor(socket.gettime() * 1000 + seq)%1000 |
| 230 |
end |
| 231 |
|
| 232 |
|
| 233 |
local format_codes = { |
| 234 |
binary = 'l', |
| 235 |
text = 'f', |
| 236 |
ps = 'o', |
| 237 |
pr = 'p', |
| 238 |
fortran = 'r', |
| 239 |
l = 'l', |
| 240 |
r = 'r', |
| 241 |
o = 'o', |
| 242 |
p = 'p', |
| 243 |
f = 'f' |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
send = socket.protect(function(option) |
| 250 |
socket.try(option and base.type(option) == "table", "invalid options") |
| 251 |
local file = option.file |
| 252 |
socket.try(file, "invalid file name") |
| 253 |
local fh = socket.try(io.open(file,"rb")) |
| 254 |
local datafile_size = fh:seek("end") |
| 255 |
fh:seek("set") |
| 256 |
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") |
| 257 |
or "localhost" |
| 258 |
local con = connect(localhost, option) |
| 259 |
|
| 260 |
local jobno = newjob() |
| 261 |
local localip = socket.dns.toip(localhost) |
| 262 |
localhost = string.sub(localhost,1,31) |
| 263 |
local user = string.sub(option.user or os.getenv("LPRUSER") or |
| 264 |
os.getenv("USERNAME") or os.getenv("USER") or "anonymous", 1,31) |
| 265 |
local lpfile = string.format("dfA%3.3d%-s", jobno, localhost); |
| 266 |
local fmt = format_codes[option.format] or 'l' |
| 267 |
local class = string.sub(option.class or localip or localhost,1,31) |
| 268 |
local _,_,ctlfn = string.find(file,".*[%/%\\](.*)") |
| 269 |
ctlfn = string.sub(ctlfn or file,1,131) |
| 270 |
local cfile = |
| 271 |
string.format("H%-s\nC%-s\nJ%-s\nP%-s\n%.1s%-s\nU%-s\nN%-s\n", |
| 272 |
localhost, |
| 273 |
class, |
| 274 |
option.job or "LuaSocket", |
| 275 |
user, |
| 276 |
fmt, lpfile, |
| 277 |
lpfile, |
| 278 |
ctlfn); |
| 279 |
if (option.banner) then cfile = cfile .. 'L'..user..'\10' end |
| 280 |
if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end |
| 281 |
if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end |
| 282 |
if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end |
| 283 |
if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then |
| 284 |
cfile = cfile .. 'W'..base.tonumber(option,width)..'\10' |
| 285 |
end |
| 286 |
|
| 287 |
con.skt:settimeout(option.timeout or 65) |
| 288 |
|
| 289 |
send_queue(con, option.queue) |
| 290 |
|
| 291 |
local cfilecmd = string.format("\2%d cfA%3.3d%-s\n",string.len(cfile), jobno, localhost); |
| 292 |
send_hdr(con,cfilecmd) |
| 293 |
|
| 294 |
|
| 295 |
send_control(con,cfile) |
| 296 |
|
| 297 |
|
| 298 |
local dfilecmd = string.format("\3%d dfA%3.3d%-s\n",datafile_size, jobno, localhost); |
| 299 |
send_hdr(con,dfilecmd) |
| 300 |
|
| 301 |
|
| 302 |
send_data(con,fh,datafile_size) |
| 303 |
fh:close() |
| 304 |
con.skt:close(); |
| 305 |
return jobno, datafile_size |
| 306 |
end) |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
query = socket.protect(function(p) |
| 312 |
p = p or {} |
| 313 |
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") |
| 314 |
or "localhost" |
| 315 |
local con = connect(localhost,p) |
| 316 |
local fmt |
| 317 |
if string.sub(p.format or 's',1,1) == 's' then fmt = 3 else fmt = 4 end |
| 318 |
con.try(con.skt:send(string.format("%c%s %s\n", fmt, p.queue or "*", |
| 319 |
p.list or ""))) |
| 320 |
local data = con.try(con.skt:receive("*a")) |
| 321 |
con.skt:close() |
| 322 |
return data |
| 323 |
end) |