Debian Codegraph @

hfaxd/RecvQueue.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 /*
28 * Support related to received facsimile.
29 */
30 #include "HylaFAXServer.h"
31 #include "Sys.h"
32 #include "Socket.h"
33 #include "tiffio.h"
34
35 #include <ctype.h>
36 #include <sys/file.h>
37
38 RecvInfo::RecvInfo()
39 {
40 beingReceived = false;
41 recvTime = 0;
42 }
43 RecvInfo::RecvInfo(const char* qf)
44 {
45 qfile = qf;
46 beingReceived = false;
47 recvTime = 0;
48 }
49 RecvInfo::~RecvInfo() {}
50
51 fxIMPLEMENT_StrKeyPtrValueDictionary(RecvInfoDict, RecvInfo*)
52
53
54 /*
55 * This tests whether the tif file is a "fax" image.
56 * Traditional fax images are MH, MR, MMR, but some can
57 * be JPEG, JBIG, and possibly others. So we use
58 * TIFFTAG_FAXRECVPARAMS as a "fax" image identifier,
59 * and if it's not there, then we resort to traditional
60 * tactics.
61 */
62 static bool
63 isFAXImage(TIFF* tif)
64 {
65 #ifdef TIFFTAG_FAXRECVPARAMS
66 uint32 v;
67 if (TIFFGetField(tif, TIFFTAG_FAXRECVPARAMS, &v) && v != 0)
68 return (true);
69 #endif
70 uint16 w;
71 if (TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &w) && w != 1)
72 return (false);
73 if (TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &w) && w != 1)
74 return (false);
75 if (!TIFFGetField(tif, TIFFTAG_COMPRESSION, &w) ||
76 (w != COMPRESSION_CCITTFAX3 && w != COMPRESSION_CCITTFAX4))
77 return (false);
78 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &w) ||
79 (w != PHOTOMETRIC_MINISWHITE && w != PHOTOMETRIC_MINISBLACK))
80 return (false);
81 return (true);
82 }
83
84 /*
85 * Construct receive information from a file's contents.
86 */
87 bool
88 HylaFAXServer::getRecvDocStatus(RecvInfo& ri)
89 {
90 int fd = Sys::open(ri.qfile, O_RDWR); // RDWR for flock emulation
91 if (fd < 0)
92 return (false);
93 /*
94 * Files that are being received are locked
95 * for exclusive use by faxgetty.
96 */
97 ri.beingReceived = (flock(fd, LOCK_SH|LOCK_NB) < 0 && errno == EWOULDBLOCK);
98 TIFF* tif = TIFFFdOpen(fd, ri.qfile, "r");
99 if (!tif) {
100 Sys::close(fd);
101 /*
102 * File may not have an IFD written yet,
103 * if it's locked just assume so...
104 */
105 return (ri.beingReceived);
106 }
107 /*
108 * We know that faxgetty will write received
109 * data in only a limited set for formats.
110 */
111 if (!isFAXImage(tif)) {
112 TIFFClose(tif);
113 return (false);
114 }
115 /*
116 * Should be a received facsimile, build up status.
117 * Note that certain information was not recorded
118 * in older versions of the software; thus the careful
119 * checks for certain tags and their values.
120 */
121 uint32 v;
122 #ifdef TIFFTAG_FAXRECVPARAMS
123 if (TIFFGetField(tif, TIFFTAG_FAXRECVPARAMS, &v))
124 ri.params.decode((u_int) v); // page transfer params
125 else {
126 #endif
127 float vres = 3.85; // XXX default
128 if (TIFFGetField(tif, TIFFTAG_YRESOLUTION, &vres)) {
129 uint16 resunit = RESUNIT_INCH; // TIFF spec default
130 TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
131 if (resunit == RESUNIT_INCH)
132 vres /= 25.4;
133 if (resunit == RESUNIT_NONE)
134 vres /= 720.0; // postscript units ?
135 }
136 float hres = 8.03; // XXX default
137 if (TIFFGetField(tif, TIFFTAG_XRESOLUTION, &hres)) {
138 uint16 resunit = RESUNIT_INCH; // TIFF spec default
139 TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
140 if (resunit == RESUNIT_INCH)
141 hres /= 25.4;
142 if (resunit == RESUNIT_NONE)
143 hres /= 720.0; // postscript units ?
144 }
145 ri.params.setRes((u_int) hres, (u_int) vres); // resolution
146 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &v);
147 ri.params.setPageWidthInPixels((u_int) v); // page width
148 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &v);
149 ri.params.setPageLengthInMM((u_int)(v / vres)); // page length
150 #ifdef TIFFTAG_FAXRECVPARAMS
151 }
152 #endif
153 char* cp;
154 #ifdef TIFFTAG_FAXDCS
155 if (TIFFGetField(tif, TIFFTAG_FAXDCS, &cp) && strncmp(cp, "00 00 00", 8) != 0) {
156 // cannot trust br from faxdcs as V.34-Fax does not provide it there
157 u_int brhold = ri.params.br;
158 fxStr faxdcs(cp);
159 sanitize(faxdcs);
160 ri.params.asciiDecode((const char*) faxdcs); // params per Table 2/T.30
161 ri.params.setFromDCS(ri.params);
162 ri.params.br = brhold;
163 }
164 #endif
165 ri.sender = "";
166 CallID empty_callid;
167 ri.callid = empty_callid;
168 if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) {
169 while (cp[0] != '\0' && cp[0] != '\n') { // sender
170 ri.sender.append(cp[0]);
171 cp++;
172 }
173 sanitize(ri.sender);
174 u_int i = 0;
175 while (cp[0] == '\n') {
176 cp++;
177 ri.callid.resize(i+1);
178 while (cp[0] != '\0' && cp[0] != '\n') {
179 ri.callid[i].append(cp[0]);
180 cp++;
181 }
182 sanitize(ri.callid[i]);
183 i++;
184 }
185 } else
186 ri.sender = "<unknown>";
187 #ifdef TIFFTAG_FAXSUBADDRESS
188 if (TIFFGetField(tif, TIFFTAG_FAXSUBADDRESS, &cp)) {
189 ri.subaddr = cp;
190 sanitize(ri.subaddr);
191 } else
192 #endif
193 ri.subaddr = "";
194 fxStr date;
195 if (TIFFGetField(tif, TIFFTAG_DATETIME, &cp)) { // time received
196 date = cp;
197 sanitize(date);
198 }
199 ri.time = 0;
200 ri.npages = 0; // page count
201 do {
202 ri.npages++;
203 #ifdef TIFFTAG_FAXRECVTIME
204 if (TIFFGetField(tif, TIFFTAG_FAXRECVTIME, &v))
205 ri.time += (u_int) v;
206 #endif
207 } while (TIFFReadDirectory(tif));
208 TIFFClose(tif);
209 return (true);
210 }
211
212 bool
213 HylaFAXServer::isVisibleRecvQFile(const char* filename, const struct stat& sb)
214 {
215 return (strncmp(filename, "fax", 3) == 0) && (publicRecvQ || checkFileRights(R_OK, sb) );
216 }
217
218 RecvInfo*
219 HylaFAXServer::getRecvInfo(const fxStr& qfile, const struct stat& sb)
220 {
221 RecvInfo* rip = recvq[qfile];
222 if (!rip) {
223 rip = new RecvInfo(qfile);
224 if (!getRecvDocStatus(*rip)) {
225 delete rip;
226 return (NULL);
227 }
228 // NB: this will be wrong if the file is copied
229 rip->recvTime = sb.st_mtime; // time recv completed
230 recvq[qfile] = rip;
231 } else if (rip->beingReceived && rip->recvTime < sb.st_mtime) {
232 if (!getRecvDocStatus(*rip)) {
233 recvq.remove(qfile);
234 delete rip;
235 return (NULL);
236 }
237 // NB: this will be wrong if the file is copied
238 rip->recvTime = sb.st_mtime; // time recv completed
239 }
240 return (rip);
241 }
242
243 void
244 HylaFAXServer::listRecvQ(FILE* fd, const SpoolDir& sd, DIR* dir)
245 {
246 KeyStringArray listing;
247
248 /*
249 * Use an absolute pathname when doing file
250 * lookups to improve cache locality.
251 */
252 fxStr path(sd.pathname);
253 struct dirent* dp;
254 while ((dp = readdir(dir))) {
255 struct stat sb;
256 fxStr qfile(path | dp->d_name);
257 RecvInfo* rip;
258 if (!FileCache::update(qfile, sb))
259 continue;
260 if (!isVisibleRecvQFile(dp->d_name, sb))
261 continue;
262 rip = getRecvInfo(qfile, sb);
263 if (rip) {
264 if (recvSortFormat.length() == 0) {
265 Rprintf(fd, recvFormat, *rip, sb);
266 fputs("\r\n", fd);
267 } else {
268 fxStackBuffer buf;
269 Rprintf(buf, recvFormat, *rip, sb);
270 fxStr content(buf, buf.getLength());
271 buf.reset();
272 Rprintf(buf, recvSortFormat, *rip, sb);
273 fxStr key(buf, buf.getLength());
274 listing.append(KeyString(key, content));
275 }
276 }
277 }
278
279 if (listing.length() > 1)
280 listing.qsort();
281
282 for (u_int i = 0; i < listing.length(); i++)
283 {
284 fwrite(listing[i], listing[i].length(), 1, fd);
285 fputs("\r\n", fd);
286 }
287 }
288
289 void
290 HylaFAXServer::listRecvQFile(FILE* fd, const SpoolDir& dir,
291 const char* filename, const struct stat& sb)
292 {
293 RecvInfo* rip =
294 getRecvInfo(fxStr::format("%s%s", dir.pathname, filename), sb);
295 if (rip)
296 Rprintf(fd, recvFormat, *rip, sb);
297 else
298 listUnixFile(fd, dir, filename, sb);
299 }
300
301 static const char rformat[] = {
302 'A', // A
303 'B', // B
304 'C', // C
305 'D', // D
306 'E', // E
307 'F', // F
308 'G', // G
309 'H', // H
310 'I', // I
311 'J', // J
312 'K', // K
313 'L', // L
314 'M', // M
315 'N', // N
316 'O', // O
317 'P', // P
318 'Q', // Q
319 'R', // R
320 'S', // S
321 'T', // T
322 'U', // U
323 'V', // V
324 'W', // W
325 'X', // X
326 's', // Y (tts in strftime %Y/%m/%d %H.%M.%S format)
327 'u', // Z (tts as decimal time_t)
328 '[', // [
329 '\\', // \ (must have something after the backslash)
330 ']', // ]
331 '^', // ^
332 '_', // _
333 '`', // `
334 's', // a (subaddr)
335 'u', // b (bitrate)
336 'c', // c
337 's', // d (data format)
338 's', // e (error a.k.a. reason)
339 's', // f (filename)
340 'g', // g
341 's', // h (time spent receiving)
342 's', // i (CIDName)
343 's', // j (CIDNumber)
344 'k', // k
345 'u', // l (pagelength)
346 's', // m (protection mode)
347 'u', // n (file size)
348 's', // o (owner)
349 'u', // p (npages)
350 'q', // q (UNIX-style protection mode)
351 'u', // r (resolution)
352 's', // s (sender TSI)
353 's', // t (time received)
354 'u', // u
355 'v', // v
356 'u', // w (pagewidth)
357 'x', // x
358 'y', // y
359 's' // z (``*'' if being received)
360 };
361
362 #define rounddown(x, y) (((x)/(y))*(y))
363
364 /*
365 * Return a compact notation for the specified
366 * time. This notation is guaranteed to fit in
367 * a 7-character field. We select one of 5
368 * representations based on how close the time
369 * is to ``now''.
370 */
371 const char*
372 HylaFAXServer::compactRecvTime(time_t t)
373 {
374 time_t now = Sys::now();
375 if (t < now) { // in the past
376 static char buf[15];
377 const struct tm* tm = cvtTime(t);
378 if (t > rounddown(now, 24*60*60)) // today, use 19:37
379 strftime(buf, sizeof (buf), "%H:%M", tm);
380 else if (t > now-7*24*60*60) // within a week, use Sun 6pm
381 strftime(buf, sizeof (buf), "%a%I%p", tm);
382 else // over a week, use 25Dec95
383 strftime(buf, sizeof (buf), "%d%b%y", tm);
384 return (buf);
385 } else
386 return ("");
387 }
388
389 /*
390 * Print a formatted string with fields filled in from
391 * the specified received facsimile state. This
392 * functionality is used to permit clients to get recv
393 * queue state listings in preferred formats.
394 */
395 void
396 HylaFAXServer::Rprintf(fxStackBuffer& buf, const char* fmt,
397 const RecvInfo& ri, const struct stat& sb)
398 {
399 for (const char* cp = fmt; *cp; cp++) {
400 if (*cp == '%') {
401 #define MAXSPEC 20
402 char fspec[MAXSPEC];
403 char* fp = fspec;
404 *fp++ = '%';
405 char c = *++cp;
406 if (c == '-')
407 *fp++ = c, c = *++cp;
408 if (isdigit(c)) {
409 do {
410 *fp++ = c;
411 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-3]);
412 }
413 if (c == '.') {
414 do {
415 *fp++ = c;
416 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-2]);
417 }
418 if (!isalpha(c)) {
419 if (c == '%') // %% -> %
420 buf.put(c);
421 else
422 buf.fput("%.*s%c", fp-fspec, fspec, c);
423 continue;
424 }
425 fp[0] = rformat[c-'A']; // printf format string
426 fp[1] = '\0';
427 switch (c) {
428 case 'a':
429 buf.fput(fspec, (const char*) ri.subaddr);
430 break;
431 case 'b':
432 buf.fput(fspec, ri.params.bitRate());
433 break;
434 case 'd':
435 buf.fput(fspec, ri.params.dataFormatName());
436 break;
437 case 'e':
438 buf.fput(fspec, (const char*) ri.reason);
439 break;
440 case 'f':
441 fp = (char *) strrchr(ri.qfile, '/');
442 buf.fput(fspec, fp ? fp+1 : (const char*) ri.qfile);
443 break;
444 case 'h':
445 buf.fput(fspec, fmtTime(ri.time));
446 break;
447 case 'i':
448 buf.fput(fspec, ri.callid.size() > CallID::NAME ? (const char*) ri.callid.id(CallID::NAME) : "");
449 break;
450 case 'j':
451 buf.fput(fspec, ri.callid.size() > CallID::NUMBER ? (const char*) ri.callid.id(CallID::NUMBER) : "");
452 break;
453 case 'l':
454 buf.fput(fspec, ri.params.pageLength());
455 break;
456 case 'm':
457 case 'q':
458 { char prot[8]; // XXX HP C++
459 makeProt(sb, c == 'q', prot);
460 buf.fput(fspec, prot);
461 }
462 break;
463 case 'n':
464 buf.fput(fspec, (u_int) sb.st_size); // XXX
465 break;
466 case 'o':
467 buf.fput(fspec, userName((u_int) sb.st_gid));
468 break;
469 case 'p':
470 buf.fput(fspec, ri.npages);
471 break;
472 case 'r':
473 buf.fput(fspec, ri.params.verticalRes());
474 break;
475 case 's':
476 buf.fput(fspec, (const char*) ri.sender);
477 break;
478 case 't':
479 buf.fput(fspec, compactRecvTime(ri.recvTime));
480 break;
481 case 'w':
482 buf.fput(fspec, ri.params.pageWidth());
483 break;
484 case 'z':
485 buf.fput(fspec, ri.beingReceived ? "*" : " ");
486 break;
487 case 'Y':
488 { char tbuf[30]; // XXX HP C++
489 strftime(tbuf, sizeof (tbuf), "%Y/%m/%d %H.%M.%S",
490 IS(USEGMT) ? gmtime(&ri.recvTime) : localtime(&ri.recvTime));
491 buf.fput(fspec, tbuf);
492 }
493 break;
494 case 'Z':
495 buf.fput(fspec, ri.recvTime);
496 break;
497 }
498 } else
499 buf.put(*cp);
500 }
501 }
502
503 void
504 HylaFAXServer::Rprintf(FILE* fd, const char* fmt,
505 const RecvInfo& ri, const struct stat& sb)
506 {
507 fxStackBuffer buf;
508 Rprintf(buf, fmt, ri, sb);
509 fwrite((const char*)buf, buf.getLength(), 1, fd);
510 }