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 that nasty FROM
167 for (pc = email->body; *pc; pc++){
168 write_char_to_mailbox(*pc, fd);
171 flush_mbox_buffer(fd);
175 /* try to truncate to the original length */
176 ftruncate(fd, mb_end);
184 deliver_local_email(char* folder, struct email* email)
187 int is_default_mailbox = 0;
190 if (!strcmp(default_mailbox, folder))
191 is_default_mailbox = 1;
193 fd = open_mailbox(folder, is_default_mailbox);
195 if (is_default_mailbox)
197 else /* try to save to default mailbox instead */
198 return deliver_local_email(default_mailbox, email);
201 res = copy_email(fd, email);
204 /* try to deliver to the default mailbox */
205 if (is_default_mailbox)
208 return deliver_local_email(default_mailbox, email);
211 close_mailbox(fd, folder, is_default_mailbox);
217 do_action(struct action* a)
220 if (! a->r && !a->l ){
221 deliver_local_email(a->s, &a->e);