Annotation of parser3/src/types/pa_vmail.C, revision 1.47

1.1       paf         1: /**    @file
                      2:        Parser: @b mail class.
                      3:        relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
                      4: 
1.45      paf         5:        Copyright(c) 2001-2003 ArtLebedev Group(http://www.artlebedev.com)
1.1       paf         6:        Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
                      7: */
1.13      paf         8: 
1.47    ! paf         9: static const char* IDENT_VMAIL_C="$Date: 2003/08/18 08:27:41 $";
1.1       paf        10: 
                     11: #include "pa_sapi.h"
                     12: #include "pa_vmail.h"
                     13: #include "pa_vstring.h"
                     14: #include "pa_request.h"
                     15: #include "pa_common.h"
                     16: #include "pa_charset.h"
                     17: #include "pa_charsets.h"
                     18: #include "pa_vdate.h"
                     19: #include "pa_vfile.h"
                     20: #include "pa_uue.h"
                     21: 
1.3       paf        22: #ifdef WITH_MAILRECEIVE
1.4       paf        23: extern "C" {
1.1       paf        24: #include "gmime.h"
1.4       paf        25: }
1.45      paf        26: 
                     27: #include "pa_charsets.h"
1.1       paf        28: #endif
                     29: 
                     30: // defines
                     31: 
                     32: #define RAW_NAME "raw"
                     33: 
                     34: // internals
                     35: 
                     36: enum PartType {
                     37:        P_TEXT,
                     38:        P_HTML,
                     39:        P_FILE,
                     40:        P_MESSAGE,
                     41: 
                     42:        P_TYPES_COUNT
                     43: };
                     44: 
1.45      paf        45: static const char* const part_name_begins[P_TYPES_COUNT]={"text", "html", "file", "message"};
                     46: 
                     47: // defines for statics
                     48: 
                     49: #define FORMAT_NAME "format"
                     50: #define CHARSET_NAME "charset"
                     51: 
                     52: // statics
                     53: 
                     54: static const String format_name(FORMAT_NAME);
                     55: static const String charset_name(CHARSET_NAME);
1.1       paf        56: 
                     57: // VMail
                     58: 
1.45      paf        59: extern Methoded* mail_base_class;
1.1       paf        60: 
1.45      paf        61: VMail::VMail(): VStateless_class(0, mail_base_class) {}
1.1       paf        62: 
1.3       paf        63: #ifdef WITH_MAILRECEIVE
1.1       paf        64: 
1.46      paf        65: static const String& maybeUpperCase(Charset& source_charset, 
                     66:                                    const String& src, bool toUpperCase) {
                     67:        return toUpperCase?src.change_case(source_charset, String::CC_UPPER):src;
1.1       paf        68: }
                     69: 
1.47    ! paf        70: static String::C UTF8toSource(Charset& source_charset, 
        !            71:                         const char* source_body, size_t source_content_length) {
        !            72:        String::C result(0, 0);
1.1       paf        73:        if(source_body) {
                     74:                if(!source_content_length)
                     75:                        source_content_length=strlen(source_body);
1.47    ! paf        76:                result=Charset::transcode(String::C(source_body, source_content_length),
        !            77:                        UTF8_charset, source_charset);
1.1       paf        78:        }
1.47    ! paf        79: 
        !            80:        return result;
1.1       paf        81: }
                     82: 
1.45      paf        83: static void putReceived(Charset& source_charset, 
                     84:                        HashStringValue& received, 
                     85:                        const char* name, Value* value, 
                     86:                        bool nameToUpperCase=false) {
                     87: 
1.1       paf        88:        if(name && value) {
                     89:                received.put(
1.45      paf        90:                        maybeUpperCase(source_charset, 
1.47    ! paf        91:                                *new String(pa_strdup(name), 0, true/*tainted*/), nameToUpperCase),
1.1       paf        92:                        value);
                     93:        }
                     94: }
                     95: 
1.45      paf        96: static void putReceived(Charset& source_charset, 
                     97:                        HashStringValue& received, 
1.47    ! paf        98:                        const char* must_clone_name, const char* must_clone_value, size_t help_must_clone_value_size=0, 
1.45      paf        99:                        bool nameToUpperCase=false) {
1.47    ! paf       100:        if(must_clone_name && must_clone_value) {
        !           101:                String::C cloned_value=UTF8toSource(source_charset,
        !           102:                        must_clone_value, help_must_clone_value_size);
1.1       paf       103:                
1.45      paf       104:                putReceived(source_charset,
                    105:                        received, 
1.47    ! paf       106:                        pa_strdup(must_clone_name), 
        !           107:                        new VString(*new String(pa_strdup(cloned_value.str, cloned_value.length), true/*tainted*/))
1.45      paf       108:                );
1.1       paf       109:        }
                    110: }
                    111: 
1.47    ! paf       112: static void putReceived(HashStringValue& received, const char* must_clone_name, time_t value) {
        !           113:        if(must_clone_name)
1.45      paf       114:                received.put(
1.47    ! paf       115:                        *new StringBody(pa_strdup(must_clone_name)), 
        !           116:                        new VDate(value)
1.45      paf       117:                );
1.1       paf       118: }
                    119: 
1.45      paf       120: #ifndef DOXYGEN
                    121: struct MimeHeaderField2received_info {
                    122:        Charset* source_charset;
                    123:        HashStringValue* received;
                    124: };
                    125: #endif
1.47    ! paf       126: static void MimeHeaderField2received(const char* must_clone_name, const char* must_clone_value, gpointer data) {
1.45      paf       127:        MimeHeaderField2received_info& info=*static_cast<MimeHeaderField2received_info *>(data);
1.1       paf       128: 
1.47    ! paf       129:        putReceived(*info.source_charset, 
1.45      paf       130:                *info.received, 
1.47    ! paf       131:                must_clone_name, must_clone_value, 0, true/*nameInUpperCase*/);
1.1       paf       132: }
                    133: 
1.45      paf       134: static void parse(Request& r, GMimeStream *stream, HashStringValue& received);
1.1       paf       135: 
                    136: #ifndef DOXYGEN
1.45      paf       137: struct MimePart2body_info {
                    138:        Request* r;
                    139:        HashStringValue* body;
1.1       paf       140:        int partCounts[P_TYPES_COUNT];
                    141: };
                    142: #endif
1.27      paf       143: /// @test why no copy to global in P_HTML putReceived?
1.47    ! paf       144: static void MimePart2body(GMimePart *part, gpointer data) {
1.45      paf       145:        MimePart2body_info& info=*static_cast<MimePart2body_info *>(data);
                    146:        Charset& source_charset=info.r->charsets.source();
1.1       paf       147: 
                    148:        if(const GMimeContentType *type=g_mime_part_get_content_type(part)) {
                    149:                if(g_mime_content_type_is_type(type, "multipart", "*"))
                    150:                        return; // skipping frames
                    151: 
1.45      paf       152:                PartType partType;
                    153:                if(g_mime_content_type_is_type(type, "text", "plain"))
                    154:                        partType=P_TEXT;
                    155:                else if(g_mime_content_type_is_type(type, "text", "html"))
                    156:                        partType=P_HTML;
                    157:                else if(g_mime_content_type_is_type(type, "message", "*"))
                    158:                        partType=P_MESSAGE;
                    159:                else
                    160:                        partType=P_FILE;
1.1       paf       161:                
                    162:                // partName
1.45      paf       163:                const char* partName;
1.1       paf       164:                char partNameBuf[MAX_STRING];
1.45      paf       165:                const char* partNameStart=part_name_begins[partType];
                    166:                if(int partNo=info.partCounts[partType]++) {
1.1       paf       167:                        snprintf(partNameBuf, MAX_STRING, "%s%d", partNameStart, partNo);
                    168:                        partName=partNameBuf;
                    169:                } else
                    170:                        partName=partNameStart;
                    171:                
                    172:                // $.partX[ 
1.47    ! paf       173:                VHash* vpartX(new VHash);  putReceived(source_charset, *info.body, partName, vpartX);
1.45      paf       174:                HashStringValue& partX=vpartX->hash();
1.1       paf       175:                {
                    176:                        // $.raw[
1.47    ! paf       177:                        VHash* vraw(new VHash);  putReceived(source_charset, partX, RAW_NAME, vraw);
1.45      paf       178:                        MimeHeaderField2received_info hfr_info={
1.47    ! paf       179:                                &source_charset, &vraw->hash()};
1.45      paf       180:                        g_mime_header_foreach(part->headers, MimeHeaderField2received, &hfr_info);
1.1       paf       181:                }
1.45      paf       182:                const char* content_filename=0;
1.1       paf       183:                {
                    184:                        // $.content-type[ 
1.47    ! paf       185:                        VHash* vcontent_type(new VHash);  putReceived(source_charset, partX, "content-type", vcontent_type);
1.45      paf       186:                        HashStringValue& content_type=vcontent_type->hash(); 
1.1       paf       187:                        {
                    188:                                // $.value[text/plain] 
                    189:                                char value[MAX_STRING];
                    190:                                snprintf(value, MAX_STRING, "%s/%s", 
                    191:                                        type->type?type->type:"x-unknown",
                    192:                                        type->subtype?type->subtype:"x-unknown");
1.45      paf       193:                                putReceived(source_charset, content_type, VALUE_NAME, value);
1.1       paf       194:                        }
                    195:                        GMimeParam *param=type->params;
                    196:                        while(param) {
                    197:                                // $.charset[windows-1251]  && co
1.45      paf       198:                                putReceived(source_charset, content_type, param->name, param->value, true);
1.1       paf       199:                                if(strcasecmp(param->name, "name")==0)
                    200:                                        content_filename=param->value;
                    201:                                param=param->next;
                    202:                        }
                    203:                }
                    204:                // $.description
1.45      paf       205:                putReceived(source_charset, partX, "description", part->description);
1.1       paf       206:                // $.content-id
1.45      paf       207:                putReceived(source_charset, partX, "content-id", part->content_id);
1.1       paf       208:                // $.content-md5
1.45      paf       209:                putReceived(source_charset, partX, "content-md5", part->content_md5);
1.1       paf       210:                // $.content-location
1.45      paf       211:                putReceived(source_charset, partX, "content-location", part->content_location);
1.1       paf       212:                
                    213:                // todo GMimePart:
                    214:                //   GMimePartEncodingType encoding;
                    215:                //   GMimeDisposition *disposition;
                    216:                if(part->disposition) {
                    217:                        GMimeParam *param=part->disposition->params;
                    218:                        while(param) {
                    219:                                // $.charset[windows-1251]  && co
                    220:                                if(strcasecmp(param->name, "filename")==0)
                    221:                                        content_filename=param->value;
                    222:                                param=param->next;
                    223:                        }
                    224:                }
                    225: 
                    226:                // MESSAGE
                    227:                if(partType==P_MESSAGE) {
                    228:                        if(part->content)
                    229:                                if(GMimeStream *stream=part->content->stream)
1.45      paf       230:                                        parse(*info.r, stream, partX);
1.1       paf       231:                } else {
                    232:                        // $.value[string|file]
                    233:                        size_t buf_len;
1.45      paf       234:                        const char* local_buf=(const char*)g_mime_part_get_content(part, &buf_len);
1.1       paf       235:                        if(partType==P_FILE) {
1.45      paf       236:                                VFile* vfile(new VFile);
1.47    ! paf       237:                                char *pooled_buf=pa_strdup(local_buf, buf_len);
        !           238:                                VString* vcontent_type=content_filename?
        !           239:                                        new VString(info.r->mime_type_of(content_filename)):0;
1.45      paf       240:                                vfile->set(true/*tainted*/, pooled_buf, buf_len, content_filename, vcontent_type);
1.47    ! paf       241:                                putReceived(source_charset, partX, VALUE_NAME, vfile);
1.1       paf       242:                        } else {
                    243:                                // P_TEXT, P_HTML
1.45      paf       244:                                putReceived(source_charset, partX, VALUE_NAME,(const char*)local_buf, buf_len);
1.1       paf       245:                        }
                    246:                }
                    247:        }
                    248: }
                    249: 
1.30      paf       250: int gmt_offset() {
                    251: #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
1.31      paf       252:        return timezone+(daylight?60*60*(timezone<0?-1:timezone>0?+1:0):0);
1.30      paf       253: #else
                    254:        time_t t=time(0);
                    255:        tm *tm=localtime(&t);
                    256: #if defined(HAVE_TM_GMTOFF)
                    257:        return tm->tm_gmtoff;
                    258: #elif defined(HAVE_TM_TZADJ)
                    259:        return tm->tm_tzadj;
                    260: #else
                    261: #error neither HAVE_TIMEZONE&HAVE_DAYIGHT nor HAVE_TM_GMTOFF nor HAVE_TM_TZADJ defined
                    262: #endif
                    263: #endif
                    264: }
                    265: 
