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

1.1       paf         1: /**    @file
                      2:        Parser: @b mail class.
                      3:        relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
                      4: 
1.115     moko        5:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       paf         6:        Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
                      7: */
1.13      paf         8: 
1.1       paf         9: #include "pa_sapi.h"
                     10: #include "pa_vmail.h"
                     11: #include "pa_vstring.h"
                     12: #include "pa_request.h"
                     13: #include "pa_common.h"
                     14: #include "pa_charset.h"
                     15: #include "pa_charsets.h"
                     16: #include "pa_vdate.h"
                     17: #include "pa_vfile.h"
                     18: #include "pa_uue.h"
                     19: 
1.117   ! moko       20: volatile const char * IDENT_PA_VMAIL_C="$Id: pa_vmail.C,v 1.116 2016/03/31 21:46:21 moko Exp $" IDENT_PA_VMAIL_H;
1.98      moko       21: 
1.3       paf        22: #ifdef WITH_MAILRECEIVE
1.4       paf        23: extern "C" {
1.99      moko       24: #include "gmime/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:        P_TYPES_COUNT
                     42: };
                     43: 
1.99      moko       44: static const char* const part_name_begins[P_TYPES_COUNT] = {
                     45:        "text",
                     46:        "html",
                     47:        "file",
                     48:        "message"
                     49: };
1.45      paf        50: 
                     51: // defines for statics
                     52: 
                     53: #define FORMAT_NAME "format"
                     54: #define CHARSET_NAME "charset"
1.76      misha      55: #define CID_NAME "content-id"
1.45      paf        56: 
1.112     moko       57: #define CONTENT_TRANSFER_ENCODING_NAME "content-transfer-encoding"
                     58: #define CONTENT_TRANSFER_ENCODING_CAPITALIZED "Content-Transfer-Encoding"
                     59: 
1.45      paf        60: // statics
                     61: 
                     62: static const String format_name(FORMAT_NAME);
                     63: static const String charset_name(CHARSET_NAME);
1.76      misha      64: static const String cid_name(CID_NAME);
1.1       paf        65: 
1.112     moko       66: static const String content_transfer_encoding_name(CONTENT_TRANSFER_ENCODING_NAME);
                     67: 
1.61      paf        68: // consts
                     69: 
                     70: const int MAX_CHARS_IN_HEADER_LINE=500;
                     71: 
1.1       paf        72: // VMail
                     73: 
1.116     moko       74: extern Methoded* mail_class;
1.1       paf        75: 
1.117   ! moko       76: VMail::VMail(): VStateless_class(mail_class) {}
1.1       paf        77: 
1.3       paf        78: #ifdef WITH_MAILRECEIVE
1.1       paf        79: 
1.48      paf        80: #define EXCEPTION_VALUE "x-exception"
                     81: 
1.99      moko       82: static Charset* source_charset;
                     83: 
1.107     moko       84: static const char *transcode(const char *value) {
                     85:        if(value && !source_charset->isUTF8()){
                     86:                String::C transcoded=Charset::transcode(String::C(value, strlen(value)), UTF8_charset, *source_charset);
                     87:                value=transcoded.str;
                     88:        }
                     89:        return value;
                     90: }
                     91: 
1.99      moko       92: static void putReceived(HashStringValue& received, const char* name, Value* value, bool capitalizeName=false) {
                     93:        if(name && value)
                     94:                received.put(capitalizeName ? capitalize(pa_strdup(name)) : pa_strdup(name), value);
1.1       paf        95: }
                     96: 
1.99      moko       97: static void putReceived(HashStringValue& received, const char* name, const char* value, bool capitalizeName=false) {
                     98:        if(name && value)
1.108     moko       99:                putReceived(received, name, new VString(*new String(pa_strdup(value), String::L_TAINTED)), capitalizeName);
1.1       paf       100: }
                    101: 
1.99      moko      102: static void putReceived(HashStringValue& received, const char* name, time_t value) {
                    103:        if(name)
                    104:                received.put(pa_strdup(name), new VDate(value) );
1.1       paf       105: }
                    106: 
1.99      moko      107: static void MimeHeaderField2received(const char* name, const char* value, gpointer data) {
                    108:        HashStringValue* received=static_cast<HashStringValue*>(data);
                    109:        putReceived(*received, name, value, true /*capitalizeName*/);
1.1       paf       110: }
                    111: 
1.99      moko      112: static void parse(Request& r, GMimeMessage *message, HashStringValue& received);
1.1       paf       113: 
                    114: #ifndef DOXYGEN
1.45      paf       115: struct MimePart2body_info {
                    116:        Request* r;
                    117:        HashStringValue* body;
1.1       paf       118:        int partCounts[P_TYPES_COUNT];
                    119: };
                    120: #endif
1.99      moko      121: 
                    122: 
                    123: static char *readStream(GMimeStream* gstream, size_t &length){
                    124:        length=MAX_STRING;
                    125:        char *result=(char*)pa_malloc_atomic(length+1);
                    126:        char *ptr=result;
                    127: 
                    128:        while(true) {
                    129:                size_t current_size=ptr-result;
                    130:                ssize_t todo_size=length-current_size;
                    131:                ssize_t received_size=g_mime_stream_read (gstream, ptr, todo_size);
                    132: 
                    133:                if(received_size<0)
                    134:                        throw Exception(PARSER_RUNTIME, 0,"mail content stream read error");
                    135:                if(received_size==0)
                    136:                        break;
                    137:                if(received_size==todo_size) {
                    138:                        length=length*2;
                    139:                        result=(char *)pa_realloc(result, length+1);
                    140:                        ptr=result+current_size+received_size;
                    141:                } else {
                    142:                        ptr+=received_size;
                    143:                }
                    144:        }
                    145: 
                    146:        length=ptr-result;
                    147:        result[length]='\0';
                    148:        return result;
                    149: }
                    150: 
                    151: static void MimePart2body(GMimeObject *parent, GMimeObject *part, gpointer data) {
1.45      paf       152:        MimePart2body_info& info=*static_cast<MimePart2body_info *>(data);
1.1       paf       153: 
1.99      moko      154:        // skipping message/partial & frames
1.101     moko      155:        if (GMIME_IS_MESSAGE_PARTIAL (part) || GMIME_IS_MULTIPART (part))
1.99      moko      156:                return;
                    157:        
                    158:        if (GMimeContentType *type=g_mime_object_get_content_type(part)) {
                    159:                PartType partType=P_FILE;
                    160:                
1.107     moko      161:                if (GMIME_IS_MESSAGE_PART(part)){
1.99      moko      162:                        partType=P_MESSAGE;
1.107     moko      163:                } else {
                    164:                        const char *disposition=g_mime_object_get_disposition(part);
                    165:                        if(!disposition || strcmp(disposition, GMIME_DISPOSITION_ATTACHMENT)){
                    166:                                if(g_mime_content_type_is_type(type, "text", "plain"))
                    167:                                        partType=P_TEXT;
                    168:                                else if(g_mime_content_type_is_type(type, "text", "html"))
                    169:                                        partType=P_HTML;
                    170:                        }
                    171:                }
1.99      moko      172: 
1.1       paf       173:                // partName
1.99      moko      174:                int partNumber=++info.partCounts[partType];
                    175:                const char *partName=part_name_begins[partType];
1.1       paf       176:                
1.99      moko      177:                char partNameNumbered[MAX_STRING];
                    178:                snprintf(partNameNumbered, MAX_STRING, "%s%d", partName, partNumber);
1.1       paf       179: 
1.99      moko      180:                // $.partN[
                    181:                VHash* vpartHash(new VHash);
                    182:                if(partNumber==1)
                    183:                        putReceived(*info.body, partName, vpartHash);
                    184:                putReceived(*info.body, partNameNumbered, vpartHash);
                    185: 
                    186:                HashStringValue& partHash=vpartHash->hash();
                    187: 
                    188:                // $.raw[
                    189:                VHash* vraw(new VHash);
                    190:                putReceived(partHash, RAW_NAME, vraw);
                    191:                g_mime_header_list_foreach(part->headers, MimeHeaderField2received, &vraw->hash());
                    192: 
                    193:                // $.content-type[
                    194:                VHash* vcontent_type(new VHash);
                    195:                putReceived(partHash, "content-type", vcontent_type);
                    196: 
                    197:                // $.value[text/plain]
                    198:                char value[MAX_STRING];
                    199:                snprintf(value, MAX_STRING, "%s/%s", type->type ? type->type : "x-unknown", type->subtype ? type->subtype : "x-unknown");
                    200:                putReceived(vcontent_type->hash(), VALUE_NAME, value);
                    201: 
                    202:                const GMimeParam *param=g_mime_content_type_get_params(type);
                    203:                while(param) {
                    204:                        // $.charset[windows-1251]  && co
1.107     moko      205:                        putReceived(vcontent_type->hash(), g_mime_param_get_name(param), transcode(g_mime_param_get_value(param)), true /*capitalizeName*/);
1.99      moko      206:                        param=g_mime_param_next(param);
                    207:                }
                    208: 
                    209:                if (GMIME_IS_MESSAGE_PART (part)) {
1.102     moko      210:                        /* message/rfc822, $.raw[] will be overwitten */
1.99      moko      211:                        GMimeMessage *message = g_mime_message_part_get_message ((GMimeMessagePart *) part);
                    212:                        parse(*info.r, message, partHash);
1.1       paf       213:                } else {
1.101     moko      214:                        GMimePart *gpart = (GMimePart *)part;
                    215: 
                    216:                        putReceived(partHash, "description", g_mime_part_get_content_description(gpart));
                    217:                        putReceived(partHash, "content-id", g_mime_part_get_content_id(gpart));
                    218:                        putReceived(partHash, "content-md5", g_mime_part_get_content_md5(gpart));
                    219:                        putReceived(partHash, "content-location", g_mime_part_get_content_location(gpart));
                    220: 
1.1       paf       221:                        // $.value[string|file]
1.103     moko      222:                        if(GMimeDataWrapper* gcontent=g_mime_part_get_content_object(gpart)){
                    223:                                GMimeStream* gstream=g_mime_stream_filter_new(g_mime_data_wrapper_get_stream(gcontent));
                    224:                                
                    225:                                if(GMimeFilter* filter=g_mime_filter_basic_new(g_mime_part_get_content_encoding(gpart), false))
                    226:                                        g_mime_stream_filter_add(GMIME_STREAM_FILTER(gstream), filter);
                    227:                                
                    228:                                size_t length;
1.99      moko      229:                                
1.103     moko      230:                                if(partType==P_FILE) {
                    231:                                        char *content=readStream(gstream, length);
1.107     moko      232:                                        const char* content_filename=transcode(g_mime_part_get_filename(gpart));
1.103     moko      233:                                        VFile* vfile(new VFile);
                    234:                                        vfile->set_binary(true/*tainted*/, content, length, new String(content_filename), content_filename ? new VString(info.r->mime_type_of(content_filename)) : 0);
                    235:                                        putReceived(partHash, VALUE_NAME, vfile);
                    236:                                } else {
                    237:                                        // P_TEXT, P_HTML
                    238:                                        if(Value *charset=vcontent_type->hash().get("Charset"))
                    239:                                                if(GMimeFilter* filter=g_mime_filter_charset_new(charset->get_string()->cstr(), source_charset->NAME_CSTR()))
                    240:                                                        g_mime_stream_filter_add(GMIME_STREAM_FILTER(gstream), filter);
                    241:                                        
                    242:                                        char *content=readStream(gstream, length);
                    243:                                        putReceived(partHash, VALUE_NAME,new VString(*new String(content)));
                    244:                                }
1.1       paf       245:                        }
                    246:                }
                    247:        }
                    248: }
                    249: 
1.99      moko      250: static void parse(Request& r, GMimeMessage *message, HashStringValue& received) {
1.1       paf       251:        try {
                    252:                // firstly user-defined strings go
                    253:                //  user headers
                    254:                {
                    255:                        // $.raw[
1.99      moko      256:                        VHash* vraw(new VHash);  putReceived( received, RAW_NAME, vraw);
                    257:                        g_mime_header_list_foreach(g_mime_object_get_header_list(GMIME_OBJECT(message)), MimeHeaderField2received, &vraw->hash());
                    258:                }
                    259: 
                    260:                //  secondly standard headers
                    261:                putReceived(received, "message-id", g_mime_message_get_message_id(message));
1.107     moko      262:                putReceived(received, "from", transcode(g_mime_message_get_sender(message)));
                    263: 
                    264:                const char *msg_to=internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_TO), false);
                    265:                putReceived(received, "to", transcode(msg_to));
                    266:                const char *msg_cc=internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_CC), false);
                    267:                putReceived(received, "cc", transcode(msg_cc));
                    268: 
                    269:                putReceived(received, "reply-to", transcode(g_mime_message_get_reply_to(message)));
                    270:                putReceived(received, "subject", transcode(g_mime_message_get_subject(message)));
                    271: 
1.99      moko      272:                // @todo: g_mime_message_get_recipients(message)
                    273:                
1.113     moko      274:                // .date(time_t in UTC)
1.99      moko      275:                time_t date;
1.113     moko      276:                g_mime_message_get_date(message, &date, 0);
1.114     moko      277:                putReceived(received, "date", date);
1.1       paf       278: 
                    279:                // .body[part/parts
1.55      paf       280:                MimePart2body_info info={&r, &received, {0}};
1.99      moko      281:                g_mime_message_foreach(message, MimePart2body, &info);
1.1       paf       282: 
1.48      paf       283:        } catch(const Exception& e) {
1.99      moko      284:                putReceived(received, VALUE_NAME, "<exception occured while parsing message>");
                    285:                putReceived(received, EXCEPTION_VALUE, e.comment());
1.1       paf       286:        } catch(...) {
1.99      moko      287:                putReceived(received, VALUE_NAME, "<exception occured while parsing message>");
1.1       paf       288:        }
                    289: }
                    290: 
1.99      moko      291: void VMail::fill_received(Request& r) {
1.45      paf       292:        if(r.request_info.mail_received) {
1.99      moko      293:                source_charset=&r.charsets.source();
1.50      paf       294:                g_mime_init(0);
1.1       paf       295: 
                    296:                // create stream with CRLF filter
1.104     moko      297:                GMimeStream *stream = g_mime_stream_filter_new(g_mime_stream_file_new(stdin));
1.99      moko      298:                g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream), g_mime_filter_crlf_new(false, false));
1.1       paf       299:                try {
1.99      moko      300:                        // parse incoming message
                    301:                        GMimeMessage *message=g_mime_parser_construct_message(g_mime_parser_new_with_stream(stream));
                    302:                        parse(r, message, vreceived.hash());
1.101     moko      303:                        g_object_unref(GMIME_OBJECT(message));
1.48      paf       304:                } catch(const Exception& e) {
                    305:                        HashStringValue& received=vreceived.hash();
1.99      moko      306:                        putReceived(received, VALUE_NAME, "<exception occured while parsing message>");
                    307:                        putReceived(received, EXCEPTION_VALUE, e.comment());
1.1       paf       308:                } catch(...) {
1.99      moko      309:                        // abnormal stream free
                    310:                        g_object_unref(stream);
1.48      paf       311:                        rethrow;
1.1       paf       312:                }
1.99      moko      313:                g_object_unref(stream);
                    314:                
                    315:                g_mime_shutdown();
1.1       paf       316:        }
                    317: }
                    318: 
1.99      moko      319: #else // WITH_MAILRECEIVE
                    320: void VMail::fill_received(Request&){}
                    321: #endif // WITH_MAILRECEIVE
                    322: 
1.9       paf       323: typedef int (*string_contains_char_which_check)(int);
1.99      moko      324: 
1.45      paf       325: static bool string_contains_char_which(const char* string, string_contains_char_which_check check) {
1.9       paf       326:        while(char c=*string++) {
1.62      paf       327:                if(check((unsigned char)c))
1.9       paf       328:                        return true;
                    329:        }
                    330:        return false;
                    331: }
1.99      moko      332: 
1.32      paf       333: static char *trimBoth(char *s) {
                    334:        // sanity check
                    335:        if(!s)
                    336:                return 0;
                    337: 
                    338:        // trim head whitespace
1.62      paf       339:        while(*s && isspace((unsigned char)*s))
1.32      paf       340:                s++;
                    341:        // trim tail whitespace
                    342:        char *tail=s+strlen(s);
                    343:        if(tail>s) {
                    344:                do {
                    345:                        --tail;
1.62      paf       346:                        if(isspace((unsigned char)*tail))
1.32      paf       347:                                *tail=0;
                    348:                } while(tail>s);
                    349:        }
                    350:        // return it
                    351:        return s;
                    352: }
1.99      moko      353: 
1.45      paf       354: static void extractEmail(String& result, char *email) {
1.32      paf       355:        email=trimBoth(email);
1.45      paf       356:        result.append_help_length(email, 0, String::L_TAINTED);
1.9       paf       357: 
                    358:        /*
                    359:                http://www.faqs.org/rfcs/rfc822.html
                    360: 
                    361:                addr-spec   =  local-part "@" domain        ; global address
                    362:        
                    363:                local-part  =  word *("." word)             ; uninterpreted case-preserved
                    364:                word        =  atom / quoted-string
                    365: 
                    366:                domain      =  sub-domain *("." sub-domain)
                    367:                sub-domain  =  domain-ref / domain-literal
                    368:                domain-ref  =  atom                         ; symbolic reference
                    369: 
1.92      misha     370:                domain-literal << ignoring for now
1.9       paf       371:                quoted-string in word << ignoring for now
                    372: 
                    373:                atom        =  1*<any CHAR except specials, SPACE and CTLs>  << the ONLY to check
                    374: 
                    375:                specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
1.92      misha     376:                                /  "," / ";" / ":" / "\" / <">  ;  string, to use
                    377:                                /  "." / "[" / "]"              ;  within a word.
1.9       paf       378: 
                    379:        */
1.45      paf       380:        const char* exception_type="email.format";
1.9       paf       381:        if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17      paf       382:                throw Exception(exception_type,
1.9       paf       383:                        &result,
1.32      paf       384:                        "email contains bad characters (specials)");
1.9       paf       385:        if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17      paf       386:                throw Exception(exception_type,
1.9       paf       387:                        &result,
1.32      paf       388:                        "email contains bad characters (whitespace)");
1.9       paf       389:        if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17      paf       390:                throw Exception(exception_type,
1.9       paf       391:                        &result,
1.32      paf       392:                        "email contains bad characters (control)");
1.16      paf       393:        if(result.is_empty())
1.17      paf       394:                throw Exception(exception_type,
1.45      paf       395:                        0,
1.16      paf       396:                        "email is empty");
1.39      paf       397: }
                    398: 
1.45      paf       399: static const String& extractEmails(const String& string) {
                    400:        char *emails=string.cstrm();
                    401:        String& result=*new String;
1.39      paf       402:        while(char *email=lsplit(&emails, ',')) {
                    403:                rsplit(email, '>');
                    404:                if(char *in_brackets=lsplit(email, '<'))
                    405:                        email=in_brackets;
                    406:                if(!result.is_empty())
                    407:                        result<<",";
1.45      paf       408:                extractEmail(result, email);
1.39      paf       409:        }
1.9       paf       410: 
                    411:        return result;
                    412: }
1.45      paf       413: 
                    414: #ifndef DOXYGEN
                    415: struct Store_message_element_info {
                    416:        Request_charsets& charsets;
                    417:        String& header;
                    418:        const String* & from;
1.60      paf       419:        bool extract_to; String* & to;
1.45      paf       420:        const String* errors_to;
                    421:        bool mime_version_specified;
                    422:        ArrayValue* parts[P_TYPES_COUNT];
                    423:        int parts_count;
                    424:        bool backward_compatibility;
                    425:        Value* content_type;
1.66      paf       426:        bool had_content_disposition;
1.45      paf       427: 
                    428:        Store_message_element_info(
                    429:                Request_charsets& acharsets,
                    430:                String& aheader,
                    431:                const String* & afrom,
1.60      paf       432:                bool aextract_to, String* & ato
1.45      paf       433:                ):
                    434:                
                    435:                charsets(acharsets),
                    436:                header(aheader),
                    437:                from(afrom),
1.60      paf       438:                extract_to(aextract_to), to(ato),
1.45      paf       439:                errors_to(0),
                    440:                mime_version_specified(false),
                    441:                parts_count(0),
1.66      paf       442:                backward_compatibility(false), content_type(0),
                    443:                had_content_disposition(false){
1.45      paf       444:        }
                    445: };
                    446: #endif
1.99      moko      447: 
1.45      paf       448: static void store_message_element(HashStringValue::key_type raw_element_name, 
1.92      misha     449:                                HashStringValue::value_type element_value, 
                    450:                                Store_message_element_info *info) {
1.45      paf       451:        const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
                    452:                info->charsets.source(), String::CC_LOWER);
1.1       paf       453: 
                    454:        // exclude internals
1.52      paf       455:        if(low_element_name==MAIL_OPTIONS_NAME
                    456:                || low_element_name==CHARSET_NAME
1.5       paf       457:                || low_element_name==VALUE_NAME
                    458:                || low_element_name==RAW_NAME
1.64      paf       459:                || low_element_name==FORMAT_NAME
1.76      misha     460:                || low_element_name==NAME_NAME
1.94      misha     461:                || low_element_name==CID_NAME
                    462:                || low_element_name==MAIL_DEBUG_NAME)
1.1       paf       463:                return;
                    464: 
                    465:        // grep parts
                    466:        for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.45      paf       467:                if(low_element_name.starts_with(part_name_begins[pt])) {
1.29      paf       468:                        // check that $.message# '#' is digit
1.45      paf       469:                        size_t start_len=strlen(part_name_begins[pt]);
                    470:                        if(low_element_name.length()>start_len) {
                    471:                                const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.63      paf       472:                                if(!isdigit((unsigned char)*at_num))
1.29      paf       473:                                        continue;
                    474:                        }
1.45      paf       475:                        *info->parts[pt]+=element_value;
                    476:                        info->parts_count++;
1.1       paf       477:                        return;
                    478:                }
                    479:        }
                    480: 
1.10      paf       481:        // fetch some special headers
1.45      paf       482:        if(low_element_name=="from")
                    483:                info->from=&extractEmails(element_value->as_string());
1.90      misha     484:        if(low_element_name==CONTENT_DISPOSITION)
1.66      paf       485:                info->had_content_disposition=true;
1.60      paf       486:        if(info->extract_to) { // defined only when SMTP used, see mail.C [collecting info for RCPT to-s]
1.39      paf       487:                bool is_to=low_element_name=="to" ;
                    488:                bool is_cc=low_element_name=="cc" ;
                    489:                bool is_bcc=low_element_name=="bcc" ;
                    490:                if(is_to||is_cc||is_bcc) {
1.45      paf       491:                        if(!info->to)
                    492:                                info->to=new String;
1.39      paf       493:                        else
1.45      paf       494:                                *info->to << ",";
                    495:                        *info->to << extractEmails(element_value->as_string());
1.39      paf       496:                }
                    497: 
                    498:                if(is_bcc) // blinding it
1.45      paf       499:                        return;
1.39      paf       500:        }
1.12      paf       501:        if(low_element_name=="errors-to")
1.45      paf       502:                info->errors_to=&extractEmails(element_value->as_string());     
1.37      paf       503:        if(low_element_name=="mime-version")
1.45      paf       504:                info->mime_version_specified=true;
1.1       paf       505: 
1.45      paf       506:        // has content type?
                    507:        if(low_element_name==CONTENT_TYPE_NAME) {
                    508:                info->content_type=element_value;
                    509:                if(info->backward_compatibility)
                    510:                        return;
1.39      paf       511:        }
1.1       paf       512: 
1.45      paf       513:        // preparing header line
                    514:        const String& source_line=attributed_meaning_to_string(*element_value,
                    515:                String::L_PASS_APPENDED/*does not matter, would cstr(AS_IS) right away*/);
1.66      paf       516:        if(source_line.is_empty())
                    517:                return; // we don't need empty headers here [used in clearing content-disposition]
                    518: 
1.45      paf       519:        const char* source_line_cstr=source_line.cstr();
                    520:        String::C mail=Charset::transcode(
                    521:                String::C(source_line_cstr, source_line.length()),
                    522:                info->charsets.source(), 
                    523:                info->charsets.mail());
1.92      misha     524: 
1.45      paf       525:        String& mail_line=*new String;
1.73      paf       526:        if(low_element_name=="to"
                    527:                || low_element_name=="cc" 
                    528:                || low_element_name=="bcc") 
                    529:        {
                    530:                // never wrap address lines, mailer can not handle wrapped properly
                    531:                mail_line.append_strdup(mail.str, mail.length, String::L_MAIL_HEADER);
                    532:        } else {
                    533:                while(mail.length) {
                    534:                        bool too_long=mail.length>MAX_CHARS_IN_HEADER_LINE;
                    535:                        size_t length=too_long
                    536:                                ? MAX_CHARS_IN_HEADER_LINE
                    537:                                : mail.length;
                    538: 
                    539:                        mail_line.append_strdup(mail.str, length, String::L_MAIL_HEADER);
                    540:                        mail.length-=length;
                    541: 
                    542:                        if(too_long)
                    543:                                mail_line << "\n "; // break header and continue it on next line
                    544:                }       
                    545:        }
1.45      paf       546: 
                    547:        // append header line
                    548:        info->header 
1.91      misha     549:                << capitalize(raw_element_name.cstr())
1.96      misha     550:                << ": " << mail_line.untaint_cstr(String::L_AS_IS, 0, &info->charsets)
1.45      paf       551:                << "\n";
                    552: }
                    553: 
                    554: static const String& file_value_to_string(Request& r, Value* send_value) {
1.64      paf       555:        String& result=*new String;
                    556: 
1.45      paf       557:        VFile* vfile;
                    558:        const String* file_name=0;
                    559:        Value* vformat=0;
1.76      misha     560:        Value* vcid=0;
1.66      paf       561:        const String* dummy_from;
                    562:        String* dummy_to;
1.95      misha     563:        Store_message_element_info info(r.charsets, result, dummy_from, false, dummy_to);
                    564: 
1.45      paf       565:        if(HashStringValue *send_hash=send_value->get_hash()) { // hash
1.74      paf       566:                send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.64      paf       567: 
1.1       paf       568:                // $.value
1.45      paf       569:                if(Value* value=send_hash->get(value_name))
1.85      misha     570:                        vfile=value->as_vfile(String::L_AS_IS);
1.1       paf       571:                else
1.78      misha     572:                        throw Exception(PARSER_RUNTIME,
1.45      paf       573:                                0,
1.1       paf       574:                                "file part has no $value");
                    575: 
                    576:                // $.format
1.45      paf       577:                vformat=send_hash->get(format_name);
1.1       paf       578: 
1.76      misha     579:                // $.content-id
                    580:                vcid=send_hash->get(cid_name);
                    581: 
1.6       paf       582:                // $.name
1.45      paf       583:                if(Value* vfile_name=send_hash->get(name_name)) // $name specified 
1.1       paf       584:                        file_name=&vfile_name->as_string();
1.28      paf       585:        } else  // must be VFile then
1.45      paf       586:                vfile=send_value->as_vfile(String::L_AS_IS);
1.28      paf       587: 
                    588:        if(!file_name)
1.45      paf       589:                file_name=&vfile->fields().get(name_name)->as_string();
1.28      paf       590: 
1.95      misha     591:        const char* file_name_cstr;
                    592:        const char* quoted_file_name_cstr;
                    593:        {
                    594:                Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.97      misha     595:                file_name_cstr=file_name->untaint_and_transcode_cstr(String::L_FILE_SPEC, &charsets);
1.95      misha     596:                quoted_file_name_cstr=String(file_name_cstr).taint_cstr(String::L_MAIL_HEADER, 0, &charsets);
                    597:        }
1.1       paf       598: 
1.95      misha     599:        // Content-Type: application/octet-stream
                    600:        result
                    601:                << HTTP_CONTENT_TYPE_CAPITALIZED ": "
                    602:                << r.mime_type_of(file_name_cstr)
                    603:                << "; name=\""
                    604:                << quoted_file_name_cstr
                    605:                << "\"\n";
1.66      paf       606: 
1.95      misha     607:        if(!info.had_content_disposition) // $.Content-Disposition wasn't specified by user
1.79      misha     608:                result
1.91      misha     609:                        << CONTENT_DISPOSITION_CAPITALIZED ": "
1.79      misha     610:                        << ( vcid ? CONTENT_DISPOSITION_INLINE : CONTENT_DISPOSITION_ATTACHMENT )
                    611:                        << "; "
1.95      misha     612:                        << CONTENT_DISPOSITION_FILENAME_NAME"=\"" << quoted_file_name_cstr << "\"\n";
1.1       paf       613: 
1.79      misha     614:        if(vcid)
1.93      misha     615:                result
                    616:                        << "Content-Id: <"
                    617:                        << vcid->as_string()
                    618:                        << ">\n"; // @todo: value must be escaped as %hh
1.79      misha     619: 
1.45      paf       620:        const String* type=vformat?&vformat->as_string():0;
1.93      misha     621:        if(!type/*default*/ || *type=="base64") {
                    622:                result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": base64\n\n";
                    623:                result << pa_base64_encode(vfile->value_ptr(), vfile->value_size());
1.75      paf       624:        } else {
1.93      misha     625:                if(*type=="uue") {
                    626:                        result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": x-uuencode\n\n";
1.95      misha     627:                        result << pa_uuencode((const unsigned char*)vfile->value_ptr(), vfile->value_size(), file_name_cstr);
                    628:                } else
1.82      misha     629:                        throw Exception(PARSER_RUNTIME,
                    630:                                type,
                    631:                                "unknown attachment encode format");
1.75      paf       632:        }
1.95      misha     633: 
1.1       paf       634:        return result;
                    635: }
                    636: 
1.45      paf       637: static const String& text_value_to_string(Request& r,
1.92      misha     638:                                        PartType pt, Value* send_value,
                    639:                                        Store_message_element_info& info) {
1.45      paf       640:        String& result=*new String;
1.1       paf       641: 
1.45      paf       642:        Value* text_value;
1.81      misha     643:        Value* content_transfer_encoding=0;
1.45      paf       644:        if(HashStringValue* send_hash=send_value->get_hash()) {
1.1       paf       645:                // $.USER-HEADERS
1.92      misha     646:                info.content_type=0;
                    647:                info.backward_compatibility=false; // reset
1.74      paf       648:                send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.1       paf       649:                // $.value
1.45      paf       650:                text_value=send_hash->get(value_name);
1.1       paf       651:                if(!text_value)
1.78      misha     652:                        throw Exception(PARSER_RUNTIME,
1.45      paf       653:                                0,
                    654:                                "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.81      misha     655:                content_transfer_encoding=send_hash->get(content_transfer_encoding_name);
1.1       paf       656:        } else
1.45      paf       657:                text_value=send_value;
1.1       paf       658: 
1.45      paf       659:        if(!info.content_type) {
                    660:                result 
1.91      misha     661:                        << HTTP_CONTENT_TYPE_CAPITALIZED ": text/" << (pt==P_TEXT?"plain":"html")
1.45      paf       662:                        << "; charset=" << info.charsets.mail().NAME()
                    663:                        << "\n";
1.1       paf       664:        }
1.81      misha     665:        if(!content_transfer_encoding)
1.91      misha     666:                result << CONTENT_TRANSFER_ENCODING_CAPITALIZED << ": 8bit\n";
1.1       paf       667: 
                    668:        // header|body separator
                    669:        result << "\n"; 
                    670: 
                    671:        // body
1.45      paf       672:        const String* body;
1.1       paf       673:        switch(pt) {
                    674:        case P_TEXT:
1.92      misha     675:                {
                    676:                        body=&text_value->as_string();
                    677:                        break;
                    678:                }
1.1       paf       679:        case P_HTML: 
                    680:                {
1.45      paf       681:                        Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
1.55      paf       682:                        if(text_value->get_junction())
1.41      paf       683:                                body=&r.process_to_string(*text_value);
1.92      misha     684:                        else
1.78      misha     685:                                throw Exception(PARSER_RUNTIME,
1.45      paf       686:                                        0,
1.1       paf       687:                                        "html part value must be code");
                    688: 
                    689:                        break;
                    690:                }
1.53      paf       691:        default:
                    692:                throw Exception(0,
                    693:                        0,
                    694:                        "unhandled part type #%d", pt);
1.41      paf       695:        }
                    696:        if(body) {
1.69      paf       697:                Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.97      misha     698:                const char* body_cstr=body->untaint_and_transcode_cstr(String::L_AS_IS, &charsets);
1.95      misha     699:                result.append_know_length(body_cstr, strlen(body_cstr), String::L_CLEAN);
1.1       paf       700:        }
                    701: 
                    702:        return result;
1.111     moko      703: }
1.1       paf       704: 
                    705: /// @todo files and messages in order (file, file2, ...)
1.45      paf       706: const String& VMail::message_hash_to_string(Request& r,
1.92      misha     707:                                        HashStringValue* message_hash, int level, 
                    708:                                        const String* & from, bool extract_to, String* & to) {
1.34      paf       709:        
1.1       paf       710:        if(!message_hash)
1.78      misha     711:                throw Exception(PARSER_RUNTIME,
1.45      paf       712:                        0,
1.1       paf       713:                        "message must be hash");
                    714: 
1.45      paf       715:        String& result=*new String;
1.1       paf       716: 
1.45      paf       717:        if(Value* vrecodecharset_name=message_hash->get(charset_name))
                    718:                r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
                    719:                        .change_case(r.charsets.source(), String::CC_UPPER)));
1.1       paf       720:        else
1.45      paf       721:                r.charsets.set_mail(r.charsets.source());
                    722:        // no big deal that we leave it set. they wont miss this point which would reset it
1.1       paf       723: 
1.45      paf       724:        Store_message_element_info info(r.charsets, 
1.60      paf       725:                result, from, extract_to, to);
1.1       paf       726:        {
1.45      paf       727:                // for backward compatibilyty $.body+$.content-type -> 
                    728:                // $.text[$.value[] $.content-type[]]
                    729: 
1.1       paf       730:                for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45      paf       731:                        info.parts[pt]=new ArrayValue(1);
                    732: 
                    733:                Value* body=message_hash->get("body");
                    734:                if(body) {
                    735:                        message_hash->remove("body");
                    736:                        info.backward_compatibility=true;
                    737:                }               
1.74      paf       738:                message_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.45      paf       739: 
                    740:                if(body) {
                    741:                        VHash& text_part=*new VHash(); 
                    742:                        HashStringValue& hash=text_part.hash();
                    743:                        hash.put(value_name, body);
                    744:                        if(info.content_type)
                    745:                                hash.put(content_type_name, info.content_type);
                    746:                        
                    747:                        *info.parts[P_TEXT]+=&text_part;
                    748:                        info.parts_count++;
                    749:                }
                    750: 
1.10      paf       751:                if(!info.errors_to)
1.91      misha     752:                        result << "Errors-To: postmaster\n"; // errors-to: default
1.37      paf       753:                if(!info.mime_version_specified)
                    754:                        result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1       paf       755:        }
                    756: 
1.45      paf       757:        int textCount=info.parts[P_TEXT]->count();
1.1       paf       758:        if(textCount>1)
1.78      misha     759:                throw Exception(PARSER_RUNTIME,
1.45      paf       760:                        0,
1.1       paf       761:                        "multiple text parts not supported, use file part");
1.45      paf       762:        int htmlCount=info.parts[P_HTML]->count();
1.1       paf       763:        if(htmlCount>1)
1.78      misha     764:                throw Exception(PARSER_RUNTIME,
1.45      paf       765:                        0,
1.1       paf       766:                        "multiple html parts not supported, use file part");
                    767: 
                    768: 
                    769:        bool multipart=info.parts_count>1;
                    770:        bool alternative=textCount && htmlCount;
                    771:        // header
                    772:        char *boundary=0;
                    773:        if(multipart) {
1.45      paf       774:                boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1       paf       775:                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.76      misha     776: 
                    777:                bool is_inline = false;
                    778:                {
                    779:                        ArrayValue& files=*info.parts[P_FILE];
                    780:                        for(size_t i=0; i<files.count(); i++) {
1.82      misha     781:                                HashStringValue* file;
                    782:                                if((file=files.get(i)->get_hash()) && file->get(cid_name)){
1.76      misha     783:                                        is_inline = true;
                    784:                                        break;
1.77      misha     785:                                }
1.76      misha     786:                        }
                    787:                }
                    788:                
1.91      misha     789:                result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << ( is_inline ? HTTP_CONTENT_TYPE_MULTIPART_RELATED : HTTP_CONTENT_TYPE_MULTIPART_MIXED ) << ";";
1.76      misha     790: 
1.1       paf       791:                // multi-part
1.45      paf       792:                result 
1.76      misha     793:                         << " boundary=\"" << boundary << "\"\n"
1.45      paf       794:                                "\n"
                    795:                                "This is a multi-part message in MIME format.";
1.1       paf       796:        }
                    797: 
                    798:        // alternative or not
                    799:        {
                    800:                if(alternative) {
1.45      paf       801:                        result << "\n\n--" << boundary << "\n" // intermediate boundary
1.91      misha     802:                                HTTP_CONTENT_TYPE_CAPITALIZED ": multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1       paf       803:                } 
                    804:                for(int i=0; i<2; i++) {
                    805:                        PartType pt=i==0?P_TEXT:P_HTML;
1.45      paf       806:                        if(info.parts[pt]->count()) {
1.1       paf       807:                                if(alternative)
                    808:                                        result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
                    809:                                else if(boundary)
                    810:                                        result << "\n\n--" << boundary << "\n";  // intermediate boundary
1.45      paf       811:                                result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1       paf       812:                        }
                    813:                }
                    814:                if(alternative)
                    815:                        result << "\n\n--ALT" << boundary << "--\n";
                    816:        }
                    817: 
                    818:        // files
                    819:        {
1.45      paf       820:                ArrayValue& files=*info.parts[P_FILE];
                    821:                for(size_t i=0; i<files.count(); i++) {
1.1       paf       822:                        if(boundary)
                    823:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
1.45      paf       824:                        result << file_value_to_string(r, files.get(i));
1.1       paf       825:                }
                    826:        }
                    827: 
                    828:        // messages
                    829:        {
1.45      paf       830:                ArrayValue& messages=*info.parts[P_MESSAGE];
                    831:                for(size_t i=0; i<messages.count(); i++) {
1.1       paf       832:                        if(boundary)
                    833:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    834:                        
1.45      paf       835:                        const String* dummy_from;
                    836:                        String* dummy_to;
                    837:                        result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
1.60      paf       838:                                dummy_from, false, dummy_to);
1.1       paf       839:                }
                    840:        }
                    841:        
                    842:        // tailer
                    843:        if(boundary)
                    844:                result << "\n\n--" << boundary << "--\n"; // finish boundary
                    845: 
                    846:        // return
                    847:        return result;
                    848: }
                    849: 
                    850: 
1.88      misha     851: Value* VMail::get_element(const String& aname) {
1.1       paf       852:        // $fields
1.3       paf       853: #ifdef WITH_MAILRECEIVE
1.1       paf       854:        if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.48      paf       855:                return &vreceived;
1.1       paf       856: #endif
                    857: 
1.110     misha     858:        // $method
1.88      misha     859:        if(Value* result=VStateless_class::get_element(aname))
1.1       paf       860:                return result;
                    861: 
1.109     moko      862:        return bark("%s field not found", &aname);
1.1       paf       863: }

E-mail: