13 char mbox_write_buf[BUFSIZE];
18 new_header(char* buf, struct list* h)
23 new = xmalloc(sizeof(struct hlist));
27 new->value = xstrdup("");
30 new->value = xstrdup(p+1);
31 p = strrchr(new->value, '\n');
35 new->name = xstrdup(buf);
37 list_add_last(h, &new->car);
41 char gmc_buf[BUFSIZE];
51 gmc_read = read(fd, gmc_buf, BUFSIZE);
56 if (gmc_unget != EOF) {
67 if (gmc_pos < gmc_read) {
68 return gmc_buf[gmc_pos++];
71 gmc_read = read(fd, gmc_buf, BUFSIZE);
77 return gmc_buf[gmc_pos++];
91 struct list* l = xmalloc(sizeof(struct list));
93 int i = 0; /* current position */
97 buf = xmalloc(BUFSIZE);
100 while ((c = give_me_char(fd)) != EOF){
104 if (i >= curbufsize-2)
105 buf = xrealloc(buf, curbufsize *= 2);
111 if ((c = give_me_char(fd)) != ' ' && c != '\t'){
134 int curbufsize = MAIL_LEN;
138 buf = xmalloc(MAIL_LEN);
139 b = xmalloc(sizeof(struct email));
144 /* read mail body, if it is too big, try to make tmp file */
145 while ((c = give_me_char(rfd)) != EOF){
147 if (i >= curbufsize - 1) {
148 tmpfile = xstrdup("/tmp/umpf.XXXXXX");
149 fd = mkstemp(tmpfile);
150 /* cannot create tmpfile, try to continue reading */
152 bye(EX_TEMPFAIL, "%m");
156 b->tmpfile = tmpfile;
158 res = write(fd, buf, MAIL_LEN);
159 if (res < MAIL_LEN) {
161 bye(EX_TEMPFAIL, "%m");
168 /* save rest of the body to the tmpfile */
171 while ((c = give_me_char(rfd)) != EOF){
176 res = write(fd, buf, MAIL_LEN);
177 if (res < MAIL_LEN) {
179 bye(EX_TEMPFAIL, "%m");
183 res = write(fd, buf, j);
186 bye(EX_TEMPFAIL, "%m");
193 print_headers(struct list* l)
198 printf("%s:%s\n",p->name,p->value);
202 flush_mbox_buffer(int fd)
204 if (mbox_write_err || !mbox_write_pos)
208 res = write(fd, mbox_write_buf, mbox_write_pos);
215 write_char_to_mailbox(char c, int fd)
219 if (mbox_write_pos >= BUFSIZE){
220 res = write(fd, mbox_write_buf, BUFSIZE);
226 mbox_write_buf[mbox_write_pos++] = c;
242 read_email(struct email* em)
247 if (! email_opened) {
252 if (! headers_sent) {
254 int curbufsize = BUFSIZE;
255 buf = xmalloc(BUFSIZE);
258 LIST_FOREACH(ph, em->headers){
259 int needed = strlen(ph->name) + strlen(ph->value) + 3;
260 if (curbufsize < pos + needed)
261 buf = xrealloc(buf, curbufsize*=2);
262 strcpy(buf + pos, ph->name);
263 pos += strlen(ph->name);
265 strcpy(buf + pos, ph->value);
266 pos += strlen(ph->value);
277 buf = xstrdup(em->body);
278 chars_written = em->body_len;
279 //printf("%d: %s\n", em->body_len, em->body);
285 lseek(em->fd, 0, SEEK_SET);
287 buf = xmalloc(MAIL_LEN + 1);
288 r = read(em->fd, buf, MAIL_LEN);
297 write_email_to_fd(int fd, struct email* email)
304 buf = read_email(email);
305 wr = write(fd, buf, chars_written);
306 if (wr < chars_written)
308 } while (chars_written);
313 /* try to copy e-mail to mailbox, if it fails,
314 truncate mailbox to the original size */
316 copy_email(int fd, struct email* email)
320 mb_end = lseek(fd, 0, SEEK_END);
331 LIST_FOREACH(ph, email->headers){
332 for (pc = ph->name; *pc; pc++)
333 write_char_to_mailbox(*pc, fd);
334 write_char_to_mailbox(':', fd);
335 for (pc = ph->value; *pc; pc++)
336 write_char_to_mailbox(*pc, fd);
337 write_char_to_mailbox('\n', fd);
340 write_char_to_mailbox('\n', fd);
342 /* FIXME: do not forget change Content-Length */
344 for (pc = email->body; pc < email->body + email->body_len; pc++){
345 write_char_to_mailbox(*pc, fd);
347 if ((pc + 5 < email->body + email->body_len)
348 && pc[1] == 'F' && pc[2] == 'r'
349 && pc[3] == 'o' && pc[4] == 'm'
351 write_char_to_mailbox('>', fd);
357 for(i = 0; i < email->body_len; i+=MAIL_LEN) {
359 if (i >= email->body_len)
360 len = len - (i - email->body_len);
361 read(email->fd, buf, len); //FIXME: check it?
362 for (pc = buf; pc < buf + len; pc++) {
363 write_char_to_mailbox(*pc, fd);
365 if ((pc + 5 < email->body + email->body_len)
366 && pc[1] == 'F' && pc[2] == 'r'
367 && pc[3] == 'o' && pc[4] == 'm'
369 write_char_to_mailbox('>', fd);
375 flush_mbox_buffer(fd);
379 /* try to truncate to the original length */
380 ftruncate(fd, mb_end);
388 deliver_local_email(char* folder, struct email* email)
391 int is_default_mailbox = 0;
394 if (!strcmp(default_mailbox, folder))
395 is_default_mailbox = 1;
397 fd = open_mailbox(folder, is_default_mailbox);
399 if (is_default_mailbox)
401 else /* try to save to default mailbox instead */
402 return deliver_local_email(default_mailbox, email);
405 res = copy_email(fd, email);
408 /* try to deliver to the default mailbox */
409 if (is_default_mailbox)
412 return deliver_local_email(default_mailbox, email);
415 close_mailbox(fd, folder, is_default_mailbox);