Ticket #12609: 0001-Detect-terminal-input-end-of-file-parse-arguments-be.patch

File 0001-Detect-terminal-input-end-of-file-parse-arguments-be.patch, 4.9 KB (added by AGMS, 9 years ago)

Patch to fix this ticket, and quite a few other bugs.

  • src/bin/mail_utils/mail.cpp

    From 367c0099b1feeba8ec9197b4b0354cc851548d50 Mon Sep 17 00:00:00 2001
    From: "Alexander G. M. Smith" <agmsmith@ncf.ca>
    Date: Sun, 28 Feb 2016 11:27:52 +0000
    Subject: [PATCH] Detect terminal input, end of file, parse arguments better.
    
    * If the input is a terminal rather than a file or pipe, only then look
      for the single period on a line as end of text.  Also look for end of
      file as an end of the text, so that piped in text works.
    * Parse multiple e-mail addresses properly, adding a comma between them
      (a space doesn't work).  Also allow mixing of "to" e-mail addresses
      and command line switches, previously all "to" addresses had to be
      at the end.
    * Fewer blank lines in the output, make it look nicer, remove things
      like a redundant display of the body text before text was read.  Also
      no output text when just piping in a message.
    ---
     src/bin/mail_utils/mail.cpp | 73 ++++++++++++++++++++++++---------------------
     1 file changed, 39 insertions(+), 34 deletions(-)
    
    diff --git a/src/bin/mail_utils/mail.cpp b/src/bin/mail_utils/mail.cpp
    index f33bc07..5142890 100644
    a b  
    99 */
    1010
    1111
    12 #include <Application.h>
    13 #include <String.h>
    14 #include <E-mail.h>
    15 
    1612#include <stdio.h>
     13#include <unistd.h>
    1714
     15#include <Application.h>
     16#include <E-mail.h>
     17#include <String.h>
    1818
    19 #define APP_SIG             "application/x-vnd.Haiku-mail_utils-mail"
     19#define APP_SIG "application/x-vnd.Haiku-mail_utils-mail"
    2020
    2121
    2222int main(int argc, char* argv[])
    int main(int argc, char* argv[])  
    2828        fprintf(stdout,"This program can only send mail, not read it.\n");
    2929        fprintf(stdout,"usage: %s [-v] [-s subject] [-c cc-addr] "
    3030            "[-b bcc-addr] to-addr ...\n", argv[0]);
    31         fflush(stdout);
    3231        return 0;
    3332    }
    3433
    35     char *subject = "No title";
     34    char *subject = "No Subject";
    3635    char *cc = "";
    3736    char *bcc = "";
    3837    BString to = "";
    39     BString body = "";
     38    bool verbose = false;
    4039
    41     bool verbose =false;
    4240    // Parse arguments
    4341    for (int i = 1; i < argc; i++) {
    4442        if (strcmp(argv[i], "-v") == 0)
    int main(int argc, char* argv[])  
    5351            bcc = argv[i+1];
    5452            i++;
    5553        } else {
     54            if (to.Length() > 0)
     55                to.Append(", ");
    5656            to.Append(argv[i]);
    57             if (i < argc - 1)
    58                 to.Append(" ");
    59         }
     57        }
    6058    }
    6159
    6260    if (verbose) {
    6361        fprintf(stdout, "\n");
    64         fprintf(stdout, "To:\t<%s> \n", to.String());
    65         fprintf(stdout, "Cc:\t<%s> \n", cc);
    66         fprintf(stdout, "Bcc:\t<%s> \n", bcc);
    67         fprintf(stdout, "Subj:\t<%s> \n", subject);
    68         fprintf(stdout, "Body:\t<%s> \n", body.String());
    69         fprintf(stdout, "\n");
     62        fprintf(stdout, "To:\t%s\n", to.String());
     63        fprintf(stdout, "Cc:\t%s\n", cc);
     64        fprintf(stdout, "Bcc:\t%s\n", bcc);
     65        fprintf(stdout, "Subj:\t%s\n", subject);
     66        fprintf(stdout, "\n");
    7067    }
    7168
    7269    // Check if recipients are valid
    73     if (strcmp(to.String(), "") == 0 &&
    74         strcmp(cc, "") == 0 &&
    75         strcmp(bcc, "") == 0) {
     70    if (strcmp(to.String(), "") == 0 &&
     71        strcmp(cc, "") == 0 &&
     72        strcmp(bcc, "") == 0) {
    7673
    7774        fprintf(stdout, "[Error]: You must specify at least one recipient "
    7875            "in to, cc or bcc fields.\n");
    7976        return -1;
    8077    }
    8178
    82     // Read each line until we get a single dot "." on a line
     79    bool isTerminal = isatty (STDIN_FILENO) != 0;
     80    if (isTerminal)
     81        printf("Now type your message.\n"
     82            "Type '.' alone on a line to end your text and send it.\n");
     83
     84    BString body = "";
    8385    char line[32768] = "";
    8486
    85     printf("Now type your message.\nType '.' alone on a line to send it.\n");
     87    // Read each line and collect the body text until we get an end of text
     88    // marker.  That's a single dot "." on a line typed in by the user,
     89    // or end of file when reading a file.
    8690    do {
    87         gets(line);
     91        if (fgets(line, sizeof(line), stdin) == NULL)
     92            break; // End of file or an error happened.
    8893
    89         if (strcmp(line, ".") != 0) {
    90             body.Append(line).Append("\n");
    91         }
    92         // fprintf(stdout,"Line: %s \n",line);
    93     } while (strcmp(line, ".") != 0);
     94        if (isTerminal && strcmp(line, ".\n") == 0)
     95            break;
    9496
     97        body.Append(line);
     98    } while (true);
    9599
    96100    if (verbose)
    97         fprintf(stdout, "\nBody:\n%s\n", body.String());
     101        fprintf(stdout, "\nBody:\n%s\n", body.String());
    98102
    99103    if (verbose)
    100         fprintf(stdout, "\nSending E-mail...\n");
     104        fprintf(stdout, "Sending E-mail...\n");
    101105    fflush(stdout);
    102106
    103107    BMailMessage mail;
    int main(int argc, char* argv[])  
    105109    mail.AddHeaderField(B_MAIL_CC, cc);
    106110    mail.AddHeaderField(B_MAIL_BCC, bcc);
    107111    mail.AddHeaderField(B_MAIL_SUBJECT, subject);
    108     mail.AddContent(body.String(), strlen(body.String()));
     112    mail.AddContent(body.String(), body.Length());
    109113    status_t result = mail.Send();
    110114
    111115    if (result == B_OK) {
    112         fprintf(stdout, "\nMessage was sent successfully.\n");
     116        if (verbose)
     117            fprintf(stdout, "Message was sent successfully.\n");
    113118        return 0;
    114119    }
    115120
    116     fprintf(stdout, "Message failed to send: %s", strerror(result));
     121    fprintf(stdout, "Message failed to send: %s\n", strerror(result));
    117122    return result;
    118123}