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

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

E-mail: