11 char mbox_write_buf[BUFSIZE];
16 new_header(char* buf, struct list* h)
21 new = xmalloc(sizeof(struct hlist));
25 new->value = xstrdup("");
28 new->value = xstrdup(p+1);
29 p = strrchr(new->value, '\n');
33 new->name = xstrdup(buf);
35 list_add_last(h, &new->car);
41 struct list* l = xmalloc(sizeof(struct list));
43 int i = 0; /* current position */
47 buf = xmalloc(BUFSIZE);
50 while ((c = getchar()) != EOF){
54 if (i >= curbufsize-2)
55 buf = xrealloc(buf, curbufsize *= 2);
61 if ((c = getchar()) != ' ' && c != '\t'){
83 int curbufsize = BUFSIZE;
85 buf = xmalloc(BUFSIZE);
86 while ((c = getchar()) != EOF){
89 if (i >= curbufsize - 1)
90 buf = xrealloc(buf, curbufsize *= 2);
93 b = xmalloc(sizeof(struct email));
101 print_headers(struct list* l)
106 printf("%s:%s\n",p->name,p->value);
110 flush_mbox_buffer(int fd)
112 if (mbox_write_err || !mbox_write_pos)
116 res = write(fd, mbox_write_buf, mbox_write_pos);
123 write_char_to_mailbox(char c, int fd)
127 if (mbox_write_pos >= BUFSIZE){
128 res = write(fd, mbox_write_buf, BUFSIZE);
134 mbox_write_buf[mbox_write_pos++] = c;
137 /* try to copy e-mail to mailbox, if it fails,
138 truncate mailbox to the original size */
140 copy_email(int fd, struct email* email)
144 mb_end = lseek(fd, 0, SEEK_END);
155 LIST_FOREACH(ph, email->headers){
156 for (pc = ph->name; *pc; pc++)
157 write_char_to_mailbox(*pc, fd);
158 write_char_to_mailbox(':', fd);
159 write_char_to_mailbox(' ', fd);
160 for (pc = ph->value; *pc; pc++)
161 write_char_to_mailbox(*pc, fd);
162 write_char_to_mailbox('\n', fd);
165 write_char_to_mailbox('\n', fd);
167 /* FIXME: do not forget change Content-Length */
168 for (pc = email->body; pc < email->body + email->body_len; pc++){
169 write_char_to_mailbox(*pc, fd);
171 if ((pc + 5 < email->body + email->body_len)
172 && pc[1] == 'F' && pc[2] == 'r'
173 && pc[3] == 'o' && pc[4] == 'm'
175 write_char_to_mailbox('>', fd);
179 flush_mbox_buffer(fd);
183 /* try to truncate to the original length */
184 ftruncate(fd, mb_end);
192 deliver_local_email(char* folder, struct email* email)
195 int is_default_mailbox = 0;
198 if (!strcmp(default_mailbox, folder))
199 is_default_mailbox = 1;
201 fd = open_mailbox(folder, is_default_mailbox);
203 if (is_default_mailbox)
205 else /* try to save to default mailbox instead */
206 return deliver_local_email(default_mailbox, email);
209 res = copy_email(fd, email);
212 /* try to deliver to the default mailbox */
213 if (is_default_mailbox)
216 return deliver_local_email(default_mailbox, email);
219 close_mailbox(fd, folder, is_default_mailbox);