mbox_write_buf[mbox_write_pos++] = c;
}
+int email_pos;
+int headers_sent;
+int email_opened;
+
+void
+open_email(void)
+{
+ email_pos = 0;
+ headers_sent = 0;
+ email_opened = 1;
+}
+
+char*
+read_email(struct email* em)
+{
+ char* buf;
+ int r, pos;
+
+ if (! email_opened) {
+ chars_written = 0;
+ return NULL;
+ }
+
+ if (! headers_sent) {
+ struct hlist* ph;
+ int curbufsize = BUFSIZE;
+ buf = xmalloc(BUFSIZE);
+ pos = 0;
+
+ LIST_FOREACH(ph, em->headers){
+ int needed = strlen(ph->name) + strlen(ph->value) + 3;
+ if (curbufsize < pos + needed)
+ buf = xrealloc(buf, curbufsize*=2);
+ strcpy(buf + pos, ph->name);
+ pos += strlen(ph->name);
+ buf[pos++] = ':';
+ strcpy(buf + pos, ph->value);
+ pos += strlen(ph->value);
+ buf[pos++] = '\n';
+
+ }
+ buf[pos] = '\n';
+ headers_sent = 1;
+ chars_written = pos;
+ return buf;
+ }
+
+ if (em->body) {
+ buf = xstrdup(em->body);
+ chars_written = em->body_len;
+ //printf("%d: %s\n", em->body_len, em->body);
+ email_opened = 0;
+ return buf;
+ }
+
+ if (! email_pos)
+ lseek(em->fd, 0, SEEK_SET);
+
+ buf = xmalloc(MAIL_LEN + 1);
+ r = read(em->fd, buf, MAIL_LEN);
+ if (r < MAIL_LEN)
+ email_opened = 0;
+ chars_written = r;
+
+ return buf;
+}
+
+int
+write_email_to_fd(int fd, struct email* email)
+{
+ char* buf;
+ int wr;
+
+ open_email();
+ do {
+ buf = read_email(email);
+ wr = write(fd, buf, chars_written);
+ if (wr < chars_written)
+ return 1;
+ } while (chars_written);
+
+ return 0;
+}
+
/* try to copy e-mail to mailbox, if it fails,
truncate mailbox to the original size */
static int
return 0;
}
-int
-write_email_to_fd(int fd, struct email* email)
-{
- int written;
-
- /* headers */
- struct hlist* ph;
- LIST_FOREACH(ph, email->headers){
- written = write(fd, ph->name, strlen(ph->name));
- if (written < (ssize_t) strlen(ph->name))
- return 1;
- written = write(fd, ":", 1);
- if (written < 1)
- return 1;
- written = write(fd, ph->value, strlen(ph->value));
- if (written < (ssize_t) strlen(ph->value))
- return 1;
- written = write(fd, "\n", 1);
- if (written < 1)
- return 1;
- }
- written = write(fd, "\n", 1);
- if (written < 1)
- return 1;
-
- /* body */
- if (email->body) {
- written = write(fd, email->body, email->body_len);
- if (written < email->body_len)
- return 1;
- } else {
- char buf[MAIL_LEN];
- int i;
- for(i = 0; i < email->body_len; i+=MAIL_LEN) {
- int len = MAIL_LEN;
- if (i >= email->body_len)
- len = len - (i - email->body_len);
- read(email->fd, buf, len); //FIXME: check it?
- write(fd, buf, len);
- }
- }
-
- return 0;
-}
-
-int email_pos;
-int headers_sent;
-int email_opened;
-
-void
-open_email(void)
-{
- email_pos = 0;
- headers_sent = 0;
- email_opened = 1;
-}
-
-char*
-read_email(struct email* em)
-{
- char* buf;
- int r, pos;
-
- if (! email_opened) {
- chars_written = 0;
- return NULL;
- }
-
- if (! headers_sent) {
- struct hlist* ph;
- int curbufsize = BUFSIZE;
- buf = xmalloc(BUFSIZE);
- pos = 0;
-
- LIST_FOREACH(ph, em->headers){
- int needed = strlen(ph->name) + strlen(ph->value) + 3;
- if (curbufsize < pos + needed)
- buf = xrealloc(buf, curbufsize*=2);
- strcpy(buf + pos, ph->name);
- pos += strlen(ph->name);
- buf[pos++] = ':';
- strcpy(buf + pos, ph->value);
- pos += strlen(ph->value);
- buf[pos++] = '\n';
-
- }
- buf[pos] = '\n';
- headers_sent = 1;
- chars_written = pos;
- return buf;
- }
-
- if (em->body) {
- buf = xstrdup(em->body);
- chars_written = em->body_len;
- //printf("%d: %s\n", em->body_len, em->body);
- email_opened = 0;
- return buf;
- }
-
- if (! email_pos)
- lseek(em->fd, 0, SEEK_SET);
-
- buf = xmalloc(MAIL_LEN + 1);
- r = read(em->fd, buf, MAIL_LEN);
- if (r < MAIL_LEN)
- email_opened = 0;
- chars_written = r;
-
- return buf;
-}
-
int
deliver_local_email(char* folder, struct email* email)
{