Annotation of parser3/src/classes/mail.C, revision 1.4

1.1       paf         1: /** @file
                      2:        Parser: @b mail parser class.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5: 
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.4     ! paf         8:        $Id: mail.C,v 1.3 2001/04/07 12:13:13 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_config_includes.h"
                     12: 
                     13: #include "_mail.h"
                     14: #include "pa_common.h"
                     15: #include "pa_request.h"
                     16: 
1.4     ! paf        17: #ifdef WIN32
        !            18: #      include "smtp/smtp.h"
        !            19: #endif
        !            20: 
1.1       paf        21: // global var
                     22: 
                     23: VStateless_class *mail_class;
                     24: 
1.2       paf        25: // consts
                     26: 
1.1       paf        27: // methods
                     28: 
                     29: struct Mail_info {
                     30:        String *attribute_to_exclude;
                     31:        String *header;
1.2       paf        32:        const String **from, **to;
1.1       paf        33: };
                     34: 
                     35: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                     36:                                                                 void *info) {
                     37: 
                     38:        Value& lmeaning=*static_cast<Value *>(ameaning);
                     39:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf        40: 
                     41:        // exclude one attribute [body]
1.1       paf        42:        if(aattribute==*mi.attribute_to_exclude)
                     43:                return;
                     44: 
1.2       paf        45:        // fetch from & to from header for SMTP
                     46:        if(mi.from && aattribute=="from")
                     47:                *mi.from=&lmeaning.as_string();
                     48:        if(mi.to && aattribute=="to")
                     49:                *mi.to=&lmeaning.as_string();
                     50: 
                     51:        // append header line
                     52:        *mi.header+=aattribute;
                     53:        *mi.header+=":";
                     54:        *mi.header+=attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER);
                     55:        *mi.header+="\n";
                     56: }
                     57: struct Seq_item {
                     58:        const String *part_number;
                     59:        Value *part_value;
                     60: };
                     61: static void add_part(const Hash::Key& part_number, Hash::Val *part_value, 
                     62:                                         void *info) {
                     63:        Seq_item **seq_ref=static_cast<Seq_item **>(info);
                     64:        (**seq_ref).part_number=&part_number;
                     65:        (**seq_ref).part_value=static_cast<Value *>(part_value);
                     66:        (*seq_ref)++;
                     67: }
                     68: static double key_of_part(const void *item) {
                     69:        const char *cstr=static_cast<const Seq_item *>(item)->part_number->cstr();
                     70:        char *error_pos;
                     71:        return strtod(cstr, &error_pos);
                     72: }
                     73: static int sort_cmp_string_double_value(const void *a, const void *b) {
                     74:        double va=key_of_part(a);
                     75:        double vb=key_of_part(b);
                     76:        if(va<vb)
                     77:                return -1;
                     78:        else if(va>vb)
                     79:                return +1;
                     80:        else 
                     81:                return 0;
1.1       paf        82: }
1.2       paf        83: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                     84:                                                                                   Hash& letter_hash, int level,
                     85:                                                                                   const String **from, const String **to) {
                     86:        Pool& pool=r.pool();
                     87: 
                     88:        // prepare header: 'hash' without "body"
                     89:        String& result=*new(pool) String(pool);
                     90:        Mail_info mail_info={
                     91:                /*excluding*/ body_name,
                     92:                &result,
                     93:                from, to
                     94:        };
                     95:        letter_hash.for_each(add_header_attribute, &mail_info);
                     96: 
                     97:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                     98:                if(Hash *body_hash=body_element->get_hash()) {
                     99:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.3       paf       100:                        snprintf(boundary, MAX_NUMBER-6/*level_*/, "level_%d", level);
1.2       paf       101:                        // multi-part
1.3       paf       102:                        ((result+=
                    103:                                "content-type: multipart/mixed;\n"
                    104:                                "    boundary=\"----=")+=boundary)+="\"\n"
1.2       paf       105:                                "\n"
                    106:                                "This is a multi-part message in MIME format.";
                    107: 
1.3       paf       108:                        // body parts..
                    109:                        // ..collect
1.2       paf       110:                        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
                    111:                        Seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       112:                        // ..sort
1.2       paf       113:                        _qsort(seq, body_hash->size(), sizeof(Seq_item), 
                    114:                                sort_cmp_string_double_value);
1.3       paf       115:                        // ..insert in 'seq' order
1.2       paf       116:                        for(int i=0; i<body_hash->size(); i++) {
                    117:                                // intermediate boundary
1.3       paf       118:                                ((result+="\n------=")+=boundary)+="\n";
1.2       paf       119: 
                    120:                                if(Hash *part_hash=seq[i].part_value->get_hash())
                    121:                                        result+=letter_hash_to_string(r, method_name, *part_hash, 
                    122:                                                level+1, 0, 0);
                    123:                                else
                    124:                                        PTHROW(0, 0,
                    125:                                                seq[i].part_number,
                    126:                                                "part is not hash");
                    127:                        }
                    128: 
                    129:                        // finish boundary
1.3       paf       130:                        ((result+="\n------=")+=boundary)+="--\n";
1.2       paf       131:                } else {
                    132:                        result+="\n"; // header|body separator
1.4     ! paf       133:                        result+=body_element->as_string();  // body
1.2       paf       134:                }
                    135:        } else 
                    136:                PTHROW(0, 0,
                    137:                        &method_name,
                    138:                        "has no $body");
                    139: 
                    140:        return result;
                    141: }
                    142: 
1.4     ! paf       143: 
        !           144: /// @test unix ver
        !           145: static void sendmail(Request& r, const String& method_name, 
        !           146:                                         const String& letter, 
        !           147:                                         const String *from, const String *to) {
        !           148:        Pool& pool=r.pool();
        !           149: 
        !           150: #ifdef WIN32
        !           151:        if(!from)
        !           152:                PTHROW(0, 0,
        !           153:                        &method_name,
        !           154:                        "not specified 'from'");
        !           155:        if(!to)
        !           156:                PTHROW(0, 0,
        !           157:                        &method_name,
        !           158:                        "not specified 'to'");
        !           159: 
        !           160:        char *letter_cstr=letter.cstr();
        !           161:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
        !           162:        const String *server_port;
        !           163:        // $MAIN:MAIL.SMTP[mail.design.ru]
        !           164:        if(r.mail && (server_port=r.mail->get_string(*new(pool) String(pool, "SMTP")))) {
        !           165:                char *server=server_port->cstr();
        !           166:                char *port=rsplit(server, ':');
        !           167:                if(!port)
        !           168:                        port="25";
        !           169: 
        !           170:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
        !           171:        } else
        !           172:                PTHROW(0, 0,
        !           173:                        &method_name,
        !           174:                        "$MAIN:MAIL.SMTP not defined");
        !           175: #else
        !           176:        PTHROW(0, 0,
        !           177:                &method_name,
        !           178:                "todo");
        !           179: #endif
        !           180: }
        !           181: 
1.1       paf       182: static void _send(Request& r, const String& method_name, Array *params) {
                    183:        Pool& pool=r.pool();
                    184: 
                    185:        Value& vhash=*static_cast<Value *>(params->get(0));
                    186:        // forcing [this body type]
                    187:        r.fail_if_junction_(true, vhash, method_name, "message must not be code");
                    188: 
                    189:        Hash *hash=vhash.get_hash();
                    190:        if(!hash)
                    191:                PTHROW(0, 0,
                    192:                        &method_name,
                    193:                        "message must be hash");
                    194: 
1.2       paf       195:        const String *from, *to;
1.4     ! paf       196:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       197: 
1.4     ! paf       198:        sendmail(r, method_name, letter, from, to);
1.1       paf       199: }
                    200: 
                    201: // initialize
                    202: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
                    203:        // ^mail:send{hash}
                    204:        vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
                    205: }

E-mail: