From f75390e7aaae7ae6abfa7d8857d8c9814a5922d2 Mon Sep 17 00:00:00 2001
From: "Alexander G. M. Smith" <agmsmith@ncf.ca>
Date: Wed, 2 Mar 2016 18:45:08 +0000
Subject: [PATCH] Detect terminal input, end of file, better argument parsing.
* 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.
* Avoid buffer overrun by using fgets instead of gets.
* Use stderr for text the user likely doesn't want to save, and for
prompts that would be invisible if stdout was redirected to a file.
---
src/bin/mail_utils/mail.cpp | 81 +++++++++++++++++++++++++--------------------
1 file changed, 45 insertions(+), 36 deletions(-)
diff --git a/src/bin/mail_utils/mail.cpp b/src/bin/mail_utils/mail.cpp
index f33bc07..97aff49 100644
a
|
b
|
|
6 | 6 | * Santiago (Jacques) Lema |
7 | 7 | * Jérôme Duval, jerome.duval@gmail.com |
8 | 8 | * Augustin Cavalier, <waddlesplash> |
| 9 | * Alexander G. M. Smith <agmsmith@ncf.ca> |
9 | 10 | */ |
10 | 11 | |
11 | 12 | |
12 | | #include <Application.h> |
13 | | #include <String.h> |
14 | | #include <E-mail.h> |
15 | | |
16 | 13 | #include <stdio.h> |
| 14 | #include <unistd.h> |
17 | 15 | |
| 16 | #include <Application.h> |
| 17 | #include <E-mail.h> |
| 18 | #include <String.h> |
18 | 19 | |
19 | | #define APP_SIG "application/x-vnd.Haiku-mail_utils-mail" |
| 20 | #define APP_SIG "application/x-vnd.Haiku-mail_utils-mail" |
20 | 21 | |
21 | 22 | |
22 | 23 | int main(int argc, char* argv[]) |
… |
… |
int main(int argc, char* argv[])
|
28 | 29 | fprintf(stdout,"This program can only send mail, not read it.\n"); |
29 | 30 | fprintf(stdout,"usage: %s [-v] [-s subject] [-c cc-addr] " |
30 | 31 | "[-b bcc-addr] to-addr ...\n", argv[0]); |
31 | | fflush(stdout); |
32 | 32 | return 0; |
33 | 33 | } |
34 | 34 | |
35 | | char *subject = "No title"; |
| 35 | char *subject = "No subject"; |
36 | 36 | char *cc = ""; |
37 | 37 | char *bcc = ""; |
38 | | BString to = ""; |
39 | | BString body = ""; |
| 38 | BString to; |
| 39 | bool verbose = false; |
40 | 40 | |
41 | | bool verbose =false; |
42 | 41 | // Parse arguments |
43 | 42 | for (int i = 1; i < argc; i++) { |
44 | 43 | if (strcmp(argv[i], "-v") == 0) |
… |
… |
int main(int argc, char* argv[])
|
53 | 52 | bcc = argv[i+1]; |
54 | 53 | i++; |
55 | 54 | } else { |
| 55 | if (to.Length() > 0) |
| 56 | to.Append(", "); |
56 | 57 | to.Append(argv[i]); |
57 | | if (i < argc - 1) |
58 | | to.Append(" "); |
59 | | } |
| 58 | } |
60 | 59 | } |
61 | 60 | |
62 | 61 | if (verbose) { |
63 | 62 | 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"); |
| 63 | fprintf(stdout, "To:\t%s\n", to.String()); |
| 64 | fprintf(stdout, "Cc:\t%s\n", cc); |
| 65 | fprintf(stdout, "Bcc:\t%s\n", bcc); |
| 66 | fprintf(stdout, "Subj:\t%s\n", subject); |
| 67 | fprintf(stdout, "\n"); |
70 | 68 | } |
71 | 69 | |
72 | 70 | // Check if recipients are valid |
73 | | if (strcmp(to.String(), "") == 0 && |
74 | | strcmp(cc, "") == 0 && |
75 | | strcmp(bcc, "") == 0) { |
| 71 | if (strcmp(to.String(), "") == 0 && |
| 72 | strcmp(cc, "") == 0 && |
| 73 | strcmp(bcc, "") == 0) { |
76 | 74 | |
77 | | fprintf(stdout, "[Error]: You must specify at least one recipient " |
| 75 | fprintf(stderr, "[Error]: You must specify at least one recipient " |
78 | 76 | "in to, cc or bcc fields.\n"); |
79 | 77 | return -1; |
80 | 78 | } |
81 | 79 | |
82 | | // Read each line until we get a single dot "." on a line |
| 80 | bool isTerminal = isatty(STDIN_FILENO) != 0; |
| 81 | if (isTerminal) { |
| 82 | fprintf(stderr, "Now type your message.\n" |
| 83 | "Type '.' alone on a line to end your text and send it.\n"); |
| 84 | } |
| 85 | |
| 86 | BString body; |
83 | 87 | char line[32768] = ""; |
84 | 88 | |
85 | | printf("Now type your message.\nType '.' alone on a line to send it.\n"); |
| 89 | // Read each line and collect the body text until we get an end of text |
| 90 | // marker. That's a single dot "." on a line typed in by the user, |
| 91 | // or end of file when reading a file. |
86 | 92 | do { |
87 | | gets(line); |
88 | | |
89 | | if (strcmp(line, ".") != 0) { |
90 | | body.Append(line).Append("\n"); |
| 93 | if (fgets(line, sizeof(line), stdin) == NULL) { |
| 94 | // End of file or an error happened, just send collected body text. |
| 95 | break; |
91 | 96 | } |
92 | | // fprintf(stdout,"Line: %s \n",line); |
93 | | } while (strcmp(line, ".") != 0); |
94 | 97 | |
| 98 | if (isTerminal && strcmp(line, ".\n") == 0) |
| 99 | break; |
| 100 | |
| 101 | body.Append(line); |
| 102 | } while (true); |
95 | 103 | |
96 | 104 | if (verbose) |
97 | | fprintf(stdout, "\nBody:\n%s\n", body.String()); |
| 105 | fprintf(stdout, "\nBody:\n%s\n", body.String()); |
98 | 106 | |
99 | 107 | if (verbose) |
100 | | fprintf(stdout, "\nSending E-mail...\n"); |
| 108 | fprintf(stderr, "Sending E-mail...\n"); |
101 | 109 | fflush(stdout); |
102 | 110 | |
103 | 111 | BMailMessage mail; |
… |
… |
int main(int argc, char* argv[])
|
105 | 113 | mail.AddHeaderField(B_MAIL_CC, cc); |
106 | 114 | mail.AddHeaderField(B_MAIL_BCC, bcc); |
107 | 115 | mail.AddHeaderField(B_MAIL_SUBJECT, subject); |
108 | | mail.AddContent(body.String(), strlen(body.String())); |
| 116 | mail.AddContent(body.String(), body.Length()); |
109 | 117 | status_t result = mail.Send(); |
110 | 118 | |
111 | 119 | if (result == B_OK) { |
112 | | fprintf(stdout, "\nMessage was sent successfully.\n"); |
| 120 | if (verbose) |
| 121 | fprintf(stderr, "Message was sent successfully.\n"); |
113 | 122 | return 0; |
114 | 123 | } |
115 | 124 | |
116 | | fprintf(stdout, "Message failed to send: %s", strerror(result)); |
| 125 | fprintf(stderr, "Message failed to send: %s\n", strerror(result)); |
117 | 126 | return result; |
118 | 127 | } |