1.45      paf       266: static void parse(Request& r, GMimeStream *stream, HashStringValue& received) {
                    267:        Charset& source_charset=r.charsets.source();
1.1       paf       268: 
                    269:        GMimeMessage *message=g_mime_parser_construct_message(stream);
                    270:        try {
                    271:                const GMimeMessageHeader *messageHeader=message->header;
                    272:                if(!messageHeader)
                    273:                        return;
                    274: 
                    275:                // firstly user-defined strings go
                    276:                //  user headers
                    277:                {
                    278:                        // $.raw[
1.47    ! paf       279:                        VHash* vraw(new VHash);  putReceived(source_charset, received, RAW_NAME, vraw);
1.45      paf       280:                        MimeHeaderField2received_info hfr_info={
1.47    ! paf       281:                                &source_charset, &vraw->hash()};
1.45      paf       282:                        g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &hfr_info);
1.1       paf       283:                }
                    284: 
                    285:                // maybe-todo-recipients
                    286:                // x(messageHeader->recipients)
                    287: 
                    288:                //  secondly standard headers&body go
                    289:                //  standard header
                    290:                // .from
1.45      paf       291:                putReceived(source_charset, received, "from", messageHeader->from);
1.1       paf       292:                // .reply-to
1.45      paf       293:                putReceived(source_charset, received, "reply-to", messageHeader->reply_to);
1.1       paf       294:                // .to
                    295:                // todo: messageHeader->recipients
                    296:                // .subject
1.45      paf       297:                putReceived(source_charset, received, "subject", messageHeader->subject);
1.1       paf       298:                // .date(date+gmt_offset)
                    299:                int tt_offset = 
                    300:                        ((messageHeader->gmt_offset / 100) *(60 * 60)) 
                    301:                        +(messageHeader->gmt_offset % 100) * 60;                        
1.30      paf       302: 
1.1       paf       303:                putReceived(received, "date", 
                    304:                        messageHeader->date // local sender
                    305:                        -tt_offset // move local sender to GMT sender
1.30      paf       306:                        -gmt_offset() // move GMT sender to our local time
1.1       paf       307:                );
                    308:                // .message-id
1.45      paf       309:                putReceived(source_charset, received, "message-id", messageHeader->message_id);
1.1       paf       310: 
                    311:                // .body[part/parts
                    312:                GMimePart *part=message->mime_part;
                    313:                const GMimeContentType *type=g_mime_part_get_content_type(part);
1.45      paf       314:                MimePart2body_info info={&r, &received};
1.1       paf       315:                g_mime_part_foreach(part, MimePart2body, &info);
                    316: 
                    317:                // normal unref
                    318:                g_mime_object_unref(GMIME_OBJECT(message));
                    319:        } catch(...) {
                    320:                // abnormal unref
                    321:                g_mime_object_unref(GMIME_OBJECT(message));
                    322:        }
                    323: }
                    324: #endif
                    325: 
                    326: 
                    327: 
1.23      paf       328: void VMail::fill_received(Request& r) {
1.1       paf       329:        // store letter to received
1.3       paf       330: #ifdef WITH_MAILRECEIVE
1.45      paf       331:        if(r.request_info.mail_received) {
1.1       paf       332:                // init
                    333:                g_mime_init(GMIME_INIT_FLAG_UTF8);
                    334: 
                    335:                // create stream with CRLF filter
                    336:                GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
                    337:                GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
                    338:                GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
                    339:                g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
1.45      paf       340:                g_mime_stream_unref(stream);
1.1       paf       341:                stream = istream;
                    342:                try {
                    343:                        // parse incoming stream
1.45      paf       344:                        parse(r, stream, vreceived->hash());
1.1       paf       345:                        // normal stream free 
1.45      paf       346:                        g_mime_stream_unref(stream);
1.1       paf       347:                } catch(...) {
                    348:                        // abnormal stream free 
1.45      paf       349:                        g_mime_stream_unref(stream);
1.1       paf       350:                }
                    351:        }
                    352: #endif
                    353: }
                    354: 
1.9       paf       355: typedef int (*string_contains_char_which_check)(int);
1.45      paf       356: static bool string_contains_char_which(const char* string, string_contains_char_which_check check) {
1.9       paf       357:        while(char c=*string++) {
                    358:                if(check(c))
                    359:                        return true;
                    360:        }
                    361:        return false;
                    362: }
1.32      paf       363: static char *trimBoth(char *s) {
                    364:        // sanity check
                    365:        if(!s)
                    366:                return 0;
                    367: 
                    368:        // trim head whitespace
                    369:        while(*s && isspace(*s))
                    370:                s++;
                    371:        // trim tail whitespace
                    372:        char *tail=s+strlen(s);
                    373:        if(tail>s) {
                    374:                do {
                    375:                        --tail;
                    376:                        if(isspace(*tail))
                    377:                                *tail=0;
                    378:                } while(tail>s);
                    379:        }
                    380:        // return it
                    381:        return s;
                    382: }
1.45      paf       383: static void extractEmail(String& result, char *email) {
1.32      paf       384:        email=trimBoth(email);
1.45      paf       385:        result.append_help_length(email, 0, String::L_TAINTED);
1.9       paf       386: 
                    387:        /*
                    388:                http://www.faqs.org/rfcs/rfc822.html
                    389: 
                    390:                addr-spec   =  local-part "@" domain        ; global address
                    391:        
                    392:                local-part  =  word *("." word)             ; uninterpreted case-preserved
                    393:                word        =  atom / quoted-string
                    394: 
                    395:                domain      =  sub-domain *("." sub-domain)
                    396:                sub-domain  =  domain-ref / domain-literal
                    397:                domain-ref  =  atom                         ; symbolic reference
                    398: 
                    399:         domain-literal << ignoring for now
                    400:                quoted-string in word << ignoring for now
                    401: 
                    402:                atom        =  1*<any CHAR except specials, SPACE and CTLs>  << the ONLY to check
                    403: 
                    404:                specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
                    405:                  /  "," / ";" / ":" / "\" / <">  ;  string, to use
                    406:                  /  "." / "[" / "]"              ;  within a word.
                    407: 
                    408:        */
1.45      paf       409:        const char* exception_type="email.format";
1.9       paf       410:        if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17      paf       411:                throw Exception(exception_type,
1.9       paf       412:                        &result,
1.32      paf       413:                        "email contains bad characters (specials)");
1.9       paf       414:        if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17      paf       415:                throw Exception(exception_type,
1.9       paf       416:                        &result,
1.32      paf       417:                        "email contains bad characters (whitespace)");
1.9       paf       418:        if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17      paf       419:                throw Exception(exception_type,
1.9       paf       420:                        &result,
1.32      paf       421:                        "email contains bad characters (control)");
1.16      paf       422:        if(result.is_empty())
1.17      paf       423:                throw Exception(exception_type,
1.45      paf       424:                        0,
1.16      paf       425:                        "email is empty");
1.39      paf       426: }
                    427: 
1.45      paf       428: static const String& extractEmails(const String& string) {
                    429:        char *emails=string.cstrm();
                    430:        String& result=*new String;
1.39      paf       431:        while(char *email=lsplit(&emails, ',')) {
                    432:                rsplit(email, '>');
                    433:                if(char *in_brackets=lsplit(email, '<'))
                    434:                        email=in_brackets;
                    435:                if(!result.is_empty())
                    436:                        result<<",";
1.45      paf       437:                extractEmail(result, email);
1.39      paf       438:        }
1.9       paf       439: 
                    440:        return result;
                    441: }
1.45      paf       442: 
                    443: #ifndef DOXYGEN
                    444: struct Store_message_element_info {
                    445:        Request_charsets& charsets;
                    446:        String& header;
                    447:        const String* & from;
                    448:        bool extract_to; String* & to;
                    449:        const String* errors_to;
                    450:        bool mime_version_specified;
                    451:        ArrayValue* parts[P_TYPES_COUNT];
                    452:        int parts_count;
                    453:        bool backward_compatibility;
                    454:        Value* content_type;
                    455: 
                    456:        Store_message_element_info(
                    457:                Request_charsets& acharsets,
                    458:                String& aheader,
                    459:                const String* & afrom,
                    460:                bool aextract_to, String* & ato
                    461:                ):
                    462:                
                    463:                charsets(acharsets),
                    464:                header(aheader),
                    465:                from(afrom),
                    466:                extract_to(aextract_to), to(ato),
                    467:                errors_to(0),
                    468:                mime_version_specified(false),
                    469:                parts_count(0),
                    470:                backward_compatibility(false), content_type(0) {
                    471:        }
                    472: };
                    473: #endif
                    474: static void store_message_element(HashStringValue::key_type raw_element_name, 
                    475:                                  HashStringValue::value_type element_value, 
                    476:                                  Store_message_element_info *info) {
                    477:        const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
                    478:                info->charsets.source(), String::CC_LOWER);
1.1       paf       479: 
                    480:        // exclude internals
1.5       paf       481:        if(low_element_name==CHARSET_NAME
                    482:                || low_element_name==VALUE_NAME
                    483:                || low_element_name==RAW_NAME
                    484:                || low_element_name=="date")
1.1       paf       485:                return;
                    486: 
                    487:        // grep parts
                    488:        for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.45      paf       489:                if(low_element_name.starts_with(part_name_begins[pt])) {
1.29      paf       490:                        // check that $.message# '#' is digit
1.45      paf       491:                        size_t start_len=strlen(part_name_begins[pt]);
                    492:                        if(low_element_name.length()>start_len) {
                    493:                                const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.29      paf       494:                                if(!isdigit(*at_num))
                    495:                                        continue;
                    496:                        }
1.45      paf       497:                        *info->parts[pt]+=element_value;
                    498:                        info->parts_count++;
1.1       paf       499:                        return;
                    500:                }
                    501:        }
                    502: 
1.10      paf       503:        // fetch some special headers
1.45      paf       504:        if(low_element_name=="from")
                    505:                info->from=&extractEmails(element_value->as_string());
                    506:        if(info->extract_to) { // defined only on WIN32, see mail.C [collecting info for RCPT to-s]
1.39      paf       507:                bool is_to=low_element_name=="to" ;
                    508:                bool is_cc=low_element_name=="cc" ;
                    509:                bool is_bcc=low_element_name=="bcc" ;
                    510:                if(is_to||is_cc||is_bcc) {
1.45      paf       511:                        if(!info->to)
                    512:                                info->to=new String;
1.39      paf       513:                        else
1.45      paf       514:                                *info->to << ",";
                    515:                        *info->to << extractEmails(element_value->as_string());
1.39      paf       516:                }
                    517: 
                    518:                if(is_bcc) // blinding it
1.45      paf       519:                        return;
1.39      paf       520:        }
1.12      paf       521:        if(low_element_name=="errors-to")
1.45      paf       522:                info->errors_to=&extractEmails(element_value->as_string());     
1.37      paf       523:        if(low_element_name=="mime-version")
1.45      paf       524:                info->mime_version_specified=true;
1.1       paf       525: 
1.45      paf       526:        // has content type?
                    527:        if(low_element_name==CONTENT_TYPE_NAME) {
                    528:                info->content_type=element_value;
                    529:                if(info->backward_compatibility)
                    530:                        return;
1.39      paf       531:        }
1.1       paf       532: 
1.45      paf       533:        // preparing header line
                    534:        const String& source_line=attributed_meaning_to_string(*element_value,
                    535:                String::L_PASS_APPENDED/*does not matter, would cstr(AS_IS) right away*/);
                    536:        const char* source_line_cstr=source_line.cstr();
                    537:        String::C mail=Charset::transcode(
                    538:                String::C(source_line_cstr, source_line.length()),
                    539:                info->charsets.source(), 
                    540:                info->charsets.mail());
                    541:        String& mail_line=*new String;
                    542:        ///@todo can convert to append_help_length if transcode would return zero-terminated
                    543:        mail_line.append_know_length(mail.str, mail.length, String::L_MAIL_HEADER);
                    544: 
                    545:        // append header line
                    546:        info->header 
                    547:                << raw_element_name 
                    548:                << ":" << mail_line.cstr(String::L_UNSPECIFIED, 0, &info->charsets)
                    549:                << "\n";
                    550: }
                    551: 
                    552: static const String& file_value_to_string(Request& r, Value* send_value) {
                    553:        VFile* vfile;
                    554:        const String* file_name=0;
                    555:        Value* vformat=0;
                    556:        if(HashStringValue *send_hash=send_value->get_hash()) { // hash
1.1       paf       557:                // $.value
1.45      paf       558:                if(Value* value=send_hash->get(value_name))
                    559:                        vfile=value->as_vfile(String::L_AS_IS);
1.1       paf       560:                else
                    561:                        throw Exception("parser.runtime",
1.45      paf       562:                                0,
1.1       paf       563:                                "file part has no $value");
                    564: 
                    565:                // $.format
1.45      paf       566:                vformat=send_hash->get(format_name);
1.1       paf       567: 
1.6       paf       568:                // $.name
1.45      paf       569:                if(Value* vfile_name=send_hash->get(name_name)) // $name specified 
1.1       paf       570:                        file_name=&vfile_name->as_string();
1.28      paf       571:        } else  // must be VFile then
1.45      paf       572:                vfile=send_value->as_vfile(String::L_AS_IS);
1.28      paf       573: 
                    574:        if(!file_name)
1.45      paf       575:                file_name=&vfile->fields().get(name_name)->as_string();
1.28      paf       576: 
1.45      paf       577:        const char* file_name_cstr=file_name->cstr();
1.1       paf       578: 
1.45      paf       579:        String& result=*new String;
1.1       paf       580: 
                    581:        // content-type: application/octet-stream
                    582:        result << "content-type: " << r.mime_type_of(file_name_cstr) 
                    583:                << "; name=\"" << file_name_cstr << "\"\n";
                    584:        // content-disposition: attachment; filename="user_file_name"
1.35      paf       585:        result << 
                    586:                CONTENT_DISPOSITION_NAME ": "CONTENT_DISPOSITION_VALUE"; "
                    587:                CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.1       paf       588: 
1.45      paf       589:        const String* type=vformat?&vformat->as_string():0;
1.1       paf       590:        if(!type/*default = uue*/ || *type=="uue") {
1.45      paf       591:                pa_uuencode(result, *file_name, *vfile);
1.1       paf       592:        } else // for now
                    593:                throw Exception("parser.runtime",
                    594:                        type,
                    595:                        "unknown attachment encode format");
                    596:        
                    597:        return result;
                    598: }
                    599: 
1.45      paf       600: static const String& text_value_to_string(Request& r,
                    601:                                          PartType pt, Value* send_value,
                    602:                                          Store_message_element_info& info) {
                    603:        String& result=*new String;
1.1       paf       604: 
1.45      paf       605:        Value* text_value;
                    606:        if(HashStringValue* send_hash=send_value->get_hash()) {
1.1       paf       607:                // $.USER-HEADERS
1.45      paf       608:                info.content_type=0; info.backward_compatibility=false; // reset
1.1       paf       609:                send_hash->for_each(store_message_element, &info);
                    610:                // $.value
1.45      paf       611:                text_value=send_hash->get(value_name);
1.1       paf       612:                if(!text_value)
                    613:                        throw Exception("parser.runtime",
1.45      paf       614:                                0,
                    615:                                "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.1       paf       616:        } else
1.45      paf       617:                text_value=send_value;
1.1       paf       618: 
1.45      paf       619:        if(!info.content_type) {
                    620:                result 
                    621:                        << "content-type: text/" << (pt==P_TEXT?"plain":"html")
                    622:                        << "; charset=" << info.charsets.mail().NAME()
                    623:                        << "\n";
1.1       paf       624:        }
                    625: 
                    626:        // header|body separator
                    627:        result << "\n"; 
                    628: 
                    629:        // body
1.45      paf       630:        const String* body;
1.1       paf       631:        switch(pt) {
                    632:        case P_TEXT:
1.41      paf       633:                body=&text_value->as_string();
1.1       paf       634:                break;
                    635:        case P_HTML: 
                    636:                {
1.45      paf       637:                        Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
                    638:                        if(Junction* junction=text_value->get_junction())
1.41      paf       639:                                body=&r.process_to_string(*text_value);
1.26      paf       640:                        else
1.1       paf       641:                                throw Exception("parser.runtime",
1.45      paf       642:                                        0,
1.1       paf       643:                                        "html part value must be code");
                    644: 
                    645:                        break;
                    646:                }
1.41      paf       647:        }
                    648:        if(body) {
1.45      paf       649:                const char* body_cstr=strdup(body->cstr(String::L_UNSPECIFIED));  // body
                    650:                String::C mail=Charset::transcode(
                    651:                        String::C(body_cstr, strlen(body_cstr)),
                    652:                        r.charsets.source(),
                    653:                        r.charsets.mail()/*always set - either mail.charset or $request:charset*/);
                    654:                ///@todo
                    655:                result.append_know_length(mail.str, mail.length, String::L_CLEAN);
1.1       paf       656:        }
                    657: 
                    658:        return result;
                    659: };
                    660: 
                    661: /// @todo files and messages in order (file, file2, ...)
1.45      paf       662: const String& VMail::message_hash_to_string(Request& r,
                    663:                                            HashStringValue* message_hash, int level, 
                    664:                                            const String* & from, bool extract_to, String* & to) {
1.34      paf       665:        
1.1       paf       666:        if(!message_hash)
                    667:                throw Exception("parser.runtime",
1.45      paf       668:                        0,
1.1       paf       669:                        "message must be hash");
                    670: 
1.45      paf       671:        String& result=*new String;
1.1       paf       672: 
1.45      paf       673:        if(Value* vrecodecharset_name=message_hash->get(charset_name))
                    674:                r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
                    675:                        .change_case(r.charsets.source(), String::CC_UPPER)));
1.1       paf       676:        else
1.45      paf       677:                r.charsets.set_mail(r.charsets.source());
                    678:        // no big deal that we leave it set. they wont miss this point which would reset it
1.1       paf       679: 
1.45      paf       680:        Store_message_element_info info(r.charsets, 
                    681:                result, from, extract_to, to);
1.1       paf       682:        {
1.45      paf       683:                // for backward compatibilyty $.body+$.content-type -> 
                    684:                // $.text[$.value[] $.content-type[]]
                    685: 
1.1       paf       686:                for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45      paf       687:                        info.parts[pt]=new ArrayValue(1);
                    688: 
                    689:                Value* body=message_hash->get("body");
                    690:                if(body) {
                    691:                        message_hash->remove("body");
                    692:                        info.backward_compatibility=true;
                    693:                }               
1.1       paf       694:                message_hash->for_each(store_message_element, &info);
1.45      paf       695: 
                    696:                if(body) {
                    697:                        VHash& text_part=*new VHash(); 
                    698:                        HashStringValue& hash=text_part.hash();
                    699:                        hash.put(value_name, body);
                    700:                        if(info.content_type)
                    701:                                hash.put(content_type_name, info.content_type);
                    702:                        
                    703:                        *info.parts[P_TEXT]+=&text_part;
                    704:                        info.parts_count++;
                    705:                }
                    706: 
1.10      paf       707:                if(!info.errors_to)
                    708:                        result << "errors-to: postmaster\n"; // errors-to: default
1.37      paf       709:                if(!info.mime_version_specified)
                    710:                        result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1       paf       711:        }
                    712: 
1.45      paf       713:        int textCount=info.parts[P_TEXT]->count();
1.1       paf       714:        if(textCount>1)
                    715:                throw Exception("parser.runtime",
1.45      paf       716:                        0,
1.1       paf       717:                        "multiple text parts not supported, use file part");
1.45      paf       718:        int htmlCount=info.parts[P_HTML]->count();
1.1       paf       719:        if(htmlCount>1)
                    720:                throw Exception("parser.runtime",
1.45      paf       721:                        0,
1.1       paf       722:                        "multiple html parts not supported, use file part");
                    723: 
                    724: 
                    725:        bool multipart=info.parts_count>1;
                    726:        bool alternative=textCount && htmlCount;
                    727:        // header
                    728:        char *boundary=0;
                    729:        if(multipart) {
1.45      paf       730:                boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1       paf       731:                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                    732:                // multi-part
1.45      paf       733:                result 
                    734:                        << "content-type: multipart/mixed; boundary=\"" 
                    735:                        << boundary << "\"\n"
                    736:                                "\n"
                    737:                                "This is a multi-part message in MIME format.";
1.1       paf       738:        }
                    739: 
                    740:        // alternative or not
                    741:        {
                    742:                if(alternative) {
1.45      paf       743:                        result << "\n\n--" << boundary << "\n" // intermediate boundary
                    744:                                "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1       paf       745:                } 
                    746:                for(int i=0; i<2; i++) {
                    747:                        PartType pt=i==0?P_TEXT:P_HTML;
1.45      paf       748:                        if(info.parts[pt]->count()) {
1.1       paf       749:                                if(alternative)
                    750:                                        result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
                    751:                                else if(boundary)
                    752:                                        result << "\n\n--" << boundary << "\n";  // intermediate boundary
1.45      paf       753:                                result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1       paf       754:                        }
                    755:                }
                    756:                if(alternative)
                    757:                        result << "\n\n--ALT" << boundary << "--\n";
                    758:        }
                    759: 
                    760:        // files
                    761:        {
1.45      paf       762:                ArrayValue& files=*info.parts[P_FILE];
                    763:                for(size_t i=0; i<files.count(); i++) {
1.1       paf       764:                        if(boundary)
                    765:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
1.45      paf       766:                        result << file_value_to_string(r, files.get(i));
1.1       paf       767:                }
                    768:        }
                    769: 
                    770:        // messages
                    771:        {
1.45      paf       772:                ArrayValue& messages=*info.parts[P_MESSAGE];
                    773:                for(size_t i=0; i<messages.count(); i++) {
1.1       paf       774:                        if(boundary)
                    775:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    776:                        
1.45      paf       777:                        const String* dummy_from;
                    778:                        String* dummy_to;
                    779:                        result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
                    780:                                dummy_from, false, dummy_to);
1.1       paf       781:                }
                    782:        }
                    783:        
                    784:        // tailer
                    785:        if(boundary)
                    786:                result << "\n\n--" << boundary << "--\n"; // finish boundary
                    787: 
                    788:        // return
                    789:        return result;
                    790: }
                    791: 
                    792: 
1.45      paf       793: Value* VMail::get_element(const String& aname, Value& aself, bool looking_up) {
1.1       paf       794:        // $fields
1.3       paf       795: #ifdef WITH_MAILRECEIVE
1.1       paf       796:        if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.45      paf       797:                return vreceived;
1.1       paf       798: #endif
                    799: 
                    800:        // $CLASS,$method
1.45      paf       801:        if(Value* result=VStateless_class::get_element(aname, aself, looking_up))
1.1       paf       802:                return result;
                    803: 
                    804:        return 0;
                    805: }
                    806: 
1.3       paf       807: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1       paf       808: #      define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
                    809: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
                    810: #      ifdef _DEBUG
                    811: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
                    812: #      else
                    813: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
                    814: #      endif
                    815: #endif

E-mail: