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);
32 new->name = xstrdup(buf);
34 list_add_last(h, &new->car);
40 struct list* l = xmalloc(sizeof(struct list));
42 int i = 0; /* current position */
46 buf = xmalloc(BUFSIZE);
49 while ((c = getchar()) != EOF){
53 if (i >= curbufsize-2)
54 buf = xrealloc(buf, curbufsize *= 2);
60 if ((c = getchar()) != ' ' && c != '\t'){
82 int curbufsize = BUFSIZE;
84 buf = xmalloc(BUFSIZE);
85 while ((c = getchar()) != EOF){
88 if (i >= curbufsize - 1)
89 buf = xrealloc(buf, curbufsize *= 2);
92 b = xmalloc(sizeof(struct email));
100 print_headers(struct list* l)
105 printf("%s:%s",p->name,p->value);
109 flush_mbox_buffer(int fd)
111 if (mbox_write_err || !mbox_write_pos)
115 res = write(fd, mbox_write_buf, mbox_write_pos);
122 write_char_to_mailbox(char c, int fd)
126 if (mbox_write_pos >= BUFSIZE){
127 res = write(fd, mbox_write_buf, BUFSIZE);
133 mbox_write_buf[mbox_write_pos++] = c;
136 /* try to copy e-mail to mailbox, if it fails,
137 truncate mailbox to the original size */
139 copy_email(int fd, struct email* email)
143 mb_end = lseek(fd, 0, SEEK_END);
154 LIST_FOREACH(ph, email->headers){
155 for (pc = ph->name; *pc; pc++)
156 write_char_to_mailbox(*pc, fd);
157 write_char_to_mailbox(':', fd);
158 write_char_to_mailbox(' ', fd);
159 for (pc = ph->value; *pc; pc++)
160 write_char_to_mailbox(*pc, fd);
161 write_char_to_mailbox('\n', fd);
164 write_char_to_mailbox('\n', fd);
166 /* FIXME: do not forget change Content-Length */
167 for (pc = email->body; pc < email->body + email->body_len; pc++){
168 write_char_to_mailbox(*pc, fd);
170 if ((pc + 5 < email->body + email->body_len)
171 && pc[1] == 'F' && pc[2] == 'r'
172 && pc[3] == 'o' && pc[4] == 'm'
174 write_char_to_mailbox('>', fd);
178 flush_mbox_buffer(fd);
182 /* try to truncate to the original length */
183 ftruncate(fd, mb_end);
191 deliver_local_email(char* folder, struct email* email)
194 int is_default_mailbox = 0;
197 if (!strcmp(default_mailbox, folder))
198 is_default_mailbox = 1;
200 fd = open_mailbox(folder, is_default_mailbox);
202 if (is_default_mailbox)
204 else /* try to save to default mailbox instead */
205 return deliver_local_email(default_mailbox, email);
208 res = copy_email(fd, email);
211 /* try to deliver to the default mailbox */
212 if (is_default_mailbox)
215 return deliver_local_email(default_mailbox, email);
218 close_mailbox(fd, folder, is_default_mailbox);
224 do_action(struct action* a)
227 if (! a->r && !a->l ){
228 deliver_local_email(a->s, &a->e);