]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/tools/bsmtp.c
Change old get_Jobxxx to getJobxxx
[bacula/bacula] / bacula / src / tools / bsmtp.c
index 03b5f5c3a718c7a0335aa75a057671ec5550d42f..acb4cf25dcfc8e477f9994000277d1e4655ce4d6 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2001-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2001-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -20,7 +20,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula® is a registered trademark of John Walker.
+   Bacula® is a registered trademark of Kern Sibbald.
    The licensor of Bacula is the Free Software Foundation Europe
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
 /*
    Derived from a SMTPclient:
 
+  ======== Original copyrights ==========  
+
        SMTPclient -- simple SMTP client
 
-       Copyright (C) 1997 Ralf S. Engelschall, All Rights Reserved.
-       rse@engelschall.com
-       www.engelschall.com
+       Copyright (c) 1997 Ralf S. Engelschall, All rights reserved.
+
+       This program is free software; it may be redistributed and/or modified
+       only under the terms of either the Artistic License or the GNU General
+       Public License, which may be found in the SMTP source distribution.
+       Look at the file COPYING.
+
+       This program is distributed in the hope that it will be useful, but
+       WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       ======================================================================
+
+       smtpclient_main.c -- program source
+
+       Based on smtp.c as of August 11, 1995 from
+           W.Z. Venema,
+           Eindhoven University of Technology,
+           Department of Mathematics and Computer Science,
+           Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands.
+
+   =========
+
 
    Kern Sibbald, July 2001
 
+     Note, the original W.Z. Venema smtp.c had no license and no
+     copyright.  See:
+        http://archives.neohapsis.com/archives/postfix/2000-05/1520.html
+
    Version $Id$
 
  */
@@ -79,7 +107,7 @@ static char *cleanup_addr(char *addr, char *buf, int buf_len)
 {
    char *p, *q;
 
-   if ((p = strchr(from_addr, '<')) == NULL) {
+   if ((p = strchr(addr, '<')) == NULL) {
       snprintf(buf, buf_len, "<%s>", addr);
    } else {
       /* Copy <addr> */
@@ -157,20 +185,56 @@ static void usage()
    fprintf(stderr,
 _("\n"
 "Usage: %s [-f from] [-h mailhost] [-s subject] [-c copy] [recipient ...]\n"
-"       -8          set charset utf-8\n"
+"       -8          set charset to UTF-8\n"
 "       -c          set the Cc: field\n"
-"       -dnn        set debug level to nn\n"
+"       -d <nn>     set debug level to <nn>\n"
+"       -dt         print a timestamp in debug output\n"
 "       -f          set the From: field\n"
 "       -h          use mailhost:port as the SMTP server\n"
 "       -s          set the Subject: field\n"
 "       -r          set the Reply-To: field\n"
-"       -l          set the maximum number of lines that should be sent (default: unlimited)\n"
+"       -l          set the maximum number of lines to send (default: unlimited)\n"
 "       -?          print this message.\n"
 "\n"), MY_NAME);
 
    exit(1);
 }
 
+/*
+ * Return the offset west from localtime to UTC in minutes
+  * Same as timezone.tz_minuteswest
+  *   Unix tz_offset coded by:  Attila Fülöp
+  */
+
+static long tz_offset(time_t lnow, struct tm &tm)
+{
+#if defined(HAVE_WIN32)
+#if defined(HAVE_MINGW)
+__MINGW_IMPORT long     _dstbias;
+#endif
+#if defined(MINGW64)
+# define _tzset tzset
+#endif
+   /* Win32 code */
+   long offset;
+   _tzset();
+   offset = _timezone;
+   if (tm.tm_isdst) {
+      offset += _dstbias;
+   }
+   return offset /= 60;
+#else
+
+   /* Unix/Linux code */
+   struct tm tm_utc;
+   time_t now = lnow;
+
+   (void)gmtime_r(&now, &tm_utc);
+   tm_utc.tm_isdst = tm.tm_isdst;
+   return (long)difftime(mktime(&tm_utc), now) / 60;
+#endif
+}
+
 static void get_date_string(char *buf, int buf_len)
 {
    time_t now = time(NULL);
@@ -181,21 +245,7 @@ static void get_date_string(char *buf, int buf_len)
    /* Add RFC822 date */
    (void)localtime_r(&now, &tm);
 
-#if defined(HAVE_WIN32)
-#if defined(HAVE_MINGW)
-__MINGW_IMPORT long     _dstbias;
-#endif
-   _tzset();
-   my_timezone = _timezone;
-   my_timezone += _dstbias;
-   my_timezone /= 60;
-
-#else
-   struct timeval tv;
-   struct timezone tz;
-   gettimeofday(&tv, &tz);
-   my_timezone = tz.tz_minuteswest; /* timezone offset in mins */
-#endif
+   my_timezone = tz_offset(now, tm);
    strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S", &tm);
    sprintf(tzbuf, " %+2.2ld%2.2u", -my_timezone / 60, abs(my_timezone) % 60);
    strcat(buf, tzbuf);              /* add +0100 */
@@ -241,9 +291,13 @@ int main (int argc, char *argv[])
          break;
 
       case 'd':                    /* set debug level */
-         debug_level = atoi(optarg);
-         if (debug_level <= 0) {
-            debug_level = 1;
+         if (*optarg == 't') {
+            dbg_timestamp = true;
+         } else {
+            debug_level = atoi(optarg);
+            if (debug_level <= 0) {
+               debug_level = 1;
+            }
          }
          Dmsg1(20, "Debug level = %d\n", debug_level);
          break;
@@ -428,19 +482,19 @@ hp:
     *   we do.
     */
    get_response(); /* banner */
-   chat("helo %s\r\n", my_hostname);
-   chat("mail from:%s\r\n", cleanup_addr(from_addr, buf, sizeof(buf)));
+   chat("HELO %s\r\n", my_hostname);
+   chat("MAIL FROM:%s\r\n", cleanup_addr(from_addr, buf, sizeof(buf)));
    
    for (i = 0; i < argc; i++) {
       Dmsg1(20, "rcpt to: %s\n", argv[i]);
-      chat("rcpt to:%s\r\n", cleanup_addr(argv[i], buf, sizeof(buf)));
+      chat("RCPT TO:%s\r\n", cleanup_addr(argv[i], buf, sizeof(buf)));
    }
 
    if (cc_addr) {
-      chat("rcpt to:%s\r\n", cleanup_addr(cc_addr, buf, sizeof(buf)));
+      chat("RCPT TO:%s\r\n", cleanup_addr(cc_addr, buf, sizeof(buf)));
    }
    Dmsg0(20, "Data\n");
-   chat("data\r\n");
+   chat("DATA\r\n");
 
    /*
     *  Send message header
@@ -513,28 +567,30 @@ hp:
    while (fgets(buf, sizeof(buf), stdin)) {
       if (maxlines > 0 && ++lines > maxlines) {
          Dmsg1(20, "skip line because of maxlines limit: %lu\n", maxlines);
+         while (fgets(buf, sizeof(buf), stdin)) {
+            ++lines;
+         }
          break;
       }
       buf[sizeof(buf)-1] = '\0';
       buf[strlen(buf)-1] = '\0';
-      if (buf[0] == '.' && buf[1] == '\0') { /* quote lone dots */
-         fputs("..\r\n", sfp);
-      } else {                     /* pass body through unchanged */
-         fputs(buf, sfp);
-         fputs("\r\n", sfp);
+      if (buf[0] == '.') {         /* add extra . see RFC 2821 4.5.2 */
+         fputs(".", sfp);
       }
+      fputs(buf, sfp);
+      fputs("\r\n", sfp);
    }
 
    if (lines > maxlines) {
       Dmsg1(10, "hit maxlines limit: %lu\n", maxlines);
-      fprintf(sfp, "\r\n[maximum of %lu lines exceeded, skipped %lu lines of output]\r\n", maxlines, lines-maxlines);
+      fprintf(sfp, "\r\n\r\n[maximum of %lu lines exceeded, skipped %lu lines of output]\r\n", maxlines, lines-maxlines);
    }
 
    /*
     *  Send SMTP quit command
     */
    chat(".\r\n");
-   chat("quit\r\n");
+   chat("QUIT\r\n");
 
    /*
     *  Go away gracefully ...