File: faxalter.c%2B%2B

package info (click to toggle)
hylafax 2%3A6.0.5-4.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,776 kB
  • ctags: 7,663
  • sloc: sh: 15,158; ansic: 13,231; makefile: 1,543; cpp: 781; awk: 529
file content (340 lines) | stat: -rw-r--r-- 9,099 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
/*	$Id$ */
/*
 * Copyright (c) 1990-1996 Sam Leffler
 * Copyright (c) 1991-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.
 */
#include "FaxClient.h"
#include "Sys.h"
#include "NLS.h"
#include "config.h"

extern int
parseAtSyntax(const char* s, const struct tm& ref, struct tm& at0, fxStr& emsg);

class faxAlterApp : public FaxClient {
private:
    bool	groups;			// group or job id's
    fxStr	script;			// commands to send for each job

    void usage();
public:
    faxAlterApp();
    ~faxAlterApp();

    int run(int argc, char** argv);
    bool duplicate ();
};
faxAlterApp::faxAlterApp() { groups = false; }
faxAlterApp::~faxAlterApp() {}

bool faxAlterApp::duplicate ()
{
    /*
     * Before we dup the job, we need to get the list of documents
     * Documents are sent as continuations to the 213, like:
     * -> JPARM document
     * 213-PS docq/doc281.ps
     * 213-PS docq/doc283.ps
     * 213-PS docq/doc284.ps
     * 213 End of documents.
     */
    command("JPARM document");
    fxStr docs = getLastContinuation();

    fxStr jid, gid, emsg;
    if (! newJob(jid, gid, emsg) ) {
	printError("%s", (const char*) emsg);
	return false;
    }

    for ( u_int pos = 0, next; (next = docs.next(pos, '\n')) < docs.length(); )
    {
	/*
	 * The document is in the form "<TYPE> <filename>"
	 * - separate it and resubmit the document
	 */
	fxStr tmp = docs.extract(pos, next-pos);
	int c = tmp.find(0, " ");
	if (c)
	    jobDocument(&tmp[c+1]);
	pos = next+1;
    }

    return true;
}

int
faxAlterApp::run(int argc, char** argv)
{
    resetConfig();
    readConfig(FAX_SYSCONF);
    readConfig(FAX_USERCONF);

    fxStr emsg;
    time_t now = Sys::now();
    struct tm tts = *localtime(&now);
    struct tm when;
    bool useadmin = false;
    bool resubmit = false;
    bool errorexit = true;
    int rc;

    int c;
    while ((c = Sys::getopt(argc, argv, "Ca:d:h:k:m:n:P:t:ADQRgprv")) != -1)
	switch (c) {
	case 'C':
	    errorexit = false;
	case 'A':			// connect with administrative privileges
	    useadmin = true;
	    break;
	case 'D':			// set notification to when done
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("NOTIFY DONE\n");
	    break;
	case 'Q':			// no notification (quiet)
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("NOTIFY DONE\n");
	    break;
	case 'R':			// set notification to when requeued
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("NOTIFY DONE+REQUEUE\n");
	    break;
	case 'a':			// send at specified time
	    if (strcasecmp(optarg, "NOW")) {
		if (!parseAtSyntax(optarg, *localtime(&now), tts, emsg)) {
		    printError("%s", (const char*) emsg);
		    return (1);
		}
		now = mktime(&tts);
		when = *gmtime(&now);	// NB: must be relative to GMT
		script.append(groups ? "JGPARM " : "JPARM ");
		script.append("SENDTIME ");
		/* Block here is for scope in the switch */
		{
		    fxStr tmpbuf = fxStr::format(
			"%d%02d%02d%02d%02d"
				, when.tm_year+1900
				, when.tm_mon+1
				, when.tm_mday
				, when.tm_hour
				, when.tm_min
			    );
		    script.append(tmpbuf);
		    script.append("\n");
		}
	    } else {
		script.append(groups ? "JGPARM " : "JPARM ");
		script.append("SENDTIME NOW\n");
	    }
	    break;
	case 'd':			// destination number
            script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("EXTERNAL ");
            script.append(optarg);
            script.append("\n");
            script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("DIALSTRING ");
            script.append(optarg);
            script.append("\n");
	    break;
	case 'g':			// apply to groups, not jobs
	    groups = true;
	    break;
	case 'h':			// server's host
	    setHost(optarg);
	    break;
	case 'k':			// kill job at specified time
	    if (!parseAtSyntax(optarg, tts, when, emsg)) {
		printError("%s", optarg, (const char*) emsg);
		return (1);
	    }
	    /* Block here is for scope in the switch */
	    {
	    	time_t tv = mktime(&when) - now;
		script.append(groups ? "JGPARM " : "JPARM ");
		script.append("LASTTIME ");
		fxStr tmpbuf = fxStr::format("%02d%02d%02d"
		     , tv/(24*60*60)
		     , (tv/(60*60))%24
		     , (tv/60)%60
		    );
		script.append(tmpbuf);
		script.append("\n");
	    }
	    break;
	case 'm':			// modem
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("MODEM ");
	    script.append(optarg);
	    script.append("\n");
	    break;
	case 'n':			// set notification
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("NOTIFY ");
	    if (strcasecmp(optarg, "done") == 0) {
		script.append("DONE\n");
	    } else if (strcasecmp(optarg, "requeued") == 0) {
		script.append("DONE+REQUEUE\n");
	    } else {
		script.append(optarg);
		script.append("\n");
	    }
	    break;
	case 'p':			// send now (push)
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("SENDTIME NOW\n");
	    break;
	case 'P':			// scheduling priority
	    if ((u_int) atoi(optarg) > 255)
		fxFatal("Invalid job priority %s;"
		    " values must be in the range [0,255]", optarg);
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("SCHEDPRI ");
	    script.append(optarg);
            script.append("\n");
	    break;
	case 'r':
	    resubmit = true;
	    break;
	case 't':			// set max number of retries
	    if (atoi(optarg) < 0)
		fxFatal("Bad number of retries for -t option: %s", optarg);
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("MAXDIALS ");
	    script.append(optarg);
            script.append("\n");
	    break;
	case 'v':			// trace protocol
	    setVerbose(true);
	    break;
	case 'Z':
	    script.append(groups ? "JGPARM " : "JPARM ");
	    script.append("PAGERANGE \"");
	    script.append(optarg);
	    script.append("\"\n");
	    break;
	case '?':
	    usage();
	}
    if (optind >= argc)
	usage();
    if (script == "" && !resubmit)
	fxFatal(_("No job parameters specified for alteration."));
    if (callServer(emsg)) {
	if (login(NULL, emsg) &&
	    (!useadmin || admin(NULL, emsg))) {
	    for (; optind < argc; optind++) {
		const char* jobid = argv[optind];
		if (setCurrentJob(jobid)) {
		    /*
		     * We take the approach that if we can't do the work on a
		     * job, we continue on to the next
		     */
		    if (resubmit) {
			if (! duplicate()) {
			    rc = 3;
			    if (errorexit)
				break;
			    else
				continue;
			}
			const char* old_job = jobid;
			jobid = getCurrentJob();
			printf(_("Job %s: duplicated as job %s.\n"), old_job, jobid);
		    } else if (! jobSuspend(jobid)) {
			emsg = getLastResponse();
			printError("%s", (const char*) emsg);
			rc = 3;
			if (errorexit)
			    break;
			else
			    continue;
		    }
		    if (!runScript(script, script.length(), "<stdin>", emsg)) {
			printError("%s", (const char*) emsg);
			rc = 3;
			if (errorexit)
			    break;
			else
			    continue;
		    }
		    if (!jobSubmit(jobid)) {
			emsg = getLastResponse();
			printError("%s", (const char*) emsg);
			rc = 3;
			if (errorexit)
			    break;
			else
			    continue;
		    }
		    printf(_("Job %s: done.\n"), jobid);
		} else {
		    emsg = getLastResponse();
		    printError("%s", (const char*) emsg);
		    rc = 3;
		    if (errorexit)
			break;
		    else
			continue;
		}
	    }
	} else {
	    printError("%s", (const char*) emsg);
	    rc = 2;
	}
	hangupServer();
    } else {
	printError("%s", (const char*) emsg);
	rc = 2;
    }
    return (rc);
}

void
faxAlterApp::usage()
{
    fxFatal(_("usage: faxalter [-C]"
      " [-h server-host]"
      " [-a time]"
      " [-d number]"
      " [-k time]"
      " [-m modem]"
      " [-n notify]"
      " [-P priority]"
      " [-t tries]"
      " [-A]"
      " [-g]"
      " [-p]"
      " [-r]"
      " [-v]"
      " [-DQR]"
      " jobID..."));
}

int
main(int argc, char** argv)
{
    NLS::Setup("hylafax-clients");
    faxAlterApp app;
    return (app.run(argc, argv));
}