| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1990-1996 Sam Leffler |
| 4 |
* Copyright (c) 1991-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 |
#include "SendFaxClient.h" |
| 34 |
#include "TypeRules.h" |
| 35 |
#include "DialRules.h" |
| 36 |
#include "Array.h" |
| 37 |
#include "NLS.h" |
| 38 |
|
| 39 |
|
| 40 |
struct FileInfo : public fxObj { |
| 41 |
fxStr name; // user-specified document file |
| 42 |
fxStr temp; // converted temporary file |
| 43 |
fxStr doc; // document name on server |
| 44 |
const TypeRule* rule; |
| 45 |
|
| 46 |
FileInfo(); |
| 47 |
FileInfo(const FileInfo& other); |
| 48 |
~FileInfo(); |
| 49 |
}; |
| 50 |
fxDECLARE_ObjArray(FileInfoArray, FileInfo) |
| 51 |
|
| 52 |
struct PollRequest : public fxObj { |
| 53 |
fxStr sep; |
| 54 |
fxStr pwd; |
| 55 |
|
| 56 |
PollRequest(); |
| 57 |
PollRequest(const PollRequest& other); |
| 58 |
~PollRequest(); |
| 59 |
}; |
| 60 |
fxDECLARE_ObjArray(PollRequestArray, PollRequest) |
| 61 |
|
| 62 |
SendFaxClient::SendFaxClient() |
| 63 |
{ |
| 64 |
jobs = new SendFaxJobArray; |
| 65 |
typeRules = NULL; |
| 66 |
dialRules = NULL; |
| 67 |
files = new FileInfoArray; |
| 68 |
polls = new PollRequestArray; |
| 69 |
setup = false; |
| 70 |
|
| 71 |
setupConfig(); |
| 72 |
} |
| 73 |
|
| 74 |
SendFaxClient::~SendFaxClient() |
| 75 |
{ |
| 76 |
if (tmpFile != "") |
| 77 |
Sys::unlink(tmpFile); |
| 78 |
delete typeRules; |
| 79 |
delete dialRules; |
| 80 |
delete polls; |
| 81 |
delete files; |
| 82 |
delete jobs; |
| 83 |
} |
| 84 |
|
| 85 |
void SendFaxClient::vprintError(const char* fmt, va_list ap) |
| 86 |
{ FaxClient::vprintError(fmt, ap); } |
| 87 |
void SendFaxClient::vprintWarning(const char* fmt, va_list ap) |
| 88 |
{ FaxClient::vprintWarning(fmt, ap); } |
| 89 |
void SendFaxClient::vtraceServer(const char* fmt, va_list ap) |
| 90 |
{ FaxClient::vtraceServer(fmt, ap); } |
| 91 |
|
| 92 |
bool |
| 93 |
SendFaxClient::prepareForJobSubmissions(fxStr& emsg) |
| 94 |
{ |
| 95 |
if (senderName == "" && !setupSenderIdentity(from, emsg)) |
| 96 |
return (false); |
| 97 |
/* |
| 98 |
* Prepare documents for transmission. |
| 99 |
*/ |
| 100 |
if (typeRules == NULL) { |
| 101 |
typeRules = TypeRules::read(typeRulesFile); |
| 102 |
if (!typeRules) { |
| 103 |
emsg = NLS::TEXT("Unable to setup file typing and conversion rules"); |
| 104 |
return (false); |
| 105 |
} |
| 106 |
} |
| 107 |
typeRules->setVerbose(verbose); |
| 108 |
if (dialRules == NULL) { |
| 109 |
dialRules = new DialStringRules(dialRulesFile); |
| 110 |
dialRules->setVerbose(verbose); |
| 111 |
/* |
| 112 |
* NB: Not finding a client-side dialrules file is not fatal; it |
| 113 |
* used to generate a warning message but so many people were |
| 114 |
* confused by this that the message has been removed so I no |
| 115 |
* longer have to explain why it's not a problem. |
| 116 |
*/ |
| 117 |
(void) dialRules->parse(false); |
| 118 |
} else |
| 119 |
dialRules->setVerbose(verbose); |
| 120 |
/* |
| 121 |
* Lock down job page size information. |
| 122 |
*/ |
| 123 |
u_int i, n; |
| 124 |
for (i = 0, n = jobs->length(); i < n; i++) { |
| 125 |
SendFaxJob& job = (*jobs)[i]; |
| 126 |
if (job.getPageWidth() != 0 && job.getPageLength() != 0) |
| 127 |
continue; |
| 128 |
if (!job.setPageSize(job.getPageSize())) { |
| 129 |
emsg = NLS::TEXT("Unknown page size ") | job.getPageSize(); |
| 130 |
return (false); |
| 131 |
} |
| 132 |
} |
| 133 |
/* |
| 134 |
* NB: Not (currently) smart enough to recognize when |
| 135 |
* documents need to be reprocessed. For now we |
| 136 |
* just assume document conversions are not affected |
| 137 |
* by job state changes. |
| 138 |
*/ |
| 139 |
totalPages = 0; |
| 140 |
for (i = 0, n = files->length(); i < n; i++) |
| 141 |
if (!prepareFile((*files)[i], emsg)) |
| 142 |
return (false); |
| 143 |
/* |
| 144 |
* Prepare cover pages. |
| 145 |
*/ |
| 146 |
for (i = 0, n = jobs->length(); i < n; i++) { |
| 147 |
SendFaxJob& job = (*jobs)[i]; |
| 148 |
/* |
| 149 |
* Convert dialstrings to a displayable format. This |
| 150 |
* deals with problems like calling card access codes |
| 151 |
* getting stuck on the cover sheet and/or displayed in |
| 152 |
* status messages. |
| 153 |
*/ |
| 154 |
job.setExternalNumber(dialRules->displayNumber(job.getDialString())); |
| 155 |
/* |
| 156 |
* Suppress the cover page if we're just doing a poll; |
| 157 |
* otherwise, generate a cover sheet for each destination |
| 158 |
* This done now so that we can be sure everything is ready |
| 159 |
* to send before we setup a connection to the server. |
| 160 |
*/ |
| 161 |
if (job.getAutoCoverPage() && getNumberOfFiles() > 0) { |
| 162 |
fxStr file; |
| 163 |
if (!makeCoverPage(job, file, emsg)) |
| 164 |
return (false); |
| 165 |
job.setCoverPageFile(file, true); |
| 166 |
} |
| 167 |
} |
| 168 |
return (setup = true); |
| 169 |
} |
| 170 |
|
| 171 |
static fxStr |
| 172 |
joinargs(const char* app, const char* av[]) |
| 173 |
{ |
| 174 |
fxStr s(app); |
| 175 |
for (int i = 1; av[i] != NULL; i += 2) |
| 176 |
s.append(fxStr::format(" %s '%s'", av[i], av[i+1])); |
| 177 |
return s; |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
addarg(const char* av[], int& ac, const char* flag, const fxStr& opt) |
| 182 |
{ |
| 183 |
if (opt != "") { |
| 184 |
av[ac++] = flag; |
| 185 |
av[ac++] = opt; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
* Invoke the cover page generation program. |
| 191 |
*/ |
| 192 |
bool |
| 193 |
SendFaxClient::makeCoverPage(const SendFaxJob& job, fxStr& file, fxStr& emsg) |
| 194 |
{ |
| 195 |
const char* templ = _PATH_TMP "/sndfaxXXXXXX"; |
| 196 |
char* buff = strcpy(new char[strlen(templ) + 1], templ); |
| 197 |
int fd = Sys::mkstemp(buff); |
| 198 |
tmpFile = buff; |
| 199 |
delete [] buff; |
| 200 |
if (fd >= 0) { |
| 201 |
#define MAXARGS 128 |
| 202 |
const char* av[MAXARGS]; |
| 203 |
int ac = 0; |
| 204 |
const char* cp = strrchr(coverCmd, '/'); |
| 205 |
// NB: can't use ?: 'cuz of AIX compiler (XXX) |
| 206 |
if (cp) |
| 207 |
av[ac++] = cp+1; // program name |
| 208 |
else |
| 209 |
av[ac++] = coverCmd; |
| 210 |
addarg(av, ac, "-C", job.getCoverTemplate()); |
| 211 |
addarg(av, ac, "-D", dateFormat); |
| 212 |
addarg(av, ac, "-c", job.getCoverComments()); |
| 213 |
addarg(av, ac, "-f", senderName); |
| 214 |
addarg(av, ac, "-M", job.getMailbox()); |
| 215 |
addarg(av, ac, "-l", job.getCoverLocation()); |
| 216 |
addarg(av, ac, "-n", job.getExternalNumber()); |
| 217 |
addarg(av, ac, "-r", job.getCoverRegarding()); |
| 218 |
addarg(av, ac, "-s", job.getPageSize()); |
| 219 |
addarg(av, ac, "-t", job.getCoverName()); |
| 220 |
addarg(av, ac, "-v", job.getCoverVoiceNumber()); |
| 221 |
addarg(av, ac, "-x", job.getCoverCompany()); |
| 222 |
addarg(av, ac, "-L", job.getCoverFromLocation()); |
| 223 |
addarg(av, ac, "-N", job.getCoverFromFax()); |
| 224 |
addarg(av, ac, "-V", job.getCoverFromVoice()); |
| 225 |
addarg(av, ac, "-X", job.getCoverFromCompany()); |
| 226 |
fxStr pages; |
| 227 |
if (totalPages > 0) { |
| 228 |
pages = fxStr::format("%u", totalPages); |
| 229 |
addarg(av, ac, "-p", pages); |
| 230 |
} |
| 231 |
av[ac] = NULL; |
| 232 |
if (verbose) |
| 233 |
printf("COVER SHEET \"%s\"\n", (const char*) joinargs(coverCmd,av)); |
| 234 |
int pfd[2]; |
| 235 |
if (pipe(pfd) >= 0) { |
| 236 |
pid_t pid = fork(); |
| 237 |
switch (pid) { |
| 238 |
case -1: // error |
| 239 |
emsg = fxStr::format(NLS::TEXT("Error creating cover sheet; " |
| 240 |
"could not fork subprocess: %s"), strerror(errno)); |
| 241 |
Sys::close(pfd[1]); |
| 242 |
break; |
| 243 |
case 0: // child, exec command |
| 244 |
if (pfd[1] != STDOUT_FILENO) |
| 245 |
dup2(pfd[1], STDOUT_FILENO); |
| 246 |
// XXX should close other descriptors |
| 247 |
dup2(STDOUT_FILENO, STDERR_FILENO); |
| 248 |
Sys::execv(coverCmd, (char* const*) av); |
| 249 |
_exit(-1); |
| 250 |
/*NOTREACHED*/ |
| 251 |
default: // parent, read from pipe and wait |
| 252 |
Sys::close(pfd[1]); |
| 253 |
{ char buf[16*1024]; // XXX for HP C++ compiler |
| 254 |
int cc; |
| 255 |
while ((cc = read(pfd[0], buf, sizeof (buf))) > 0) |
| 256 |
(void) write(fd, buf, cc); |
| 257 |
} |
| 258 |
(void) Sys::close(pfd[0]); |
| 259 |
(void) Sys::close(fd); |
| 260 |
int status; |
| 261 |
if (Sys::waitpid(pid, status) == pid && status == 0) { |
| 262 |
file = tmpFile; |
| 263 |
return (true); |
| 264 |
} |
| 265 |
emsg = fxStr::format(NLS::TEXT("Error creating cover sheet; " |
| 266 |
"command was \"%s\"; exit status %x") |
| 267 |
, (const char*) joinargs(coverCmd, av) |
| 268 |
, status |
| 269 |
); |
| 270 |
break; |
| 271 |
} |
| 272 |
Sys::close(pfd[0]); |
| 273 |
} else { |
| 274 |
emsg = fxStr::format(NLS::TEXT("Error creating cover sheet; " |
| 275 |
"unable to create pipe to subprocess: %s"), strerror(errno)); |
| 276 |
} |
| 277 |
#undef MAXARGS |
| 278 |
} else |
| 279 |
emsg = fxStr::format(NLS::TEXT("%s: Can not create temporary file for cover page"), |
| 280 |
(const char*) tmpFile); |
| 281 |
Sys::unlink(tmpFile); |
| 282 |
return (false); |
| 283 |
} |
| 284 |
|
| 285 |
SendFaxJob& |
| 286 |
SendFaxClient::addJob(void) |
| 287 |
{ |
| 288 |
u_int ix = jobs->length(); |
| 289 |
jobs->resize(ix+1); |
| 290 |
(*jobs)[ix] = proto; |
| 291 |
setup = false; |
| 292 |
return ((*jobs)[ix]); |
| 293 |
} |
| 294 |
u_int SendFaxClient::getNumberOfJobs() const { return jobs->length(); } |
| 295 |
|
| 296 |
SendFaxJob* |
| 297 |
SendFaxClient::findJob(const fxStr& number, const fxStr& name) |
| 298 |
{ |
| 299 |
for (u_int i = 0, n = jobs->length(); i < n; i++) { |
| 300 |
SendFaxJob& job = (*jobs)[i]; |
| 301 |
if (job.getDialString() != number) |
| 302 |
continue; |
| 303 |
if (name != "" && job.getCoverName() == name) |
| 304 |
return (&job); |
| 305 |
} |
| 306 |
return (NULL); |
| 307 |
} |
| 308 |
|
| 309 |
SendFaxJob* |
| 310 |
SendFaxClient::findJobByTag(const fxStr& tag) |
| 311 |
{ |
| 312 |
for (u_int i = 0, n = jobs->length(); i < n; i++) { |
| 313 |
SendFaxJob& job = (*jobs)[i]; |
| 314 |
if (job.getJobTag() == tag) |
| 315 |
return (&job); |
| 316 |
} |
| 317 |
return (NULL); |
| 318 |
} |
| 319 |
|
| 320 |
void |
| 321 |
SendFaxClient::removeJob(const SendFaxJob& job) |
| 322 |
{ |
| 323 |
u_int ix = jobs->find(job); |
| 324 |
if (ix != fx_invalidArrayIndex) |
| 325 |
jobs->remove(ix); |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* Add a new file to send to each destination. |
| 330 |
*/ |
| 331 |
u_int |
| 332 |
SendFaxClient::addFile(const fxStr& filename) |
| 333 |
{ |
| 334 |
u_int ix = files->length(); |
| 335 |
files->resize(ix+1); |
| 336 |
(*files)[ix].name = filename; |
| 337 |
setup = false; |
| 338 |
return (ix); |
| 339 |
} |
| 340 |
|
| 341 |
const fxStr& |
| 342 |
SendFaxClient::getFileDocument(u_int ix) const |
| 343 |
{ |
| 344 |
return (ix < files->length() ? (*files)[ix].doc : fxStr::null); |
| 345 |
} |
| 346 |
|
| 347 |
u_int SendFaxClient::getNumberOfFiles() const { return files->length(); } |
| 348 |
|
| 349 |
u_int |
| 350 |
SendFaxClient::findFile(const fxStr& filename) const |
| 351 |
{ |
| 352 |
for (u_int i = 0, n = files->length(); i < n; i++) |
| 353 |
if ((*files)[i].name == filename) |
| 354 |
return (i); |
| 355 |
return (fx_invalidArrayIndex); |
| 356 |
} |
| 357 |
|
| 358 |
void |
| 359 |
SendFaxClient::removeFile(u_int ix) |
| 360 |
{ |
| 361 |
if (ix < files->length()) |
| 362 |
files->remove(ix); |
| 363 |
} |
| 364 |
|
| 365 |
void |
| 366 |
SendFaxClient::purgeFileConversions() |
| 367 |
{ |
| 368 |
for (u_int i = 0, n = files->length(); i < n; i++) { |
| 369 |
FileInfo& info = (*files)[i]; |
| 370 |
if (info.temp != "" && info.temp != info.name) { |
| 371 |
Sys::unlink(info.temp); |
| 372 |
info.temp = ""; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
u_int |
| 378 |
SendFaxClient::addPollRequest() |
| 379 |
{ |
| 380 |
return addPollRequest(fxStr::null, fxStr::null); |
| 381 |
} |
| 382 |
u_int |
| 383 |
SendFaxClient::addPollRequest(const fxStr& sep) |
| 384 |
{ |
| 385 |
return addPollRequest(sep, fxStr::null); |
| 386 |
} |
| 387 |
u_int |
| 388 |
SendFaxClient::addPollRequest(const fxStr& sep, const fxStr& pwd) |
| 389 |
{ |
| 390 |
u_int ix = polls->length(); |
| 391 |
polls->resize(ix+1); |
| 392 |
(*polls)[ix].sep = sep; |
| 393 |
(*polls)[ix].pwd = pwd; |
| 394 |
setup = false; |
| 395 |
return (ix); |
| 396 |
} |
| 397 |
u_int SendFaxClient::getNumberOfPollRequests() const { return polls->length(); } |
| 398 |
|
| 399 |
void |
| 400 |
SendFaxClient::getPollRequest(u_int ix, fxStr& sep, fxStr& pwd) |
| 401 |
{ |
| 402 |
if (ix < polls->length()) { |
| 403 |
sep = (*polls)[ix].sep; |
| 404 |
pwd = (*polls)[ix].pwd; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
void |
| 409 |
SendFaxClient::removePollRequest(u_int ix) |
| 410 |
{ |
| 411 |
if (ix < polls->length()) |
| 412 |
polls->remove(ix); |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* Submit documents and jobs to the server. |
| 417 |
*/ |
| 418 |
bool |
| 419 |
SendFaxClient::submitJobs(fxStr& emsg) |
| 420 |
{ |
| 421 |
if (!setup) { |
| 422 |
emsg = NLS::TEXT("Documents not prepared"); |
| 423 |
return (false); |
| 424 |
} |
| 425 |
if (!isLoggedIn()) { |
| 426 |
emsg = NLS::TEXT("Not logged in to server"); |
| 427 |
return (false); |
| 428 |
} |
| 429 |
/* |
| 430 |
* Transfer documents to the server. |
| 431 |
*/ |
| 432 |
if (!sendDocuments(emsg)) |
| 433 |
return (false); |
| 434 |
/* |
| 435 |
* Construct jobs and submit them. |
| 436 |
*/ |
| 437 |
for (u_int i = 0, n = jobs->length(); i < n; i++) { |
| 438 |
SendFaxJob& job = (*jobs)[i]; |
| 439 |
if (!job.createJob(*this, emsg)) |
| 440 |
return (false); |
| 441 |
if (!jobSubmit(job.getJobID())) { |
| 442 |
emsg = getLastResponse(); |
| 443 |
return (false); |
| 444 |
} |
| 445 |
notifyNewJob(job); // notify client |
| 446 |
} |
| 447 |
return (true); |
| 448 |
} |
| 449 |
|
| 450 |
/* |
| 451 |
* Transfer the document files to the server and |
| 452 |
* record the serve-side documents for job submission. |
| 453 |
*/ |
| 454 |
bool |
| 455 |
SendFaxClient::sendDocuments(fxStr& emsg) |
| 456 |
{ |
| 457 |
emsg = ""; |
| 458 |
for (u_int i = 0, n = files->length(); i < n; i++) { |
| 459 |
FileInfo& info = (*files)[i]; |
| 460 |
int fd = Sys::open(info.temp, O_RDONLY); |
| 461 |
if (fd < 0) { |
| 462 |
emsg = fxStr::format(info.temp | NLS::TEXT(": Can not open: %s"), |
| 463 |
strerror(errno)); |
| 464 |
return (false); // XXX |
| 465 |
} |
| 466 |
bool fileSent; |
| 467 |
if (info.rule->getResult() == TypeRule::TIFF) { |
| 468 |
fileSent = setFormat(FORM_TIFF) |
| 469 |
&& setType(TYPE_I) |
| 470 |
&& sendData(fd, &FaxClient::storeTemp, info.doc, emsg); |
| 471 |
} else if (info.rule->getResult() == TypeRule::PCL) { |
| 472 |
fileSent = setFormat(FORM_PCL) |
| 473 |
&& setType(TYPE_I) |
| 474 |
&& sendData(fd, &FaxClient::storeTemp, info.doc, emsg); |
| 475 |
} else if (info.rule->getResult() == TypeRule::PDF) { |
| 476 |
fileSent = setFormat(FORM_PDF) |
| 477 |
&& setType(TYPE_I) |
| 478 |
&& sendData(fd, &FaxClient::storeTemp, info.doc, emsg); |
| 479 |
} else { |
| 480 |
fileSent = setFormat(FORM_PS) |
| 481 |
&& setType(TYPE_I) // XXX TYPE_A??? |
| 482 |
&& sendZData(fd, &FaxClient::storeTemp, info.doc, emsg); |
| 483 |
} |
| 484 |
Sys::close(fd); |
| 485 |
if (!fileSent) { |
| 486 |
if (emsg == "") |
| 487 |
emsg = NLS::TEXT("Document transfer failed: ") | getLastResponse(); |
| 488 |
return (false); |
| 489 |
} |
| 490 |
} |
| 491 |
return (true); |
| 492 |
} |
| 493 |
|
| 494 |
/* |
| 495 |
* Default notification handler for when a new job is created. |
| 496 |
*/ |
| 497 |
void |
| 498 |
SendFaxClient::notifyNewJob(const SendFaxJob& job) |
| 499 |
{ |
| 500 |
int nfiles = files->length(); |
| 501 |
printf(P_("request id is %s (group id %s) for host %s (%u file)\n", |
| 502 |
"request id is %s (group id %s) for host %s (%u files)\n", nfiles) |
| 503 |
, (const char*) job.getJobID() |
| 504 |
, (const char*) job.getGroupID() |
| 505 |
, (const char*) getHost() |
| 506 |
, nfiles |
| 507 |
); |
| 508 |
} |
| 509 |
|
| 510 |
/* |
| 511 |
* Configuration file support. |
| 512 |
*/ |
| 513 |
#define N(a) (sizeof (a) / sizeof (a[0])) |
| 514 |
|
| 515 |
const SendFaxClient::SF_stringtag SendFaxClient::strings[] = { |
| 516 |
{ "typerules", &SendFaxClient::typeRulesFile, |
| 517 |
FAX_LIBDATA "/" FAX_TYPERULES }, |
| 518 |
{ "dialrules", &SendFaxClient::dialRulesFile, |
| 519 |
FAX_LIBDATA "/" FAX_DIALRULES }, |
| 520 |
{ "covercmd", &SendFaxClient::coverCmd, |
| 521 |
FAX_CLIENTBIN "/" "faxcover" }, |
| 522 |
{ "from", &SendFaxClient::from, NULL }, |
| 523 |
{ "dateformat", &SendFaxClient::dateFormat, NULL }, |
| 524 |
}; |
| 525 |
|
| 526 |
void |
| 527 |
SendFaxClient::setupConfig() |
| 528 |
{ |
| 529 |
for (int i = N(strings)-1; i >= 0; i--) |
| 530 |
(*this).*strings[i].p = (strings[i].def ? strings[i].def : ""); |
| 531 |
verbose = false; |
| 532 |
delete typeRules, typeRules = NULL; |
| 533 |
delete dialRules, dialRules = NULL; |
| 534 |
|
| 535 |
proto.setupConfig(); |
| 536 |
} |
| 537 |
|
| 538 |
void |
| 539 |
SendFaxClient::resetConfig() |
| 540 |
{ |
| 541 |
FaxClient::resetConfig(); |
| 542 |
setupConfig(); |
| 543 |
} |
| 544 |
|
| 545 |
bool |
| 546 |
SendFaxClient::setConfigItem(const char* tag, const char* value) |
| 547 |
{ |
| 548 |
u_int ix; |
| 549 |
if (findTag(tag, (const tags*) strings, N(strings), ix)) { |
| 550 |
(*this).*strings[ix].p = value; |
| 551 |
} else if (streq(tag, "verbose")) { |
| 552 |
setVerbose(getBoolean(value)); |
| 553 |
FaxClient::setVerbose(getVerbose()); // XXX |
| 554 |
} else if (proto.setConfigItem(tag, value)) { |
| 555 |
; |
| 556 |
} else if (!FaxClient::setConfigItem(tag, value)) |
| 557 |
return (false); |
| 558 |
return (true); |
| 559 |
} |
| 560 |
#undef N |
| 561 |
|
| 562 |
/* |
| 563 |
* Setup the sender's identity. |
| 564 |
*/ |
| 565 |
bool |
| 566 |
SendFaxClient::setupSenderIdentity(const fxStr& from, fxStr& emsg) |
| 567 |
{ |
| 568 |
FaxClient::setupUserIdentity(emsg); // client identity |
| 569 |
|
| 570 |
if (from != "") { |
| 571 |
u_int l = from.next(0, '<'); |
| 572 |
if (l == from.length()) { |
| 573 |
l = from.next(0, '('); |
| 574 |
if (l != from.length()) { // joe@foobar (Joe Schmo) |
| 575 |
setBlankMailboxes(from.head(l)); |
| 576 |
l++, senderName = from.token(l, ')'); |
| 577 |
} else { // joe |
| 578 |
setBlankMailboxes(from); |
| 579 |
if (from == getUserName()) |
| 580 |
senderName = FaxClient::getSenderName(); |
| 581 |
else |
| 582 |
senderName = ""; |
| 583 |
} |
| 584 |
} else { // Joe Schmo <joe@foobar> |
| 585 |
senderName = from.head(l); |
| 586 |
l++, setBlankMailboxes(from.token(l, '>')); |
| 587 |
} |
| 588 |
|
| 589 |
// strip leading&trailing white space and quotes |
| 590 |
senderName.remove(0, senderName.skip(0, " \t\"")); |
| 591 |
senderName.resize(senderName.skipR(senderName.length(), " \t\"")); |
| 592 |
|
| 593 |
if (senderName == "" && getNonBlankMailbox(senderName)) { |
| 594 |
/* |
| 595 |
* Mail address, but no "real name"; construct one from |
| 596 |
* the account name. Do this by first stripping anything |
| 597 |
* to the right of an '@' and then stripping any leading |
| 598 |
* uucp patch (host!host!...!user). |
| 599 |
*/ |
| 600 |
senderName.resize(senderName.next(0, '@')); |
| 601 |
senderName.remove(0, senderName.nextR(senderName.length(), '!')); |
| 602 |
} |
| 603 |
|
| 604 |
// strip and leading&trailing white space |
| 605 |
senderName.remove(0, senderName.skip(0, " \t")); |
| 606 |
senderName.resize(senderName.skipR(senderName.length(), " \t")); |
| 607 |
} else { |
| 608 |
senderName = FaxClient::getSenderName(); |
| 609 |
setBlankMailboxes(getUserName()); |
| 610 |
} |
| 611 |
fxStr mbox; |
| 612 |
if (senderName == "" || !getNonBlankMailbox(mbox)) { |
| 613 |
emsg = NLS::TEXT("Malformed (null) sender name or mail address"); |
| 614 |
return (false); |
| 615 |
} else |
| 616 |
return (true); |
| 617 |
} |
| 618 |
|
| 619 |
void SendFaxClient::setFromIdentity(const char* s) { from = s; } |
| 620 |
|
| 621 |
/* |
| 622 |
* Assign the specified string to any unspecified email |
| 623 |
* addresses used for notification mail. |
| 624 |
*/ |
| 625 |
void |
| 626 |
SendFaxClient::setBlankMailboxes(const fxStr& s) |
| 627 |
{ |
| 628 |
for (u_int i = 0, n = jobs->length(); i < n; i++) { |
| 629 |
SendFaxJob& job = (*jobs)[i]; |
| 630 |
if (job.getMailbox() == "") |
| 631 |
job.setMailbox(s); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
/* |
| 636 |
* Return the first non-null mailbox string |
| 637 |
* in the set of jobs. |
| 638 |
*/ |
| 639 |
bool |
| 640 |
SendFaxClient::getNonBlankMailbox(fxStr& s) |
| 641 |
{ |
| 642 |
for (u_int i = 0, n = jobs->length(); i < n; i++) { |
| 643 |
SendFaxJob& job = (*jobs)[i]; |
| 644 |
if (job.getMailbox() != "") { |
| 645 |
s = job.getMailbox(); |
| 646 |
return (true); |
| 647 |
} |
| 648 |
} |
| 649 |
return (false); |
| 650 |
} |
| 651 |
|
| 652 |
/* |
| 653 |
* Process a file submitted for transmission. |
| 654 |
*/ |
| 655 |
bool |
| 656 |
SendFaxClient::prepareFile(FileInfo& info, fxStr& emsg) |
| 657 |
{ |
| 658 |
info.rule = fileType(info.name, emsg); |
| 659 |
if (!info.rule) |
| 660 |
return (false); |
| 661 |
if (info.temp != "" && info.temp != info.name) |
| 662 |
Sys::unlink(info.temp); |
| 663 |
if (info.rule->getCmd() != "") { // conversion required |
| 664 |
const char* templ = _PATH_TMP "/sndfaxXXXXXX"; |
| 665 |
char* buff = strcpy(new char[strlen(templ) + 1], templ); |
| 666 |
Sys::mktemp(buff); |
| 667 |
tmpFile = buff; |
| 668 |
delete [] buff; |
| 669 |
/* |
| 670 |
* XXX **** WARNING **** XXXX |
| 671 |
* |
| 672 |
* We need to generate files according to each job's |
| 673 |
* parameters (page dimensions, resolution, etc.) but |
| 674 |
* the existing protocol does not support doing this |
| 675 |
* right so for now we assume all jobs share these |
| 676 |
* parameters and just use the values from the first |
| 677 |
* prototype job. |
| 678 |
*/ |
| 679 |
fxStr sysCmd = info.rule->getFmtdCmd(info.name, tmpFile, |
| 680 |
proto.getHResolution(), proto.getVResolution(), |
| 681 |
"1", proto.getPageSize()); |
| 682 |
if (verbose) |
| 683 |
printf("CONVERT \"%s\"\n", (const char*) sysCmd); |
| 684 |
if (system(sysCmd) != 0) { |
| 685 |
Sys::unlink(tmpFile); |
| 686 |
emsg = fxStr::format(NLS::TEXT("Error converting document; command was \"%s\""), |
| 687 |
(const char*) sysCmd); |
| 688 |
return (false); |
| 689 |
} |
| 690 |
info.temp = tmpFile; |
| 691 |
} else // already postscript, pdf, pcl, or tiff |
| 692 |
info.temp = info.name; |
| 693 |
switch (info.rule->getResult()) { |
| 694 |
case TypeRule::TIFF: |
| 695 |
countTIFFPages(info.temp); |
| 696 |
break; |
| 697 |
case TypeRule::PCL: |
| 698 |
// maybe use pclcount/pcl6count from http://www.fea.unicamp.br/pclcount/ |
| 699 |
break; |
| 700 |
case TypeRule::POSTSCRIPT: |
| 701 |
case TypeRule::PDF: |
| 702 |
estimatePostScriptPages(info.temp); |
| 703 |
break; |
| 704 |
} |
| 705 |
return (true); |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
* Return a TypeRule for the specified file. |
| 710 |
*/ |
| 711 |
const TypeRule* |
| 712 |
SendFaxClient::fileType(const char* filename, fxStr& emsg) |
| 713 |
{ |
| 714 |
struct stat sb; |
| 715 |
int fd = Sys::open(filename, O_RDONLY); |
| 716 |
if (fd < 0) { |
| 717 |
emsg = fxStr::format(NLS::TEXT("%s: Can not open file"), filename); |
| 718 |
return (NULL); |
| 719 |
} |
| 720 |
if (Sys::fstat(fd, sb) < 0) { |
| 721 |
emsg = fxStr::format(NLS::TEXT("%s: Can not stat file"), filename); |
| 722 |
Sys::close(fd); |
| 723 |
return (NULL); |
| 724 |
} |
| 725 |
if ((sb.st_mode & S_IFMT) != S_IFREG) { |
| 726 |
emsg = fxStr::format(NLS::TEXT("%s: Not a regular file"), filename); |
| 727 |
Sys::close(fd); |
| 728 |
return (NULL); |
| 729 |
} |
| 730 |
char buf[512]; |
| 731 |
int cc = Sys::read(fd, buf, sizeof (buf)); |
| 732 |
Sys::close(fd); |
| 733 |
if (cc == 0) { |
| 734 |
emsg = fxStr::format(NLS::TEXT("%s: Empty file"), filename); |
| 735 |
return (NULL); |
| 736 |
} |
| 737 |
const TypeRule* tr = typeRules->match(buf, cc); |
| 738 |
if (!tr) { |
| 739 |
emsg = fxStr::format(NLS::TEXT("%s: Can not determine file type"), filename); |
| 740 |
return (NULL); |
| 741 |
} |
| 742 |
if (tr->getResult() == TypeRule::ERROR) { |
| 743 |
emsg = fxStr::format("%s: ", filename) | tr->getErrMsg(); |
| 744 |
return (NULL); |
| 745 |
} |
| 746 |
return tr; |
| 747 |
} |
| 748 |
|
| 749 |
#include "tiffio.h" |
| 750 |
|
| 751 |
/* |
| 752 |
* Count the number of ``pages'' in a TIFF file. |
| 753 |
*/ |
| 754 |
void |
| 755 |
SendFaxClient::countTIFFPages(const char* filename) |
| 756 |
{ |
| 757 |
TIFF* tif = TIFFOpen(filename, "r"); |
| 758 |
if (tif) { |
| 759 |
do { |
| 760 |
totalPages++; |
| 761 |
} while (TIFFReadDirectory(tif)); |
| 762 |
TIFFClose(tif); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
/* |
| 767 |
* Count the number of pages in a PostScript file. |
| 768 |
* We can really only estimate the number as we |
| 769 |
* depend on the DSC comments to figure this out. |
| 770 |
*/ |
| 771 |
void |
| 772 |
SendFaxClient::estimatePostScriptPages(const char* filename) |
| 773 |
{ |
| 774 |
FILE* fd = fopen(filename, "r"); |
| 775 |
if (fd != NULL) { |
| 776 |
char line[2048]; |
| 777 |
if (fgets(line, sizeof (line)-1, fd) != NULL) { |
| 778 |
/* |
| 779 |
* We only consider ``conforming'' PostScript documents. |
| 780 |
*/ |
| 781 |
if (line[0] == '%' && line[1] == '!') { |
| 782 |
int npagecom = 0; // # %%Page comments |
| 783 |
int npages = 0; // # pages according to %%Pages comments |
| 784 |
while (fgets(line, sizeof (line)-1, fd) != NULL) { |
| 785 |
int n; |
| 786 |
if (strncmp(line, "%%Page:", 7) == 0) |
| 787 |
npagecom++; |
| 788 |
else if (sscanf(line, "%%%%Pages: %u", &n) == 1) |
| 789 |
npages += n; |
| 790 |
} |
| 791 |
/* |
| 792 |
* Believe %%Pages comments over counting of %%Page comments. |
| 793 |
*/ |
| 794 |
if (npages > 0) |
| 795 |
totalPages += npages; |
| 796 |
else if (npagecom > 0) |
| 797 |
totalPages += npagecom; |
| 798 |
} else if (memcmp(line, "%PDF", 4) == 0) { |
| 799 |
int npages = 0; // # pages according to "/Type /Page" |
| 800 |
const int slen = 12; // len of "/Type /Page" + 1 char to check for 's' |
| 801 |
char* endbuf = line+sizeof(line); |
| 802 |
|
| 803 |
rewind(fd); |
| 804 |
char *cp = line; |
| 805 |
int len; |
| 806 |
while ((len = fread(cp, 1, endbuf-cp, fd)) > slen+line-cp) { |
| 807 |
endbuf = cp+len; // Will only change on the last pass. |
| 808 |
cp = line; |
| 809 |
while ((cp = (char *) memchr((const char*) cp, '/', endbuf-cp-slen))) { |
| 810 |
if ((memcmp(cp, "/Type /Page", slen-1) == 0 && *(cp+slen-1) != 's') || |
| 811 |
(memcmp(cp, "/Type/Page", slen-2) == 0 && *(cp+slen-2) != 's')) |
| 812 |
npages++; |
| 813 |
cp++; |
| 814 |
} |
| 815 |
if ((cp = (char *) memchr((const char*) endbuf-slen, '/', slen))) { |
| 816 |
memcpy(line, cp, endbuf-cp); |
| 817 |
cp = line+(endbuf-cp); |
| 818 |
} else |
| 819 |
cp = line; |
| 820 |
} |
| 821 |
if (npages > 0) |
| 822 |
totalPages += npages; |
| 823 |
} |
| 824 |
} |
| 825 |
fclose(fd); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
FileInfo::FileInfo() |
| 830 |
{ |
| 831 |
rule = NULL; |
| 832 |
} |
| 833 |
FileInfo::FileInfo(const FileInfo& other) |
| 834 |
: fxObj(other) |
| 835 |
, name(other.name) |
| 836 |
, temp(other.temp) |
| 837 |
, rule(other.rule) |
| 838 |
{ |
| 839 |
} |
| 840 |
FileInfo::~FileInfo() |
| 841 |
{ |
| 842 |
if (temp != name) |
| 843 |
Sys::unlink(temp); |
| 844 |
} |
| 845 |
fxIMPLEMENT_ObjArray(FileInfoArray, FileInfo) |
| 846 |
|
| 847 |
PollRequest::PollRequest() {} |
| 848 |
PollRequest::~PollRequest() {} |
| 849 |
PollRequest::PollRequest(const PollRequest& other) |
| 850 |
: fxObj(other) |
| 851 |
, sep(other.sep) |
| 852 |
, pwd(other.pwd) |
| 853 |
{} |
| 854 |
fxIMPLEMENT_ObjArray(PollRequestArray, PollRequest) |