File: RecvQueue.c%2B%2B

package info (click to toggle)
hylafax 1%3A4.1.1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,400 kB
  • ctags: 7,270
  • sloc: sh: 15,895; ansic: 12,661; makefile: 1,439; cpp: 850
file content (429 lines) | stat: -rw-r--r-- 11,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*	$Id: RecvQueue.c++,v 1.2 1999/06/13 07:41:15 robert Exp $ */
/*
 * Copyright (c) 1995-1996 Sam Leffler
 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
 * HylaFAX is a trademark of Silicon Graphics
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */

/*
 * Support related to received facsimile.
 */
#include "HylaFAXServer.h"
#include "Sys.h"
#include "Socket.h"
#include "tiffio.h"

#include <ctype.h>
#include <sys/file.h>

RecvInfo::RecvInfo()
{
    beingReceived = false;
    recvTime = 0;
}
RecvInfo::RecvInfo(const char* qf)
{
    qfile = qf;
    beingReceived = false;
    recvTime = 0;
}
RecvInfo::~RecvInfo() {}

fxIMPLEMENT_StrKeyPtrValueDictionary(RecvInfoDict, RecvInfo*)

static bool
isFAXImage(TIFF* tif)
{
    uint16 w;
    if (TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &w) && w != 1)
	return (false);
    if (TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &w) && w != 1)
	return (false);
    if (!TIFFGetField(tif, TIFFTAG_COMPRESSION, &w) ||
      (w != COMPRESSION_CCITTFAX3 && w != COMPRESSION_CCITTFAX4))
	return (false);
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &w) ||
      (w != PHOTOMETRIC_MINISWHITE && w != PHOTOMETRIC_MINISBLACK))
	return (false);
    return (true);
}

/*
 * Construct receive information from a file's contents.
 */
bool
HylaFAXServer::getRecvDocStatus(RecvInfo& ri)
{
    int fd = Sys::open(ri.qfile, O_RDWR);	// RDWR for flock emulation
    if (fd < 0)
	return (false);
    /*
     * Files that are being received are locked
     * for exclusive use by faxgetty.  
     */
    ri.beingReceived = (flock(fd, LOCK_SH|LOCK_NB) < 0 && errno == EWOULDBLOCK);
    TIFF* tif = TIFFFdOpen(fd, ri.qfile, "r");
    if (!tif) {
	Sys::close(fd);
	/*
	 * File may not have an IFD written yet,
	 * if it's locked just assume so...
	 */
	return (ri.beingReceived);
    }
    /*
     * We know that faxgetty will write received
     * data in only a limited set for formats.
     */
    if (!isFAXImage(tif)) {
	TIFFClose(tif);
	return (false);
    }
    /*
     * Should be a received facsimile, build up status.
     * Note that certain information was not recorded
     * in older versions of the software; thus the careful
     * checks for certain tags and their values.
     */
    uint32 v;
#ifdef TIFFTAG_FAXRECVPARAMS
    if (TIFFGetField(tif, TIFFTAG_FAXRECVPARAMS, &v))
	ri.params.decode((u_int) v);			// page transfer params
    else {
#endif
    float vres = 3.85;					// XXX default
    if (TIFFGetField(tif, TIFFTAG_YRESOLUTION, &vres)) {
	uint16 resunit = RESUNIT_NONE;
	TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
	if (resunit == RESUNIT_INCH)
	    vres /= 25.4;
    }
    ri.params.setVerticalRes((u_int) vres);		// resolution
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &v);
    ri.params.setPageWidthInPixels((u_int) v);		// page width
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &v);
    ri.params.setPageLengthInMM((u_int)(v / vres));	// page length
#ifdef TIFFTAG_FAXRECVPARAMS
    }
#endif
    char* cp;
    if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) {
	ri.sender = cp;
	sanitize(ri.sender);
    } else
	ri.sender = "<unknown>";
#ifdef TIFFTAG_FAXSUBADDRESS
    if (TIFFGetField(tif, TIFFTAG_FAXSUBADDRESS, &cp)) {
	ri.subaddr = cp;
	sanitize(ri.subaddr);
    } else
#endif
	ri.subaddr = "";
    fxStr date;
    if (TIFFGetField(tif, TIFFTAG_DATETIME, &cp)) {	// time received
	date = cp;
	sanitize(date);
    }
    ri.time = 0;
    ri.npages = 0;					// page count
    do {
	ri.npages++;
#ifdef TIFFTAG_FAXRECVTIME
	if (TIFFGetField(tif, TIFFTAG_FAXRECVTIME, &v))
	    ri.time += (u_int) v;
#endif
    } while (TIFFReadDirectory(tif));
    TIFFClose(tif);
    return (true);
}

bool
HylaFAXServer::isVisibleRecvQFile(const char* filename, const struct stat&)
{
    return (strncmp(filename, "fax", 3) == 0);
}

RecvInfo*
HylaFAXServer::getRecvInfo(const fxStr& qfile, const struct stat& sb)
{
    RecvInfo* rip = recvq[qfile];
    if (!rip) {
	rip = new RecvInfo(qfile);
	if (!getRecvDocStatus(*rip)) {
	    delete rip;
	    return (NULL);
	}
	// NB: this will be wrong if the file is copied
	rip->recvTime = sb.st_mtime;		// time recv completed
	recvq[qfile] = rip;
    } else if (rip->beingReceived && rip->recvTime < sb.st_mtime) {
	if (!getRecvDocStatus(*rip)) {
	    recvq.remove(qfile);
	    delete rip;
	    return (NULL);
	}
	// NB: this will be wrong if the file is copied
	rip->recvTime = sb.st_mtime;		// time recv completed
    }
    return (rip);
}

void
HylaFAXServer::listRecvQ(FILE* fd, const SpoolDir& sd, DIR* dir)
{
    /*
     * Use an absolute pathname when doing file
     * lookups to improve cache locality.
     */
    fxStr path(sd.pathname);
    struct dirent* dp;
    while (dp = readdir(dir)) {
	struct stat sb;
	if (!isVisibleRecvQFile(dp->d_name, sb))
	    continue;
	fxStr qfile(path | dp->d_name);
	RecvInfo* rip;
	if (FileCache::update(qfile, sb) && (rip = getRecvInfo(qfile, sb))) {
	    Rprintf(fd, recvFormat, *rip, sb);
	    fputs("\r\n", fd);
	}
    }
}

void
HylaFAXServer::listRecvQFile(FILE* fd, const SpoolDir& dir,
    const char* filename, const struct stat& sb)
{
    RecvInfo* rip =
	getRecvInfo(fxStr::format("%s%s", dir.pathname, filename), sb);
    if (rip)
	Rprintf(fd, recvFormat, *rip, sb);
    else
	listUnixFile(fd, dir, filename, sb);
}

static const char rformat[] = {
    'A',		// A
    'B',		// B
    'C',		// C
    'D',		// D
    'E',		// E
    'F',		// F
    'G',		// G
    'H',		// H
    'I',		// I
    'J',		// J
    'K',		// K
    'L',		// L
    'M',		// M
    'N',		// N
    'O',		// O
    'P',		// P
    'Q',		// Q
    'R',		// R
    'S',		// S
    'T',		// T
    'U',		// U
    'V',		// V
    'W',		// W
#ifdef OLDPROTO_SUPPORT
    'u',		// X (beingReceived as 1 or 0)
    's',		// Y (recvTime in strftime %Y:%m:%d %H:%M:%S format)
    'u',		// Z (recvTime as decimal time_t)
#else
    'X',		// X
    'Y',		// Y
    'Z',		// Z
#endif
    '[',		// [
    '\\',		// \ (must have something after the backslash)
    ']',		// ]
    '^',		// ^
    '_',		// _
    '`',		// `
    's',		// a (subaddr)
    'u',		// b (bitrate)
    'c',		// c
    'd',		// d (data format)
    's',		// e (error a.k.a. reason)
    's',		// f (filename)
    'g',		// g
    's',		// h (time spent receiving)
    'i',		// i
    'j',		// j
    'k',		// k
    'u',		// l (pagelength)
    's',		// m (protection mode)
    'u',		// n (file size)
    's',		// o (owner)
    'u',		// p (npages)
    'q',		// q (UNIX-style protection mode)
    'u',		// r (resolution)
    's',		// s (sender TSI)
    's',		// t (time received)
    'u',		// u
    'v',		// v
    'u',		// w (pagewidth)
    'x',		// x
    'y',		// y
    's'			// z (``*'' if being received)
};

#define	rounddown(x, y)	(((x)/(y))*(y))

/*
 * Return a compact notation for the specified
 * time.  This notation is guaranteed to fit in
 * a 7-character field.  We select one of 5
 * representations based on how close the time
 * is to ``now''.
 */
const char*
HylaFAXServer::compactRecvTime(time_t t)
{
    time_t now = Sys::now();
    if (t < now) {				// in the past
	static char buf[15];
	const struct tm* tm = cvtTime(t);
	if (t > rounddown(now, 24*60*60))	// today, use 19:37
	    strftime(buf, sizeof (buf), "%H:%M", tm);
	else if (t > now-7*24*60*60)		// within a week, use Sun 6pm
	    strftime(buf, sizeof (buf), "%a%I%p", tm);
	else					// over a week, use 25Dec95
	    strftime(buf, sizeof (buf), "%d%b%y", tm);
	return (buf);
    } else
	return ("");
}

/*
 * Print a formatted string with fields filled in from
 * the specified received facsimile state.  This
 * functionality is used to permit clients to get recv
 * queue state listings in preferred formats.
 */
void
HylaFAXServer::Rprintf(FILE* fd, const char* fmt,
    const RecvInfo& ri, const struct stat& sb)
{
    for (const char* cp = fmt; *cp; cp++) {
	if (*cp == '%') {
#define	MAXSPEC	20
	    char fspec[MAXSPEC];
	    char* fp = fspec;
	    *fp++ = '%';
	    char c = *++cp;
	    if (c == '-')
		*fp++ = c, c = *++cp;
	    if (isdigit(c)) {
		do {
		    *fp++ = c;
		} while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-3]);
	    }
	    if (c == '.') {
		do {
		    *fp++ = c;
		} while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-2]);
	    }
	    if (!isalpha(c)) {
		if (c == '%')		// %% -> %
		    putc(c, fd);
		else
		    fprintf(fd, "%.*s%c", fp-fspec, fspec, c);
		continue;
	    }
	    fp[0] = rformat[c-'A'];	// printf format string
	    fp[1] = '\0';
	    switch (c) {
	    case 'a':
		fprintf(fd, fspec, (const char*) ri.subaddr);
		break;
	    case 'b':
		fprintf(fd, fspec, ri.params.bitRate());
		break;
	    case 'd':
		fprintf(fd, fspec, ri.params.dataFormatName());
		break;
	    case 'e':
		fprintf(fd, fspec, (const char*) ri.reason);
		break;
	    case 'f':
		fp = strrchr(ri.qfile, '/');
		fprintf(fd, fspec, fp ? fp+1 : (const char*) ri.qfile);
		break;
	    case 'h':
		fprintf(fd, fspec, fmtTime(ri.time));
		break;
	    case 'l':
		fprintf(fd, fspec, ri.params.pageLength());
		break;
	    case 'm':
	    case 'q':
		{ char prot[8];					// XXX HP C++
		  makeProt(sb, c == 'q', prot);
		  fprintf(fd, fspec, prot);
		}
		break;
	    case 'n':
		fprintf(fd, fspec, (u_int) sb.st_size);		// XXX
		break;
	    case 'o':
		fprintf(fd, fspec, userName((u_int) sb.st_gid));
		break;
	    case 'p':
		fprintf(fd, fspec, ri.npages);
		break;
	    case 'r':
		fprintf(fd, fspec, ri.params.verticalRes());
		break;
	    case 's':
		fprintf(fd, fspec, (const char*) ri.sender);
		break;
	    case 't':
		fprintf(fd, fspec, compactRecvTime(ri.recvTime));
		break;
	    case 'w':
		fprintf(fd, fspec, ri.params.pageWidth());
		break;
	    case 'z':
		fprintf(fd, fspec, ri.beingReceived ? "*" : " ");
		break;
#if OLDPROTO_SUPPORT
	    case 'X':
		fprintf(fd, fspec, ri.beingReceived);
		break;
	    case 'Y':
		{ char buf[30];					// XXX HP C++
		  strftime(buf, sizeof (buf), "%Y:%m:%d %H:%M:%S",
		      localtime(&ri.recvTime));
		  fprintf(fd, fspec, buf);
		}
		break;
	    case 'Z':
		fprintf(fd, fspec, (u_int) ri.recvTime);
		break;
#endif /* OLDPROTO_SUPPORT */
	    }
	} else
	    putc(*cp, fd);
    }
}