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

1.1       paf         1: /**    @file
                      2:        Parser: @b mail class.
                      3:        relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
                      4: 
                      5:        Copyright(c) 2001, 2002 ArtLebedev Group(http://www.artlebedev.com)
                      6:        Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
                      7: */
1.13      paf         8: 
1.28    ! paf         9: static const char* IDENT_VMAIL_C="$Date: 2002/09/10 08:41:00 $";
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.1       paf        26: #endif
                     27: 
                     28: // defines
                     29: 
                     30: #define RAW_NAME "raw"
                     31: 
                     32: // internals
                     33: 
                     34: enum PartType {
                     35:        P_TEXT,
                     36:        P_HTML,
                     37:        P_FILE,
                     38:        P_MESSAGE,
                     39: 
                     40:        P_TYPES_COUNT
                     41: };
                     42: 
                     43: static const char * const part_name_starts[P_TYPES_COUNT]={"text", "html", "file", "message"};
                     44: 
                     45: // VMail
                     46: 
                     47: extern Methoded *mail_base_class;
                     48: 
                     49: VMail::VMail(Pool& apool) : VStateless_class(apool, 0, mail_base_class),
                     50:        vreceived(apool) {
                     51: }
                     52: 
1.3       paf        53: #ifdef WITH_MAILRECEIVE
1.1       paf        54: 
                     55: static const String& maybeUpperCase(Pool& pool, const String& src, bool toUpperCase) {
                     56:        return toUpperCase?src.change_case(pool, String::CC_UPPER):src;
                     57: }
                     58: 
                     59: static void UTF8toSource(Pool& pool, const char *source_body, size_t source_content_length,
                     60:                                                 const void *& dest_body, size_t& dest_content_length) {
                     61:        if(source_body) {
                     62:                if(!source_content_length)
                     63:                        source_content_length=strlen(source_body);
                     64:                Charset::transcode(pool,
                     65:                                                                           *utf8_charset, source_body, source_content_length,
                     66:                                                                           pool.get_source_charset(), dest_body, dest_content_length);
                     67:        } else {
                     68:                dest_body=0;
                     69:                dest_content_length=0;
                     70:        }
                     71: }
                     72: 
                     73: static void putReceived(Hash& received, const char *name, Value *value, bool nameToUpperCase=false) {
                     74:        Pool& pool=received.pool();
                     75:        if(name && value) {
                     76:                received.put(
                     77:                        maybeUpperCase(pool, String::OnPool(pool, name, 0, true/*tainted*/), nameToUpperCase),
                     78:                        value);
                     79:        }
                     80: }
                     81: 
                     82: static void putReceived(Hash& received, const char *name, const char *value, size_t value_size=0, bool nameToUpperCase=false) {
                     83:        if(value) {
                     84:                Pool& pool=received.pool();
                     85: 
                     86:                const void *value_dest_body;
                     87:                size_t value_dest_content_length;
1.21      paf        88:                // UTF8toSource(pool, value, value_size, value_dest_body, value_dest_content_length);
                     89:                value_dest_body=value;
                     90:                value_dest_content_length=value_size;
1.1       paf        91:                
                     92:                putReceived(received, name, 
                     93:                        new(pool) VString(
                     94:                                String::OnPool(pool,(const char *)value_dest_body, value_dest_content_length, true/*tainted*/)));
                     95:        }
                     96: }
                     97: 
                     98: static void putReceived(Hash& received, const char *name, time_t value) {
                     99:        Pool& pool=received.pool();
                    100:        if(name)
                    101:                received.put(String::OnPool(pool, name, 0, true/*tainted*/), new(pool) VDate(pool, value));
                    102: }
                    103: 
                    104: static void MimeHeaderField2received(const char *name, const char *value, gpointer data) {
                    105:        Hash& received=*static_cast<Hash *>(data);
                    106: 
                    107:        putReceived(received, name, value, 0, true/*nameInUpperCase*/);
                    108: }
                    109: 
1.23      paf       110: static void parse(Request& r, GMimeStream *stream, Hash& received);
1.1       paf       111: 
                    112: #ifndef DOXYGEN
                    113: struct MimePart2bodyInfo {
1.23      paf       114:        Request *r;
1.1       paf       115:        Hash *body;
                    116:        int partCounts[P_TYPES_COUNT];
                    117: };
                    118: #endif
1.27      paf       119: /// @test why no copy to global in P_HTML putReceived?
1.1       paf       120: static void MimePart2body(GMimePart *part,
                    121:                                                  gpointer data) {
                    122:        MimePart2bodyInfo& i=*static_cast<MimePart2bodyInfo *>(data);
                    123:        Pool& pool=i.body->pool();
                    124: 
                    125:        if(const GMimeContentType *type=g_mime_part_get_content_type(part)) {
                    126:                if(g_mime_content_type_is_type(type, "multipart", "*"))
                    127:                        return; // skipping frames
                    128: 
                    129:                PartType partType;
                    130:                if(g_mime_content_type_is_type(type, "text", "plain"))
                    131:                        partType=P_TEXT;
                    132:                else if(g_mime_content_type_is_type(type, "text", "html"))
                    133:                        partType=P_HTML;
                    134:                else if(g_mime_content_type_is_type(type, "message", "*"))
                    135:                        partType=P_MESSAGE;
                    136:                else
                    137:                        partType=P_FILE;
                    138:                
                    139:                // partName
                    140:                const char *partName;
                    141:                char partNameBuf[MAX_STRING];
                    142:                const char *partNameStart=part_name_starts[partType];
                    143:                if(int partNo=i.partCounts[partType]++) {
                    144:                        snprintf(partNameBuf, MAX_STRING, "%s%d", partNameStart, partNo);
                    145:                        partName=partNameBuf;
                    146:                } else
                    147:                        partName=partNameStart;
                    148:                
                    149:                // $.partX[ 
                    150:                VHash& vpartX=*new(pool) VHash(pool);  putReceived(*i.body, partName, &vpartX);
                    151:                Hash& partX=vpartX.hash(0);                     
                    152:                {
                    153:                        // $.raw[
                    154:                        VHash& vraw=*new(pool) VHash(pool);  putReceived(partX, RAW_NAME, &vraw);
                    155:                        g_mime_header_foreach(part->headers, MimeHeaderField2received, &vraw.hash(0));
                    156:                }
                    157:                const char *content_filename=0;
                    158:                {
                    159:                        // $.content-type[ 
                    160:                        VHash& vcontent_type=*new(pool) VHash(pool);  putReceived(partX, "content-type", &vcontent_type);
                    161:                        Hash& content_type=vcontent_type.hash(0); 
                    162:                        {
                    163:                                // $.value[text/plain] 
                    164:                                char value[MAX_STRING];
                    165:                                snprintf(value, MAX_STRING, "%s/%s", 
                    166:                                        type->type?type->type:"x-unknown",
                    167:                                        type->subtype?type->subtype:"x-unknown");
                    168:                                putReceived(content_type, VALUE_NAME, value);
                    169:                        }
                    170:                        GMimeParam *param=type->params;
                    171:                        while(param) {
                    172:                                // $.charset[windows-1251]  && co
                    173:                                putReceived(content_type, param->name, param->value, true);
                    174:                                if(strcasecmp(param->name, "name")==0)
                    175:                                        content_filename=param->value;
                    176:                                param=param->next;
                    177:                        }
                    178:                }
                    179:                // $.description
                    180:                putReceived(partX, "description", part->description);
                    181:                // $.content-id
                    182:                putReceived(partX, "content-id", part->content_id);
                    183:                // $.content-md5
                    184:                putReceived(partX, "content-md5", part->content_md5);
                    185:                // $.content-location
                    186:                putReceived(partX, "content-location", part->content_location);
                    187:                
                    188:                // todo GMimePart:
                    189:                //   GMimePartEncodingType encoding;
                    190:                //   GMimeDisposition *disposition;
                    191:                if(part->disposition) {
                    192:                        GMimeParam *param=part->disposition->params;
                    193:                        while(param) {
                    194:                                // $.charset[windows-1251]  && co
                    195:                                if(strcasecmp(param->name, "filename")==0)
                    196:                                        content_filename=param->value;
                    197:                                param=param->next;
                    198:                        }
                    199:                }
                    200: 
                    201:                // MESSAGE
                    202:                if(partType==P_MESSAGE) {
                    203:                        if(part->content)
                    204:                                if(GMimeStream *stream=part->content->stream)
1.23      paf       205:                                        parse(*i.r, stream, partX);
1.1       paf       206:                } else {
                    207:                        // $.value[string|file]
                    208:                        size_t buf_len;
1.15      paf       209:                        const void *local_buf=g_mime_part_get_content(part, &buf_len);
1.1       paf       210:                        if(partType==P_FILE) {
                    211:                                VFile& vfile=*new(pool) VFile(pool);
1.15      paf       212:                                char *global_buf=(char *)pool.malloc(buf_len);
                    213:                                memcpy(global_buf, local_buf, buf_len);
1.23      paf       214:                                VString *vcontent_type=content_filename?
                    215:                                        new(pool) VString(i.r->mime_type_of(content_filename)):0;
                    216:                                vfile.set(true/*tainted*/, global_buf, buf_len, content_filename, vcontent_type);
1.1       paf       217:                                putReceived(partX, VALUE_NAME, &vfile);
                    218:                        } else {
                    219:                                // P_TEXT, P_HTML
1.15      paf       220:                                putReceived(partX, VALUE_NAME,(const char*)local_buf, buf_len);
1.1       paf       221:                        }
                    222:                }
                    223:        }
                    224: }
                    225: 
1.23      paf       226: static void parse(Request& r, GMimeStream *stream, Hash& received) {
1.1       paf       227:        Pool& pool=received.pool();
                    228: 
                    229:        GMimeMessage *message=g_mime_parser_construct_message(stream);
                    230:        try {
                    231:                const GMimeMessageHeader *messageHeader=message->header;
                    232:                if(!messageHeader)
                    233:                        return;
                    234: 
                    235:                // firstly user-defined strings go
                    236:                //  user headers
                    237:                {
                    238:                        // $.raw[
1.5       paf       239:                        VHash& vraw=*new(pool) VHash(pool);  putReceived(received, RAW_NAME, &vraw);
1.1       paf       240:                        g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &vraw.hash(0));
                    241:                }
                    242: 
                    243:                // maybe-todo-recipients
                    244:                // x(messageHeader->recipients)
                    245: 
                    246:                //  secondly standard headers&body go
                    247:                //  standard header
                    248:                // .from
                    249:                putReceived(received, "from", messageHeader->from);
                    250:                // .reply-to
                    251:                putReceived(received, "reply-to", messageHeader->reply_to);
                    252:                // .to
                    253:                // todo: messageHeader->recipients
                    254:                // .subject
                    255:                putReceived(received, "subject", messageHeader->subject);
                    256:                // .date(date+gmt_offset)
                    257:                int tt_offset = 
                    258:                        ((messageHeader->gmt_offset / 100) *(60 * 60)) 
                    259:                        +(messageHeader->gmt_offset % 100) * 60;                        
                    260:                putReceived(received, "date", 
                    261:                        messageHeader->date // local sender
                    262:                        -tt_offset // move local sender to GMT sender
                    263:                        -(timezone+(daylight?60*60*sign(timezone):0)) // move GMT sender to our local time
                    264:                );
                    265:                // .message-id
                    266:                putReceived(received, "message-id", messageHeader->message_id);
                    267: 
                    268:                // .body[part/parts
                    269:                GMimePart *part=message->mime_part;
                    270:                const GMimeContentType *type=g_mime_part_get_content_type(part);
1.23      paf       271:                MimePart2bodyInfo info={&r, &received};
1.1       paf       272:                g_mime_part_foreach(part, MimePart2body, &info);
                    273: 
                    274:                // normal unref
                    275:                g_mime_object_unref(GMIME_OBJECT(message));
                    276:        } catch(...) {
                    277:                // abnormal unref
                    278:                g_mime_object_unref(GMIME_OBJECT(message));
                    279:        }
                    280: }
                    281: #endif
                    282: 
                    283: 
                    284: 
1.23      paf       285: void VMail::fill_received(Request& r) {
1.1       paf       286:        // store letter to received
1.3       paf       287: #ifdef WITH_MAILRECEIVE
1.23      paf       288:        if(r.info.mail_received) {
1.1       paf       289:                // init
                    290:                g_mime_init(GMIME_INIT_FLAG_UTF8);
                    291: 
                    292:                // create stream with CRLF filter
                    293:                GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
                    294:                GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
                    295:                GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
                    296:                g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
                    297:                g_mime_stream_unref(stream);
                    298:                stream = istream;
                    299:                try {
                    300:                        // parse incoming stream
1.23      paf       301:                        parse(r, stream, vreceived.hash(0));
1.1       paf       302:                        // normal stream free 
                    303:                        g_mime_stream_unref(stream);
                    304:                } catch(...) {
                    305:                        // abnormal stream free 
                    306:                        g_mime_stream_unref(stream);
                    307:                }
                    308:        }
                    309: #endif
                    310: }
                    311: 
                    312: #ifndef DOXYGEN
                    313: struct Store_message_element_info {
                    314:        Charset *charset;
                    315:        String *header;
1.11      paf       316:        const String **from, **to;
                    317:        const String *errors_to;
1.1       paf       318:        Array *parts[P_TYPES_COUNT];
                    319:        int parts_count;
                    320:        bool has_content_type;
                    321: };
                    322: #endif
1.9       paf       323: typedef int (*string_contains_char_which_check)(int);
                    324: static bool string_contains_char_which(const char *string, string_contains_char_which_check check) {
                    325:        while(char c=*string++) {
                    326:                if(check(c))
                    327:                        return true;
                    328:        }
                    329:        return false;
                    330: }
                    331: const String& extractEmail(const String& string) {
                    332:        Pool& pool=string.pool();
                    333: 
                    334:        char *email=string.cstr();
                    335:        lsplit(email, '>'); lsplit(email, '\x0D');lsplit(email, '\x0A');
                    336:        char *next=rsplit(email, '<');
                    337:        if(next) email=next;
                    338: 
                    339:        String& result=*new(pool) String(pool);
                    340:        result.APPEND_TAINTED(email, 0, string.origin().file, string.origin().line);
                    341: 
                    342:        /*
                    343:                http://www.faqs.org/rfcs/rfc822.html
                    344: 
                    345:                addr-spec   =  local-part "@" domain        ; global address
                    346:        
                    347:                local-part  =  word *("." word)             ; uninterpreted case-preserved
                    348:                word        =  atom / quoted-string
                    349: 
                    350:                domain      =  sub-domain *("." sub-domain)
                    351:                sub-domain  =  domain-ref / domain-literal
                    352:                domain-ref  =  atom                         ; symbolic reference
                    353: 
                    354:         domain-literal << ignoring for now
                    355:                quoted-string in word << ignoring for now
                    356: 
                    357:                atom        =  1*<any CHAR except specials, SPACE and CTLs>  << the ONLY to check
                    358: 
                    359:                specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
                    360:                  /  "," / ";" / ":" / "\" / <">  ;  string, to use
                    361:                  /  "." / "[" / "]"              ;  within a word.
                    362: 
                    363:        */
1.17      paf       364:        const char *exception_type="email.format";
1.9       paf       365:        if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17      paf       366:                throw Exception(exception_type,
1.9       paf       367:                        &result,
                    368:                        "email contains characters (specials)");
                    369:        if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17      paf       370:                throw Exception(exception_type,
1.9       paf       371:                        &result,
                    372:                        "email contains characters (whitespace)");
                    373:        if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17      paf       374:                throw Exception(exception_type,
1.9       paf       375:                        &result,
                    376:                        "email contains characters (control)");
1.16      paf       377:        if(result.is_empty())
1.17      paf       378:                throw Exception(exception_type,
1.16      paf       379:                        &string,
                    380:                        "email is empty");
1.9       paf       381: 
                    382:        return result;
                    383: }
1.5       paf       384: static void store_message_element(const Hash::Key& raw_element_name, Hash::Val *aelement_value, 
1.1       paf       385:                                                                  void *info) {
                    386:        Value& element_value=*static_cast<Value *>(aelement_value);
1.5       paf       387:        const String& low_element_name=raw_element_name.change_case(raw_element_name.pool(), String::CC_LOWER);
1.1       paf       388:        Store_message_element_info& i=*static_cast<Store_message_element_info *>(info);
                    389: 
                    390:        // exclude internals
1.5       paf       391:        if(low_element_name==CHARSET_NAME
                    392:                || low_element_name==VALUE_NAME
                    393:                || low_element_name==RAW_NAME
                    394:                || low_element_name=="date")
1.1       paf       395:                return;
                    396: 
                    397:        // grep parts
                    398:        for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.5       paf       399:                if(low_element_name.starts_with(part_name_starts[pt])) {
1.1       paf       400:                        *i.parts[pt]+=&element_value;
                    401:                        i.parts_count++;
                    402:                        return;
                    403:                }
                    404:        }
                    405: 
1.10      paf       406:        // fetch some special headers
1.5       paf       407:        if(i.from && low_element_name=="from")
1.9       paf       408:                *i.from=&extractEmail(element_value.as_string());
1.5       paf       409:        if(i.to && low_element_name=="to")
1.9       paf       410:                *i.to=&extractEmail(element_value.as_string());
1.12      paf       411:        if(low_element_name=="errors-to")
1.11      paf       412:                i.errors_to=&extractEmail(element_value.as_string());   
1.1       paf       413: 
                    414:        // append header line
                    415:        *i.header << 
1.5       paf       416:                raw_element_name << ":" << 
1.1       paf       417:                attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER).
                    418:                        cstr(String::UL_UNSPECIFIED, 0, i.charset, i.charset?i.charset->name().cstr():0) << 
                    419:                "\n";
                    420: 
                    421:        // has content type?
1.5       paf       422:        if(low_element_name==CONTENT_TYPE_NAME)
1.1       paf       423:                i.has_content_type=true;
                    424: }
                    425: 
                    426: static const String& file_value_to_string(Request& r, const String *source, 
                    427:                                                                                 Value& send_value) {
                    428:        Pool& pool=r.pool();
                    429:        const VFile *vfile;
1.28    ! paf       430:        const String *file_name=0;
        !           431:        Value *vformat=0;
1.1       paf       432:        if(Hash *send_hash=send_value.get_hash(source)) { // hash
                    433:                // $.value
                    434:                if(Value *value=static_cast<Value *>(send_hash->get(*value_name)))
                    435:                        vfile=value->as_vfile(String::UL_AS_IS);
                    436:                else
                    437:                        throw Exception("parser.runtime",
                    438:                                source,
                    439:                                "file part has no $value");
                    440: 
                    441:                // $.format
                    442:                vformat=static_cast<Value *>(send_hash->get(*new(pool) String(pool, "format")));
                    443: 
1.6       paf       444:                // $.name
1.1       paf       445:                if(Value *vfile_name=static_cast<Value *>(send_hash->get(
1.6       paf       446:                        *new(pool) String(pool, "name")))) // specified $name
1.1       paf       447:                        file_name=&vfile_name->as_string();
1.28    ! paf       448:        } else  // must be VFile then
1.1       paf       449:                vfile=send_value.as_vfile(String::UL_AS_IS);
1.28    ! paf       450: 
        !           451:        if(!file_name)
1.1       paf       452:                file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.28    ! paf       453: 
1.1       paf       454:        const char *file_name_cstr=file_name->cstr();
                    455: 
                    456:        String& result=*new(pool) String(pool);
                    457: 
                    458:        // content-type: application/octet-stream
                    459:        result << "content-type: " << r.mime_type_of(file_name_cstr) 
                    460:                << "; name=\"" << file_name_cstr << "\"\n";
                    461:        // content-disposition: attachment; filename="user_file_name"
                    462:        result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
                    463: 
                    464:        const String *type=vformat?&vformat->as_string():0;
                    465:        if(!type/*default = uue*/ || *type=="uue") {
                    466:                pa_uuencode(result, file_name_cstr, *vfile);
                    467:        } else // for now
                    468:                throw Exception("parser.runtime",
                    469:                        type,
                    470:                        "unknown attachment encode format");
                    471:        
                    472:        return result;
                    473: }
                    474: 
                    475: static const String& text_value_to_string(Request& r, const String *source, 
                    476:                                                                 PartType pt, Value& send_value,
                    477:                                                                 Store_message_element_info& info) {
                    478:        Pool& pool=r.pool();
                    479:        String& result=*new(pool) String(pool);
                    480: 
                    481:        Value *text_value;
                    482:        if(Hash *send_hash=send_value.get_hash(source)) {
                    483:                // $.USER-HEADERS
                    484:                info.has_content_type=false; // reset
                    485:                send_hash->for_each(store_message_element, &info);
                    486:                // $.value
                    487:                text_value=static_cast<Value *>(send_hash->get(*value_name));
                    488:                if(!text_value)
                    489:                        throw Exception("parser.runtime",
                    490:                                source,
                    491:                                "%s part has no $" VALUE_NAME, part_name_starts[pt]);
                    492:        } else
                    493:                text_value=&send_value;
                    494: 
                    495:        if(!info.has_content_type) {
                    496:                result << "content-type: text/" << (pt==P_TEXT?"plain":"html");
                    497:                if(info.charset)
                    498:                        result << "; charset=" << info.charset->name();
                    499:                result << "\n";
                    500:        }
                    501: 
                    502:        // header|body separator
                    503:        result << "\n"; 
                    504: 
                    505:        // body
                    506:        switch(pt) {
                    507:        case P_TEXT:
1.18      paf       508:                result<<text_value->as_string();
1.1       paf       509:                break;
                    510:        case P_HTML: 
                    511:                {
1.25      paf       512:                        Temp_lang temp_lang(r, String::UL_HTML | String::UL_OPTIMIZE_BIT);
1.26      paf       513:                        if(Junction *junction=text_value->get_junction())
1.1       paf       514:                                result << r.process_to_string(*text_value);
1.26      paf       515:                        else
1.1       paf       516:                                throw Exception("parser.runtime",
                    517:                                        source,
                    518:                                        "html part value must be code");
                    519: 
                    520:                        break;
                    521:                }
                    522:        }
                    523: 
                    524:        return result;
                    525: };
                    526: 
                    527: /// @todo files and messages in order (file, file2, ...)
                    528: const String& VMail::message_hash_to_string(Request& r, const String *source,
                    529:                                                                                        Hash *message_hash, int level, 
                    530:                                                                                        const String **from, const String **to) {
                    531:        if(!message_hash)
                    532:                throw Exception("parser.runtime",
                    533:                        source,
                    534:                        "message must be hash");
                    535: 
                    536:        String& result=*NEW String(pool());
                    537: 
                    538:        Charset *charset;
                    539:        if(Value *vrecodecharset_name=static_cast<Value *>(message_hash->get(*charset_name)))
                    540:                charset=&charsets->get_charset(vrecodecharset_name->as_string());
                    541:        else
                    542:                charset=&pool().get_source_charset();
                    543: 
                    544:        Store_message_element_info info={
                    545:                charset,
                    546:                &result,
                    547:                from, to
                    548:        };
                    549:        {
                    550:                if(from)
                    551:                        *from=0;
                    552:                if(to)
                    553:                        *to=0;
                    554:                for(int pt=0; pt<P_TYPES_COUNT; pt++)
                    555:                        info.parts[pt]=NEW Array(pool());
                    556:                message_hash->for_each(store_message_element, &info);
1.10      paf       557:                if(!info.errors_to)
                    558:                        result << "errors-to: postmaster\n"; // errors-to: default
1.1       paf       559:        }
                    560: 
                    561:        int textCount=info.parts[P_TEXT]->size();
                    562:        if(textCount>1)
                    563:                throw Exception("parser.runtime",
                    564:                        source,
                    565:                        "multiple text parts not supported, use file part");
                    566:        int htmlCount=info.parts[P_HTML]->size();
                    567:        if(htmlCount>1)
                    568:                throw Exception("parser.runtime",
                    569:                        source,
                    570:                        "multiple html parts not supported, use file part");
                    571: 
                    572: 
                    573:        bool multipart=info.parts_count>1;
                    574:        bool alternative=textCount && htmlCount;
                    575:        // header
                    576:        char *boundary=0;
                    577:        if(multipart) {
                    578:                boundary=(char *)malloc(MAX_NUMBER);
                    579:                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                    580:                // multi-part
                    581:                result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    582:                result << "\n" 
                    583:                        "This is a multi-part message in MIME format.";
                    584:        }
                    585: 
                    586:        // alternative or not
                    587:        {
                    588:                if(alternative) {
                    589:                        result << "\n\n--" << boundary << "\n"; // intermediate boundary
                    590:                        result << "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
                    591:                } 
                    592:                for(int i=0; i<2; i++) {
                    593:                        PartType pt=i==0?P_TEXT:P_HTML;
                    594:                        if(info.parts[pt]->size()) {
                    595:                                if(alternative)
                    596:                                        result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
                    597:                                else if(boundary)
                    598:                                        result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    599:                                result << text_value_to_string(r, source, pt, 
                    600:                                        *static_cast<Value *>(info.parts[pt]->get(0)), info);
                    601:                        }
                    602:                }
                    603:                if(alternative)
                    604:                        result << "\n\n--ALT" << boundary << "--\n";
                    605:        }
                    606: 
                    607:        // files
                    608:        {
                    609:                Array& files=*info.parts[P_FILE];
                    610:                for(int i=0; i<files.size(); i++) {
                    611:                        if(boundary)
                    612:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    613:                        result << file_value_to_string(r, source, *static_cast<Value *>(files.get(i)));
                    614:                }
                    615:        }
                    616: 
                    617:        // messages
                    618:        {
                    619:                Array& messages=*info.parts[P_MESSAGE];
                    620:                for(int i=0; i<messages.size(); i++) {
                    621:                        if(boundary)
                    622:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    623:                        
                    624:                        result << message_hash_to_string(r, source,
                    625:                                static_cast<Value *>(messages.get(i))->get_hash(source), 
                    626:                                level+1);
                    627:                }
                    628:        }
                    629:        
                    630:        // tailer
                    631:        if(boundary)
                    632:                result << "\n\n--" << boundary << "--\n"; // finish boundary
                    633: 
                    634:        // return
                    635:        return result;
                    636: }
                    637: 
                    638: 
1.22      paf       639: Value *VMail::get_element(const String& aname, Value *aself, bool looking_up) {
1.1       paf       640:        // $fields
1.3       paf       641: #ifdef WITH_MAILRECEIVE
1.1       paf       642:        if(aname==MAIL_RECEIVED_ELEMENT_NAME)
                    643:                return &vreceived;
                    644: #endif
                    645: 
                    646:        // $CLASS,$method
1.22      paf       647:        if(Value *result=VStateless_class::get_element(aname, aself, looking_up))
1.1       paf       648:                return result;
                    649: 
                    650:        return 0;
                    651: }
                    652: 
1.3       paf       653: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1       paf       654: #      define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
                    655: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
                    656: #      ifdef _DEBUG
                    657: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
                    658: #      else
                    659: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
                    660: #      endif
                    661: #endif

E-mail: