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

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

E-mail: