]> mj.ucw.cz Git - umpf.git/blob - ham.c
rewrite write_email_to_fd
[umpf.git] / ham.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sysexits.h>
5
6 #include <unistd.h>
7
8 #include "umpf.h"
9 #define MAIL_LEN 65536
10
11 int curbufsize;
12
13 char mbox_write_buf[BUFSIZE];
14 int mbox_write_pos;
15 int mbox_write_err;
16
17 static void
18 new_header(char* buf, struct list* h)
19 {
20         char* p;
21         struct hlist* new;
22
23         new = xmalloc(sizeof(struct hlist));
24
25         p = strchr(buf, ':');
26         if (!p)
27                 new->value = xstrdup("");
28         else {
29                 *p = 0;
30                 new->value = xstrdup(p+1);
31                 p = strrchr(new->value, '\n');
32                 if (p && !*(p+1))
33                         *p = 0;
34         }
35         new->name = xstrdup(buf);
36
37         list_add_last(h, &new->car);
38 }
39
40 struct list*
41 make_hlist(void)
42 {
43         struct list* l = xmalloc(sizeof(struct list));
44         char* buf;
45         int i = 0; /* current position */
46         int c, last = 0;
47
48         list_init(l);
49         buf = xmalloc(BUFSIZE);
50         curbufsize = BUFSIZE;
51         
52         while ((c = getchar()) != EOF){
53                 if (c == '\r')
54                         continue;
55
56                 if (i >= curbufsize-2)
57                         buf = xrealloc(buf, curbufsize *= 2);
58                 
59                 buf[i++] = c; 
60                 if (c == '\n'){
61                         if (last == '\n')
62                                 break;
63                         if ((c = getchar()) != ' ' && c != '\t'){
64                                 if (c != EOF)
65                                         ungetc(c, stdin);
66                                 buf[i] = 0;
67                                 new_header(buf, l);
68                                 i = 0;
69                         } else
70                                 buf[i++] = c;
71                 }
72                         last = c;
73         }
74         free_string(buf);
75         return l;
76 }
77
78 struct email*
79 get_body(void)
80 {
81         char* buf;
82         struct email* b;
83         int c, fd;
84         int will_save = 0;
85         int i = 0;
86         int curbufsize = MAIL_LEN;
87         char* tmpfile;
88         int res;
89         
90         buf = xmalloc(MAIL_LEN);
91         b = xmalloc(sizeof(struct email));
92         b->body = buf;
93         b->tmpfile = NULL;
94         b->fd = -1;
95
96         /* read mail body, if it is too big, try to make tmp file */
97         while ((c = getchar()) != EOF){
98                 buf[i++] = c;
99                 if (i >= curbufsize - 1) {
100                         tmpfile = xstrdup("/tmp/umpf.XXXXXX");
101                         fd = mkstemp(tmpfile);
102                         /* cannot create tmpfile, try to continue reading */
103                         if (fd < 0)
104                                 bye(EX_TEMPFAIL, "%m");
105                         else {
106                                 will_save = 1;
107                                 b->body = NULL;
108                                 b->tmpfile = tmpfile;
109                                 b->fd = fd;
110                                 res = write(fd, buf, MAIL_LEN);
111                                 if (res < MAIL_LEN) {
112                                         unlink(b->tmpfile);
113                                         bye(EX_TEMPFAIL, "%m");
114                                 }
115                                 break;  
116                         }
117                 }
118         }
119         b->body_len = i;
120         /* save rest of the body to the tmpfile */
121         if (will_save) {
122                 int j = 0;
123                 while ((c = getchar()) != EOF){
124                         buf[j++] = c;
125                         b->body_len++;
126                         if (j >= MAIL_LEN) {
127                                 j = 0;
128                                 res = write(fd, buf, MAIL_LEN);
129                                 if (res < MAIL_LEN) {
130                                         unlink(b->tmpfile);
131                                         bye(EX_TEMPFAIL, "%m");
132                                 }
133                         }
134                 }
135                 res = write(fd, buf, j);
136                 if (res < j) {
137                         unlink(b->tmpfile);
138                         bye(EX_TEMPFAIL, "%m");
139                 }
140         }
141         return b; 
142 }
143
144 void
145 print_headers(struct list* l)
146 {
147         struct hlist* p;
148
149         LIST_FOREACH(p,l)
150                 printf("%s:%s\n",p->name,p->value);
151 }
152
153 static void
154 flush_mbox_buffer(int fd)
155 {
156         if (mbox_write_err || !mbox_write_pos)
157                 return;
158
159         int res;
160         res = write(fd, mbox_write_buf, mbox_write_pos);
161         if (res < 0)
162                 mbox_write_err++;
163         mbox_write_pos = 0;
164 }
165
166 static void
167 write_char_to_mailbox(char c, int fd)
168 {
169         int res;
170
171         if (mbox_write_pos >= BUFSIZE){
172                 res = write(fd, mbox_write_buf, BUFSIZE);
173                 if (res < 0)
174                         mbox_write_err++;
175                 mbox_write_pos = 0;
176                 return;
177         }
178         mbox_write_buf[mbox_write_pos++] = c;
179 }
180
181 int email_pos;
182 int headers_sent;
183 int email_opened;
184
185 void
186 open_email(void)
187 {
188         email_pos = 0;
189         headers_sent = 0;
190         email_opened = 1;
191 }
192
193 char*
194 read_email(struct email* em)
195 {
196         char* buf;
197         int r, pos;
198
199         if (! email_opened) {
200                 chars_written = 0;      
201                 return NULL;
202         }
203
204         if (! headers_sent) {
205                 struct hlist* ph;
206                 int curbufsize = BUFSIZE;
207                 buf = xmalloc(BUFSIZE);
208                 pos = 0;
209
210                 LIST_FOREACH(ph, em->headers){
211                         int needed = strlen(ph->name) + strlen(ph->value) + 3;
212                         if (curbufsize < pos + needed)
213                                 buf = xrealloc(buf, curbufsize*=2);
214                         strcpy(buf + pos, ph->name);
215                         pos += strlen(ph->name);
216                         buf[pos++] = ':';
217                         strcpy(buf + pos, ph->value);
218                         pos += strlen(ph->value);
219                         buf[pos++] = '\n';
220         
221                 }
222                 buf[pos] = '\n';
223                 headers_sent = 1;
224                 chars_written = pos;
225                 return buf;
226         }
227
228         if (em->body) {
229                 buf = xstrdup(em->body);
230                 chars_written = em->body_len; 
231                 //printf("%d: %s\n", em->body_len, em->body); 
232                 email_opened = 0;
233                 return buf;
234         }
235
236         if (! email_pos)
237                 lseek(em->fd, 0, SEEK_SET);
238
239         buf = xmalloc(MAIL_LEN + 1);
240         r = read(em->fd, buf, MAIL_LEN);
241         if (r < MAIL_LEN)
242                 email_opened = 0;
243         chars_written = r;
244         
245         return buf;
246 }
247
248 int
249 write_email_to_fd(int fd, struct email* email)
250 {
251         char* buf;
252         int wr;
253
254         open_email();
255         do {
256                 buf = read_email(email);
257                 wr = write(fd, buf, chars_written);
258                 if (wr < chars_written)
259                         return 1;
260         } while (chars_written);
261         
262         return 0;
263 }
264
265 /* try to copy e-mail to mailbox, if it fails,
266  truncate mailbox to the original size */
267 static int
268 copy_email(int fd, struct email* email)
269 {
270         off_t mb_end;
271
272         mb_end = lseek(fd, 0, SEEK_END);
273         if (mb_end < 0)
274                 return -1;
275
276         /* init globals */
277         mbox_write_err = 0;
278         mbox_write_pos = 0;
279
280         /* headers */
281         struct hlist* ph;
282         char* pc;
283         LIST_FOREACH(ph, email->headers){
284                 for (pc = ph->name; *pc; pc++)
285                         write_char_to_mailbox(*pc, fd);
286                 write_char_to_mailbox(':', fd); 
287                 for (pc = ph->value; *pc; pc++)
288                         write_char_to_mailbox(*pc, fd); 
289                         write_char_to_mailbox('\n', fd);        
290         }
291
292         write_char_to_mailbox('\n', fd);
293         /* body */
294         /* FIXME: do not forget change Content-Length */
295         if (email->body) {
296                 for (pc = email->body; pc < email->body + email->body_len; pc++){
297                                 write_char_to_mailbox(*pc, fd);
298                                 if (*pc == '\n'){
299                                         if ((pc + 5 < email->body + email->body_len)
300                                                 && pc[1] == 'F' && pc[2] == 'r'
301                                                 && pc[3] == 'o' && pc[4] == 'm'
302                                                 && pc[5] == ' ')
303                                         write_char_to_mailbox('>', fd); 
304                                 }
305                 }
306         } else {
307                 int i;
308                 char buf[MAIL_LEN];
309                 for(i = 0; i < email->body_len; i+=MAIL_LEN) {
310                         int len = MAIL_LEN;
311                         if (i >= email->body_len)
312                                 len = len - (i - email->body_len);
313                         read(email->fd, buf, len); //FIXME: check it?
314                         for (pc = buf; pc < buf + len; pc++) {
315                                 write_char_to_mailbox(*pc, fd);
316                                 if (*pc == '\n'){
317                                         if ((pc + 5 < email->body + email->body_len)
318                                         && pc[1] == 'F' && pc[2] == 'r'
319                                         && pc[3] == 'o' && pc[4] == 'm'
320                                         && pc[5] == ' ')
321                                         write_char_to_mailbox('>', fd); 
322                                 }
323                         }
324                 }       
325         }
326
327         flush_mbox_buffer(fd);
328
329         /* errors? */
330         if (mbox_write_err){
331                 /* try to truncate to the original length */
332                 ftruncate(fd, mb_end);  
333                 return -1;
334         }       
335
336         return 0; 
337 }
338
339 int
340 deliver_local_email(char* folder, struct email* email)
341 {
342         int res = -1;
343         int is_default_mailbox = 0;
344         int fd;
345
346         if (!strcmp(default_mailbox, folder))   
347                 is_default_mailbox = 1;
348
349         fd = open_mailbox(folder, is_default_mailbox);
350         if (fd < 0){
351                 if (is_default_mailbox)
352                         return res;
353                 else /* try to save to default mailbox instead */
354                         return deliver_local_email(default_mailbox, email);
355         }
356
357         res = copy_email(fd, email);
358         if (res < 0){
359
360                 /* try to deliver to the default mailbox */
361                 if (is_default_mailbox)
362                         return res;
363                 else
364                         return deliver_local_email(default_mailbox, email);
365         }
366
367         close_mailbox(fd, folder, is_default_mailbox);  
368
369         return res; 
370 }