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

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

E-mail: