]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/mailto.c
2 * "$Id: mailto.c 4961 2006-01-20 22:19:13Z mike $"
4 * "mailto" notifier for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for the mailto notifier.
27 * email_message() - Email a notification message.
28 * load_configuration() - Load the mailto.conf file.
29 * pipe_sendmail() - Open a pipe to sendmail...
30 * print_attributes() - Print the attributes in a request...
34 * Include necessary headers...
37 #include <cups/cups.h>
38 #include <cups/i18n.h>
39 #include <cups/string.h>
49 char mailtoCc
[1024]; /* Cc email address */
50 char mailtoFrom
[1024]; /* From email address */
51 char mailtoReplyTo
[1024]; /* Reply-To email address */
52 char mailtoSubject
[1024]; /* Subject prefix */
53 char mailtoSMTPServer
[1024]; /* SMTP server to use */
54 char mailtoSendmail
[1024]; /* Sendmail program to use */
61 void email_message(const char *to
, const char *subject
,
63 int load_configuration(void);
64 cups_file_t
*pipe_sendmail(const char *to
);
65 void print_attributes(ipp_t
*ipp
, int indent
);
69 * 'main()' - Main entry for the mailto notifier.
72 int /* O - Exit status */
73 main(int argc
, /* I - Number of command-line arguments */
74 char *argv
[]) /* I - Command-line arguments */
76 int i
; /* Looping var */
77 ipp_t
*msg
; /* Event message from scheduler */
78 ipp_state_t state
; /* IPP event state */
79 char *subject
, /* Subject for notification message */
80 *text
; /* Text for notification message */
81 cups_lang_t
*lang
; /* Language info */
82 char temp
[1024]; /* Temporary string */
83 int templen
; /* Length of temporary string */
84 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
85 struct sigaction action
; /* POSIX sigaction data */
86 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
90 * Don't buffer stderr...
96 * Ignore SIGPIPE signals...
100 sigset(SIGPIPE
, SIG_IGN
);
101 #elif defined(HAVE_SIGACTION)
102 memset(&action
, 0, sizeof(action
));
103 action
.sa_handler
= SIG_IGN
;
104 sigaction(SIGPIPE
, &action
, NULL
);
106 signal(SIGPIPE
, SIG_IGN
);
107 #endif /* HAVE_SIGSET */
110 * Validate command-line options...
115 fputs("Usage: mailto mailto:user@domain.com notify-user-data\n", stderr
);
119 if (strncmp(argv
[1], "mailto:", 7))
121 fprintf(stderr
, "ERROR: Bad recipient \"%s\"!\n", argv
[1]);
125 fprintf(stderr
, "DEBUG: argc=%d\n", argc
);
126 for (i
= 0; i
< argc
; i
++)
127 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
130 * Load configuration data...
133 if ((lang
= cupsLangDefault()) == NULL
)
136 if (!load_configuration())
140 * Get the reply-to address...
143 templen
= sizeof(temp
);
144 httpDecode64_2(temp
, &templen
, argv
[2]);
146 if (!strncmp(temp
, "mailto:", 7))
147 strlcpy(mailtoReplyTo
, temp
+ 7, sizeof(mailtoReplyTo
));
149 fprintf(stderr
, "WARNING: Bad notify-user-data value (%d bytes) ignored!\n",
153 * Loop forever until we run out of events...
159 * Get the next event...
163 while ((state
= ippReadFile(0, msg
)) != IPP_DATA
)
165 if (state
<= IPP_IDLE
)
169 fprintf(stderr
, "DEBUG: state=%d\n", state
);
171 if (state
== IPP_ERROR
)
172 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr
);
174 if (state
<= IPP_IDLE
)
177 * Out of messages, free memory and then exit...
185 * Get the subject and text for the message, then email it...
188 subject
= cupsNotifySubject(lang
, msg
);
189 text
= cupsNotifyText(lang
, msg
);
191 fprintf(stderr
, "DEBUG: subject=\"%s\"\n", subject
);
192 fprintf(stderr
, "DEBUG: text=\"%s\"\n", text
);
195 email_message(argv
[1] + 7, subject
, text
);
198 fputs("ERROR: Missing attributes in event notification!\n", stderr
);
199 print_attributes(msg
, 4);
203 * Free the memory used for this event...
218 * 'email_message()' - Email a notification message.
222 email_message(const char *to
, /* I - Recipient of message */
223 const char *subject
, /* I - Subject of message */
224 const char *text
) /* I - Text of message */
226 cups_file_t
*fp
; /* Pipe/socket to mail server */
227 const char *nl
; /* Newline to use */
228 char response
[1024]; /* SMTP response buffer */
232 * Connect to the mail server...
235 if (mailtoSendmail
[0])
238 * Use the sendmail command...
241 fp
= pipe_sendmail(to
);
251 * Use an SMTP server...
254 char hostbuf
[1024]; /* Local hostname */
257 if (strchr(mailtoSMTPServer
, ':'))
258 fp
= cupsFileOpen(mailtoSMTPServer
, "s");
261 char spec
[1024]; /* Host:service spec */
264 snprintf(spec
, sizeof(spec
), "%s:smtp", mailtoSMTPServer
);
265 fp
= cupsFileOpen(spec
, "s");
270 fprintf(stderr
, "ERROR: Unable to connect to SMTP server \"%s\"!\n",
275 fprintf(stderr
, "DEBUG: Connected to \"%s\"...\n", mailtoSMTPServer
);
277 cupsFilePrintf(fp
, "HELO %s\r\n", httpGetHostname(hostbuf
, sizeof(hostbuf
)));
278 fprintf(stderr
, "DEBUG: >>> HELO %s\n", hostbuf
);
280 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
282 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
284 cupsFilePrintf(fp
, "MAIL FROM:%s\r\n", mailtoFrom
);
285 fprintf(stderr
, "DEBUG: >>> MAIL FROM:%s\n", mailtoFrom
);
287 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
289 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
291 cupsFilePrintf(fp
, "RCPT TO:%s\r\n", to
);
292 fprintf(stderr
, "DEBUG: >>> RCPT TO:%s\n", to
);
294 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
296 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
298 cupsFilePuts(fp
, "DATA\r\n");
299 fputs("DEBUG: DATA\n", stderr
);
301 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
303 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
309 * Send the message...
312 cupsFilePrintf(fp
, "Date: %s%s", httpGetDateString(time(NULL
)), nl
);
313 cupsFilePrintf(fp
, "From: %s%s", mailtoFrom
, nl
);
314 cupsFilePrintf(fp
, "Subject: %s %s%s", mailtoSubject
, subject
, nl
);
315 if (mailtoReplyTo
[0])
317 cupsFilePrintf(fp
, "Sender: %s%s", mailtoReplyTo
, nl
);
318 cupsFilePrintf(fp
, "Reply-To: %s%s", mailtoReplyTo
, nl
);
320 cupsFilePrintf(fp
, "To: %s%s", to
, nl
);
322 cupsFilePrintf(fp
, "Cc: %s%s", mailtoCc
, nl
);
323 cupsFilePrintf(fp
, "Content-Type: text/plain%s", nl
);
324 cupsFilePuts(fp
, nl
);
325 cupsFilePrintf(fp
, "%s%s", text
, nl
);
326 cupsFilePrintf(fp
, ".\n", nl
);
329 * Close the connection to the mail server...
332 if (mailtoSendmail
[0])
335 * Close the pipe and wait for the sendmail command to finish...
338 int status
; /* Exit status */
347 * Report any non-zero status...
352 if (WIFEXITED(status
))
353 fprintf(stderr
, "ERROR: Sendmail command returned status %d!\n",
354 WEXITSTATUS(status
));
356 fprintf(stderr
, "ERROR: Sendmail command crashed on signal %d!\n",
363 * Finish up the SMTP submission and close the connection...
366 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
368 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
371 * Process SMTP errors here...
376 cupsFilePuts(fp
, "QUIT\r\n");
377 fputs("DEBUG: QUIT\n", stderr
);
379 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
381 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
385 fprintf(stderr
, "DEBUG: Closed connection to \"%s\"...\n",
392 * 'load_configuration()' - Load the mailto.conf file.
395 int /* I - 1 on success, 0 on failure */
396 load_configuration(void)
398 cups_file_t
*fp
; /* mailto.conf file */
399 const char *server_root
, /* CUPS_SERVERROOT environment variable */
400 *server_admin
; /* SERVER_ADMIN environment variable */
401 char line
[1024], /* Line from file */
402 *value
; /* Value for directive */
403 int linenum
; /* Line number in file */
407 * Initialize defaults...
412 if ((server_admin
= getenv("SERVER_ADMIN")) != NULL
)
413 strlcpy(mailtoFrom
, server_admin
, sizeof(mailtoFrom
));
415 snprintf(mailtoFrom
, sizeof(mailtoFrom
), "root@%s",
416 httpGetHostname(line
, sizeof(line
)));
418 strlcpy(mailtoSendmail
, "/usr/sbin/sendmail", sizeof(mailtoSendmail
));
420 mailtoSMTPServer
[0] = '\0';
422 mailtoSubject
[0] = '\0';
425 * Try loading the config file...
428 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
429 server_root
= CUPS_SERVERROOT
;
431 snprintf(line
, sizeof(line
), "%s/mailto.conf", server_root
);
433 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
435 fprintf(stderr
, "ERROR: Unable to open \"%s\" - %s\n", line
,
442 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
446 fprintf(stderr
, "ERROR: No value found for %s directive on line %d!\n",
452 if (!strcasecmp(line
, "Cc"))
453 strlcpy(mailtoCc
, value
, sizeof(mailtoCc
));
454 else if (!strcasecmp(line
, "From"))
455 strlcpy(mailtoFrom
, value
, sizeof(mailtoFrom
));
456 else if (!strcasecmp(line
, "Sendmail"))
458 strlcpy(mailtoSendmail
, value
, sizeof(mailtoSendmail
));
459 mailtoSMTPServer
[0] = '\0';
461 else if (!strcasecmp(line
, "SMTPServer"))
463 mailtoSendmail
[0] = '\0';
464 strlcpy(mailtoSMTPServer
, value
, sizeof(mailtoSMTPServer
));
466 else if (!strcasecmp(line
, "Subject"))
467 strlcpy(mailtoSubject
, value
, sizeof(mailtoSubject
));
471 "ERROR: Unknown configuration directive \"%s\" on line %d!\n",
477 * Close file and return...
487 * 'pipe_sendmail()' - Open a pipe to sendmail...
490 cups_file_t
* /* O - CUPS file */
491 pipe_sendmail(const char *to
) /* I - To: address */
493 cups_file_t
*fp
; /* CUPS file */
494 int pid
; /* Process ID */
495 int pipefds
[2]; /* Pipe file descriptors */
496 int argc
; /* Number of arguments */
497 char *argv
[100], /* Argument array */
498 line
[1024], /* Sendmail command + args */
499 *lineptr
; /* Pointer into line */
503 * First break the mailtoSendmail string into arguments...
506 strlcpy(line
, mailtoSendmail
, sizeof(line
));
510 for (lineptr
= strchr(line
, ' '); lineptr
; lineptr
= strchr(lineptr
, ' '))
512 while (*lineptr
== ' ')
518 * Point to the next argument...
521 argv
[argc
++] = lineptr
;
524 * Stop if we have too many...
527 if (argc
>= (int)(sizeof(argv
) / sizeof(argv
[0]) - 2))
532 argv
[argc
++] = (char *)to
;
541 perror("ERROR: Unable to create pipe");
546 * Then run the command...
549 if ((pid
= fork()) == 0)
552 * Child goes here - redirect stdin to the input side of the pipe,
553 * redirect stdout to stderr, and exec...
565 execvp(argv
[0], argv
);
571 * Unable to fork - error out...
574 perror("ERROR: Unable to fork command");
583 * Create a CUPS file using the output side of the pipe and close the
589 if ((fp
= cupsFileOpenFd(pipefds
[1], "w")) == NULL
)
591 int status
; /* Status of command */
603 * 'print_attributes()' - Print the attributes in a request...
607 print_attributes(ipp_t
*ipp
, /* I - IPP request */
608 int indent
) /* I - Indentation */
610 int i
; /* Looping var */
611 ipp_tag_t group
; /* Current group */
612 ipp_attribute_t
*attr
; /* Current attribute */
613 ipp_value_t
*val
; /* Current value */
614 static const char * const tags
[] = /* Value/group tag strings */
617 "operation-attributes-tag",
618 "job-attributes-tag",
619 "end-of-attributes-tag",
620 "printer-attributes-tag",
621 "unsupported-attributes-tag",
622 "subscription-attributes-tag",
623 "event-attributes-tag",
681 "textWithoutLanguage",
682 "nameWithoutLanguage",
694 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
696 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
698 group
= IPP_TAG_ZERO
;
703 if (group
!= attr
->group_tag
)
705 group
= attr
->group_tag
;
707 fprintf(stderr
, "DEBUG: %*s%s:\n\n", indent
- 4, "", tags
[group
]);
710 fprintf(stderr
, "DEBUG: %*s%s (", indent
, "", attr
->name
);
711 if (attr
->num_values
> 1)
712 fputs("1setOf ", stderr
);
713 fprintf(stderr
, "%s):", tags
[attr
->value_tag
]);
715 switch (attr
->value_tag
)
718 case IPP_TAG_INTEGER
:
719 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
720 fprintf(stderr
, " %d", val
->integer
);
724 case IPP_TAG_BOOLEAN
:
725 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
726 fprintf(stderr
, " %s", val
->boolean
? "true" : "false");
731 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
732 fprintf(stderr
, " %d-%d", val
->range
.lower
, val
->range
.upper
);
738 time_t vtime
; /* Date/Time value */
739 struct tm
*vdate
; /* Date info */
740 char vstring
[256]; /* Formatted time */
742 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
744 vtime
= ippDateToTime(val
->date
);
745 vdate
= localtime(&vtime
);
746 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
747 fprintf(stderr
, " (%s)", vstring
);
753 case IPP_TAG_RESOLUTION
:
754 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
755 fprintf(stderr
, " %dx%d%s", val
->resolution
.xres
,
756 val
->resolution
.yres
,
757 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
761 case IPP_TAG_STRING
:
762 case IPP_TAG_TEXTLANG
:
763 case IPP_TAG_NAMELANG
:
766 case IPP_TAG_KEYWORD
:
768 case IPP_TAG_URISCHEME
:
769 case IPP_TAG_CHARSET
:
770 case IPP_TAG_LANGUAGE
:
771 case IPP_TAG_MIMETYPE
:
772 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
773 fprintf(stderr
, " \"%s\"", val
->string
.text
);
777 case IPP_TAG_BEGIN_COLLECTION
:
780 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
784 print_attributes(val
->collection
, indent
+ 4);
789 fprintf(stderr
, "UNKNOWN (%d values)\n", attr
->num_values
);
797 * End of "$Id: mailto.c 4961 2006-01-20 22:19:13Z mike $".