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

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

E-mail: