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

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.54    ! paf         9: static const char * const IDENT_VMAIL_C="$Date: 2003/11/20 15:35:32 $";
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.53      paf       324: void VMail::fill_received(Request& 
                    325: #ifdef WITH_MAILRECEIVE
                    326:                                                  r
                    327: #endif
                    328:                                                  ) {
1.1       paf       329:        // store letter to received
1.3       paf       330: #ifdef WITH_MAILRECEIVE
1.45      paf       331:        if(r.request_info.mail_received) {
1.1       paf       332:                // init
1.50      paf       333:                g_mime_init(0);
                    334:                //      g_mime_init(GMIME_INIT_FLAG_UTF8);
1.1       paf       335: 
                    336:                // create stream with CRLF filter
                    337:                GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
                    338:                GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
                    339:                GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
                    340:                g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
1.45      paf       341:                g_mime_stream_unref(stream);
1.1       paf       342:                stream = istream;
                    343:                try {
                    344:                        // parse incoming stream
1.48      paf       345:                        parse(r, stream, vreceived.hash());
1.1       paf       346:                        // normal stream free 
1.45      paf       347:                        g_mime_stream_unref(stream);
1.48      paf       348:                } catch(const Exception& e) {
                    349:                        // abnormal stream free 
                    350:                        g_mime_stream_unref(stream);
                    351:                        Charset& source_charset=r.charsets.source();
                    352:                        HashStringValue& received=vreceived.hash();
                    353:                        putReceived(source_charset, received, VALUE_NAME, "<exception occured while parsing messages>");
                    354:                        putReceived(source_charset, received, EXCEPTION_VALUE, e.comment());
1.1       paf       355:                } catch(...) {
                    356:                        // abnormal stream free 
1.45      paf       357:                        g_mime_stream_unref(stream);
1.48      paf       358:                        rethrow;
1.1       paf       359:                }
                    360:        }
                    361: #endif
                    362: }
                    363: 
1.9       paf       364: typedef int (*string_contains_char_which_check)(int);
1.45      paf       365: static bool string_contains_char_which(const char* string, string_contains_char_which_check check) {
1.9       paf       366:        while(char c=*string++) {
                    367:                if(check(c))
                    368:                        return true;
                    369:        }
                    370:        return false;
                    371: }
1.32      paf       372: static char *trimBoth(char *s) {
                    373:        // sanity check
                    374:        if(!s)
                    375:                return 0;
                    376: 
                    377:        // trim head whitespace
                    378:        while(*s && isspace(*s))
                    379:                s++;
                    380:        // trim tail whitespace
                    381:        char *tail=s+strlen(s);
                    382:        if(tail>s) {
                    383:                do {
                    384:                        --tail;
                    385:                        if(isspace(*tail))
                    386:                                *tail=0;
                    387:                } while(tail>s);
                    388:        }
                    389:        // return it
                    390:        return s;
                    391: }
1.45      paf       392: static void extractEmail(String& result, char *email) {
1.32      paf       393:        email=trimBoth(email);
1.45      paf       394:        result.append_help_length(email, 0, String::L_TAINTED);
1.9       paf       395: 
                    396:        /*
                    397:                http://www.faqs.org/rfcs/rfc822.html
                    398: 
                    399:                addr-spec   =  local-part "@" domain        ; global address
                    400:        
                    401:                local-part  =  word *("." word)             ; uninterpreted case-preserved
                    402:                word        =  atom / quoted-string
                    403: 
                    404:                domain      =  sub-domain *("." sub-domain)
                    405:                sub-domain  =  domain-ref / domain-literal
                    406:                domain-ref  =  atom                         ; symbolic reference
                    407: 
                    408:         domain-literal << ignoring for now
                    409:                quoted-string in word << ignoring for now
                    410: 
                    411:                atom        =  1*<any CHAR except specials, SPACE and CTLs>  << the ONLY to check
                    412: 
                    413:                specials    =  "(" / ")" / "<" / ">" / "@"  ; Must be in quoted-
                    414:                  /  "," / ";" / ":" / "\" / <">  ;  string, to use
                    415:                  /  "." / "[" / "]"              ;  within a word.
                    416: 
                    417:        */
1.45      paf       418:        const char* exception_type="email.format";
1.9       paf       419:        if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17      paf       420:                throw Exception(exception_type,
1.9       paf       421:                        &result,
1.32      paf       422:                        "email contains bad characters (specials)");
1.9       paf       423:        if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17      paf       424:                throw Exception(exception_type,
1.9       paf       425:                        &result,
1.32      paf       426:                        "email contains bad characters (whitespace)");
1.9       paf       427:        if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17      paf       428:                throw Exception(exception_type,
1.9       paf       429:                        &result,
1.32      paf       430:                        "email contains bad characters (control)");
1.16      paf       431:        if(result.is_empty())
1.17      paf       432:                throw Exception(exception_type,
1.45      paf       433:                        0,
1.16      paf       434:                        "email is empty");
1.39      paf       435: }
                    436: 
1.45      paf       437: static const String& extractEmails(const String& string) {
                    438:        char *emails=string.cstrm();
                    439:        String& result=*new String;
1.39      paf       440:        while(char *email=lsplit(&emails, ',')) {
                    441:                rsplit(email, '>');
                    442:                if(char *in_brackets=lsplit(email, '<'))
                    443:                        email=in_brackets;
                    444:                if(!result.is_empty())
                    445:                        result<<",";
1.45      paf       446:                extractEmail(result, email);
1.39      paf       447:        }
1.9       paf       448: 
                    449:        return result;
                    450: }
1.45      paf       451: 
                    452: #ifndef DOXYGEN
                    453: struct Store_message_element_info {
                    454:        Request_charsets& charsets;
                    455:        String& header;
                    456:        const String* & from;
                    457:        bool extract_to; String* & to;
                    458:        const String* errors_to;
                    459:        bool mime_version_specified;
                    460:        ArrayValue* parts[P_TYPES_COUNT];
                    461:        int parts_count;
                    462:        bool backward_compatibility;
                    463:        Value* content_type;
                    464: 
                    465:        Store_message_element_info(
                    466:                Request_charsets& acharsets,
                    467:                String& aheader,
                    468:                const String* & afrom,
                    469:                bool aextract_to, String* & ato
                    470:                ):
                    471:                
                    472:                charsets(acharsets),
                    473:                header(aheader),
                    474:                from(afrom),
                    475:                extract_to(aextract_to), to(ato),
                    476:                errors_to(0),
                    477:                mime_version_specified(false),
                    478:                parts_count(0),
                    479:                backward_compatibility(false), content_type(0) {
                    480:        }
                    481: };
                    482: #endif
                    483: static void store_message_element(HashStringValue::key_type raw_element_name, 
                    484:                                  HashStringValue::value_type element_value, 
                    485:                                  Store_message_element_info *info) {
                    486:        const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
                    487:                info->charsets.source(), String::CC_LOWER);
1.1       paf       488: 
                    489:        // exclude internals
1.52      paf       490:        if(low_element_name==MAIL_OPTIONS_NAME
                    491:                || low_element_name==CHARSET_NAME
1.5       paf       492:                || low_element_name==VALUE_NAME
                    493:                || low_element_name==RAW_NAME
                    494:                || low_element_name=="date")
1.1       paf       495:                return;
                    496: 
                    497:        // grep parts
                    498:        for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.45      paf       499:                if(low_element_name.starts_with(part_name_begins[pt])) {
1.29      paf       500:                        // check that $.message# '#' is digit
1.45      paf       501:                        size_t start_len=strlen(part_name_begins[pt]);
                    502:                        if(low_element_name.length()>start_len) {
                    503:                                const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.29      paf       504:                                if(!isdigit(*at_num))
                    505:                                        continue;
                    506:                        }
1.45      paf       507:                        *info->parts[pt]+=element_value;
                    508:                        info->parts_count++;
1.1       paf       509:                        return;
                    510:                }
                    511:        }
                    512: 
1.10      paf       513:        // fetch some special headers
1.45      paf       514:        if(low_element_name=="from")
                    515:                info->from=&extractEmails(element_value->as_string());
                    516:        if(info->extract_to) { // defined only on WIN32, see mail.C [collecting info for RCPT to-s]
1.39      paf       517:                bool is_to=low_element_name=="to" ;
                    518:                bool is_cc=low_element_name=="cc" ;
                    519:                bool is_bcc=low_element_name=="bcc" ;
                    520:                if(is_to||is_cc||is_bcc) {
1.45      paf       521:                        if(!info->to)
                    522:                                info->to=new String;
1.39      paf       523:                        else
1.45      paf       524:                                *info->to << ",";
                    525:                        *info->to << extractEmails(element_value->as_string());
1.39      paf       526:                }
                    527: 
                    528:                if(is_bcc) // blinding it
1.45      paf       529:                        return;
1.39      paf       530:        }
1.12      paf       531:        if(low_element_name=="errors-to")
1.45      paf       532:                info->errors_to=&extractEmails(element_value->as_string());     
1.37      paf       533:        if(low_element_name=="mime-version")
1.45      paf       534:                info->mime_version_specified=true;
1.1       paf       535: 
1.45      paf       536:        // has content type?
                    537:        if(low_element_name==CONTENT_TYPE_NAME) {
                    538:                info->content_type=element_value;
                    539:                if(info->backward_compatibility)
                    540:                        return;
1.39      paf       541:        }
1.1       paf       542: 
1.45      paf       543:        // preparing header line
                    544:        const String& source_line=attributed_meaning_to_string(*element_value,
                    545:                String::L_PASS_APPENDED/*does not matter, would cstr(AS_IS) right away*/);
                    546:        const char* source_line_cstr=source_line.cstr();
                    547:        String::C mail=Charset::transcode(
                    548:                String::C(source_line_cstr, source_line.length()),
                    549:                info->charsets.source(), 
                    550:                info->charsets.mail());
                    551:        String& mail_line=*new String;
                    552:        ///@todo can convert to append_help_length if transcode would return zero-terminated
                    553:        mail_line.append_know_length(mail.str, mail.length, String::L_MAIL_HEADER);
                    554: 
                    555:        // append header line
                    556:        info->header 
                    557:                << raw_element_name 
                    558:                << ":" << mail_line.cstr(String::L_UNSPECIFIED, 0, &info->charsets)
                    559:                << "\n";
                    560: }
                    561: 
                    562: static const String& file_value_to_string(Request& r, Value* send_value) {
                    563:        VFile* vfile;
                    564:        const String* file_name=0;
                    565:        Value* vformat=0;
                    566:        if(HashStringValue *send_hash=send_value->get_hash()) { // hash
1.1       paf       567:                // $.value
1.45      paf       568:                if(Value* value=send_hash->get(value_name))
                    569:                        vfile=value->as_vfile(String::L_AS_IS);
1.1       paf       570:                else
                    571:                        throw Exception("parser.runtime",
1.45      paf       572:                                0,
1.1       paf       573:                                "file part has no $value");
                    574: 
                    575:                // $.format
1.45      paf       576:                vformat=send_hash->get(format_name);
1.1       paf       577: 
1.6       paf       578:                // $.name
1.45      paf       579:                if(Value* vfile_name=send_hash->get(name_name)) // $name specified 
1.1       paf       580:                        file_name=&vfile_name->as_string();
1.28      paf       581:        } else  // must be VFile then
1.45      paf       582:                vfile=send_value->as_vfile(String::L_AS_IS);
1.28      paf       583: 
                    584:        if(!file_name)
1.45      paf       585:                file_name=&vfile->fields().get(name_name)->as_string();
1.28      paf       586: 
1.45      paf       587:        const char* file_name_cstr=file_name->cstr();
1.1       paf       588: 
1.45      paf       589:        String& result=*new String;
1.1       paf       590: 
                    591:        // content-type: application/octet-stream
                    592:        result << "content-type: " << r.mime_type_of(file_name_cstr) 
                    593:                << "; name=\"" << file_name_cstr << "\"\n";
                    594:        // content-disposition: attachment; filename="user_file_name"
1.35      paf       595:        result << 
                    596:                CONTENT_DISPOSITION_NAME ": "CONTENT_DISPOSITION_VALUE"; "
                    597:                CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.1       paf       598: 
1.45      paf       599:        const String* type=vformat?&vformat->as_string():0;
1.1       paf       600:        if(!type/*default = uue*/ || *type=="uue") {
1.45      paf       601:                pa_uuencode(result, *file_name, *vfile);
1.1       paf       602:        } else // for now
                    603:                throw Exception("parser.runtime",
                    604:                        type,
                    605:                        "unknown attachment encode format");
                    606:        
                    607:        return result;
                    608: }
                    609: 
1.45      paf       610: static const String& text_value_to_string(Request& r,
                    611:                                          PartType pt, Value* send_value,
                    612:                                          Store_message_element_info& info) {
                    613:        String& result=*new String;
1.1       paf       614: 
1.45      paf       615:        Value* text_value;
                    616:        if(HashStringValue* send_hash=send_value->get_hash()) {
1.1       paf       617:                // $.USER-HEADERS
1.45      paf       618:                info.content_type=0; info.backward_compatibility=false; // reset
1.1       paf       619:                send_hash->for_each(store_message_element, &info);
                    620:                // $.value
1.45      paf       621:                text_value=send_hash->get(value_name);
1.1       paf       622:                if(!text_value)
                    623:                        throw Exception("parser.runtime",
1.45      paf       624:                                0,
                    625:                                "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.1       paf       626:        } else
1.45      paf       627:                text_value=send_value;
1.1       paf       628: 
1.45      paf       629:        if(!info.content_type) {
                    630:                result 
                    631:                        << "content-type: text/" << (pt==P_TEXT?"plain":"html")
                    632:                        << "; charset=" << info.charsets.mail().NAME()
                    633:                        << "\n";
1.1       paf       634:        }
                    635: 
                    636:        // header|body separator
                    637:        result << "\n"; 
                    638: 
                    639:        // body
1.45      paf       640:        const String* body;
1.1       paf       641:        switch(pt) {
                    642:        case P_TEXT:
1.41      paf       643:                body=&text_value->as_string();
1.1       paf       644:                break;
                    645:        case P_HTML: 
                    646:                {
1.45      paf       647:                        Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
                    648:                        if(Junction* junction=text_value->get_junction())
1.41      paf       649:                                body=&r.process_to_string(*text_value);
1.53      paf       650:                        else {
1.1       paf       651:                                throw Exception("parser.runtime",
1.45      paf       652:                                        0,
1.1       paf       653:                                        "html part value must be code");
1.53      paf       654:                        }
1.1       paf       655: 
                    656:                        break;
                    657:                }
1.53      paf       658:        default:
                    659:                throw Exception(0,
                    660:                        0,
                    661:                        "unhandled part type #%d", pt);
1.41      paf       662:        }
                    663:        if(body) {
1.45      paf       664:                const char* body_cstr=strdup(body->cstr(String::L_UNSPECIFIED));  // body
                    665:                String::C mail=Charset::transcode(
                    666:                        String::C(body_cstr, strlen(body_cstr)),
                    667:                        r.charsets.source(),
                    668:                        r.charsets.mail()/*always set - either mail.charset or $request:charset*/);
                    669:                ///@todo
                    670:                result.append_know_length(mail.str, mail.length, String::L_CLEAN);
1.1       paf       671:        }
                    672: 
                    673:        return result;
                    674: };
                    675: 
                    676: /// @todo files and messages in order (file, file2, ...)
1.45      paf       677: const String& VMail::message_hash_to_string(Request& r,
                    678:                                            HashStringValue* message_hash, int level, 
                    679:                                            const String* & from, bool extract_to, String* & to) {
1.34      paf       680:        
1.1       paf       681:        if(!message_hash)
                    682:                throw Exception("parser.runtime",
1.45      paf       683:                        0,
1.1       paf       684:                        "message must be hash");
                    685: 
1.45      paf       686:        String& result=*new String;
1.1       paf       687: 
1.45      paf       688:        if(Value* vrecodecharset_name=message_hash->get(charset_name))
                    689:                r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
                    690:                        .change_case(r.charsets.source(), String::CC_UPPER)));
1.1       paf       691:        else
1.45      paf       692:                r.charsets.set_mail(r.charsets.source());
                    693:        // no big deal that we leave it set. they wont miss this point which would reset it
1.1       paf       694: 
1.45      paf       695:        Store_message_element_info info(r.charsets, 
                    696:                result, from, extract_to, to);
1.1       paf       697:        {
1.45      paf       698:                // for backward compatibilyty $.body+$.content-type -> 
                    699:                // $.text[$.value[] $.content-type[]]
                    700: 
1.1       paf       701:                for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45      paf       702:                        info.parts[pt]=new ArrayValue(1);
                    703: 
                    704:                Value* body=message_hash->get("body");
                    705:                if(body) {
                    706:                        message_hash->remove("body");
                    707:                        info.backward_compatibility=true;
                    708:                }               
1.1       paf       709:                message_hash->for_each(store_message_element, &info);
1.45      paf       710: 
                    711:                if(body) {
                    712:                        VHash& text_part=*new VHash(); 
                    713:                        HashStringValue& hash=text_part.hash();
                    714:                        hash.put(value_name, body);
                    715:                        if(info.content_type)
                    716:                                hash.put(content_type_name, info.content_type);
                    717:                        
                    718:                        *info.parts[P_TEXT]+=&text_part;
                    719:                        info.parts_count++;
                    720:                }
                    721: 
1.10      paf       722:                if(!info.errors_to)
                    723:                        result << "errors-to: postmaster\n"; // errors-to: default
1.37      paf       724:                if(!info.mime_version_specified)
                    725:                        result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1       paf       726:        }
                    727: 
1.45      paf       728:        int textCount=info.parts[P_TEXT]->count();
1.1       paf       729:        if(textCount>1)
                    730:                throw Exception("parser.runtime",
1.45      paf       731:                        0,
1.1       paf       732:                        "multiple text parts not supported, use file part");
1.45      paf       733:        int htmlCount=info.parts[P_HTML]->count();
1.1       paf       734:        if(htmlCount>1)
                    735:                throw Exception("parser.runtime",
1.45      paf       736:                        0,
1.1       paf       737:                        "multiple html parts not supported, use file part");
                    738: 
                    739: 
                    740:        bool multipart=info.parts_count>1;
                    741:        bool alternative=textCount && htmlCount;
                    742:        // header
                    743:        char *boundary=0;
                    744:        if(multipart) {
1.45      paf       745:                boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1       paf       746:                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                    747:                // multi-part
1.45      paf       748:                result 
                    749:                        << "content-type: multipart/mixed; boundary=\"" 
                    750:                        << boundary << "\"\n"
                    751:                                "\n"
                    752:                                "This is a multi-part message in MIME format.";
1.1       paf       753:        }
                    754: 
                    755:        // alternative or not
                    756:        {
                    757:                if(alternative) {
1.45      paf       758:                        result << "\n\n--" << boundary << "\n" // intermediate boundary
                    759:                                "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1       paf       760:                } 
                    761:                for(int i=0; i<2; i++) {
                    762:                        PartType pt=i==0?P_TEXT:P_HTML;
1.45      paf       763:                        if(info.parts[pt]->count()) {
1.1       paf       764:                                if(alternative)
                    765:                                        result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
                    766:                                else if(boundary)
                    767:                                        result << "\n\n--" << boundary << "\n";  // intermediate boundary
1.45      paf       768:                                result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1       paf       769:                        }
                    770:                }
                    771:                if(alternative)
                    772:                        result << "\n\n--ALT" << boundary << "--\n";
                    773:        }
                    774: 
                    775:        // files
                    776:        {
1.45      paf       777:                ArrayValue& files=*info.parts[P_FILE];
                    778:                for(size_t i=0; i<files.count(); i++) {
1.1       paf       779:                        if(boundary)
                    780:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
1.45      paf       781:                        result << file_value_to_string(r, files.get(i));
1.1       paf       782:                }
                    783:        }
                    784: 
                    785:        // messages
                    786:        {
1.45      paf       787:                ArrayValue& messages=*info.parts[P_MESSAGE];
                    788:                for(size_t i=0; i<messages.count(); i++) {
1.1       paf       789:                        if(boundary)
                    790:                                result << "\n\n--" << boundary << "\n";  // intermediate boundary
                    791:                        
1.45      paf       792:                        const String* dummy_from;
                    793:                        String* dummy_to;
                    794:                        result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
                    795:                                dummy_from, false, dummy_to);
1.1       paf       796:                }
                    797:        }
                    798:        
                    799:        // tailer
                    800:        if(boundary)
                    801:                result << "\n\n--" << boundary << "--\n"; // finish boundary
                    802: 
                    803:        // return
                    804:        return result;
                    805: }
                    806: 
                    807: 
1.45      paf       808: Value* VMail::get_element(const String& aname, Value& aself, bool looking_up) {
1.1       paf       809:        // $fields
1.3       paf       810: #ifdef WITH_MAILRECEIVE
1.1       paf       811:        if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.48      paf       812:                return &vreceived;
1.1       paf       813: #endif
                    814: 
                    815:        // $CLASS,$method
1.45      paf       816:        if(Value* result=VStateless_class::get_element(aname, aself, looking_up))
1.1       paf       817:                return result;
                    818: 
                    819:        return 0;
                    820: }
                    821: 
1.3       paf       822: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.48      paf       823: #      define GNOME_LIBS "../../../../win32/gnome"
1.1       paf       824: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
                    825: #      ifdef _DEBUG
                    826: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
                    827: #      else
                    828: #              pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
                    829: #      endif
                    830: #endif

E-mail: