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

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

E-mail: