Debian Codegraph @

hfaxd/Jobs.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1995-1996 Sam Leffler
4 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 #include "port.h"
28 #include "config.h"
29 #include "Sys.h"
30 #include "HylaFAXServer.h"
31 #include "Dispatcher.h"
32 #include "Timeout.h"
33 #include "class2.h"
34
35 #include <sys/file.h>
36 #include <ctype.h>
37
38 /*
39 * Job state query and control commands.
40 */
41 Job::Job(const fxStr& qf, int f) : FaxRequest(qf,f)
42 {
43 lastmod = 0;
44 queued = false;
45 }
46 Job::~Job() {}
47
48 /*
49 * We need to override the default logic because faxq
50 * assumes it's working directory is at the top of the
51 * spooling area and we may be elsewhere. We can also
52 * simplify things because we know we're chroot'd to
53 * the top of the spooling area--thus we don't need to
54 * check for ``..'' and ``/'' being used to reference
55 * files outside the spooling area.
56 */
57 bool
58 Job::checkDocument(const char* pathname)
59 {
60 struct stat sb;
61 if (FileCache::lookup(fxStr::format("/%s", pathname), sb))
62 return (true);
63 int fd = Sys::open(pathname, 0);
64 if (fd == -1) {
65 logError("Can not access document file \"%s\": %s",
66 pathname, strerror(errno));
67 return (false);
68 }
69 Sys::close(fd);
70 logError("Undetermined error with document file \"%s\"", pathname);
71 return (false);
72 }
73
74 fxIMPLEMENT_StrKeyPtrValueDictionary(JobDict, Job*)
75
76 /*
77 * Job state parameter access controls.
78 *
79 * There are three levels of access control applied to job state
80 * information: administrator, owner, and other.
81 * Access is controlled on a read+write basis. Write accesses
82 * can be further restricted to constrain written values to
83 * be within a range; this is used to allow users to change
84 * parameters within a restricted range (e.g. to up the maximum
85 * number of tries but still constrain it to a sane value).
86 */
87 #define A_RUSR 0400 // read permission: owner
88 #define A_WUSR 0200 // abitrary write permission: owner
89 #define A_MUSR 0100 // restricted write permission: owner
90 #define A_RADM 0040 // read permission: administrator
91 #define A_WADM 0020 // abitrary write permission: administrator
92 #define A_MADM 0010 // restricted write permission: administrator
93 #define A_ROTH 0004 // read permission: other
94 #define A_WOTH 0002 // abitrary write permission: other
95 #define A_MOTH 0001 // restricted write permission: other
96
97 #define A_READ 004
98 #define A_WRITE 002
99 #define A_MODIFY 001
100
101 #define N(a) (sizeof (a) / sizeof (a[0]))
102
103 static const struct {
104 Token t;
105 u_int protect; // read+write protection
106 } params[] = {
107 { T_BEGBR, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
108 { T_BEGST, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
109 { T_CHOPTHRESH, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
110 { T_CLIENT, A_RUSR|A_RADM|A_WADM|A_ROTH },
111 { T_COMMID, A_RUSR|A_RADM|A_ROTH },
112 { T_COMMENTS, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
113 { T_COVER, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
114 { T_DATAFORMAT, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
115 { T_DIALSTRING, A_RUSR|A_WUSR|A_RADM|A_WADM },
116 { T_DOCUMENT, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
117 { T_DONEOP, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
118 { T_EXTERNAL, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
119 { T_ERRORCODE, A_RUSR|A_RADM|A_ROTH }, // compat STATUSCODE
120 { T_FAXNUMBER, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
121 { T_FROM_COMPANY, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
122 { T_FROM_LOCATION, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
123 { T_FROM_USER, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
124 { T_FROM_VOICE, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
125 { T_GROUPID, A_RUSR|A_RADM|A_ROTH },
126 { T_JOBID, A_RUSR|A_RADM|A_ROTH },
127 { T_JOBINFO, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
128 { T_JOBTYPE, A_RUSR|A_RADM|A_ROTH },
129 { T_LASTTIME, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
130 { T_MAXDIALS, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
131 { T_MAXPAGES, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
132 { T_MAXTRIES, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
133 { T_MINBR, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
134 { T_MODEM, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
135 { T_OWNER, A_RUSR|A_RADM|A_WADM|A_ROTH },
136 { T_NDIALS, A_RUSR|A_RADM|A_ROTH },
137 { T_NOTIFY, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
138 { T_NOTIFYADDR, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
139 { T_NPAGES, A_RUSR|A_RADM|A_ROTH },
140 { T_NTRIES, A_RUSR|A_RADM|A_ROTH },
141 { T_PAGECHOP, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
142 { T_PAGELENGTH, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
143 { T_PAGERANGE, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
144 { T_PAGEWIDTH, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
145 { T_PASSWD, A_RUSR|A_WUSR|A_RADM|A_WADM },
146 { T_POLL, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
147 { T_REGARDING, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
148 { T_RETRYTIME, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
149 { T_SCHEDPRI, A_RUSR|A_MUSR|A_RADM|A_WADM|A_ROTH },
150 { T_SENDTIME, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
151 { T_STATE, A_RUSR|A_RADM|A_ROTH },
152 { T_STATUS, A_RUSR|A_RADM|A_ROTH },
153 { T_STATUSCODE, A_RUSR|A_RADM|A_ROTH },
154 { T_SUBADDR, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
155 { T_TAGLINE, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
156 { T_TOTDIALS, A_RUSR|A_RADM|A_ROTH },
157 { T_TOTPAGES, A_RUSR|A_RADM|A_ROTH },
158 { T_TOTTRIES, A_RUSR|A_RADM|A_ROTH },
159 { T_TO_COMPANY, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
160 { T_TO_LOCATION, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
161 { T_TO_USER, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
162 { T_TO_VOICE, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
163 { T_TSI, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
164 { T_USE_CONTCOVER, A_RUSR|A_RADM|A_WADM|A_ROTH },
165 { T_USE_ECM, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
166 { T_USE_TAGLINE, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
167 { T_USE_XVRES, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
168 { T_USRKEY, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
169 { T_VRES, A_RUSR|A_WUSR|A_RADM|A_WADM|A_ROTH },
170 { T_NIL, 0 },
171 };
172
173 /*
174 * Check client permission to do the specified operation
175 * on the specified job state parameter. Operation can
176 * be a combination of A_READ+A_WRITE+A_MODIFY. Note that
177 * ownership is based on login account and not fax uid;
178 * this may need to be rethought.
179 */
180 bool
181 HylaFAXServer::checkAccess(const Job& job, Token t, u_int op)
182 {
183 fxAssert(t != T_JOB, "checkAccess token should *not* be T_JOB");
184
185 u_int n = N(params)-1;
186 u_int i = 0;
187 while (i < n && params[i].t != t)
188 i++;
189 u_int m = params[i].protect;
190 if (m&op) // other/public access
191 return (true);
192 if (IS(PRIVILEGED) && ((m>>3)&op)) // administrative access
193 return (true);
194 if (job.owner == the_user && ((m>>6)&op)) // owner access
195 return (true);
196
197 /*
198 * If file access writes give
199 * us write access to the job,
200 * we check job.protect as if
201 * we are the owner
202 */
203 struct stat sb;
204 if (FileCache::update(job.qfile, sb)
205 && checkFileRights(W_OK, sb)
206 && ((m>>6)&op) )
207 return (true);
208 return (false);
209 }
210
211 static struct {
212 Token t;
213 fxStr Job::* p;
214 } strvals[] = {
215 { T_JOBTYPE, &Job::jobtype },
216 { T_EXTERNAL, &Job::external },
217 { T_DIALSTRING, &Job::number },
218 { T_NOTIFYADDR, &Job::mailaddr },
219 { T_USRKEY, &Job::jobtag },
220 { T_MODEM, &Job::modem },
221 { T_TO_USER, &Job::receiver },
222 { T_TO_COMPANY, &Job::company },
223 { T_TO_LOCATION, &Job::location },
224 { T_TO_VOICE, &Job::voice },
225 { T_FROM_USER, &Job::sender },
226 { T_FROM_COMPANY, &Job::fromcompany },
227 { T_FROM_LOCATION, &Job::fromlocation },
228 { T_FROM_VOICE, &Job::fromvoice },
229 { T_PASSWD, &Job::passwd },
230 { T_CLIENT, &Job::client },
231 { T_FAXNUMBER, &Job::faxnumber },
232 { T_TSI, &Job::tsi },
233 { T_TAGLINE, &Job::tagline },
234 { T_SUBADDR, &Job::subaddr },
235 { T_GROUPID, &Job::groupid },
236 { T_JOBID, &Job::jobid },
237 { T_JOBINFO, &Job::jobtag },
238 { T_OWNER, &Job::owner },
239 { T_DONEOP, &Job::doneop },
240 { T_COMMID, &Job::commid },
241 { T_REGARDING, &Job::regarding },
242 { T_COMMENTS, &Job::comments },
243 { T_PAGERANGE, &Job::pagerange },
244 };
245 static struct {
246 Token t;
247 u_short Job::* p;
248 } shortvals[] = {
249 { T_TOTPAGES, &Job::totpages },
250 { T_NPAGES, &Job::npages },
251 { T_NTRIES, &Job::ntries },
252 { T_NDIALS, &Job::ndials },
253 { T_TOTDIALS, &Job::totdials },
254 { T_MAXDIALS, &Job::maxdials },
255 { T_TOTTRIES, &Job::tottries },
256 { T_MAXTRIES, &Job::maxtries },
257 { T_PAGEWIDTH, &Job::pagewidth },
258 { T_PAGELENGTH, &Job::pagelength },
259 { T_VRES, &Job::resolution },
260 { T_SCHEDPRI, &Job::usrpri },
261 { T_MINBR, &Job::minbr },
262 { T_BEGBR, &Job::desiredbr },
263 { T_BEGST, &Job::desiredst },
264 };
265 static const char* notifyVals[4] = {
266 "NONE", // no_notice
267 "DONE", // when_done
268 "REQUEUE", // when_requeued
269 "DONE+REQUEUE" // when_done|when_requeued
270 };
271 static const char* chopVals[4] = {
272 "DEFAULT", // chop_default
273 "NONE", // chop_none
274 "ALL", // chop_all
275 "LAST" // chop_last
276 };
277 static const char* dataVals[] = {
278 "G31D", // Group 3, 1-D
279 "G32D", // Group 3, 2-D
280 "G32DUNC", // Group 3, 2-D (w/ uncompressed)
281 "G4" // Group 4
282 };
283 static const char* stateVals[] = {
284 "UNDEFINED", // undefined state (should never be used)
285 "SUSPENDED", // not being scheduled
286 "PENDING", // waiting for time to send
287 "SLEEPING", // waiting for scheduled timeout
288 "BLOCKED", // blocked by concurrent activity
289 "READY", // ready to be go, waiting for resources
290 "ACTIVE", // actively being processed
291 "DONE", // processing completed with success
292 "FAILED", // processing completed with failure
293 };
294 static const char* docTypeNames[] = {
295 "FAX", // send_fax
296 "TIFF", // send_tiff
297 "TIFF", // send_tiff_saved
298 "PDF", // send_pdf
299 "PDF", // send_pdf_saved
300 "PS", // send_postscript
301 "PS", // send_postscript_saved
302 "PCL", // send_pcl
303 "PCL", // send_pcl_saved
304 "DATA", // send_data
305 "DATA", // send_data_saved
306 "POLL", // send_poll
307 "PAGE", // send_page
308 "PAGE", // send_page_saved
309 "UUCP", // send_uucp
310 "UNKNOWN", // send_unknown
311 };
312
313 static const char*
314 boolString(bool b)
315 {
316 return (b ? "YES" : "NO");
317 }
318 void
319 HylaFAXServer::replyBoolean(int code, bool b)
320 {
321 reply(code, "%s", boolString(b));
322 }
323
324 /*
325 * Check that the job's in-memory state is consistent
326 * with what is on disk. If the on-disk state is newer then
327 * read it in. If the job has been removed then remove our
328 * reference, reset the current job back to the default job,
329 * and send the client an error reply. This method is used
330 * before each place a job's state is access.
331 */
332 bool
333 HylaFAXServer::checkJobState(Job* job)
334 {
335 /*
336 * Verify job is still around (another process has
337 * not deleted it) and if the on-disk state has
338 * been updated, re-read the job description file.
339 */
340 struct stat sb;
341 if (!FileCache::update("/" | job->qfile, sb)) {
342 jobs.remove(job->jobid);
343 if (job == curJob) // make default job current
344 curJob = &defJob;
345 delete job, job = NULL;
346 return (false);
347 }
348 if (job->lastmod < sb.st_mtime) {
349 if (updateJobFromDisk(*job))
350 job->lastmod = sb.st_mtime;
351 }
352 return (true);
353 }
354
355 /*
356 * Check if it's ok to do the specified operation on the
357 * current job's state parameter. If not, return the appropriate
358 * error reply. We disallow modifications on jobs that
359 * do not appear to be suspended since otherwise the mods
360 * could be lost (in our case they will be lost due to the
361 * way that things work).
362 */
363 bool
364 HylaFAXServer::checkParm(Job& job, Token t, u_int op)
365 {
366 if (!checkJobState(&job)) { // insure consistent state
367 reply(500, "Cannot access job state; job deleted by another party.");
368 return (false);
369 } else if (!checkAccess(job, t, op)) {
370 reply(503, "Permission denied: no %s access to job parameter %s."
371 , (op == A_READ ? "read" : "write")
372 , parmToken(t)
373 );
374 return (false);
375 } else if ((op & (A_WRITE|A_MODIFY)) &&
376 job.state != FaxRequest::state_suspended && job.jobid != "default") {
377 reply(503, "Suspend the job with JSUSP first.");
378 return (false);
379 } else
380 return (true);
381 }
382
383 /*
384 * Respond to a job state parameter query.
385 */
386 void
387 HylaFAXServer::replyJobParamValue(Job& job, int code, Token t)
388 {
389 if (!checkParm(job, t, A_READ))
390 return;
391 u_int i, n;
392 switch (t) {
393 case T_SENDTIME:
394 if (job.tts != 0) {
395 const struct tm* tm = cvtTime(job.tts);
396 // XXX should this include seconds?
397 reply(code, "%4d%02d%02d%02d%02d%02d"
398 , tm->tm_year+1900
399 , tm->tm_mon+1
400 , tm->tm_mday
401 , tm->tm_hour
402 , tm->tm_min
403 , tm->tm_sec
404 );
405 } else
406 reply(code, "NOW");
407 return;
408 case T_LASTTIME:
409 time_t tv; tv = job.killtime - job.tts; // XXX for __GNUC__
410 reply(code, "%02d%02d%02d", tv/(24*60*60), (tv/(60*60))%24, (tv/60)%60);
411 return;
412 case T_RETRYTIME:
413 reply(code, "%02d%02d", job.retrytime/60, job.retrytime%60);
414 return;
415 case T_STATE:
416 reply(code, "%s", stateVals[job.state]);
417 return;
418 case T_NOTIFY:
419 reply(code, "%s", notifyVals[job.notify]);
420 return;
421 case T_PAGECHOP:
422 reply(code, "%s", chopVals[job.pagechop]);
423 return;
424 case T_CHOPTHRESH:
425 reply(code, "%g", job.chopthreshold);
426 return;
427 case T_DATAFORMAT:
428 reply(code, "%s", dataVals[job.desireddf]);
429 return;
430 case T_USE_ECM:
431 replyBoolean(code, job.desiredec);
432 return;
433 case T_USE_TAGLINE:
434 replyBoolean(code, job.desiredtl);
435 return;
436 case T_USE_XVRES:
437 replyBoolean(code, job.usexvres);
438 return;
439 case T_USE_CONTCOVER:
440 replyBoolean(code, job.useccover);
441 return;
442 case T_DOCUMENT:
443 for (i = 0, n = job.items.length(); i < n; i++) {
444 const FaxItem& fitem = job.items[i];
445 // XXX should cover page docs not be shown?
446 switch (fitem.op) {
447 case FaxRequest::send_pdf:
448 case FaxRequest::send_pdf_saved:
449 case FaxRequest::send_tiff:
450 case FaxRequest::send_tiff_saved:
451 case FaxRequest::send_postscript:
452 case FaxRequest::send_postscript_saved:
453 case FaxRequest::send_pcl:
454 case FaxRequest::send_pcl_saved:
455 lreply(code, "%s %s",
456 docTypeNames[fitem.op], (const char*) fitem.item);
457 break;
458 }
459 }
460 reply(code, "End of documents.");
461 return;
462 case T_COVER:
463 for (i = 0, n = job.items.length(); i < n; i++) {
464 const FaxItem& fitem = job.items[i];
465 if (fitem.item.length() > 7 && fitem.item.tail(6) == ".cover") {
466 switch (fitem.op) {
467 case FaxRequest::send_tiff:
468 case FaxRequest::send_tiff_saved:
469 case FaxRequest::send_pdf:
470 case FaxRequest::send_pdf_saved:
471 case FaxRequest::send_postscript:
472 case FaxRequest::send_postscript_saved:
473 case FaxRequest::send_pcl:
474 case FaxRequest::send_pcl_saved:
475 reply(code, "%s %s",
476 docTypeNames[fitem.op], (const char*) fitem.item);
477 return;
478 }
479 }
480 }
481 reply(code+1, "No cover page document.");
482 return;
483 case T_POLL:
484 for (i = 0, n = job.items.length(); i < n; i++) {
485 const FaxItem& fitem = job.items[i];
486 if (fitem.op == FaxRequest::send_poll)
487 lreply(code, "\"%s\" \"%s\"",
488 (const char*) fitem.item, (const char*) fitem.addr);
489 }
490 reply(code, "End of polling items.");
491 return;
492 case T_ERRORCODE:
493 reply(code, "E%03d", job.result.value());
494 return;
495 case T_STATUSCODE:
496 reply(code, "%03d", job.result.value());
497 return;
498 case T_STATUS:
499 reply(code, "%s", job.result.string());
500 return;
501 default:
502 break;
503 }
504 for (i = 0, n = N(strvals); i < n; i++)
505 if (strvals[i].t == t) {
506 reply(code, "%s", (const char*) (job.*strvals[i].p));
507 return;
508 }
509 for (i = 0, n = N(shortvals); i < n; i++)
510 if (shortvals[i].t == t) {
511 reply(code, "%u", job.*shortvals[i].p);
512 return;
513 }
514 reply(500, "Botch: no support for querying parameter value.");
515 }
516
517 void
518 HylaFAXServer::jstatLine(Token t, const char* fmt ...)
519 {
520 printf(" %s: ", parmToken(t));
521 va_list ap;
522 va_start(ap, fmt);
523 vprintf(fmt, ap);
524 va_end(ap);
525 printf("\r\n");
526 }
527
528 /*
529 * Implement the jparm command that returns the entire
530 * job state as a series of parameter: value pairs.
531 * This command is mainly intended for debugging and
532 * may go away in the final spec (or become a site cmd).
533 */
534 void
535 HylaFAXServer::jstatCmd(const Job& job)
536 {
537 lreply(217, "Job state: jobid %s groupid %s",
538 (const char*) job.jobid, (const char*) job.groupid);
539 if (checkAccess(job, T_SENDTIME, A_READ)) {
540 if (job.tts != 0) {
541 const struct tm* tm = cvtTime(job.tts);
542 // XXX should this include seconds? (useful for debugging)
543 jstatLine(T_SENDTIME, "%d%02d%02d%02d%02d%02d"
544 , tm->tm_year+1900
545 , tm->tm_mon+1
546 , tm->tm_mday
547 , tm->tm_hour
548 , tm->tm_min
549 , tm->tm_sec
550 );
551 } else
552 jstatLine(T_SENDTIME, "%s", "NOW");
553 }
554 if (checkAccess(job, T_LASTTIME, A_READ)) {
555 time_t tv = job.killtime - job.tts;
556 jstatLine(T_LASTTIME, "%02d%02d%02d",
557 tv/(24*60*60), (tv/(60*60))%24, (tv/60)%60);
558 }
559 if (checkAccess(job, T_RETRYTIME, A_READ))
560 jstatLine(T_RETRYTIME, "%02d%02d", job.retrytime/60, job.retrytime%60);
561 if (checkAccess(job, T_STATE, A_READ))
562 jstatLine(T_STATE, "%s", stateVals[job.state]);
563 if (checkAccess(job, T_NOTIFY, A_READ))
564 jstatLine(T_NOTIFY, "%s", notifyVals[job.notify]);
565 if (checkAccess(job, T_PAGECHOP, A_READ))
566 jstatLine(T_PAGECHOP, "%s", chopVals[job.pagechop]);
567 if (checkAccess(job, T_CHOPTHRESH, A_READ))
568 jstatLine(T_CHOPTHRESH, "%g", job.chopthreshold);
569 if (checkAccess(job, T_DATAFORMAT, A_READ))
570 jstatLine(T_DATAFORMAT, "%s", dataVals[job.desireddf]);
571 if (checkAccess(job, T_USE_ECM, A_READ))
572 jstatLine(T_USE_ECM, "%s", boolString(job.desiredec));
573 if (checkAccess(job, T_USE_TAGLINE, A_READ))
574 jstatLine(T_USE_TAGLINE,"%s", boolString(job.desiredtl));
575 if (checkAccess(job, T_USE_XVRES, A_READ))
576 jstatLine(T_USE_XVRES,"%s", boolString(job.usexvres));
577 if (checkAccess(job, T_USE_CONTCOVER, A_READ))
578 jstatLine(T_USE_CONTCOVER,"%s", boolString(job.useccover));
579 if (checkAccess(job, T_STATUSCODE, A_READ))
580 jstatLine(T_STATUSCODE,"%03d", job.result.value());
581 if (checkAccess(job, T_STATUS, A_READ))
582 jstatLine(T_STATUS, "%s", job.result.string());
583 u_int i, n;
584 for (i = 0, n = N(strvals); i < n; i++)
585 if (checkAccess(job, strvals[i].t, A_READ))
586 jstatLine(strvals[i].t, "%s", (const char*) (job.*strvals[i].p));
587 for (i = 0, n = N(shortvals); i < n; i++)
588 if (checkAccess(job, shortvals[i].t, A_READ))
589 jstatLine(shortvals[i].t, "%u", job.*shortvals[i].p);
590 /*
591 * NB: This assumes access to T_DOCUMENT is sufficient
592 * for access to T_COVER and T_POLL also.
593 */
594 if (checkAccess(job, T_DOCUMENT, A_READ)) {
595 for (i = 0, n = job.items.length(); i < n; i++) {
596 const FaxItem& fitem = job.items[i];
597 switch (fitem.op) {
598 case FaxRequest::send_fax:
599 jstatLine(T_DOCUMENT, "%s %s %u", docTypeNames[fitem.op],
600 (const char*) fitem.item, fitem.dirnum);
601 break;
602 case FaxRequest::send_tiff:
603 case FaxRequest::send_tiff_saved:
604 case FaxRequest::send_pdf:
605 case FaxRequest::send_pdf_saved:
606 case FaxRequest::send_postscript:
607 case FaxRequest::send_postscript_saved:
608 case FaxRequest::send_pcl:
609 case FaxRequest::send_pcl_saved:
610 case FaxRequest::send_data:
611 case FaxRequest::send_data_saved:
612 jstatLine(
613 (fitem.item.length() > 7 && fitem.item.tail(6) == ".cover" ?
614 T_COVER : T_DOCUMENT)
615 , "%s %s"
616 , docTypeNames[fitem.op]
617 , (const char*) fitem.item
618 );
619 break;
620 case FaxRequest::send_page:
621 case FaxRequest::send_page_saved:
622 jstatLine(T_DOCUMENT
623 , fitem.addr == "" ? "%s \"%s\"" : "%s \"%s\" \"%s\""
624 , docTypeNames[fitem.op]
625 , (const char*) fitem.item
626 , (const char*) fitem.addr
627 );
628 break;
629 case FaxRequest::send_poll:
630 jstatLine(T_POLL
631 , fitem.addr == "" ? "\"%s\"" : "\"%s\" \"%s\""
632 , (const char*) fitem.item
633 , (const char*) fitem.addr
634 );
635 break;
636 }
637 }
638 }
639 reply(217, "End of job %s state.", (const char*) job.jobid);
640 }
641
642 /*
643 * Set a job state parameter value from an array of
644 * parameter names; the value is the index into the
645 * array. Parameter names are case-insensitive.
646 * Unknown values cause an error reply.
647 */
648 bool
649 HylaFAXServer::setValue(u_short& v, const char* value, const char* what,
650 const char* valNames[], u_int nValNames)
651 {
652 for (u_int i = 0; i < nValNames; i++)
653 if (strcasecmp(value, valNames[i]) == 0) {
654 v = i;
655 return (true);
656 }
657 reply(503, "Unknown %s value %s.", what, value);
658 return (false);
659 }
660
661 void
662 HylaFAXServer::parmBotch(Token t)
663 {
664 reply(503, "Botch, don't know how to set %s parameter.", parmToken(t));
665 }
666
667 /*
668 * Discard any prepared documents so that
669 * they will be re-prepared with revised
670 * parameters.
671 */
672 void
673 HylaFAXServer::flushPreparedDocuments(Job& job)
674 {
675 u_int j = 0;
676 while (j < job.items.length()) {
677 FaxItem& fitem = job.items[j];
678 if (fitem.op == FaxRequest::send_fax) {
679 // NB: don't waste time requesting ACK
680 fxStr emsg;
681 sendQueuer(emsg, "U%s", (const char*) fitem.item);
682 job.items.remove(j);
683 continue;
684 }
685 if (fitem.isSavedOp())
686 fitem.op--; // assumes order of enum
687 j++;
688 }
689 job.pagehandling = ""; // force recalculation
690 }
691
692 /*
693 * Set a job state parameter that has a string value.
694 */
695 bool
696 HylaFAXServer::setJobParameter(Job& job, Token t, const fxStr& value)
697 {
698 if (checkParm(job, t, A_WRITE|A_MODIFY)) {
699 switch (t) {
700 case T_NOTIFY:
701 return setValue(job.notify, value, parmToken(t),
702 notifyVals, N(notifyVals));
703 case T_PAGECHOP:
704 if (setValue(job.pagechop, value, parmToken(t),
705 chopVals, N(chopVals))) {
706 job.pagehandling = ""; // force recalculation
707 return (true);
708 } else
709 return (false);
710 case T_DATAFORMAT:
711 if (setValue(job.desireddf, value, parmToken(t),
712 dataVals, N(dataVals))) {
713 flushPreparedDocuments(job);
714 return (true);
715 } else
716 return (false);
717 default:
718 break;
719 }
720 for (u_int i = 0, n = N(strvals); i < n; i++)
721 if (strvals[i].t == t) {
722 job.*strvals[i].p = value;
723 return (true);
724 }
725 parmBotch(t);
726 }
727 return (false);
728 }
729
730 /*
731 * Set a job state parameter that has a integer value.
732 */
733 bool
734 HylaFAXServer::setJobParameter(Job& job, Token t, u_short value)
735 {
736 if (checkParm(job, t, A_WRITE|A_MODIFY)) {
737 for (u_int i = 0, n = N(shortvals); i < n; i++)
738 if (shortvals[i].t == t) {
739 if (job.*shortvals[i].p != value) {
740 // XXX constrain values per A_MODIFY
741 job.*shortvals[i].p = value; // XXX
742 /*
743 * Handle parameters with side effects.
744 */
745 switch (t) {
746 case T_PAGEWIDTH:
747 case T_PAGELENGTH:
748 case T_VRES:
749 case T_HRES:
750 flushPreparedDocuments(job);
751 break;
752 case T_SCHEDPRI:
753 job.pri = job.usrpri; // reload
754 break;
755 default:
756 break;
757 }
758 }
759 return (true);
760 }
761 parmBotch(t);
762 }
763 return (false);
764 }
765
766 /*
767 * Set a job state parameter that has a time value.
768 */
769 bool
770 HylaFAXServer::setJobParameter(Job& job, Token t, time_t value)
771 {
772 if (checkParm(job, t, A_WRITE|A_MODIFY)) {
773 time_t now = Sys::now();
774 switch (t) {
775 case T_SENDTIME:
776 if (value != 0) { // explicit time
777 /*
778 * We don't complain anymore if this value is in the
779 * past. Instead, we verify that the killtime is in
780 * the future, ensuring that a window of send-time
781 * opportunity still exists.
782 */
783 job.tts = value;
784 } else // ``NOW''
785 job.tts = now;
786 return (true);
787 case T_LASTTIME:
788 /*
789 * Convert client-specified kill time (as a relative number)
790 * to an absolute time. If the time-to-send has been set,
791 * then the killtime is relative to that; otherwise it's
792 * made relative to ``now''. Note that this implies the
793 * order of setting SENDTIME and LASTTIME is important; if
794 * a client sets LASTTIME before SENDTIME then an unexpected
795 * value may be installed for LASTTIME.
796 */
797 job.killtime = value + (job.tts == 0 ? now : job.tts);
798 if (job.killtime < now) {
799 reply(503, "Bad time to send; time window is entirely in the past.");
800 return (false);
801 }
802 return (true);
803 case T_RETRYTIME:
804 job.retrytime = value;
805 return (true);
806 default:
807 break;
808 }
809 parmBotch(t);
810 }
811 return (false);
812 }
813
814 /*
815 * Set a job state parameter that has a boolean value.
816 */
817 bool
818 HylaFAXServer::setJobParameter(Job& job, Token t, bool b)
819 {
820 if (checkParm(job, t, A_WRITE|A_MODIFY)) {
821 switch (t) {
822 case T_USE_ECM:
823 job.desiredec = b;
824 return (true);
825 case T_USE_TAGLINE:
826 job.desiredtl = b;
827 return (true);
828 case T_USE_XVRES:
829 job.usexvres = b;
830 return (true);
831 case T_USE_CONTCOVER:
832 job.useccover = b;
833 return (true);
834 default:
835 break;
836 }
837 parmBotch(t);
838 }
839 return (false);
840 }
841
842 /*
843 * Set a job state parameter that has a float value.
844 */
845 bool
846 HylaFAXServer::setJobParameter(Job& job, Token t, float value)
847 {
848 if (checkParm(job, t, A_WRITE|A_MODIFY)) {
849 switch (t) {
850 case T_CHOPTHRESH:
851 if (job.chopthreshold != value) {
852 job.chopthreshold = value;
853 job.pagehandling = ""; // force recalculation
854 }
855 return (true);
856 default:
857 break;
858 }
859 parmBotch(t);
860 }
861 return (false);
862 }
863
864 /*
865 * Initialize the default job state. We
866 * explicitly set string values since this
867 * routine may be called for a JREST command
868 * to reset job state to the default settings.
869 */
870 void
871 HylaFAXServer::initDefaultJob(void)
872 {
873 defJob.jobid = "default";
874 defJob.owner = the_user;
875 defJob.state = FaxRequest::state_undefined;
876 defJob.maxdials = FAX_REDIALS;
877 defJob.maxtries = FAX_RETRIES;
878 defJob.pagewidth = 0;
879 defJob.pagelength = 0;
880 defJob.resolution = FAX_DEFVRES;
881 defJob.usrpri = FAX_DEFPRIORITY;
882 defJob.minbr = BR_2400;
883 defJob.desiredbr = BR_33600;
884 defJob.desiredst = ST_0MS;
885 defJob.desiredec = EC_ENABLE256;
886 defJob.desireddf = DF_2DMMR;
887 defJob.desiredtl = false;
888 defJob.usexvres = false;
889 defJob.useccover = true;
890 defJob.pagechop = FaxRequest::chop_default;
891 defJob.notify = FaxRequest::no_notice;// FAX_DEFNOTIFY
892 defJob.chopthreshold= 3.0;
893 defJob.tts = 0; // ``NOW''
894 defJob.killtime = 3*60*60; // FAX_TIMEOUT
895 defJob.retrytime = 0;
896 defJob.sender = the_user; // XXX usually incorrect
897 defJob.mailaddr = the_user | "@" | remotehost;
898 defJob.jobtag = "";
899 defJob.number = "";
900 defJob.subaddr = "";
901 defJob.passwd = "";
902 defJob.external = "";
903 defJob.modem = MODEM_ANY;
904 defJob.faxnumber = "";
905 defJob.tsi = "";
906 defJob.receiver = "";
907 defJob.company = "";
908 defJob.location = "";
909 defJob.voice = "";
910 defJob.fromcompany = "";
911 defJob.fromlocation = "";
912 defJob.fromvoice = "";
913 defJob.regarding = "";
914 defJob.comments = "";
915 defJob.client = remotehost;
916 defJob.tagline = "";
917 defJob.doneop = "default";
918 }
919
920 /*
921 * JNEW command; create a new job and
922 * make it the current job.
923 */
924 void
925 HylaFAXServer::newJobCmd(void)
926 {
927 fxStr emsg;
928 if (newJob(emsg) && updateJobOnDisk(*curJob, emsg)) {
929 fxStr file("/" | curJob->qfile);
930 setFileOwner(file); // force ownership
931 FileCache::chmod(file, jobProtection); // sync cache
932 curJob->lastmod = Sys::now(); // no one else should update
933 reply(200, "New job created: jobid: %s groupid: %s.",
934 (const char*) curJob->jobid, (const char*) curJob->groupid);
935 blankJobs[curJob->jobid] = curJob;
936 } else
937 reply(503, "%s.", (const char*) emsg);
938 }
939
940 /*
941 * Create a new job, inheriting state from the current
942 * job and make the new job be the current job. Note
943 * that the current job must be owned by the client;
944 * otherwise people could ``look inside'' other people's
945 * jobs by inheriting state--this would permit them to
946 * look at privileged information such as calling card
947 * information in dial strings and passwords to be
948 * transmitted with polling items.
949 */
950 bool
951 HylaFAXServer::newJob(fxStr& emsg)
952 {
953 if (!IS(PRIVILEGED) && the_user != curJob->owner) {
954 emsg = "Permission denied; cannot inherit from job " | curJob->jobid;
955 return (false);
956 }
957 u_int id = getJobNumber(emsg); // allocate unique job ID
958 if (id == (u_int) -1)
959 return (false);
960 fxStr jobid = fxStr::format("%u", id);
961 Job* job = new Job(FAX_SENDDIR "/" FAX_QFILEPREF | jobid);
962 job->jobid = jobid;
963 job->groupid = curJob->groupid;
964 if (job->groupid == "")
965 job->groupid = jobid;
966 job->owner = the_user;
967 job->state = FaxRequest::state_suspended;
968 job->maxdials = curJob->maxdials;
969 job->maxtries = curJob->maxtries;
970 job->pagewidth = curJob->pagewidth;
971 job->pagelength = curJob->pagelength;
972 job->resolution = curJob->resolution;
973 job->usrpri = curJob->usrpri;
974 job->minbr = curJob->minbr;
975 job->desiredbr = curJob->desiredbr;
976 job->desiredst = curJob->desiredst;
977 job->desiredec = curJob->desiredec;
978 job->desireddf = curJob->desireddf;
979 job->desiredtl = curJob->desiredtl;
980 job->usexvres = curJob->usexvres;
981 job->useccover = curJob->useccover;
982 job->pagechop = curJob->pagechop;
983 job->notify = curJob->notify;
984 job->chopthreshold = curJob->chopthreshold;
985 job->tts = curJob->tts;
986 job->killtime = curJob->killtime;
987 job->retrytime = curJob->retrytime;
988 job->sender = curJob->sender;
989 job->mailaddr = curJob->mailaddr;
990 job->jobtag = curJob->jobtag; // ???
991 job->number = curJob->number;
992 job->external = curJob->external;
993 job->modem = curJob->modem;
994 job->faxnumber = curJob->faxnumber;
995 job->tsi = curJob->tsi;
996 job->receiver = curJob->receiver;
997 job->company = curJob->company;
998 job->location = curJob->location;
999 job->voice = curJob->voice;
1000 job->fromcompany = curJob->fromcompany;
1001 job->fromlocation = curJob->fromlocation;
1002 job->fromvoice = curJob->fromvoice;
1003 job->regarding = curJob->regarding;
1004 job->comments = curJob->comments;
1005 job->jobtype = curJob->jobtype;
1006 job->tagline = curJob->tagline;
1007 job->client = remotehost;
1008 job->doneop = curJob->doneop;
1009 job->queued = curJob->queued;
1010 jobs[jobid] = job;
1011 curJob = job;
1012 return (true);
1013 }
1014
1015 /*
1016 * Update the job's state on disk.
1017 */
1018 bool
1019 HylaFAXServer::updateJobOnDisk(Job& job, fxStr& emsg)
1020 {
1021 if (job.fd < 0)
1022 {
1023 job.fd = Sys::open("/" | job.qfile, O_RDWR|O_CREAT, jobProtection);
1024 if (job.fd < 0)
1025 {
1026 emsg = "Cannot open/create job description file /" | job.qfile;
1027 return false;
1028 }
1029 }
1030
1031 if (lockJob(job, LOCK_EX, emsg)) {
1032 // XXX don't update in place, use temp file and rename
1033 job.writeQFile();
1034 unlockJob(job);
1035 return (true);
1036 } else
1037 return (false);
1038 }
1039
1040 /*
1041 * Look for a job in the in-memory cache.
1042 */
1043 Job*
1044 HylaFAXServer::findJobInMemmory(const char* jobid)
1045 {
1046 if (curJob->jobid == jobid) // fast check
1047 return (curJob);
1048 Job** jpp = (Job**) jobs.find(jobid);
1049 if (jpp)
1050 return (*jpp);
1051 return (jobid == defJob.jobid ? &defJob : (Job*) NULL);
1052 }
1053
1054 /*
1055 * Look for a job on disk and, if found, read it
1056 * into memory and return it.
1057 */
1058 Job*
1059 HylaFAXServer::findJobOnDisk(const char* jid, fxStr& emsg)
1060 {
1061 fxStr filename(fxStr::format("/" FAX_SENDDIR "/" FAX_QFILEPREF "%s", jid));
1062 struct stat sb;
1063 if (!FileCache::update(filename, sb)) {
1064 /*
1065 * Not in sendq, look in the doneq for the job.
1066 */
1067 filename = fxStr::format("/" FAX_DONEDIR "/" FAX_QFILEPREF "%s", jid);
1068 if (!FileCache::update(filename, sb)) {
1069 emsg = fxStr::format("job does not exist (%s)", strerror(errno));
1070 return (NULL);
1071 }
1072 }
1073 if (!S_ISREG(sb.st_mode)) {
1074 emsg = "job description file is not a regular file";
1075 return (NULL);
1076 }
1077 int fd = Sys::open(filename, O_RDWR);
1078 if (fd >= 0) {
1079 // XXX should we lock here???
1080 Job* req = new Job(&filename[1], fd);
1081 bool reject;
1082 if (req->readQFile(reject) && !reject) {
1083 Sys::close(req->fd), req->fd = -1;
1084 if (publicJobQ)
1085 return (req);
1086 if (req->owner == the_user)
1087 return (req);
1088 if (checkFileRights(A_READ, sb) )
1089 return (req);
1090 emsg = "Permission denied";
1091 delete req;
1092 return (NULL);
1093 }
1094 emsg = "invalid or corrupted job description file";
1095 delete req; // NB: closes fd
1096 } else
1097 emsg = fxStr::format("cannot open job description file %s (%s)",
1098 (const char*) filename, strerror(errno));
1099 return (NULL);
1100 }
1101
1102 /*
1103 * Update a job's state from the on-disk copy.
1104 */
1105 bool
1106 HylaFAXServer::updateJobFromDisk(Job& job)
1107 {
1108 bool status = false;
1109 if (lockJob(job, LOCK_SH)) {
1110 bool reject;
1111 status = (job.reReadQFile(reject) && !reject);
1112 unlockJob(job);
1113 }
1114 return (status);
1115 }
1116
1117 /*
1118 * Lock a job description file, creating it if
1119 * necessary. Errors are not expected--if one
1120 * occurs a descriptive message is returned for
1121 * transmission to the client.
1122 */
1123 bool
1124 HylaFAXServer::lockJob(Job& job, int how, fxStr& emsg)
1125 {
1126 if (job.fd < 0) {
1127 job.fd = Sys::open("/" | job.qfile, O_RDWR, 0600);
1128 if (job.fd < 0) {
1129 emsg = "Cannot open/create job description file /" | job.qfile;
1130 return (false);
1131 }
1132 }
1133 if (flock(job.fd, how | LOCK_NB) >= 0)
1134 return true;
1135
1136 if (errno == EWOULDBLOCK && lockTimeout > 0)
1137 {
1138 int r;
1139 Timeout timer;
1140
1141 timer.startTimeout(lockTimeout*1000);
1142 r = flock(job.fd, how);
1143 timer.stopTimeout();
1144
1145 if (timer.wasTimeout())
1146 logDebug("LOCKWAIT timeout: %ds", lockTimeout);
1147
1148 return (r >= 0);
1149 }
1150
1151 emsg = fxStr::format("Job file lock failed: %s", strerror(errno));
1152 Sys::close(job.fd);
1153 job.fd = -1;
1154 return (false);
1155 }
1156
1157
1158 /*
1159 * Like above, but no error message is returned.
1160 */
1161 bool
1162 HylaFAXServer::lockJob(Job& job, int how)
1163 {
1164 if (job.fd < 0)
1165 {
1166 job.fd = Sys::open("/" | job.qfile, O_RDWR, 0600);
1167 if (job.fd < 0)
1168 return false;
1169 }
1170 if (flock(job.fd, how | LOCK_NB) >= 0)
1171 return true;
1172
1173 if (errno == EWOULDBLOCK && lockTimeout > 0)
1174 {
1175 int r;
1176 Timeout timer;
1177
1178 timer.startTimeout(lockTimeout * 1000);
1179 r = flock(job.fd, how);
1180 timer.stopTimeout();
1181
1182 if (timer.wasTimeout())
1183 logDebug("LOCKWAIT timeout: %ds", lockTimeout);
1184
1185 return (r >= 0);
1186 }
1187
1188 return (false);
1189 }
1190
1191 /*
1192 * Unlock a previously-locked job.
1193 */
1194 void
1195 HylaFAXServer::unlockJob(Job& job)
1196 {
1197 if (job.fd >= 0)
1198 Sys::close(job.fd), job.fd = -1; // implicit unlock
1199 }
1200
1201 /*
1202 * Find a job either in memory (in the cache) or
1203 * on disk. If a job is found in memory and the
1204 * on-disk state is more current, update the state
1205 * in the cache.
1206 */
1207 Job*
1208 HylaFAXServer::findJob(const char* jobid, fxStr& emsg)
1209 {
1210 Job* job = findJobInMemmory(jobid);
1211 if (job) {
1212 /*
1213 * Verify job is still around (another process has
1214 * not deleted it) and if the on-disk state has
1215 * been updated, re-read the job description file.
1216 */
1217 if (!checkJobState(job)) {
1218 // We will re-check on disk in case a job was moved between queues
1219 job = NULL;
1220 emsg = "job deleted by another party";
1221 }
1222 }
1223 if (!job) {
1224 /*
1225 * We can only afford a certain amount of space,
1226 * unfortunately, there is no "bright" way to remove jobs
1227 * Ideally we'ld have an "aging" method, so the LRU job
1228 * would be the one deleted...
1229 */
1230 if (jobs.size() > 10)
1231 {
1232 JobDictIter iter(jobs);
1233 job = iter.value();
1234 jobs.remove(job->jobid);
1235 delete job;
1236 }
1237
1238 job = findJobOnDisk(jobid, emsg);
1239 if (job)
1240 jobs[job->jobid] = job;
1241 }
1242 return (job);
1243 }
1244
1245 /*
1246 * Purge all in-memory job state.
1247 */
1248 void
1249 HylaFAXServer::purgeJobs(void)
1250 {
1251 for (JobDictIter iter(jobs); iter.notDone(); iter++) {
1252 Job* job = iter.value();
1253 jobs.remove(job->jobid);
1254 delete job;
1255 }
1256 }
1257
1258 /*
1259 * Send a reply identifying the current job.
1260 */
1261 void
1262 HylaFAXServer::replyCurrentJob(const char* leader)
1263 {
1264 if (curJob->jobid == "default")
1265 reply(200, "%s (default).", leader);
1266 else
1267 reply(200, "%s jobid: %s groupid: %s.", leader,
1268 (const char*) curJob->jobid, (const char*) curJob->groupid);
1269 }
1270
1271 /*
1272 * Set the current job.
1273 */
1274 void
1275 HylaFAXServer::setCurrentJob(const char* jobid)
1276 {
1277 fxStr emsg;
1278 Job* job = findJob(jobid, emsg);
1279 if (job) {
1280 curJob = job;
1281 replyCurrentJob("Current job:");
1282 } else
1283 reply(500, "Cannot set job %s; %s.", jobid, (const char*) emsg);
1284 }
1285
1286 /*
1287 * Reset a job's state to what is currently on disk.
1288 */
1289 void
1290 HylaFAXServer::resetJob(const char* jobid)
1291 {
1292 fxStr emsg;
1293 Job* job = findJob(jobid, emsg);
1294 if (job) {
1295 if (job->jobid == "default") {
1296 initDefaultJob();
1297 reply(200, "Default job reset to initial state.");
1298 } else if (job->state != FaxRequest::state_suspended) {
1299 reply(504, "Job %s not reset; must be suspended.", jobid);
1300 } else {
1301 updateJobFromDisk(*job);
1302 struct stat sb;
1303 if (FileCache::lookup("/" | job->qfile, sb))
1304 job->lastmod = sb.st_mtime;
1305 reply(200, "Job %s reset to last state saved to disk.", jobid);
1306 }
1307 } else
1308 reply(500, "Cannot reset job %s; %s.", jobid, (const char*) emsg);
1309 }
1310
1311 /*
1312 * Common work done for many job-related commands.
1313 */
1314 Job*
1315 HylaFAXServer::preJobCmd(const char* op, const char* jobid, fxStr& emsg)
1316 {
1317 Job* job = findJob(jobid, emsg);
1318 if (job) {
1319 if (job->jobid == "default") {
1320 reply(504, "Cannot %s default job.", op);
1321 job = NULL;
1322 } else if (!IS(PRIVILEGED) && job->owner != the_user) {
1323 /*
1324 * If filesystem access gives us WRITE access
1325 * then we'll consider ourselves lucky, otherwise...
1326 */
1327 struct stat sb;
1328 if (FileCache::update(job->qfile, sb)
1329 && ! checkFileRights(W_OK, sb)) {
1330 reply(504, "Cannot %s job: %s.", op, strerror(EPERM));
1331 job = NULL;
1332 }
1333 }
1334 } else
1335 reply(500, "Cannot %s job %s; %s.", op, jobid, (const char*) emsg);
1336 return (job);
1337 }
1338
1339 /*
1340 * Delete all job state (both on disk and in memory).
1341 */
1342 void
1343 HylaFAXServer::deleteJob(const char* jobid)
1344 {
1345 fxStr emsg;
1346 Job* job = preJobCmd("delete", jobid, emsg);
1347 if (job) {
1348 const char* startdir = cwd->pathname;
1349 if (Sys::chdir("/") < 0) {
1350 reply(504, "Cannot change to base spool directory.");
1351 return;
1352 }
1353 if (job->state != FaxRequest::state_done &&
1354 job->state != FaxRequest::state_failed &&
1355 job->state != FaxRequest::state_suspended) {
1356 reply(504, "Job %s not deleted; use JSUSP first.", jobid);
1357 return;
1358 }
1359 if (!lockJob(*job, LOCK_EX, emsg)) {
1360 reply(504, "Cannot delete job: %s.", (const char*) emsg);
1361 return;
1362 }
1363 /*
1364 * Jobs in the doneq (state_done) have had their
1365 * documents converted to references (w/o links)
1366 * to the base document name; thus there is no
1367 * work to do to cleanup document state (a separate
1368 * scavenger program must deal with this since it
1369 * requires global knowledge of what jobs reference
1370 * what documents).
1371 *
1372 * Jobs that have yet to complete however hold links
1373 * to documents that must be removed. We do this here
1374 * and also notify the scheduler about our work so that
1375 * it can properly expunge imaged versions of the docs.
1376 */
1377 if (job->state == FaxRequest::state_suspended) {
1378 for (u_int i = 0, n = job->items.length(); i < n; i++) {
1379 const FaxItem& fitem = job->items[i];
1380 switch (fitem.op) {
1381 case FaxRequest::send_fax:
1382 if (sendQueuerACK(emsg, "U%s", (const char*) fitem.item) ||
1383 !job->isUnreferenced(i))
1384 break;
1385 /* ... fall thru */
1386 case FaxRequest::send_tiff_saved:
1387 case FaxRequest::send_tiff:
1388 case FaxRequest::send_pdf_saved:
1389 case FaxRequest::send_pdf:
1390 case FaxRequest::send_postscript:
1391 case FaxRequest::send_postscript_saved:
1392 case FaxRequest::send_pcl:
1393 case FaxRequest::send_pcl_saved:
1394 case FaxRequest::send_data:
1395 Sys::unlink(fitem.item);
1396 break;
1397 }
1398 }
1399 } else {
1400 // expunge any cover page documents
1401 for (u_int i = 0, n = job->items.length(); i < n; i++) {
1402 const FaxItem& fitem = job->items[i];
1403 switch (fitem.op) {
1404 case FaxRequest::send_tiff_saved:
1405 case FaxRequest::send_tiff:
1406 case FaxRequest::send_pdf_saved:
1407 case FaxRequest::send_pdf:
1408 case FaxRequest::send_postscript:
1409 case FaxRequest::send_postscript_saved:
1410 case FaxRequest::send_pcl:
1411 case FaxRequest::send_pcl_saved:
1412 if (fitem.item.findR(fitem.item.length(), ".cover"))
1413 Sys::unlink(fitem.item);
1414 break;
1415 }
1416 }
1417 }
1418 if (Sys::unlink(job->qfile) < 0)
1419 reply(504, "Deletion of queue file %s failed.", (const char*) job->qfile);
1420 if (Sys::chdir(startdir) < 0)
1421 reply(504, "Cannot change to %s spool directory.", startdir);
1422 jobs.remove(job->jobid);
1423 if (job == curJob) // make default job current
1424 curJob = &defJob;
1425 delete job; // NB: implicit unlock
1426 replyCurrentJob(fxStr::format("Job %s deleted; current job:", jobid));
1427 }
1428 }
1429
1430 /*
1431 * Common work for doing job state manipulations.
1432 */
1433 void
1434 HylaFAXServer::operateOnJob(const char* jobid, const char* what, const char* op)
1435 {
1436 fxStr emsg;
1437 Job* job = preJobCmd(what, jobid, emsg);
1438 if (job) {
1439 if (job->state == FaxRequest::state_done ||
1440 job->state == FaxRequest::state_failed) {
1441 reply(504, "Job %s not %sed; already done.", jobid, what);
1442 return;
1443 }
1444 if (sendQueuerACK(emsg, "%s%s", op, jobid))
1445 reply(200, "Job %s %sed.", jobid, what);
1446 else
1447 reply(460, "Failed to %s job %s: %s.",
1448 what, jobid, (const char*) emsg);
1449 }
1450 }
1451
1452 /*
1453 * Terminate a job, potentially aborting any call in progress.
1454 */
1455 void
1456 HylaFAXServer::killJob(const char* jobid)
1457 {
1458 operateOnJob(jobid, "kill", "K");
1459 }
1460
1461 /*
1462 * Suspend a job from being scheduled.
1463 */
1464 void
1465 HylaFAXServer::suspendJob(const char* jobid)
1466 {
1467 operateOnJob(jobid, "suspend", "X");
1468 }
1469
1470 /*
1471 * Interrupt a job from being scheduled.
1472 */
1473 void
1474 HylaFAXServer::interruptJob(const char* jobid)
1475 {
1476 operateOnJob(jobid, "interrupt", "Y");
1477 }
1478
1479 void
1480 HylaFAXServer::replyBadJob(const Job& job, Token t)
1481 {
1482 reply(504, "Cannot submit job %s; null or missing %s parameter.",
1483 (const char*) job.jobid, parmToken(t));
1484 }
1485
1486 /*
1487 * Submit a job for scheduling.
1488 */
1489 void
1490 HylaFAXServer::submitJob(const char* jobid)
1491 {
1492 fxStr emsg;
1493 Job* job = preJobCmd("submit", jobid, emsg);
1494 if (job) {
1495 if (job->state == FaxRequest::state_done ||
1496 job->state == FaxRequest::state_failed) {
1497 reply(504, "Job %s not submitted; already done.", jobid);
1498 return;
1499 }
1500 if (job->state != FaxRequest::state_suspended) {
1501 reply(504, "Job %s not submitted; use JSUSP first.", jobid);
1502 return;
1503 }
1504 if (job->number == "")
1505 replyBadJob(*job, T_DIALSTRING);
1506 else if (job->mailaddr == "")
1507 replyBadJob(*job, T_NOTIFYADDR);
1508 else if (job->sender == "")
1509 replyBadJob(*job, T_FROM_USER);
1510 else if (job->modem == "")
1511 replyBadJob(*job, T_MODEM);
1512 else if (job->client == "")
1513 replyBadJob(*job, T_CLIENT);
1514 else {
1515 if (job->external == "")
1516 job->external = job->number;
1517 if (updateJobOnDisk(*job, emsg)) {
1518 /*
1519 * NB: we don't mark the lastmod time for the
1520 * job since the scheduler should re-write the
1521 * queue file to reflect what it did with it
1522 * (e.g. what state it placed the job in).
1523 */
1524 if (sendQueuerACK(emsg, "S%s", jobid)) {
1525 reply(200, "Job %s submitted.", jobid);
1526 Job** jpp = (Job**) blankJobs.find(job->jobid);
1527 if (jpp)
1528 blankJobs.remove(job->jobid); // it's no longer blank
1529 } else
1530 reply(460, "Failed to submit job %s: %s.",
1531 jobid, (const char*) emsg);
1532 } else
1533 reply(450, "%s.", (const char*) emsg); // XXX 550?
1534 }
1535 }
1536 }
1537
1538 /*
1539 * Wait for a job to complete or for the operation
1540 * to be aborted. A data channel is opened and
1541 * job status information is returned on it. The
1542 * client can terminate this operation with an
1543 * ABOR command on the control channel; just like
1544 * a normal file transfer operation.
1545 */
1546 void
1547 HylaFAXServer::waitForJob(const char* jobid)
1548 {
1549 fxStr emsg;
1550 Job* job = findJob(jobid, emsg);
1551 if (job) {
1552 if (job->jobid == "default") {
1553 reply(504, "Cannot wait for default job.");
1554 return;
1555 }
1556 if (job->state == FaxRequest::state_done ||
1557 job->state == FaxRequest::state_failed) {
1558 reply(216, "Job %s done (already).", jobid);
1559 return;
1560 }
1561 state &= ~S_LOGTRIG; // just process events
1562 if (newTrigger(emsg, "J<%s>%04x", jobid, 1<<Trigger::JOB_DEAD)) {
1563 // XXX is lreply the right thing?
1564 lreply(216, "Waiting for job %s; use ABOR command to interrupt.",
1565 jobid);
1566 if (setjmp(urgcatch) == 0) {
1567 Dispatcher& disp = Dispatcher::instance();
1568 for (state |= S_WAITTRIG; IS(WAITTRIG); disp.dispatch()) {
1569 /*
1570 * The trigger event handlers update our notion
1571 * of the job state asynchronously so we can just
1572 * monitor the job's state variable. Beware however
1573 * that the job may get removed/moved to the doneq
1574 * while we're monitoring its status; so we cannot
1575 * blindly hold a reference to the in-memory structure.
1576 */
1577 job = findJob(jobid, emsg);
1578 if (!job || job->state == FaxRequest::state_done ||
1579 job->state == FaxRequest::state_failed)
1580 break;
1581 }
1582 reply(216, "Wait for job %s completed.", jobid);
1583 }
1584 state &= ~S_WAITTRIG;
1585 (void) cancelTrigger(emsg);
1586 } else
1587 reply(504, "Cannot register trigger: %s.", (const char*) emsg);
1588 } else
1589 reply(500, "Cannot wait for job %s; %s.", jobid, (const char*) emsg);
1590 }
1591
1592 /*
1593 * Do common work used in adding a document to a
1594 * job's set of documents that are to be sent.
1595 */
1596 bool
1597 HylaFAXServer::checkAddDocument(Job& job, Token type,
1598 const char* docname, FaxSendOp& op)
1599 {
1600 if (checkParm(job, type, A_WRITE)) {
1601 struct stat sb;
1602 if (fileAccess(docname, R_OK, sb)) {
1603 if (!docType(docname, op))
1604 reply(550, "%s: Document type not recognized.", docname);
1605 else
1606 return (true);
1607 }
1608 }
1609 return (false);
1610 }
1611
1612
1613 /*
1614 * Add a cover document to the current job's
1615 * set of documents that are to be sent.
1616 */
1617 void
1618 HylaFAXServer::addCoverDocument(Job& job, const char* docname)
1619 {
1620 FaxSendOp op;
1621 if (checkAddDocument(job, T_COVER, docname, op)) {
1622 fxStr covername = "/" FAX_DOCDIR "/cover" | job.jobid | ".cover";
1623 if (Sys::link(docname, covername) >= 0) {
1624 // XXX mark as cover page
1625 job.items.append(FaxItem(op, 0, "", &covername[1]));
1626 reply(200, "Added cover page document %s as %s.",
1627 docname, &covername[1]);
1628 job.pagehandling = ""; // force recalculation
1629 } else
1630 reply(550, "Unable to link cover page document %s to %s: %s.",
1631 docname, (const char*) covername, strerror(errno));
1632 }
1633 }
1634
1635 /*
1636 * Add a non-cover document to the current
1637 * job's set of documents that are to be sent.
1638 */
1639 void
1640 HylaFAXServer::addDocument(Job& job, const char* docname)
1641 {
1642 FaxSendOp op;
1643 if (checkAddDocument(job, T_DOCUMENT, docname, op)) {
1644 const char* cp = strrchr(docname, '/');
1645 if (!cp) // relative name, e.g. doc123
1646 cp = docname;
1647 fxStr document = fxStr::format("/" FAX_DOCDIR "%s.", cp) | job.jobid;
1648 if (Sys::link(docname, document) >= 0) {
1649 job.items.append(FaxItem(op, 0, "", &document[1]));
1650 reply(200, "Added document %s as %s.", docname, &document[1]);
1651 job.pagehandling = ""; // force recalculation
1652 } else
1653 reply(550, "Unable to link document %s to %s: %s.",
1654 docname, (const char*) document, strerror(errno));
1655 }
1656 }
1657
1658 /*
1659 * Add a polling operation to the current job.
1660 */
1661 void
1662 HylaFAXServer::addPollOp(Job& job, const char* sep, const char* pwd)
1663 {
1664 if (checkParm(job, T_POLL, A_WRITE)) {
1665 job.items.append(FaxItem(FaxRequest::send_poll, 0, sep, pwd));
1666 reply(200, "Added poll operation.");
1667 }
1668 }
1669
1670 /*
1671 * Directory interface support for querying job status.
1672 */
1673
1674 bool
1675 HylaFAXServer::isVisibleSendQFile(const char* filename, const struct stat& sb)
1676 {
1677 if (filename[0] == 'q') {
1678 fxStr emsg;
1679 Job* job = findJob(&filename[1], emsg);
1680 if (job && checkFileRights(R_OK, sb) )
1681 return true;
1682 }
1683 return false;
1684 }
1685
1686 #ifdef roundup
1687 #undef roundup
1688 #endif
1689 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1690
1691 /*
1692 * Return a compact notation for the specified
1693 * time. This notation is guaranteed to fit in
1694 * a 7-character field. We select one of 5
1695 * representations based on how close the time
1696 * is to ``now''.
1697 */
1698 const char*
1699 HylaFAXServer::compactTime(time_t t)
1700 {
1701 time_t now = Sys::now();
1702 if (t > now) { // already past
1703 static char buf[15];
1704 const struct tm* tm = cvtTime(t);
1705 if (t < roundup(now, 24*60*60)) // today, use 19:37
1706 strftime(buf, sizeof (buf), "%H:%M", tm);
1707 else if (t < now+7*24*60*60) // within a week, use Sun 6pm
1708 strftime(buf, sizeof (buf), "%a%I%p", tm);
1709 else // over a week, use 25Dec95
1710 strftime(buf, sizeof (buf), "%d%b%y", tm);
1711 return (buf);
1712 } else
1713 return ("");
1714 }
1715
1716 static const char jformat[] = {
1717 's', // A (subaddr)
1718 's', // B (passwd)
1719 's', // C (company)
1720 's', // D (totdials & maxdials)
1721 'u', // E (desiredbr)
1722 's', // F (tagline)
1723 'u', // G (desiredst)
1724 'u', // H (desireddf)
1725 'u', // I (usrpri)
1726 's', // J (jobtag)
1727 'c', // K (desiredec as symbol)
1728 's', // L (location)
1729 's', // M (mailaddr)
1730 'c', // N (desiredtl as symbol)
1731 'c', // O (useccover as symbol)
1732 's', // P (npages & total pages)
1733 'u', // Q (minbr)
1734 's', // R (receiver)
1735 's', // S (sender)
1736 's', // T (tottries & maxtries)
1737 's', // U (chopthreshold)
1738 's', // V (doneop)
1739 's', // W (commid)
1740 'c', // X (jobtype as symbol)
1741 's', // Y (tts in strftime %Y/%m/%d %H.%M.%S format)
1742 'u', // Z (tts as decimal time_t)
1743 '[', // [
1744 '\\', // \ (must have something after the backslash)
1745 ']', // ]
1746 '^', // ^
1747 '_', // _
1748 '`', // `
1749 'c', // a (state as symbol)
1750 'u', // b (ntries)
1751 's', // c (client)
1752 'u', // d (totdials)
1753 's', // e (external)
1754 'u', // f (ndials)
1755 's', // g (groupid)
1756 'c', // h (pagechop as symbol)
1757 'u', // i (pri)
1758 's', // j (jobid)
1759 's', // k (killtime)
1760 'u', // l (pagelength)
1761 's', // m (modem)
1762 'c', // n (notify as symbol)
1763 's', // o (owner)
1764 'u', // p (npages)
1765 's', // q (retrytime)
1766 'u', // r (resolution)
1767 's', // s (notice a.k.a. status)
1768 'u', // t (tottries)
1769 'u', // u (maxtries)
1770 's', // v (number a.k.a dialstring)
1771 'u', // w (pagewidth)
1772 'u', // x (maxdials)
1773 'u', // y (total pages)
1774 's', // z (tts)
1775 'c', // 0 (usexvres as symbol)
1776 };
1777
1778 /*
1779 * Print a formatted string with fields filled in from
1780 * the specified job's state. This functionality is
1781 * used to permit clients to get job state listings in
1782 * preferred formats.
1783 */
1784 void
1785 HylaFAXServer::Jprintf(fxStackBuffer& buf, const char* fmt, const Job& job)
1786 {
1787 /*
1788 * Check once to see if the client has access to
1789 * privileged job state. This typically is not
1790 * needed but doing it here eliminates the need to
1791 * do more work below (and the check should be fast).
1792 *
1793 * NB: This assumes that read access to T_DIALSTRING
1794 * implies read access to anything else in the
1795 * job state that is protected.
1796 */
1797 bool haveAccess = checkAccess(job, T_DIALSTRING, A_READ);
1798 for (const char* cp = fmt; *cp; cp++) {
1799 if (*cp == '%') {
1800 #define MAXSPEC 20
1801 char fspec[MAXSPEC];
1802 char* fp = fspec;
1803 *fp++ = '%';
1804 char c = *++cp;
1805 if (c == '-')
1806 *fp++ = c, c = *++cp;
1807 if (isdigit(c)) {
1808 do {
1809 *fp++ = c;
1810 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-3]);
1811 }
1812 if (c == '.') {
1813 do {
1814 *fp++ = c;
1815 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-2]);
1816 }
1817 if (!isalpha(c)) {
1818 if (c == '%') // %% -> %
1819 buf.put(c);
1820 else
1821 buf.fput("%.*s%c", fp-fspec, fspec, c);
1822 continue;
1823 }
1824 fp[0] = jformat[c-'A']; // printf format string
1825 fp[1] = '\0';
1826 switch (c) {
1827 case 'A':
1828 buf.fput(fspec, (const char*) job.subaddr);
1829 break;
1830 case 'B':
1831 buf.fput(fspec, haveAccess ? (const char*) job.passwd : "");
1832 break;
1833 case 'C':
1834 buf.fput(fspec, (const char*) job.company);
1835 break;
1836 case 'D':
1837 buf.fput(fspec, (const char*)fxStr::format("%2u:%-2u", job.totdials, job.maxdials));
1838 break;
1839 case 'E':
1840 buf.fput(fspec, job.desiredbr);
1841 break;
1842 case 'F':
1843 buf.fput(fspec, (const char*) job.tagline);
1844 break;
1845 case 'G':
1846 buf.fput(fspec, job.desiredst);
1847 break;
1848 case 'H':
1849 buf.fput(fspec, job.desireddf);
1850 break;
1851 case 'I':
1852 buf.fput(fspec, job.usrpri);
1853 break;
1854 case 'J':
1855 buf.fput(fspec, (const char*) job.jobtag);
1856 break;
1857 case 'K':
1858 buf.fput(fspec, "D HF"[job.desiredec]);
1859 break;
1860 case 'L':
1861 buf.fput(fspec, (const char*) job.location);
1862 break;
1863 case 'M':
1864 buf.fput(fspec, (const char*) job.mailaddr);
1865 break;
1866 case 'N':
1867 buf.fput(fspec, " P"[job.desiredtl]);
1868 break;
1869 case 'O':
1870 buf.fput(fspec, "N "[job.useccover]);
1871 break;
1872 case 'P':
1873 buf.fput(fspec, (const char*)fxStr::format("%2u:%-2u", job.npages, job.totpages));
1874 break;
1875 case 'Q':
1876 buf.fput(fspec, job.minbr);
1877 break;
1878 case 'R':
1879 buf.fput(fspec, (const char*) job.receiver);
1880 break;
1881 case 'S':
1882 buf.fput(fspec, (const char*) job.sender);
1883 break;
1884 case 'T':
1885 buf.fput(fspec, (const char*)fxStr::format("%2u:%-2u", job.tottries, job.maxtries));
1886 break;
1887 case 'U':
1888 buf.fput(fspec, (const char*)fxStr::format("%.1f", job.chopthreshold));
1889 break;
1890 case 'V':
1891 buf.fput(fspec, (const char*) job.doneop);
1892 break;
1893 case 'W':
1894 buf.fput(fspec, (const char*) job.commid);
1895 break;
1896 case 'X':
1897 buf.fput(fspec, toupper(job.jobtype[0]));
1898 break;
1899 case 'Y':
1900 { char tbuf[30]; // XXX HP C++
1901 strftime(tbuf, sizeof (tbuf), "%Y/%m/%d %H.%M.%S",
1902 IS(USEGMT) ? gmtime(&job.tts) : localtime(&job.tts));
1903 buf.fput(fspec, tbuf);
1904 }
1905 break;
1906 case 'Z':
1907 buf.fput(fspec, job.tts);
1908 break;
1909 case 'a':
1910 buf.fput(fspec, "?TPSBWRDF"[job.state]);
1911 break;
1912 case 'b':
1913 buf.fput(fspec, job.ntries);
1914 break;
1915 case 'c':
1916 buf.fput(fspec, (const char*) job.client);
1917 break;
1918 case 'd':
1919 buf.fput(fspec, job.totdials);
1920 break;
1921 case 'e':
1922 buf.fput(fspec, (const char*) job.external);
1923 break;
1924 case 'f':
1925 buf.fput(fspec, job.ndials);
1926 break;
1927 case 'g':
1928 buf.fput(fspec, (const char*) job.groupid);
1929 break;
1930 case 'h':
1931 buf.fput(fspec, " DAL"[job.pagechop]);
1932 break;
1933 case 'i':
1934 buf.fput(fspec, job.pri);
1935 break;
1936 case 'j':
1937 buf.fput(fspec, (const char*) job.jobid);
1938 break;
1939 case 'k':
1940 buf.fput(fspec, compactTime(job.killtime));
1941 break;
1942 case 'l':
1943 buf.fput(fspec, job.pagelength);
1944 break;
1945 case 'm':
1946 buf.fput(fspec, (const char*) job.modem);
1947 break;
1948 case 'n':
1949 buf.fput(fspec, " DQA"[job.notify]);
1950 break;
1951 case 'o':
1952 buf.fput(fspec, (const char*) job.owner);
1953 break;
1954 case 'p':
1955 buf.fput(fspec, job.npages);
1956 break;
1957 case 'q':
1958 buf.fput(fspec,
1959 job.retrytime == 0 ? "" : fmtTime(job.retrytime));
1960 break;
1961 case 'r':
1962 buf.fput(fspec, job.resolution);
1963 break;
1964 case 's':
1965 buf.fput(fspec, job.result.string());
1966 break;
1967 case 't':
1968 buf.fput(fspec, job.tottries);
1969 break;
1970 case 'u':
1971 buf.fput(fspec, job.maxtries);
1972 break;
1973 case 'v':
1974 buf.fput(fspec, haveAccess ? (const char*) job.number : "");
1975 break;
1976 case 'w':
1977 buf.fput(fspec, job.pagewidth);
1978 break;
1979 case 'x':
1980 buf.fput(fspec, job.maxdials);
1981 break;
1982 case 'y':
1983 buf.fput(fspec, job.totpages);
1984 break;
1985 case 'z':
1986 buf.fput(fspec, compactTime(job.tts));
1987 break;
1988 case '0':
1989 buf.fput(fspec, "N "[job.usexvres]);
1990 break;
1991 }
1992 } else
1993 buf.put(*cp);
1994 }
1995 }
1996
1997 void
1998 HylaFAXServer::Jprintf(FILE* fd, const char* fmt, const Job& job)
1999 {
2000 fxStackBuffer buf;
2001 Jprintf(buf, fmt, job);
2002 fwrite((const char*)buf, buf.getLength(), 1, fd);
2003 }
2004
2005 void
2006 HylaFAXServer::listSendQ(FILE* fd, const SpoolDir&, DIR* dir)
2007 {
2008 KeyStringArray listing;
2009 struct dirent* dp;
2010 while ((dp = readdir(dir)))
2011 if (dp->d_name[0] == 'q') {
2012 fxStr emsg;
2013 Job* job = findJob(&dp->d_name[1], emsg);
2014 if (job) {
2015 if (jobSortFormat.length() == 0) {
2016 Jprintf(fd, jobFormat, *job);
2017 fputs("\r\n", fd);
2018 } else {
2019 fxStackBuffer buf;
2020 Jprintf(buf, jobFormat, *job);
2021 fxStr content(buf, buf.getLength());
2022 buf.reset();
2023 Jprintf(buf, jobSortFormat, *job);
2024 fxStr key(buf, buf.getLength());
2025 listing.append(KeyString(key, content));
2026 }
2027 }
2028 }
2029
2030 if (listing.length() > 1)
2031 listing.qsort();
2032
2033 for (u_int i = 0; i < listing.length(); i++)
2034 {
2035 fwrite(listing[i], listing[i].length(), 1, fd);
2036 fputs("\r\n", fd);
2037 }
2038
2039 }
2040
2041 void
2042 HylaFAXServer::listSendQFile(FILE* fd, const SpoolDir& dir,
2043 const char* filename, const struct stat& sb)
2044 {
2045 fxStr emsg;
2046 Job* job = findJob(filename, emsg);
2047 if (job)
2048 Jprintf(fd, jobFormat, *job);
2049 else
2050 listUnixFile(fd, dir, filename, sb);
2051 }
2052
2053 void
2054 HylaFAXServer::nlstSendQ(FILE* fd, const SpoolDir&, DIR* dir)
2055 {
2056 struct dirent* dp;
2057 while ((dp = readdir(dir)))
2058 if (dp->d_name[0] == 'q') {
2059 fxStr emsg;
2060 Job* job = findJob(&dp->d_name[1], emsg);
2061 if (job)
2062 Jprintf(fd, "%j\r\n", *job);
2063 }
2064 }
2065
2066 void
2067 HylaFAXServer::nlstSendQFile(FILE* fd, const SpoolDir&,
2068 const char* filename, const struct stat&)
2069 {
2070 fxStr emsg;
2071 Job* job = findJob(filename, emsg);
2072 if (job)
2073 Jprintf(fd, "%j", *job);
2074 else
2075 fprintf(fd, "%s", filename);
2076 }