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

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.8     ! paf         8:        $Id: mail.C,v 1.7 2001/04/07 15:34:56 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_config_includes.h"
                     12: 
1.6       paf        13: #ifdef WIN32
                     14: #      include "smtp/smtp.h"
                     15: #endif
                     16: 
1.1       paf        17: #include "_mail.h"
                     18: #include "pa_common.h"
                     19: #include "pa_request.h"
1.6       paf        20: #include "pa_vfile.h"
1.4       paf        21: 
1.1       paf        22: // global var
                     23: 
                     24: VStateless_class *mail_class;
                     25: 
1.7       paf        26: // helpers
                     27: 
1.8     ! paf        28: ///
        !            29: static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
        !            30:        // content-transfer-encoding: x-uuencode
        !            31:        result+="content-transfer-encoding: x-uuencode\n";
        !            32:        result+="\n";
        !            33:        result+="todo";
        !            34: }
        !            35: 
1.7       paf        36: /// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
                     37: static const String& attach_hash_to_string(Request& r, const String& origin_string, 
                     38:                                                                                   Hash& attach_hash) {
                     39:        Pool& pool=r.pool();
                     40: 
                     41:        Value *vtype=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "type")));
                     42:        if(!vtype)
                     43:                PTHROW(0, 0,
                     44:                        &origin_string,
                     45:                        "has no $type");
                     46: 
1.8     ! paf        47:        const VFile *vfile;
        !            48:        if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
        !            49:                vfile=value->as_vfile();
        !            50:        else
1.7       paf        51:                PTHROW(0, 0,
                     52:                        &origin_string,
                     53:                        "has no $value");
                     54: 
                     55:        const String *file_name;
                     56:        if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8     ! paf        57:                *new(pool) String(pool, "file-name")))) // specified $file-name
1.7       paf        58:                file_name=&vfile_name->as_string();
1.8     ! paf        59:        else // no $file-name
        !            60:                if(Value *vfile_name=static_cast<Value *>(vfile->fields().get(*name_name)))
        !            61:                        file_name=&vfile_name->as_string(); // VFile knows name
        !            62:                else // vfile doesn't know name
        !            63:                        file_name=new(pool) String(pool, "noname.dat");
        !            64:        const char *file_name_cstr=file_name->cstr(String::UL_FILE_NAME);
1.7       paf        65: 
                     66:        String& result=*new(pool) String(pool);
                     67: 
                     68:        // content-disposition: attachment; filename="user_file_name"
                     69:        result+="content-disposition: attachment; filename=\"";
1.8     ! paf        70:        result+=file_name_cstr;
1.7       paf        71:        result+="\"\n";
                     72: 
                     73:        const String& type=vtype->as_string();
                     74:        if(type=="uue") {
1.8     ! paf        75:                uuencode(result, file_name_cstr, *vfile);
1.7       paf        76:        } else 
                     77:                PTHROW(0, 0,
                     78:                        &type,
                     79:                        "unknown encode type");
                     80:        
                     81:        return result;
                     82: }
                     83: 
1.1       paf        84: 
                     85: struct Mail_info {
                     86:        String *attribute_to_exclude;
                     87:        String *header;
1.2       paf        88:        const String **from, **to;
1.1       paf        89: };
                     90: 
                     91: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                     92:                                                                 void *info) {
                     93: 
                     94:        Value& lmeaning=*static_cast<Value *>(ameaning);
                     95:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf        96: 
                     97:        // exclude one attribute [body]
1.1       paf        98:        if(aattribute==*mi.attribute_to_exclude)
                     99:                return;
                    100: 
1.2       paf       101:        // fetch from & to from header for SMTP
                    102:        if(mi.from && aattribute=="from")
                    103:                *mi.from=&lmeaning.as_string();
                    104:        if(mi.to && aattribute=="to")
                    105:                *mi.to=&lmeaning.as_string();
                    106: 
                    107:        // append header line
                    108:        *mi.header+=aattribute;
                    109:        *mi.header+=":";
                    110:        *mi.header+=attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER);
                    111:        *mi.header+="\n";
                    112: }
                    113: struct Seq_item {
1.7       paf       114:        const String *part_name;
1.2       paf       115:        Value *part_value;
                    116: };
1.7       paf       117: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       118:                                         void *info) {
                    119:        Seq_item **seq_ref=static_cast<Seq_item **>(info);
1.7       paf       120:        (**seq_ref).part_name=&part_name;
1.2       paf       121:        (**seq_ref).part_value=static_cast<Value *>(part_value);
                    122:        (*seq_ref)++;
                    123: }
                    124: static double key_of_part(const void *item) {
1.7       paf       125:        const char *cstr=static_cast<const Seq_item *>(item)->part_name->cstr();
1.2       paf       126:        char *error_pos;
                    127:        return strtod(cstr, &error_pos);
                    128: }
                    129: static int sort_cmp_string_double_value(const void *a, const void *b) {
                    130:        double va=key_of_part(a);
                    131:        double vb=key_of_part(b);
1.7       paf       132: 
                    133:        // 0 logically equals infinity. so that attachments would go last
                    134:        if(va==0)
                    135:                return +1;
                    136:        if(vb==0)
                    137:                return -1;
                    138: 
1.2       paf       139:        if(va<vb)
                    140:                return -1;
                    141:        else if(va>vb)
                    142:                return +1;
                    143:        else 
                    144:                return 0;
1.1       paf       145: }
1.2       paf       146: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                    147:                                                                                   Hash& letter_hash, int level,
                    148:                                                                                   const String **from, const String **to) {
                    149:        Pool& pool=r.pool();
                    150: 
                    151:        // prepare header: 'hash' without "body"
                    152:        String& result=*new(pool) String(pool);
                    153:        Mail_info mail_info={
                    154:                /*excluding*/ body_name,
                    155:                &result,
                    156:                from, to
                    157:        };
                    158:        letter_hash.for_each(add_header_attribute, &mail_info);
                    159: 
                    160:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                    161:                if(Hash *body_hash=body_element->get_hash()) {
                    162:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.7       paf       163:                        snprintf(boundary, MAX_NUMBER, "%d", level);
1.2       paf       164:                        // multi-part
1.3       paf       165:                        ((result+=
1.7       paf       166:                                "content-type: multipart/mixed; boundary=\"")+=boundary)+="\"\n"
1.2       paf       167:                                "\n"
                    168:                                "This is a multi-part message in MIME format.";
                    169: 
1.3       paf       170:                        // body parts..
                    171:                        // ..collect
1.2       paf       172:                        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
                    173:                        Seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       174:                        // ..sort
1.2       paf       175:                        _qsort(seq, body_hash->size(), sizeof(Seq_item), 
                    176:                                sort_cmp_string_double_value);
1.3       paf       177:                        // ..insert in 'seq' order
1.2       paf       178:                        for(int i=0; i<body_hash->size(); i++) {
                    179:                                // intermediate boundary
1.7       paf       180:                                ((result+="\n--")+=boundary)+="\n";
1.2       paf       181: 
                    182:                                if(Hash *part_hash=seq[i].part_value->get_hash())
1.7       paf       183:                                        if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")
                    184:                                                result+=attach_hash_to_string(r, *seq[i].part_name, *part_hash);
                    185:                                        else 
                    186:                                                result+=letter_hash_to_string(r, method_name, *part_hash, 
                    187:                                                        level+1, 0, 0);
1.2       paf       188:                                else
                    189:                                        PTHROW(0, 0,
1.7       paf       190:                                                seq[i].part_name,
1.2       paf       191:                                                "part is not hash");
                    192:                        }
                    193: 
                    194:                        // finish boundary
1.7       paf       195:                        ((result+="\n--")+=boundary)+="--\n";
1.2       paf       196:                } else {
                    197:                        result+="\n"; // header|body separator
1.4       paf       198:                        result+=body_element->as_string();  // body
1.2       paf       199:                }
                    200:        } else 
                    201:                PTHROW(0, 0,
                    202:                        &method_name,
                    203:                        "has no $body");
                    204: 
                    205:        return result;
                    206: }
                    207: 
1.4       paf       208: /// @test unix ver
                    209: static void sendmail(Request& r, const String& method_name, 
                    210:                                         const String& letter, 
                    211:                                         const String *from, const String *to) {
                    212:        Pool& pool=r.pool();
                    213: 
                    214: #ifdef WIN32
                    215:        if(!from)
                    216:                PTHROW(0, 0,
                    217:                        &method_name,
                    218:                        "not specified 'from'");
                    219:        if(!to)
                    220:                PTHROW(0, 0,
                    221:                        &method_name,
                    222:                        "not specified 'to'");
                    223: 
                    224:        char *letter_cstr=letter.cstr();
                    225:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       226:        Value *server_port;
1.4       paf       227:        // $MAIN:MAIL.SMTP[mail.design.ru]
1.5       paf       228:        if(r.mail && 
                    229:                (server_port=static_cast<Value *>(r.mail->get(
                    230:                        *new(pool) String(pool, "SMTP"))))) {
                    231:                char *server=server_port->as_string().cstr();
1.4       paf       232:                char *port=rsplit(server, ':');
                    233:                if(!port)
1.5       paf       234:                        port=const_cast<char *>("25");
1.4       paf       235: 
                    236:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    237:        } else
                    238:                PTHROW(0, 0,
                    239:                        &method_name,
                    240:                        "$MAIN:MAIL.SMTP not defined");
                    241: #else
                    242:        PTHROW(0, 0,
                    243:                &method_name,
                    244:                "todo");
                    245: #endif
                    246: }
                    247: 
1.7       paf       248: 
                    249: // methods
                    250: 
1.1       paf       251: static void _send(Request& r, const String& method_name, Array *params) {
                    252:        Pool& pool=r.pool();
                    253: 
                    254:        Value& vhash=*static_cast<Value *>(params->get(0));
                    255:        // forcing [this body type]
                    256:        r.fail_if_junction_(true, vhash, method_name, "message must not be code");
                    257: 
                    258:        Hash *hash=vhash.get_hash();
                    259:        if(!hash)
                    260:                PTHROW(0, 0,
                    261:                        &method_name,
                    262:                        "message must be hash");
                    263: 
1.2       paf       264:        const String *from, *to;
1.4       paf       265:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       266: 
1.6       paf       267:        r.write_assign_lang(*new(pool) VString(letter));
                    268:        //sendmail(r, method_name, letter, from, to);
                    269: }
                    270: 
1.1       paf       271: // initialize
                    272: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
                    273:        // ^mail:send{hash}
                    274:        vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
                    275: }

E-mail: