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

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

E-mail: