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
|
|
9 | 9 | */ |
10 | 10 | |
11 | 11 | |
12 | | #include <Application.h> |
13 | | #include <String.h> |
14 | | #include <E-mail.h> |
15 | | |
16 | 12 | #include <stdio.h> |
| 13 | #include <unistd.h> |
17 | 14 | |
| 15 | #include <Application.h> |
| 16 | #include <E-mail.h> |
| 17 | #include <String.h> |
18 | 18 | |
19 | | #define APP_SIG "application/x-vnd.Haiku-mail_utils-mail" |
| 19 | #define APP_SIG "application/x-vnd.Haiku-mail_utils-mail" |
20 | 20 | |
21 | 21 | |
22 | 22 | int main(int argc, char* argv[]) |
… |
… |
int main(int argc, char* argv[])
|
28 | 28 | fprintf(stdout,"This program can only send mail, not read it.\n"); |
29 | 29 | fprintf(stdout,"usage: %s [-v] [-s subject] [-c cc-addr] " |
30 | 30 | "[-b bcc-addr] to-addr ...\n", argv[0]); |
31 | | fflush(stdout); |
32 | 31 | return 0; |
33 | 32 | } |
34 | 33 | |
35 | | char *subject = "No title"; |
| 34 | char *subject = "No Subject"; |
36 | 35 | char *cc = ""; |
37 | 36 | char *bcc = ""; |
38 | 37 | BString to = ""; |
39 | | BString body = ""; |
| 38 | bool verbose = false; |
40 | 39 | |
41 | | bool verbose =false; |
42 | 40 | // Parse arguments |
43 | 41 | for (int i = 1; i < argc; i++) { |
44 | 42 | if (strcmp(argv[i], "-v") == 0) |
… |
… |
int main(int argc, char* argv[])
|
53 | 51 | bcc = argv[i+1]; |
54 | 52 | i++; |
55 | 53 | } else { |
| 54 | if (to.Length() > 0) |
| 55 | to.Append(", "); |
56 | 56 | to.Append(argv[i]); |
57 | | if (i < argc - 1) |
58 | | to.Append(" "); |
59 | | } |
| 57 | } |
60 | 58 | } |
61 | 59 | |
62 | 60 | if (verbose) { |
63 | 61 | 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"); |
70 | 67 | } |
71 | 68 | |
72 | 69 | // 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) { |
76 | 73 | |
77 | 74 | fprintf(stdout, "[Error]: You must specify at least one recipient " |
78 | 75 | "in to, cc or bcc fields.\n"); |
79 | 76 | return -1; |
80 | 77 | } |
81 | 78 | |
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 = ""; |
83 | 85 | char line[32768] = ""; |
84 | 86 | |
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. |
86 | 90 | do { |
87 | | gets(line); |
| 91 | if (fgets(line, sizeof(line), stdin) == NULL) |
| 92 | break; // End of file or an error happened. |
88 | 93 | |
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; |
94 | 96 | |
| 97 | body.Append(line); |
| 98 | } while (true); |
95 | 99 | |
96 | 100 | if (verbose) |
97 | | fprintf(stdout, "\nBody:\n%s\n", body.String()); |
| 101 | fprintf(stdout, "\nBody:\n%s\n", body.String()); |
98 | 102 | |
99 | 103 | if (verbose) |
100 | | fprintf(stdout, "\nSending E-mail...\n"); |
| 104 | fprintf(stdout, "Sending E-mail...\n"); |
101 | 105 | fflush(stdout); |
102 | 106 | |
103 | 107 | BMailMessage mail; |
… |
… |
int main(int argc, char* argv[])
|
105 | 109 | mail.AddHeaderField(B_MAIL_CC, cc); |
106 | 110 | mail.AddHeaderField(B_MAIL_BCC, bcc); |
107 | 111 | mail.AddHeaderField(B_MAIL_SUBJECT, subject); |
108 | | mail.AddContent(body.String(), strlen(body.String())); |
| 112 | mail.AddContent(body.String(), body.Length()); |
109 | 113 | status_t result = mail.Send(); |
110 | 114 | |
111 | 115 | if (result == B_OK) { |
112 | | fprintf(stdout, "\nMessage was sent successfully.\n"); |
| 116 | if (verbose) |
| 117 | fprintf(stdout, "Message was sent successfully.\n"); |
113 | 118 | return 0; |
114 | 119 | } |
115 | 120 | |
116 | | fprintf(stdout, "Message failed to send: %s", strerror(result)); |
| 121 | fprintf(stdout, "Message failed to send: %s\n", strerror(result)); |
117 | 122 | return result; |
118 | 123 | } |