2 // LPD.pmod: an implementation of the BSD lpd protocol (RFC 1179).
3 // This is a module for pike.
4 // 3 July 1998 <hww3@riverweb.com> Bill Welliver
6 // $Id: LPD.pmod,v 1.10 2008/01/13 17:02:43 nilsson Exp $
11 //! A client for communicating with printers and print spoolers that
12 //! support the BSD lpd protocol (RFC 1179).
21 private int connect(string host, int port)
24 // try to open one of the "official" local socket ports.
25 // not having one doesn't seem to be a problem with most LPD
26 // servers, but we should at least try. will probably fail
27 // if two try to open the same local port at once. ymmv.
28 int res=conn->open_socket(721 + a);
30 return conn->connect(host, port);
33 //! @decl int set_job_type(string type)
34 //! Set the type of job to be sent to the printer to @i{type@}.
35 //! Valid types are: text, postscript and raw.
36 int set_job_type(string type)
38 type=lower_case(type);
61 //! @decl int set_job_name(string name)
62 //! Sets the name of the print job to @i{name@}.
63 int set_job_name(string name)
69 //! @decl int start_queue(string queue)
70 //! Start the queue @i{queue@} if not already printing.
72 //! Returns 0 if unable to connect, 1 otherwise.
73 int start_queue(string queue)
77 if(!connect(host, port))
80 conn->write(sprintf("%c%s\n", 01, queue));
81 string resp= conn->read();
86 //! @decl string|int send_job(string queue, string job)
87 //! Send print job consisting of data @i{job@} to printer @i{queue@}.
89 //! Returns 1 if success, 0 otherwise.
90 int send_job(string queue, string job)
96 if(!connect(host, port))
98 // werror("connected to " + host + "\n");
101 control+="H"+gethostname()+"\n";
102 #if constant(getuid) && constant(getpwuid)
103 control+="P"+(getpwuid(getuid())[0]||"nobody")+"\n";
107 control+=(jobtype||"l")+"dfA"+ sprintf("%03d%s", jobnum, gethostname())+"\n";
110 control+="J" + jobname + "\n";
111 control+="N" + jobname + "\n";
115 control+="JPike LPD Client Job " + jobnum + "\n";
116 control+="NPike LPD Client Job " + jobnum + "\n";
119 werror("job file:\n\n" + control + "\n\n");
121 conn->write(sprintf("%c%s\n", 02, queue));
125 werror("receive job failed.\n");
129 conn->write(sprintf("%c%s cfA%03d%s\n", 02, (string)sizeof(control),
130 jobnum,gethostname()));
135 werror("request receive control failed.\n");
139 conn->write(sprintf("%s%c", control, 0));
144 werror("send receive control failed.\n");
148 conn->write(sprintf("%c%s dfA%03d%s\n", 03, (string)sizeof(job), jobnum,
153 werror("request receive job failed.\n");
158 conn->write(sprintf("%s%c", job, 0));
163 werror("send receive job failed.\n");
169 // read the response.
171 // resp=conn->read();
178 // start_queue(queue);
182 //! @decl int delete_job(string queue, int|void job)
183 //! Delete job @i{job@} from printer @i{queue@}.
185 //! Returns 1 on success, 0 otherwise.
186 int delete_job(string queue, int|void job)
190 if(!connect(host, port))
193 #if constant(getpwuid) && constant(getuid)
194 string agent=(getpwuid(getuid())[0]||"nobody");
196 string agent="nobody";
200 conn->write(sprintf("%c%s %s %d\n", 05, queue, agent, job));
202 conn->write(sprintf("%c%s %s\n", 05, queue, agent));
203 string resp= conn->read();
209 //! @decl string|int status(string queue)
210 //! Check the status of queue @i{queue@}.
212 //! Returns 0 on failure, otherwise returns the status response from the printer.
213 string|int status(string queue)
217 if(!connect(host, port))
220 conn->write(sprintf("%c%s\n", 04, queue));
221 string resp= conn->read();
226 //! Create a new LPD client connection.
228 //! Contains the hostname or ipaddress of the print host.
229 //! if not provided, defaults to @i{localhost@}.
231 //! Contains the port the print host is listening on.
232 //! if not provided, defaults to port @i{515@}, the RFC 1179 standard.
233 void create(string|void hostname, int|void portnum)
235 host=hostname || "localhost";