~ubuntu-branches/ubuntu/lucid/edbrowse/lucid

« back to all changes in this revision

Viewing changes to src/sendmail.c

  • Committer: Bazaar Package Importer
  • Author(s): Kapil Hari Paranjape
  • Date: 2009-03-01 16:55:12 UTC
  • mfrom: (1.1.5 upstream) (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090301165512-97yhte20cy3c4q2w
Tags: 3.4.1-1
* New upstream version (3.4.1).
* debian/rules:
  - add "touch build-stamp" to build-stamp target.
  - modify clean target to use clean target in src/makefile.
  - remove debian/edbrowse.1 in clean target.
  - added "-lcurl" to linker flags.
* debian/control:
  - added libcurl4-openssl-dev to Build-Depends
  - Standards Version 3.8.0. No changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
241
241
        return false;
242
242
    if(!serverGetLine(secure))
243
243
        return false;
 
244
    return true;
244
245
}                               /* serverPutGet */
245
246
 
246
247
void
400
401
    return t;
401
402
}                               /* isoEncode */
402
403
 
 
404
/*********************************************************************
 
405
Return a string that defines the charset of the outgoing mail.
 
406
This just looks at your language setting - reaaly dumb.
 
407
It should interrogate each file/attachment.
 
408
Well, this will get us started.
 
409
*********************************************************************/
 
410
 
 
411
static char *
 
412
charsetString(const char *ct, const char *ce)
 
413
{
 
414
    static char buf[24];
 
415
    buf[0] = 0;
 
416
    if(!stringEqual(ce, "7bit") &&
 
417
       (stringEqual(ct, "text/plain") || stringEqual(ct, "text/html"))) {
 
418
        if(cons_utf8)
 
419
            strcpy(buf, "; charset=utf-8");
 
420
        else
 
421
            sprintf(buf, "; charset=iso-8859-%d", type8859);
 
422
    }
 
423
    return buf;
 
424
}                               /* charsetString */
 
425
 
403
426
/* Read a file into memory, mime encode it,
404
427
 * and return the type of encoding and the encoded data.
405
428
 * Last three parameters are result parameters.
540
563
        }                       /* .signature */
541
564
    }
542
565
 
543
 
    /* primary email message */
544
566
    /* Infer content type from the filename */
545
567
    ct = 0;
546
568
    s = strrchr(file, '.');
594
616
       file, buflen, nacount, nullcount, longline);
595
617
    nacount += nullcount;
596
618
 
 
619
/* Set the type of attachment */
 
620
    if(buflen > 20 && nacount * 5 > buflen) {
 
621
        if(!ct)
 
622
            ct = "application/octet-stream";    /* default type for binary */
 
623
    }
 
624
    if(!ct)
 
625
        ct = "text/plain";
 
626
 
597
627
/* Criteria for base64 encode.
598
 
 * But, some web servers won't take qp encode, so if this is a web form,
599
 
 * and we would normally qp encode, do base 64. */
 
628
 * files uploaded from a web form need not be encoded, unless they contain
 
629
 * nulls, which is a quirk of my slapped together software. */
600
630
 
601
 
    if(buflen > 20 && nacount * 5 > buflen ||
602
 
       webform && (nacount * 20 > buflen || nullcount || longline)) {
 
631
    if(!webform && (buflen > 20 && nacount * 5 > buflen) ||
 
632
       webform && nullcount) {
603
633
        if(ismail) {
604
634
            setError(MSG_MailBinary, file);
605
635
            goto freefail;
606
636
        }
607
 
 
608
637
        s = base64Encode(buf, buflen, true);
609
638
        nzFree(buf);
610
639
        buf = s;
611
 
        if(!ct)
612
 
            ct = "application/octet-stream";    /* default type */
613
640
        ce = "base64";
614
641
        goto success;
615
642
    }
616
643
 
617
 
    if(!ct)
618
 
        ct = "text/plain";
619
 
 
 
644
    if(!webform) {
620
645
/* Switch to unix newlines - we'll switch back to dos later. */
621
 
    v = buf + buflen;
622
 
    for(s = t = buf; s < v; ++s) {
623
 
        c = *s;
624
 
        if(c == '\r' && s < v - 1 && s[1] == '\n')
625
 
            continue;
626
 
        *t++ = c;
627
 
    }
628
 
    buflen = t - buf;
 
646
        v = buf + buflen;
 
647
        for(s = t = buf; s < v; ++s) {
 
648
            c = *s;
 
649
            if(c == '\r' && s < v - 1 && s[1] == '\n')
 
650
                continue;
 
651
            *t++ = c;
 
652
        }
 
653
        buflen = t - buf;
629
654
 
630
655
/* Do we need to use quoted-printable? */
631
656
/* Perhaps this hshould read (nacount > 0) */
632
 
    if(nacount * 20 > buflen || nullcount || longline) {
633
 
        char *newbuf;
634
 
        int l, colno = 0, space = 0;
 
657
        if(nacount * 20 > buflen || nullcount || longline) {
 
658
            char *newbuf;
 
659
            int l, colno = 0, space = 0;
635
660
 
636
 
        newbuf = initString(&l);
637
 
        v = buf + buflen;
638
 
        for(s = buf; s < v; ++s) {
639
 
            c = *s;
 
661
            newbuf = initString(&l);
 
662
            v = buf + buflen;
 
663
            for(s = buf; s < v; ++s) {
 
664
                c = *s;
640
665
/* do we have to =expand this character? */
641
 
            if(c < '\n' && c != '\t' ||
642
 
               c == '=' ||
643
 
               c == '\xff' ||
644
 
               (c == ' ' || c == '\t') && s < v - 1 && s[1] == '\n') {
645
 
                char expand[4];
646
 
                sprintf(expand, "=%02X", (uchar) c);
647
 
                stringAndString(&newbuf, &l, expand);
648
 
                colno += 3;
649
 
            } else {
650
 
                stringAndChar(&newbuf, &l, c);
651
 
                ++colno;
652
 
            }
653
 
            if(c == '\n') {
654
 
                colno = space = 0;
655
 
                continue;
656
 
            }
657
 
            if(c == ' ' || c == '\t')
658
 
                space = l;
659
 
            if(colno < 72)
660
 
                continue;
661
 
            if(s == v - 1)
662
 
                continue;
 
666
                if(c < '\n' && c != '\t' ||
 
667
                   c == '=' ||
 
668
                   c == '\xff' ||
 
669
                   (c == ' ' || c == '\t') && s < v - 1 && s[1] == '\n') {
 
670
                    char expand[4];
 
671
                    sprintf(expand, "=%02X", (uchar) c);
 
672
                    stringAndString(&newbuf, &l, expand);
 
673
                    colno += 3;
 
674
                } else {
 
675
                    stringAndChar(&newbuf, &l, c);
 
676
                    ++colno;
 
677
                }
 
678
                if(c == '\n') {
 
679
                    colno = space = 0;
 
680
                    continue;
 
681
                }
 
682
                if(c == ' ' || c == '\t')
 
683
                    space = l;
 
684
                if(colno < 72)
 
685
                    continue;
 
686
                if(s == v - 1)
 
687
                    continue;
663
688
/* If newline's coming up anyways, don't force another one. */
664
 
            if(s[1] == '\n')
665
 
                continue;
666
 
            i = l;
667
 
            if(!space || space == i) {
668
 
                stringAndString(&newbuf, &l, "=\n");
669
 
                colno = space = 0;
670
 
                continue;
671
 
            }
672
 
            colno = i - space;
673
 
            stringAndString(&newbuf, &l, "**"); /* make room */
674
 
            while(i > space) {
675
 
                newbuf[i + 1] = newbuf[i - 1];
676
 
                --i;
677
 
            }
678
 
            newbuf[space] = '=';
679
 
            newbuf[space + 1] = '\n';
680
 
            space = 0;
681
 
        }                       /* loop over characters */
 
689
                if(s[1] == '\n')
 
690
                    continue;
 
691
                i = l;
 
692
                if(!space || space == i) {
 
693
                    stringAndString(&newbuf, &l, "=\n");
 
694
                    colno = space = 0;
 
695
                    continue;
 
696
                }
 
697
                colno = i - space;
 
698
                stringAndString(&newbuf, &l, "**");     /* make room */
 
699
                while(i > space) {
 
700
                    newbuf[i + 1] = newbuf[i - 1];
 
701
                    --i;
 
702
                }
 
703
                newbuf[space] = '=';
 
704
                newbuf[space + 1] = '\n';
 
705
                space = 0;
 
706
            }                   /* loop over characters */
682
707
 
683
 
        nzFree(buf);
684
 
        buf = newbuf;
685
 
        ce = "quoted-printable";
686
 
        goto success;
 
708
            nzFree(buf);
 
709
            buf = newbuf;
 
710
            ce = "quoted-printable";
 
711
            goto success;
 
712
        }
687
713
    }
688
 
    /* quoted printable */
 
714
 
689
715
    buf[buflen] = 0;
690
716
    ce = (nacount ? "8bit" : "7bit");
691
717
 
757
783
char *
758
784
makeBoundary(void)
759
785
{
760
 
    static char boundary[60];
 
786
    static char boundary[24];
761
787
    sprintf(boundary, "nextpart-eb-%06d", rand() % 1000000);
762
788
    return boundary;
763
789
}                               /* makeBoundary */
1036
1062
    if(!mustmime) {
1037
1063
/* no mime components required, we can just send the mail. */
1038
1064
        sprintf(serverLine,
1039
 
           "Content-type: %s%sContent-Transfer-Encoding: %s%s%s", ct, eol, ce,
1040
 
           eol, eol);
 
1065
           "Content-Type: %s%s%sContent-Transfer-Encoding: %s%s%s", ct,
 
1066
           charsetString(ct, ce), eol, ce, eol, eol);
1041
1067
        stringAndString(&out, &j, serverLine);
1042
1068
    } else {
1043
1069
        sprintf(serverLine,
1049
1075
this format, some or all of this message may not be legible.\r\n\r\n--");
1050
1076
        stringAndString(&out, &j, boundary);
1051
1077
        sprintf(serverLine,
1052
 
           "%sContent-type: %s%sContent-Transfer-Encoding: %s%s%s", eol, ct,
1053
 
           eol, ce, eol, eol);
 
1078
           "%sContent-Type: %s%s%sContent-Transfer-Encoding: %s%s%s", eol, ct,
 
1079
           charsetString(ct, ce), eol, ce, eol, eol);
1054
1080
        stringAndString(&out, &j, serverLine);
1055
1081
    }
1056
1082
 
1063
1089
        for(i = 0; s = attachments[i]; ++i) {
1064
1090
            if(!encodeAttachment(s, 0, false, &ct, &ce, &encoded))
1065
1091
                return false;
1066
 
            sprintf(serverLine, "%s--%s%sContent-Type: %s", eol, boundary, eol,
1067
 
               ct);
 
1092
            sprintf(serverLine, "%s--%s%sContent-Type: %s%s", eol, boundary,
 
1093
               eol, ct, charsetString(ct, ce));
1068
1094
            stringAndString(&out, &j, serverLine);
1069
1095
/* If the filename has a quote in it, forget it. */
1070
1096
/* Also, suppress filename if this is an alternate presentation. */