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);
30 new->name = xstrdup(buf);
32 list_add_last(h, &new->car);
38 struct list* l = xmalloc(sizeof(struct list));
40 int i = 0; /* current position */
44 buf = xmalloc(BUFSIZE);
47 while ((c = getchar()) != EOF){
51 if (i >= curbufsize-2)
52 buf = xrealloc(buf, curbufsize *= 2);
58 if ((c = getchar()) != ' ' && c != '\t'){
80 int curbufsize = BUFSIZE;
82 buf = xmalloc(BUFSIZE);
83 while ((c = getchar()) != EOF){
86 if (i >= curbufsize - 1)
87 buf = xrealloc(buf, curbufsize *= 2);
90 b = xmalloc(sizeof(struct email));
98 print_headers(struct list* l)
103 printf("%s:%s",p->name,p->value);
107 flush_mbox_buffer(int fd)
109 if (mbox_write_err || !mbox_write_pos)
113 res = write(fd, mbox_write_buf, mbox_write_pos);
120 write_char_to_mailbox(char c, int fd)
124 if (mbox_write_pos >= BUFSIZE){
125 res = write(fd, mbox_write_buf, BUFSIZE);
131 mbox_write_buf[mbox_write_pos++] = c;
134 /* try to copy e-mail to mailbox, if it fails,
135 truncate mailbox to the original size */
137 copy_email(int fd, struct email* email)
141 mb_end = lseek(fd, 0, SEEK_END);
152 LIST_FOREACH(ph, email->headers){
153 for (pc = ph->name; *pc; pc++)
154 write_char_to_mailbox(*pc, fd);
155 write_char_to_mailbox(':', fd);
156 write_char_to_mailbox(' ', fd);
157 for (pc = ph->value; *pc; pc++)
158 write_char_to_mailbox(*pc, fd);
159 write_char_to_mailbox('\n', fd);
162 write_char_to_mailbox('\n', fd);
164 /* FIXME: do not forget change Content-Length */
165 for (pc = email->body; pc < email->body + email->body_len; pc++){
166 write_char_to_mailbox(*pc, fd);
168 if ((pc + 5 < email->body + email->body_len)
169 && pc[1] == 'F' && pc[2] == 'r'
170 && pc[3] == 'o' && pc[4] == 'm'
172 write_char_to_mailbox('>', fd);
176 flush_mbox_buffer(fd);
180 /* try to truncate to the original length */
181 ftruncate(fd, mb_end);
189 deliver_local_email(char* folder, struct email* email)
192 int is_default_mailbox = 0;
195 if (!strcmp(default_mailbox, folder))
196 is_default_mailbox = 1;
198 fd = open_mailbox(folder, is_default_mailbox);
200 if (is_default_mailbox)
202 else /* try to save to default mailbox instead */
203 return deliver_local_email(default_mailbox, email);
206 res = copy_email(fd, email);
209 /* try to deliver to the default mailbox */
210 if (is_default_mailbox)
213 return deliver_local_email(default_mailbox, email);
216 close_mailbox(fd, folder, is_default_mailbox);
222 do_action(struct action* a)
225 if (! a->r && !a->l ){
226 deliver_local_email(a->s, &a->e);