]> mj.ucw.cz Git - umpf.git/blobdiff - ham.c
fix many little bugs, release 0.1
[umpf.git] / ham.c
diff --git a/ham.c b/ham.c
index e54ec5e3b099d191a5bd30464f16f6b5ed03c2a5..7028304dd14a37205898aba1428d3493eac8beae 100644 (file)
--- a/ham.c
+++ b/ham.c
@@ -101,6 +101,8 @@ make_hlist(int fd)
        char* buf;
        int i = 0; /* current position */
        int c, last = 0;
+       int first_line = 1;
+       int want_from_line = 0;
 
        list_init(l);
        buf = xmalloc(BUFSIZE);
@@ -110,11 +112,28 @@ make_hlist(int fd)
                if (c == '\r')
                        continue;
 
-               if (i >= curbufsize-2)
+               if (i >= curbufsize-3)
                        buf = xrealloc(buf, curbufsize *= 2);
                
-               buf[i++] = c; 
+               if (first_line && 
+                       ( (!i && c != 'F') || (i == 1 && c != 'r') || (i == 2 && c != 'o') 
+                               || (i == 3 && c != 'm') || (i == 4 && c != ' ') )
+                       )
+                       first_line = 0;
+               else {
+                       if (first_line && i == 4)
+                               want_from_line = 1;
+               }
+
+               buf[i++] = c;
                if (c == '\n'){
+                       if (want_from_line) {
+                               buf[i] = 0;
+                               fromline = xstrdup(buf);
+                               want_from_line = 0;
+                               i = 0;
+                               continue;
+                       }
                        if (last == '\n')
                                break;
                        if ((c = give_me_char(fd)) != ' ' && c != '\t'){
@@ -167,7 +186,7 @@ get_body(int rfd)
                                res = write(fd, buf, MAIL_LEN);
                                if (res < MAIL_LEN) {
                                        unlink(b->tmpfile);
-                                       bye(EX_TEMPFAIL, "Cannot write to remporary file: %m");
+                                       bye(EX_TEMPFAIL, "Cannot write to temporary file: %m");
                                }
                                break;  
                        }
@@ -266,7 +285,7 @@ read_email(struct email* em)
 
                LIST_FOREACH(ph, em->headers){
                        int needed = strlen(ph->name) + strlen(ph->value) + 4;
-                       if (curbufsize < pos + needed)
+                       while (curbufsize < pos + needed)
                                buf = xrealloc(buf, curbufsize*=2);
                        strcpy(buf + pos, ph->name);
                        pos += strlen(ph->name);
@@ -307,15 +326,20 @@ int
 write_email_to_fd(int fd, struct email* email)
 {
        char* buf;
-       int wr;
+       int wr, to_write;
 
        open_email();
-       do {
+       for (;;) {
                buf = read_email(email);
-               wr = write(fd, buf, chars_written);
-               if (wr < chars_written)
-                       return 1;
-       } while (chars_written);
+               if (!chars_written)
+                       break;
+
+               to_write = chars_written;
+               while (to_write) {
+                       wr = write(fd, buf, to_write);
+                       to_write -= wr;
+               }
+       };
        
        return 0;
 }
@@ -338,33 +362,21 @@ copy_email(int fd, struct email* email)
        /* From line */
        struct hlist* ph;
        int i, len;
-       char* fromline;
-       char* from = NULL;
        time_t t;
        time(&t);
        char* date = ctime(&t);
        int datelen = strlen(date);
-       
-       LIST_FOREACH(ph, email->headers) {
-               if (!strcasecmp(ph->name, "From")) {
-                       from = ph->value;
-                       break;
-               }
-       }
-       len = 5 + datelen + 1; 
-       if (from)
-               len += strlen(from);
-       fromline = xmalloc(len);
-       if (from)
-               sprintf(fromline, "From %s %s", from, date);
-       else
-               sprintf(fromline, "From %s", date);
 
-       len = strlen(fromline);
+       if (! fromline) {       
+               len = 5 + datelen + 1; 
+               fromline = xmalloc(len);
+               sprintf(fromline, "From %s", date);
+               len = strlen(fromline);
+       } else 
+               len = strlen(fromline);
        for (i = 0; i < len; i++)
                write_char_to_mailbox(fromline[i], fd);
-
+       
        /* headers */
        char* pc;
        LIST_FOREACH(ph, email->headers){
@@ -374,7 +386,7 @@ copy_email(int fd, struct email* email)
                write_char_to_mailbox(' ', fd); 
                for (pc = ph->value; *pc; pc++)
                        write_char_to_mailbox(*pc, fd); 
-                       write_char_to_mailbox('\n', fd);        
+               write_char_to_mailbox('\n', fd);        
        }
 
        write_char_to_mailbox('\n', fd);