mbox: double the value of INCSIZE
[mailx.git] / send.c
blob2766157206659bcca3f7eea18b0f754bbcd26657
1 #include <ctype.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8 #include "config.h"
9 #include "mbox.h"
10 #include "util.h"
11 #include "send.h"
12 #include "str.h"
14 #define USERAGENT "git://repo.or.cz/mailx.git"
16 static int isaddr(int c)
18 return isalpha(c) || isdigit(c) || strchr("_.-@", c);
21 static char *cut_addr(char *dst, char *src)
23 while (*src && !isaddr(*src))
24 src++;
25 while (isaddr(*src))
26 *dst++ = *src++;
27 *dst++ = '\0';
28 return src;
31 static int parse_addr(char **recips, int size, char *s, int len)
33 int n = 0;
34 char *d = s + len;
35 while (n < size && s < d && *(s = cut_addr(recips[n], s)))
36 if (strchr(recips[n], '@'))
37 n++;
38 return n;
41 static int mail_recips(char **recips, int size, char *s)
43 struct mail mail = {{0}};
44 char *to, *cc;
45 int n = 0;
46 mail_read(&mail, s);
47 if ((to = mail_hdr(&mail, "To:")))
48 n += parse_addr(recips + n, size - n, to + 3, hdr_len(to) - 3);
49 if ((cc = mail_hdr(&mail, "CC:")))
50 n += parse_addr(recips + n, size - n, cc + 3, hdr_len(cc) - 3);
51 return n;
54 static char *put_from_(char *s)
56 time_t t;
57 time(&t);
58 s = put_str(s, "From ");
59 s = put_str(s, getlogin());
60 s += strftime(s, MAXLINE, " %a %b %d %H:%M:%S %Y\n", localtime(&t));
61 return s;
64 static char *put_date(char *s)
66 long tz;
67 time_t t;
68 time(&t);
69 tzset();
70 s = put_str(s, "Date: ");
71 s += strftime(s, MAXLINE, "%a, %d %b %Y %H:%M:%S ", localtime(&t));
72 tz = abs(timezone);
73 *s++ = timezone < 0 ? '-' : '+';
74 s = put_intz(s, tz / 3600, 2);
75 s = put_intz(s, (tz % 3600) / 60, 2);
76 s = put_str(s, "\n");
77 return s;
80 static char *put_id(char *s)
82 time_t t;
83 time(&t);
84 s = put_str(s, "Message-ID: <");
85 s += strftime(s, MAXLINE, "%Y%d%m%H%M%S", localtime(&t));
86 s = put_str(s, "@" HOSTNAME ">\n");
87 return s;
90 static char *put_from(char *s)
92 return FROM ? put_str(s, "From: " FROM "\n") : s;
95 static char *put_to(char *s, char **to, int n)
97 int i;
98 s = put_str(s, "To: ");
99 s = put_str(s, to[0]);
100 for (i = 1; i < n; i++) {
101 s = put_str(s, ",\n\t");
102 s = put_str(s, to[i]);
104 s = put_str(s, "\n");
105 return s;
108 static char *put_subj(char *s, char *subj)
110 s = put_str(s, "Subject: ");
111 s = put_str(s, subj);
112 s = put_str(s, "\n");
113 return s;
116 static char *put_agent(char *s)
118 return put_str(s, "User-Agent: " USERAGENT "\n");
121 void draft_init(struct draft *draft, char **to, int nto, char *subj)
123 char *s = draft->mail;
124 s = put_from_(s);
125 s = put_date(s);
126 s = put_from(s);
127 if (to && nto)
128 s = put_to(s, to, nto);
129 if (subj)
130 s = put_subj(s, subj);
131 s = put_id(s);
132 s = put_agent(s);
133 draft->len = s - draft->mail;
136 static char *hdr_val(char *hdr)
138 hdr = strchr(hdr, ':') + 1;
139 while (isspace(*hdr))
140 hdr++;
141 return hdr;
144 static char *put_replysubj(char *s, char *subj)
146 subj = hdr_val(subj);
147 s = put_str(s, "Subject: ");
148 if (tolower(subj[0]) != 'r' || tolower(subj[1]) != 'e')
149 s = put_str(s, "Re: ");
150 s = put_mem(s, subj, hdr_len(subj));
151 return s;
154 static char *put_replyto(char *s, char *id, char *ref)
156 s = put_str(s, "In-Reply-To: ");
157 id = hdr_val(id);
158 s = put_mem(s, id, hdr_len(id));
159 s = put_str(s, "References: ");
160 if (ref) {
161 ref = hdr_val(ref);
162 s = put_mem(s, ref, hdr_len(ref));
163 s = put_str(s, "\t");
165 s = put_mem(s, id, hdr_len(id));
166 return s;
169 static char *put_reply(char *s, char *from, char *to, char *cc)
171 if (from) {
172 from = hdr_val(from);
173 s = put_str(s, "To: ");
174 s = put_mem(s, from, hdr_len(from));
176 if (to || cc) {
177 s = put_str(s, "Cc: ");
178 if (to) {
179 to = hdr_val(to);
180 s = put_mem(s, to, hdr_len(to));
182 if (cc) {
183 cc = hdr_val(cc);
184 if (to)
185 s = put_str(s, "\t");
186 s = put_mem(s, cc, hdr_len(cc));
189 return s;
192 static char *quote_body(char *s, int size, struct mail *mail)
194 char *from = mail_hdr(mail, "From:");
195 char *r = mail->body;
196 char *rd = r + mail->body_len;
197 char *sd = s + size - 16;
198 s = put_str(s, "\n");
199 if (from) {
200 from = hdr_val(from);
201 s = put_mem(s, from, hdr_len(from) - 1);
202 s = put_str(s, " wrote:\n");
204 while (s < sd && r < rd) {
205 char *nl = memchr(r, '\n', rd - r);
206 s = put_str(s, "> ");
207 nl = nl ? nl + 1 : rd;
208 s = put_mem(s, r, MIN(sd - s, nl - r));
209 r = nl;
211 *s = '\0';
212 return s;
215 void draft_reply(struct draft *draft, struct mail *mail)
217 char *s = draft->mail;
218 char *id_hdr = mail_hdr(mail, "Message-ID:");
219 char *ref_hdr = mail_hdr(mail, "References:");
220 char *from_hdr = mail_hdr(mail, "From:");
221 char *subj_hdr = mail_hdr(mail, "Subject:");
222 char *to_hdr = mail_hdr(mail, "To:");
223 char *cc_hdr = mail_hdr(mail, "CC:");
225 s = put_from_(s);
226 s = put_date(s);
227 s = put_from(s);
228 s = put_reply(s, from_hdr, to_hdr, cc_hdr);
229 if (subj_hdr)
230 s = put_replysubj(s, subj_hdr);
231 s = put_id(s);
232 if (id_hdr)
233 s = put_replyto(s, id_hdr, ref_hdr);
234 s = put_agent(s);
235 s = quote_body(s, sizeof(draft->mail) - (s - draft->mail), mail);
236 draft->len = s - draft->mail;
239 void draft_edit(struct draft *draft)
241 char *edit_argv[] = {EDITOR, DRAFT, NULL};
242 int fd;
243 if ((fd = open(DRAFT, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
244 return;
245 xwrite(fd, draft->mail, draft->len);
246 close(fd);
247 exec_file(EDITOR, edit_argv);
248 if ((fd = open(DRAFT, O_RDONLY)) == -1)
249 return;
250 draft->len = xread(fd, draft->mail, sizeof(draft->mail) - 1);
251 close(fd);
252 draft->mail[draft->len] = '\0';
253 unlink(DRAFT);
256 void draft_send(struct draft *draft)
258 char recips[MAXRECP][MAXLINE];
259 char *send_argv[MAXRECP + 3] = {SENDMAIL, "-i"};
260 char *beg = strchr(draft->mail, '\n') + 1;
261 char *end = draft->mail + draft->len;
262 int i;
263 for (i = 0; i < MAXRECP; i++)
264 send_argv[i + 2] = recips[i];
265 send_argv[2 + mail_recips(send_argv + 2, MAXRECP, draft->mail)] = NULL;
266 exec_pipe(SENDMAIL, send_argv, beg, end - beg);
269 static void write_nofrom(int fd, char *s, int len)
271 char *beg = s;
272 char *d = s + len;
273 char *r;
274 while (s < d) {
275 if (!strncmp("\nFrom ", s, 6)) {
276 write(fd, beg, s - beg + 1);
277 write(fd, ">", 1);
278 beg = s + 1;
280 r = strchr(s + 1, '\n');
281 s = r ? r : d;
283 write(fd, beg, s - beg);
286 void draft_save(struct draft *draft, char *path)
288 int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
289 if (fd < -1)
290 return;
291 if (file_size(fd))
292 write(fd, "\n", 1);
293 write_nofrom(fd, draft->mail, draft->len);
294 close(fd);