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

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

E-mail: