10 #define MAIL_LEN 65536
14 char mbox_write_buf[BUFSIZE];
19 new_header(char* buf, struct list* h)
24 new = xmalloc(sizeof(struct hlist));
28 new->value = xstrdup("");
32 new->value = xstrdup(p + 2);
34 new->value = xstrdup(p + 1);
35 p = strrchr(new->value, '\n');
39 new->name = xstrdup(buf);
41 list_add_last(h, &new->car);
45 char gmc_buf[BUFSIZE];
55 gmc_read = read(fd, gmc_buf, BUFSIZE);
61 if (gmc_unget != EOF) {
73 if (gmc_pos < gmc_read) {
75 return gmc_buf[gmc_pos++];
78 gmc_read = read(fd, gmc_buf, BUFSIZE);
85 return gmc_buf[gmc_pos++];
100 struct list* l = xmalloc(sizeof(struct list));
102 int i = 0; /* current position */
106 buf = xmalloc(BUFSIZE);
107 curbufsize = BUFSIZE;
109 while ((c = give_me_char(fd)) != EOF){
113 if (i >= curbufsize-2)
114 buf = xrealloc(buf, curbufsize *= 2);
120 if ((c = give_me_char(fd)) != ' ' && c != '\t'){
143 int curbufsize = MAIL_LEN;
147 buf = xmalloc(MAIL_LEN);
148 b = xmalloc(sizeof(struct email));
153 /* read mail body, if it is too big, try to make tmp file */
154 while ((c = give_me_char(rfd)) != EOF){
156 if (i >= curbufsize - 1) {
157 tmpfile = xstrdup("/tmp/umpf.XXXXXX");
158 fd = mkstemp(tmpfile);
159 /* cannot create tmpfile, try to continue reading */
161 bye(EX_TEMPFAIL, "Cannot create temporary file: %m");
165 b->tmpfile = tmpfile;
167 res = write(fd, buf, MAIL_LEN);
168 if (res < MAIL_LEN) {
170 bye(EX_TEMPFAIL, "Cannot write to remporary file: %m");
177 /* save rest of the body to the tmpfile */
180 while ((c = give_me_char(rfd)) != EOF){
185 res = write(fd, buf, MAIL_LEN);
186 if (res < MAIL_LEN) {
188 bye(EX_TEMPFAIL, "Cannot write to temporary file: %m");
192 res = write(fd, buf, j);
195 bye(EX_TEMPFAIL, "Cannot write to temporary file: %m");
202 print_headers(struct list* l)
207 printf("%s:%s\n",p->name,p->value);
211 flush_mbox_buffer(int fd)
213 if (mbox_write_err || !mbox_write_pos)
217 res = write(fd, mbox_write_buf, mbox_write_pos);
224 write_char_to_mailbox(char c, int fd)
228 if (mbox_write_pos >= BUFSIZE){
229 res = write(fd, mbox_write_buf, BUFSIZE);
235 mbox_write_buf[mbox_write_pos++] = c;
251 read_email(struct email* em)
256 if (! email_opened) {
261 if (! headers_sent) {
263 int curbufsize = BUFSIZE;
264 buf = xmalloc(BUFSIZE);
267 LIST_FOREACH(ph, em->headers){
268 int needed = strlen(ph->name) + strlen(ph->value) + 4;
269 if (curbufsize < pos + needed)
270 buf = xrealloc(buf, curbufsize*=2);
271 strcpy(buf + pos, ph->name);
272 pos += strlen(ph->name);
275 strcpy(buf + pos, ph->value);
276 pos += strlen(ph->value);
287 buf = xstrdup(em->body);
288 chars_written = em->body_len;
289 //printf("%d: %s\n", em->body_len, em->body);
295 lseek(em->fd, 0, SEEK_SET);
297 buf = xmalloc(MAIL_LEN + 1);
298 r = read(em->fd, buf, MAIL_LEN);
307 write_email_to_fd(int fd, struct email* email)
314 buf = read_email(email);
315 wr = write(fd, buf, chars_written);
316 if (wr < chars_written)
318 } while (chars_written);
323 /* try to copy e-mail to mailbox, if it fails,
324 truncate mailbox to the original size */
326 copy_email(int fd, struct email* email)
330 mb_end = lseek(fd, 0, SEEK_END);
345 char* date = ctime(&t);
346 int datelen = strlen(date);
348 LIST_FOREACH(ph, email->headers) {
349 if (!strcasecmp(ph->name, "From")) {
354 len = 5 + datelen + 1;
358 fromline = xmalloc(len);
360 sprintf(fromline, "From %s %s", from, date);
362 sprintf(fromline, "From %s", date);
364 len = strlen(fromline);
365 for (i = 0; i < len; i++)
366 write_char_to_mailbox(fromline[i], fd);
370 LIST_FOREACH(ph, email->headers){
371 for (pc = ph->name; *pc; pc++)
372 write_char_to_mailbox(*pc, fd);
373 write_char_to_mailbox(':', fd);
374 write_char_to_mailbox(' ', fd);
375 for (pc = ph->value; *pc; pc++)
376 write_char_to_mailbox(*pc, fd);
377 write_char_to_mailbox('\n', fd);
380 write_char_to_mailbox('\n', fd);
382 for (pc = email->body; pc < email->body + email->body_len; pc++){
383 write_char_to_mailbox(*pc, fd);
385 if ((pc + 5 < email->body + email->body_len)
386 && pc[1] == 'F' && pc[2] == 'r'
387 && pc[3] == 'o' && pc[4] == 'm'
389 write_char_to_mailbox('>', fd);
395 for(i = 0; i < email->body_len; i+=MAIL_LEN) {
397 if (i >= email->body_len)
398 len = len - (i - email->body_len);
399 read(email->fd, buf, len); //FIXME: check it?
400 for (pc = buf; pc < buf + len; pc++) {
401 write_char_to_mailbox(*pc, fd);
403 if ((pc + 5 < email->body + email->body_len)
404 && pc[1] == 'F' && pc[2] == 'r'
405 && pc[3] == 'o' && pc[4] == 'm'
407 write_char_to_mailbox('>', fd);
413 flush_mbox_buffer(fd);
417 /* try to truncate to the original length */
418 ftruncate(fd, mb_end);
426 deliver_local_email(char* folder, struct email* email)
429 int is_default_mailbox = 0;
432 if (!strcmp(default_mailbox, folder))
433 is_default_mailbox = 1;
435 fd = open_mailbox(folder, is_default_mailbox);
437 if (is_default_mailbox)
439 else /* try to save to default mailbox instead */
440 return deliver_local_email(default_mailbox, email);
443 res = copy_email(fd, email);
446 /* try to deliver to the default mailbox */
447 if (is_default_mailbox)
450 return deliver_local_email(default_mailbox, email);
453 close_mailbox(fd, folder, is_default_mailbox);