Debian Codegraph @

libhylafax/SNPPJob.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 #include "config.h"
27 #include "Sys.h"
28
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #if CONFIG_INETTRANSPORT
34 extern "C" {
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netdb.h> // XXX
38 }
39 #endif
40
41 #include "SNPPClient.h"
42 #include "FaxConfig.h"
43
44 SNPPJob::SNPPJob() {}
45 SNPPJob::SNPPJob(const SNPPJob& other)
46 : mailbox(other.mailbox)
47 , pin(other.pin)
48 , passwd(other.passwd)
49 , subject(other.subject)
50 {
51 queued = other.queued;
52 notify = other.notify;
53 holdTime = other.holdTime;
54 retryTime = other.retryTime;
55 maxRetries = other.maxRetries;
56 maxDials = other.maxDials;
57 serviceLevel = other.serviceLevel;
58 }
59 SNPPJob::~SNPPJob() {}
60
61 #define valeq(a,b) (strcasecmp(a,b)==0)
62 #define valneq(a,b,n) (strncasecmp(a,b,n)==0)
63
64 bool
65 SNPPJob::setNotification(const char* v0)
66 {
67 const char* v = v0;
68 if (strneq(v, "when", 4)) {
69 for (v += 4; isspace(*v); v++)
70 ;
71 }
72 if (valeq(v, "done"))
73 notify = when_done;
74 else if (valneq(v, "req", 3))
75 notify = when_requeued;
76 else if (valeq(v, "none") || valeq(v, "off"))
77 notify = no_notice;
78 else if (valeq(v, "default"))
79 setNotification(SNPP_DEFNOTIFY);
80 else
81 return (false);
82 return (true);
83 }
84 void SNPPJob::setNotification(PageNotify n) { notify = n; }
85 /*
86 * Create the mail address for a local user.
87 */
88 void
89 SNPPJob::setMailbox(const char* user)
90 {
91 fxStr acct(user);
92 if (acct != "" && acct.next(0, "@!") == acct.length()) {
93 static fxStr domainName;
94 if (domainName == "") {
95 char hostname[64];
96 (void) gethostname(hostname, sizeof (hostname));
97 #if CONFIG_INETTRANSPORT
98 struct hostent* hp = gethostbyname(hostname);
99 domainName = (hp ? hp->h_name : hostname);
100 #else
101 domainName = hostname;
102 #endif
103 }
104 mailbox = acct | "@" | domainName;
105 } else
106 mailbox = acct;
107 // strip leading & trailing white space
108 mailbox.remove(0, mailbox.skip(0, " \t"));
109 mailbox.resize(mailbox.skipR(mailbox.length(), " \t"));
110 }
111
112 u_int
113 SNPPJob::parseTime(const char* v)
114 {
115 char* cp;
116 u_int t = (u_int) strtoul(v, &cp, 10);
117 if (cp) {
118 while (isspace(*cp))
119 ;
120 if (strncasecmp(cp, "min", 3) == 0)
121 t *= 60;
122 else if (strncasecmp(cp, "hour", 4) == 0)
123 t *= 60*60;
124 else if (strncasecmp(cp, "day", 3) == 0)
125 t *= 24*60*60;
126 }
127 return (t);
128 }
129 void SNPPJob::setRetryTime(const char* v) { retryTime = parseTime(v); }
130 void SNPPJob::setRetryTime(u_int v) { retryTime = v; }
131
132 extern int
133 parseAtSyntax(const char* s, const struct tm& ref, struct tm& at0, fxStr& emsg);
134
135 bool
136 SNPPJob::setHoldTime(const char* s, fxStr& emsg)
137 {
138 struct tm tts;
139 time_t now = Sys::now();
140 if (!parseAtSyntax(s, *localtime(&now), tts, emsg)) {
141 emsg.insert(fxStr::format("%s: ", s));
142 return (false);
143 } else {
144 setHoldTime((u_int) mktime(&tts));
145 return (true);
146 }
147 }
148 void SNPPJob::setHoldTime(u_int v) { holdTime = v; }
149 void SNPPJob::setMaxTries(u_int n) { maxRetries = n; }
150 void SNPPJob::setMaxDials(u_int n) { maxDials = n; }
151 void SNPPJob::setPIN(const char* s) { pin = s; }
152 void SNPPJob::setPassword(const char* s) { passwd = s; }
153 void SNPPJob::setQueued(bool v) { queued = v; }
154 void SNPPJob::setSubject(const char* s) { subject = s; }
155 void SNPPJob::setServiceLevel(u_int n) { serviceLevel = n; }
156
157 #define CHECK(x) { if (!(x)) goto failure; }
158 #define CHECKCMD(x) CHECK(client.command(x) == SNPPClient::COMPLETE)
159 #define CHECKCMD2(x,y) CHECK(client.command(x,y) == SNPPClient::COMPLETE)
160 #define CHECKPARM(a,b) CHECK(client.siteParm(a,b))
161 #define IFPARM(a,b,v) { if ((b) != (v)) CHECKPARM(a,b) }
162
163 bool
164 SNPPJob::createJob(SNPPClient& client, fxStr& emsg)
165 {
166 if (holdTime != 0 && !client.setHoldTime((u_int) holdTime))
167 goto failure;
168 if (subject != "")
169 CHECKCMD2("SUBJ %s", (const char*) subject)
170 CHECKCMD2("LEVE %u", serviceLevel)
171 if (client.hasSiteCmd()) {
172 CHECKPARM("FROMUSER", client.getSenderName())
173 if (retryTime != (u_int) -1 && !client.setRetryTime(retryTime))
174 goto failure;
175 IFPARM("MODEM", client.getModem(), ""); // XXX should be per-job state
176 IFPARM("MAXDIALS", maxDials, (u_int) -1)
177 IFPARM("MAXTRIES", maxRetries, (u_int) -1)
178 CHECKPARM("MAILADDR", mailbox)
179 CHECKPARM("NOTIFY",
180 notify == when_done ? "done" :
181 notify == when_requeued ? "done+requeue" :
182 "none")
183 CHECKPARM("JQUEUE", queued ? "yes" : "no");
184 }
185 return (client.newPage(pin, passwd, jobid, emsg));
186 failure:
187 emsg = client.getLastResponse();
188 return (false);
189 }
190 #undef IFPARM
191 #undef CHECKPARM
192 #undef CHECKCMD
193 #undef CHECK
194
195 fxIMPLEMENT_ObjArray(SNPPJobArray, SNPPJob)