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

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

E-mail: