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

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.35    ! paf         9: static const char* IDENT_VMAIL_C="$Date: 2002/11/21 09:18:19 $";
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.30      paf       226: int gmt_offset() {
                    227: #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
1.31      paf       228:        return timezone+(daylight?60*60*(timezone<0?-1:timezone>0?+1:0):0);
1.30      paf       229: #else
                    230:        time_t t=time(0);
                    231:        tm *tm=localtime(&t);
                    232: #if defined(HAVE_TM_GMTOFF)
                    233:        return tm->tm_gmtoff;
                    234: #elif defined(HAVE_TM_TZADJ)
                    235:        return tm->tm_tzadj;
                    236: #else
                    237: #error neither HAVE_TIMEZONE&HAVE_DAYIGHT nor HAVE_TM_GMTOFF nor HAVE_TM_TZADJ defined
                    238: #endif
                    239: #endif
                    240: }
                    241: 
1.23      paf       242: static void parse(Request& r, GMimeStream *stream, Hash& received) {
1.1       paf       243:        Pool& pool=received.pool();
                    244: 
                    245:        GMimeMessage *message=g_mime_parser_construct_message(stream);
                    246:        try {
                    247:                const GMimeMessageHeader *messageHeader=message->header;
                    248:                if(!messageHeader)
                    249:                        return;
                    250: 
                    251:                // firstly user-defined strings go
                    252:                //  user headers
                    253:                {
                    254:                        // $.raw[
1.5       paf       255:                        VHash& vraw=*new(pool) VHash(pool);  putReceived(received, RAW_NAME, &vraw);
1.1       paf       256:                        g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &vraw.hash(0));
                    257:                }
                    258: 
                    259:                // maybe-todo-recipients
                    260:                // x(messageHeader->recipients)
                    261: 
                    262:                //  secondly standard headers&body go
                    263:                //  standard header
                    264:                // .from
                    265:                putReceived(received, "from", messageHeader->from);
                    266:                // .reply-to
                    267:                putReceived(received, "reply-to", messageHeader->reply_to);
                    268:                // .to
                    269:                // todo: messageHeader->recipients
                    270:                // .subject
                    271:                putReceived(received, "subject", messageHeader->subject);
                    272:                // .date(date+gmt_offset)
                    273:                int tt_offset = 
                    274:                        ((messageHeader->gmt_offset / 100) *(60 * 60)) 
                    275:                        +(messageHeader->gmt_offset % 100) * 60;                        
1.30      paf       276: 
1.1       paf       277:                putReceived(received, "date", 
                    278:                        messageHeader->date // local sender
                    279:                        -tt_offset // move local sender to GMT sender
1.30      paf       280:                        -gmt_offset() // move GMT sender to our local time
1.1       paf       281:                );
                    282:                // .message-id
                    283:                putReceived(received, "message-id", messageHeader->message_id);
                    284: 
                    285:                // .body[part/parts
                    286:                GMimePart *part=message->mime_part;
                    287:                const GMimeContentType *type=g_mime_part_get_content_type(part);
1.23      paf       288:                MimePart2bodyInfo info={&r, &received};
1.1       paf       289:                g_mime_part_foreach(part, MimePart2body, &info);
                    290: 
                    291:                // normal unref
                    292:                g_mime_object_unref(GMIME_OBJECT(message));
                    293:        } catch(...) {
                    294:                // abnormal unref
                    295:                g_mime_object_unref(GMIME_OBJECT(message));
                    296:        }
                    297: }
                    298: #endif
                    299: 
                    300: 
                    301: 
1.23      paf       302: void VMail::fill_received(Request& r) {
1.1       paf       303:        // store letter to received
1.3       paf       304: #ifdef WITH_MAILRECEIVE
1.23      paf       305:        if(r.info.mail_received) {
1.1       paf       306:                // init
                    307:                g_mime_init(GMIME_INIT_FLAG_UTF8);
                    308: 
                    309:                // create stream with CRLF filter
                    310:                GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
                    311:                GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
                    312:                GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
                    313:                g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
                    314:                g_mime_stream_unref(stream);
                    315:                stream = istream;
                    316:                try {
                    317:                        // parse incoming stream
1.23      paf       318:                        parse(r, stream, vreceived.hash(0));
1.1       paf       319:                        // normal stream free 
                    320:                        g_mime_stream_unref(stream);
                    321:                } catch(...) {
                    322:                        // abnormal stream free 
                    323:                        g_mime_stream_unref(stream);
                    324:                }
                    325:        }
                    326: #endif
                    327: }
                    328: 
                    329: #ifndef DOXYGEN
                    330: struct Store_message_element_info {
                    331:        Charset *charset;
                    332:        String *header;
1.11      paf       333:        const String **from, **to;
                    334:        const String *errors_to;
1.1       paf       335:        Array *parts[P_TYPES_COUNT];
                    336:        int parts_count;
                    337:        bool has_content_type;
                    338: };
                    339: #endif
1.9       paf       340: typedef int (*string_contains_char_which_check)(int);
                    341: static bool string_contains_char_which(const char *string, string_contains_char_which_check check) {
                    342:        while(char c=*string++) {
                    343:                if(check(c))
                    344:                        return true;
                    345:        }
                    346:        return false;
                    347: }
1.32      paf       348: static char *trimBoth(char *s) {
                    349:        // sanity check
                    350:        if(!s)
                    351:                return 0;
                    352: 
                    353:        // trim head whitespace
                    354:        while(*s && isspace(*s))
                    355:                s++;
                    356:        // trim tail whitespace
                    357:        char *tail=s+strlen(s);
                    358:        if(tail>s) {
                    359:                do {
                    360:                        --tail;
                    361:                        if(isspace(*tail))
                    362:                                *tail=0;
                    363:                } while(tail>s);
                    364:        }
                    365:        // return it
                    366:        return s;
                    367: }
                    368: /*nonstatic*/const String& extractEmail(const String& string) {
1.9       paf       369:        Pool& pool=string.pool();
                    370: 
                    371:        char *email=string.cstr();
1.32      paf       372:        rsplit(email, '>');
1.9       paf       373:        char *next=rsplit(email, '<');
                    374:        if(next) email=next;
1.32      paf       375:        email=trimBoth(email);
1.9       paf       376: 
                    377:        String& result=*new(pool) String(pool);
                    378:        result.APPEND_TAINTED(email, 0, string.origin().file, string.origin().line);
                    379: 
                    380:        /*
                    381:                http://www.faqs.org/rfcs/rfc822.html
                    382: 
                    383:                addr-spec   =  local-part "@" domain        ; global address
                    384:        
                    385:                local-part  =  word *("." word)             ; uninterpreted case-preserved
                    386:                word        =  atom / quoted-string
                    387: 
                    388:                domain      =  sub-domain *("." sub-domain)
                    389:                sub-domain  =  domain-ref / domain-literal
                    390:                domain-ref  =  atom                         ; symbolic reference
                    391: 
                    392:         domain-literal << ignoring for now
                    393:                quoted-string in word << ignoring for now
                    394: 
                    395:                atom        =  1*<any CHAR except specials, SPACE and CTLs>  << the ONLY to check
                    396: 
                    397:                specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
                    398:                  /  "," / ";" / ":" / "\" / <">  ;  string, to use
                    399:                  /  "." / "[" / "]"              ;  within a word.
                    400: 
                    401:        */
1.17      paf       402:        const char *exception_type="email.format";
1.9       paf       403:        if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17      paf       404:                throw Exception(exception_type,
1.9       paf       405:                        &result,
1.32      paf       406:                        "email contains bad characters (specials)");
1.9       paf       407:        if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17      paf       408:                throw Exception(exception_type,
1.9       paf       409:                        &result,
1.32      paf       410:                        "email contains bad characters (whitespace)");
1.9       paf       411:        if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17      paf       412:                throw Exception(exception_type,
1.9       paf       413:                        &result,
1.32      paf       414:                        "email contains bad characters (control)");
1.16      paf       415:        if(result.is_empty())
1.17      paf       416:                throw Exception(exception_type,
1.16      paf       417:                        &string,
                    418:                        "email is empty");
1.9       paf       419: 
                    420:        return result;
                    421: }
1.5       paf       422: static void store_message_element(const Hash::Key& raw_element_name, Hash::Val *aelement_value, 
1.1       paf       423:                                                                  void *info) {
                    424:        Value& element_value=*static_cast<Value *>(aelement_value);
1.5       paf       425:        const String& low_element_name=raw_element_name.change_case(raw_element_name.pool(), String::CC_LOWER);
1.1       paf       426:        Store_message_element_info& i=*static_cast<Store_message_element_info *>(info);
                    427: 
                    428:        // exclude internals
1.5       paf       429:        if(low_element_name==CHARSET_NAME
                    430:                || low_element_name==VALUE_NAME
                    431:                || low_element_name==RAW_NAME
                    432:                || low_element_name=="date")
1.1       paf       433:                return;
                    434: 
                    435:        // grep parts
                    436:        for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.5       paf       437:                if(low_element_name.starts_with(part_name_starts[pt])) {
1.29      paf       438:                        // check that $.message# '#' is digit
                    439:                        size_t start_len=strlen(part_name_starts[pt]);
                    440:                        if(low_element_name.size()>start_len) {
                    441:                                const char *at_num=low_element_name.mid(start_len, start_len+1).cstr();
                    442:                                if(!isdigit(*at_num))
                    443:                                        continue;
                    444:                        }
1.1       paf       445:                        *i.parts[pt]+=&element_value;
                    446:                        i.parts_count++;
                    447:                        return;
                    448:                }
                    449:        }
                    450: 
1.10      paf       451:        // fetch some special headers
1.5       paf       452:        if(i.from && low_element_name=="from")
1.9       paf       453:                *i.from=&extractEmail(element_value.as_string());
1.5       paf       454:        if(i.to && low_element_name=="to")
1.9       paf       455:                *i.to=&extractEmail(element_value.as_string());
1.12      paf       456:        if(low_element_name=="errors-to")
1.11      paf       457:                i.errors_to=&extractEmail(element_value.as_string());   
1.1       paf       458: 
                    459:        // append header line
                    460:        *i.header << 
1.5       paf       461:                raw_element_name << ":" << 
1.1       paf       462:                attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER).
                    463:                        cstr(String::UL_UNSPECIFIED, 0, i.charset, i.charset?i.charset->name().cstr():0) << 
                    464:                "\n";
                    465: 
                    466:        // has content type?
1.5       paf       467:        if(low_element_name==CONTENT_TYPE_NAME)
1.1       paf       468:                i.has_content_type=true;
                    469: }
                    470: 
                    471: static const String& file_value_to_string(Request& r, const String *source, 
                    472:                                                                                 Value& send_value) {
                    473:        Pool& pool=r.pool();
                    474:        const VFile *vfile;
1.28      paf       475:        const String *file_name=0;
                    476:        Value *vformat=0;
1.1       paf       477:        if(Hash *send_hash=send_value.get_hash(source)) { // hash
                    478:                // $.value
                    479:                if(Value *value=static_cast<Value *>(send_hash->get(*value_name)))
                    480:                        vfile=value->as_vfile(String::UL_AS_IS);
                    481:                else
                    482:                        throw Exception("parser.runtime",
                    483:                                source,
                    484:                                "file part has no $value");
                    485: 
                    486:                // $.format
                    487:                vformat=static_cast<Value *>(send_hash->get(*new(pool) String(pool, "format")));
                    488: 
1.6       paf       489:                // $.name
1.1       paf       490:                if(Value *vfile_name=static_cast<Value *>(send_hash->get(
1.6       paf       491:                        *new(pool) String(pool, "name")))) // specified $name
1.1       paf       492:                        file_name=&vfile_name->as_string();
1.28      paf       493:        } else  // must be VFile then
1.1       paf       494:                vfile=send_value.as_vfile(String::UL_AS_IS);
1.28      paf       495: 
                    496:        if(!file_name)
1.1       paf       497:                file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.28      paf       498: 
1.1       paf       499:        const char *file_name_cstr=file_name->cstr();
                    500: 
                    501:        String& result=*new(pool) String(pool);
                    502: 
                    503:        // content-type: application/octet-stream
                    504:        result << "content-type: " << r.mime_type_of(file_name_cstr) 
                    505:                << "; name=\"" << file_name_cstr << "\"\n";
                    506:        // content-disposition: attachment; filename="user_file_name"
1.35    ! paf       507:        result << 
        !           508:                CONTENT_DISPOSITION_NAME ": "CONTENT_DISPOSITION_VALUE"; "
        !           509:                CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.1       paf       510: 
                    511:        const String *type=vformat?&vformat->as_string():0;
                    512:        if(!type/*default = uue*/ || *type=="uue") {
                    513:                pa_uuencode(result, file_name_cstr, *vfile);
                    514:        } else // for now
                    515:                throw Exception("parser.runtime",
                    516:                        type,
                    517:                        "unknown attachment encode format");
                    518:        
                    519:        return result;
                    520: }
                    521: 
                    522: static const String& text_value_to_string(Request& r, const String *source, 
                    523:                                                                 PartType pt, Value& send_value,
                    524:                                                                 Store_message_element_info& info) {
                    525:        Pool& pool=r.pool();
                    526:        String& result=*new(pool) String(pool);
                    527: 
                    528:        Value *text_value;
                    529:        if(Hash *send_hash=send_value.get_hash(source)) {
                    530:                // $.USER-HEADERS
                    531:                info.has_content_type=false; // reset
                    532:                send_hash->for_each(store_message_element, &info);
                    533:                // $.value
                    534:                text_value=static_cast<Value *>(send_hash->get(*value_name));
                    535:                if(!text_value)
                    536:                        throw Exception("parser.runtime",
                    537:                                source,
                    538:                                "%s part has no $" VALUE_NAME, part_name_starts[pt]);
                    539:        } else
                    540:                text_value=&send_value;
                    541: 
                    542:        if(!info.has_content_type) {
                    543:                result << "content-type: text/" << (pt==P_TEXT?"plain":"html");
                    544:                if(info.charset)
                    545:                        result << "; charset=" << info.charset->name();
                    546:                result << "\n";
                    547:        }
                    548: 
                    549:        // header|body separator
                    550:        result << "\n"; 
                    551: 
                    552:        // body
                    553:        switch(pt) {
                    554:        case P_TEXT:
1.18      paf       555:                result<<text_value->as_string();
1.1       paf       556:                break;
                    557:        case P_HTML: 
                    558:                {
1.25      paf       559:                        Temp_lang temp_lang(r, String::UL_HTML | String::UL_OPTIMIZE_BIT);
1.26      paf       560:                        if(Junction *junction=text_value->get_junction())
1.1       paf       561:                                result << r.process_to_string(*text_value);
1.26      paf       562:                        else
1.1       paf       563:                                throw Exception("parser.runtime",
                    564:                                        source,
                    565:                                        "html part value must be code");
                    566: 
                    567:                        break;
                    568:                }
                    569:        }
                    570: 
                    571:        return result;
                    572: };
                    573: 
                    574: /// @todo files and messages in order (file, file2, ...)
                    575: const String& VMail::message_hash_to_string(Request& r, const String *source,
                    576:                                                                                        Hash *message_hash, int level, 
                    577:                                                                                        const String **from, const String **to) {
1.34      paf       578:        Pool& pool=r.pool();
                    579:        
1.1       paf       580:        if(!message_hash)
                    581:                throw Exception("parser.runtime",
                    582:                        source,
                    583:                        "message must be hash");
                    584: 
1.34      paf       585:        String& result=*new(pool) String(pool);
1.1       paf       586: 
                    587:        Charset *charset;
                    588:        if(Value *vrecodecharset_name=static_cast<Value *>(message_hash->get(*charset_name)))
                    589:                charset=&charsets->get_charset(vrecodecharset_name->as_string());
                    590:        else
1.34      paf       591:                charset=&pool.get_source_charset();
1.1       paf       592: 
                    593:        Store_message_element_info info={
                    594:                charset,
                    595:                &result,
                    596:                from, to
                    597:        };
                    598:        {
                    599:                if(from)
                    600:                        *from=0;
                    601:                if(to)
                    602:                        *to=0;
                    603:                for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.34      paf       604:                        info.parts[pt]=new(pool) Array(pool);
1.1       paf       605:                message_hash->for_each(store_message_element, &info);
1.10      paf       606:                if(!info.errors_to)
                    607:                        result << "errors-to: postmaster\n"; // errors-to: default
1.1       paf       608:        }
                    609: 
                    610:        int textCount=info.parts[P_TEXT]->size();
                    611:        if(textCount>1)
                    612:                throw Exception("parser.runtime",
                    613:                        source,
                    614:                        "multiple text parts not supported, use file part");
                    615:        int htmlCount=info.parts[P_HTML]->size();
                    616:        if(htmlCount>1)
                    617:                throw Exception("parser.runtime",
                    618:                        source,
                    619:                        "multiple html parts not supported, use file part");
                    620: 
                    621: 
                    622:        bool multipart=info.parts_count>1;
                    623:        bool alternative=textCount && htmlCount;
                    624:        // header
                    625:        char *boundary=0;
                    626:        if(multipart) {
                    627:                boundary=(char *)malloc(MAX_NUMBER);
                    628:                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                    629:                // multi-part
                    630:                result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    631:                result << "\n" 
                    632:                        "This is a multi-part message in MIME format.";
                    633:        }
                    634: 
                    635:        // alternative or not
                    636:        {
                    637:                if(alternative) {
                    638:                        result << "\n\n--" << boundary << "\n"; // intermediate boundary
                    639:                        result << "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
                    640:                } 
                    641:                for(int i=0; i<2; i++) {
                    642:                        PartType pt=i==0?P_TEXT:P_HTML;
                    643:                        if(info.parts[pt]->size()) {
                    644:                                if(alternative)
                    645:                                        result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
                    646:                                else if(boundary)
                    647:                                        result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    648:                                result << text_value_to_string(r, source, pt, 
                    649:                                        *static_cast<Value *>(info.parts[pt]->get(0)), info);
                    650:                        }
                    651:                }
                    652:                if(alternative)
                    653:                        result << "\n\n--ALT" << boundary << "--\n";
                    654:        }
                    655: 
                    656:        // files
                    657:        {
                    658:                Array& files=*info.parts[P_FILE];
                    659:                for(int i=0; i<files.size(); i++) {
                    660:                        if(boundary)
                    661:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    662:                        result << file_value_to_string(r, source, *static_cast<Value *>(files.get(i)));
                    663:                }
                    664:        }
                    665: 
                    666:        // messages
                    667:        {
                    668:                Array& messages=*info.parts[P_MESSAGE];
                    669:                for(int i=0; i<messages.size(); i++) {
                    670:                        if(boundary)
                    671:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    672:                        
                    673:                        result << message_hash_to_string(r, source,
                    674:                                static_cast<Value *>(messages.get(i))->get_hash(source), 
                    675:                                level+1);
                    676:                }
                    677:        }
                    678:        
                    679:        // tailer
                    680:        if(boundary)
                    681:                result << "\n\n--" << boundary << "--\n"; // finish boundary
                    682: 
                    683:        // return
                    684:        return result;
                    685: }
                    686: 
                    687: 
1.33      paf       688: Value *VMail::get_element(const String& aname, Value& aself, bool looking_up) {
1.1       paf       689:        // $fields
1.3       paf       690: #ifdef WITH_MAILRECEIVE
1.1       paf       691:        if(aname==MAIL_RECEIVED_ELEMENT_NAME)
                    692:                return &vreceived;
                    693: #endif
                    694: 
                    695:        // $CLASS,$method
1.22      paf       696:        if(Value *result=VStateless_class::get_element(aname, aself, looking_up))
1.1       paf       697:                return result;
                    698: 
                    699:        return 0;
                    700: }
                    701: 
1.3       paf       702: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1       paf       703: #      define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
                    704: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
                    705: #      ifdef _DEBUG
                    706: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
                    707: #      else
                    708: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
                    709: #      endif
                    710: #endif

E-mail: