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

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.10    ! paf         8:        $Id: mail.C,v 1.9 2001/04/07 17:33:04 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.10    ! paf        28: // uuencode
        !            29: 
        !            30: static unsigned char uue_table[64] = {
        !            31:   '`', '!', '"', '#', '$', '%', '&', '\'',
        !            32:   '(', ')', '*', '+', ',', '-', '.', '/',
        !            33:   '0', '1', '2', '3', '4', '5', '6', '7',
        !            34:   '8', '9', ':', ';', '<', '=', '>', '?',
        !            35:   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
        !            36:   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        !            37:   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
        !            38:   'X', 'Y', 'Z', '[', '\\',']', '^', '_'
        !            39: };
1.8       paf        40: static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
1.10    ! paf        41:        //header
        !            42:        result << "content-transfer-encoding: x-uuencode\n" << "\n";
        !            43:        result << "begin 644 " << file_name_cstr << "\n";
        !            44: 
        !            45:        //body
        !            46:        const unsigned char *itemp;
        !            47: 
        !            48:        int index;
        !            49:        int count=45;
        !            50: 
        !            51:        const unsigned char *in=(const unsigned char *)vfile.value_ptr();
        !            52:        size_t in_length=vfile.value_size();
        !            53: 
        !            54:        for(itemp=in; itemp<(in+in_length); itemp+=count) {
        !            55:                if((itemp+count)>(in+in_length)) 
        !            56:                        count=in_length-(itemp-in);
        !            57: 
        !            58:                char *buf=(char *)result.pool().malloc(MAX_STRING);
        !            59:                char *optr=buf;
        !            60:                
        !            61:                /*
        !            62:                * for UU and XX, encode the number of bytes as first character
        !            63:                */
        !            64:                *optr++ = uue_table[count];
        !            65:                
        !            66:                for (index=0; index<=count-3; index+=3) {
        !            67:                        *optr++ = uue_table[itemp[index] >> 2];
        !            68:                        *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | (itemp[index+1] >> 4)];
        !            69:                        *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
        !            70:                        *optr++ = uue_table[  itemp[index+2] & 0x3f];
        !            71:                }
        !            72:                
        !            73:                /*
        !            74:                * Special handlitempg for itempcomplete litempes
        !            75:                */
        !            76:                if (index != count) {
        !            77:                        if (count - index == 2) {
        !            78:                                *optr++ = uue_table[itemp[index] >> 2];
        !            79:                                *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | 
        !            80:                                        ( itemp[index+1] >> 4)];
        !            81:                                *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
        !            82:                                *optr++ = uue_table[0];
        !            83:                        }
        !            84:                        else if (count - index == 1) {
        !            85:                                *optr++ = uue_table[ itemp[index] >> 2];
        !            86:                                *optr++ = uue_table[(itemp[index] & 0x03) << 4];
        !            87:                                *optr++ = uue_table[0];
        !            88:                                *optr++ = uue_table[0];
        !            89:                        }
        !            90:                }
        !            91:                /*
        !            92:                * end of line
        !            93:                */
        !            94:                *optr++ = '\n'; 
        !            95:                *optr = 0;
        !            96:                result << buf;
        !            97:        }
        !            98:        
        !            99:        //footer
        !           100:        result.APPEND_CLEAN((const char *)uue_table, 1/* one char */, 0, 0) << "\n"
        !           101:                "end\n";
1.8       paf       102: }
                    103: 
1.7       paf       104: /// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
                    105: static const String& attach_hash_to_string(Request& r, const String& origin_string, 
                    106:                                                                                   Hash& attach_hash) {
                    107:        Pool& pool=r.pool();
                    108: 
                    109:        Value *vtype=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "type")));
                    110:        if(!vtype)
                    111:                PTHROW(0, 0,
                    112:                        &origin_string,
                    113:                        "has no $type");
                    114: 
1.8       paf       115:        const VFile *vfile;
                    116:        if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
                    117:                vfile=value->as_vfile();
                    118:        else
1.7       paf       119:                PTHROW(0, 0,
                    120:                        &origin_string,
                    121:                        "has no $value");
                    122: 
                    123:        const String *file_name;
                    124:        if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8       paf       125:                *new(pool) String(pool, "file-name")))) // specified $file-name
1.7       paf       126:                file_name=&vfile_name->as_string();
1.8       paf       127:        else // no $file-name
                    128:                if(Value *vfile_name=static_cast<Value *>(vfile->fields().get(*name_name)))
                    129:                        file_name=&vfile_name->as_string(); // VFile knows name
                    130:                else // vfile doesn't know name
                    131:                        file_name=new(pool) String(pool, "noname.dat");
                    132:        const char *file_name_cstr=file_name->cstr(String::UL_FILE_NAME);
1.7       paf       133: 
                    134:        String& result=*new(pool) String(pool);
                    135: 
1.10    ! paf       136:        // content-type: application/octet-stream
        !           137:        result << "content-type: " << r.mime_type_of(file_name_cstr) << "\n";
1.7       paf       138:        // content-disposition: attachment; filename="user_file_name"
1.10    ! paf       139:        result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7       paf       140: 
                    141:        const String& type=vtype->as_string();
                    142:        if(type=="uue") {
1.8       paf       143:                uuencode(result, file_name_cstr, *vfile);
1.7       paf       144:        } else 
                    145:                PTHROW(0, 0,
                    146:                        &type,
                    147:                        "unknown encode type");
                    148:        
                    149:        return result;
                    150: }
                    151: 
1.1       paf       152: 
                    153: struct Mail_info {
                    154:        String *attribute_to_exclude;
                    155:        String *header;
1.2       paf       156:        const String **from, **to;
1.1       paf       157: };
                    158: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    159:                                                                 void *info) {
                    160: 
                    161:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    162:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf       163: 
                    164:        // exclude one attribute [body]
1.1       paf       165:        if(aattribute==*mi.attribute_to_exclude)
                    166:                return;
                    167: 
1.2       paf       168:        // fetch from & to from header for SMTP
                    169:        if(mi.from && aattribute=="from")
                    170:                *mi.from=&lmeaning.as_string();
                    171:        if(mi.to && aattribute=="to")
                    172:                *mi.to=&lmeaning.as_string();
                    173: 
                    174:        // append header line
1.10    ! paf       175:        *mi.header << 
        !           176:                aattribute << ":" << 
        !           177:                attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER) << 
        !           178:                "\n";
1.2       paf       179: }
                    180: struct Seq_item {
1.7       paf       181:        const String *part_name;
1.2       paf       182:        Value *part_value;
                    183: };
1.7       paf       184: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       185:                                         void *info) {
                    186:        Seq_item **seq_ref=static_cast<Seq_item **>(info);
1.7       paf       187:        (**seq_ref).part_name=&part_name;
1.2       paf       188:        (**seq_ref).part_value=static_cast<Value *>(part_value);
                    189:        (*seq_ref)++;
                    190: }
                    191: static double key_of_part(const void *item) {
1.7       paf       192:        const char *cstr=static_cast<const Seq_item *>(item)->part_name->cstr();
1.2       paf       193:        char *error_pos;
                    194:        return strtod(cstr, &error_pos);
                    195: }
                    196: static int sort_cmp_string_double_value(const void *a, const void *b) {
                    197:        double va=key_of_part(a);
                    198:        double vb=key_of_part(b);
1.7       paf       199: 
                    200:        // 0 logically equals infinity. so that attachments would go last
                    201:        if(va==0)
                    202:                return +1;
                    203:        if(vb==0)
                    204:                return -1;
                    205: 
1.2       paf       206:        if(va<vb)
                    207:                return -1;
                    208:        else if(va>vb)
                    209:                return +1;
                    210:        else 
                    211:                return 0;
1.1       paf       212: }
1.2       paf       213: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                    214:                                                                                   Hash& letter_hash, int level,
                    215:                                                                                   const String **from, const String **to) {
                    216:        Pool& pool=r.pool();
                    217: 
                    218:        // prepare header: 'hash' without "body"
                    219:        String& result=*new(pool) String(pool);
                    220:        Mail_info mail_info={
                    221:                /*excluding*/ body_name,
                    222:                &result,
                    223:                from, to
                    224:        };
                    225:        letter_hash.for_each(add_header_attribute, &mail_info);
                    226: 
                    227:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                    228:                if(Hash *body_hash=body_element->get_hash()) {
                    229:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.9       paf       230:                        snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.2       paf       231:                        // multi-part
1.10    ! paf       232:                        result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
        !           233:                        result << "\n" 
1.2       paf       234:                                "This is a multi-part message in MIME format.";
                    235: 
1.3       paf       236:                        // body parts..
                    237:                        // ..collect
1.2       paf       238:                        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
                    239:                        Seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       240:                        // ..sort
1.2       paf       241:                        _qsort(seq, body_hash->size(), sizeof(Seq_item), 
                    242:                                sort_cmp_string_double_value);
1.3       paf       243:                        // ..insert in 'seq' order
1.2       paf       244:                        for(int i=0; i<body_hash->size(); i++) {
                    245:                                // intermediate boundary
1.10    ! paf       246:                                result << "\n--" << boundary << "\n";
1.2       paf       247: 
                    248:                                if(Hash *part_hash=seq[i].part_value->get_hash())
1.7       paf       249:                                        if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")
1.10    ! paf       250:                                                result << attach_hash_to_string(r, *seq[i].part_name, *part_hash);
1.7       paf       251:                                        else 
1.10    ! paf       252:                                                result << letter_hash_to_string(r, method_name, *part_hash, 
1.7       paf       253:                                                        level+1, 0, 0);
1.2       paf       254:                                else
                    255:                                        PTHROW(0, 0,
1.7       paf       256:                                                seq[i].part_name,
1.2       paf       257:                                                "part is not hash");
                    258:                        }
                    259: 
                    260:                        // finish boundary
1.10    ! paf       261:                        result << "\n--" << boundary << "--\n";
1.2       paf       262:                } else {
1.10    ! paf       263:                        result << 
        !           264:                                "\n" << // header|body separator
        !           265:                                body_element->as_string();  // body
1.2       paf       266:                }
                    267:        } else 
                    268:                PTHROW(0, 0,
                    269:                        &method_name,
                    270:                        "has no $body");
                    271: 
                    272:        return result;
                    273: }
                    274: 
1.4       paf       275: /// @test unix ver
                    276: static void sendmail(Request& r, const String& method_name, 
                    277:                                         const String& letter, 
                    278:                                         const String *from, const String *to) {
                    279:        Pool& pool=r.pool();
                    280: 
                    281: #ifdef WIN32
                    282:        if(!from)
                    283:                PTHROW(0, 0,
                    284:                        &method_name,
                    285:                        "not specified 'from'");
                    286:        if(!to)
                    287:                PTHROW(0, 0,
                    288:                        &method_name,
                    289:                        "not specified 'to'");
                    290: 
                    291:        char *letter_cstr=letter.cstr();
                    292:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       293:        Value *server_port;
1.4       paf       294:        // $MAIN:MAIL.SMTP[mail.design.ru]
1.5       paf       295:        if(r.mail && 
                    296:                (server_port=static_cast<Value *>(r.mail->get(
                    297:                        *new(pool) String(pool, "SMTP"))))) {
                    298:                char *server=server_port->as_string().cstr();
1.4       paf       299:                char *port=rsplit(server, ':');
                    300:                if(!port)
1.5       paf       301:                        port=const_cast<char *>("25");
1.4       paf       302: 
                    303:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    304:        } else
                    305:                PTHROW(0, 0,
                    306:                        &method_name,
                    307:                        "$MAIN:MAIL.SMTP not defined");
                    308: #else
                    309:        PTHROW(0, 0,
                    310:                &method_name,
                    311:                "todo");
                    312: #endif
                    313: }
                    314: 
1.7       paf       315: 
                    316: // methods
                    317: 
1.1       paf       318: static void _send(Request& r, const String& method_name, Array *params) {
                    319:        Pool& pool=r.pool();
                    320: 
                    321:        Value& vhash=*static_cast<Value *>(params->get(0));
                    322:        // forcing [this body type]
                    323:        r.fail_if_junction_(true, vhash, method_name, "message must not be code");
                    324: 
                    325:        Hash *hash=vhash.get_hash();
                    326:        if(!hash)
                    327:                PTHROW(0, 0,
                    328:                        &method_name,
                    329:                        "message must be hash");
                    330: 
1.2       paf       331:        const String *from, *to;
1.4       paf       332:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       333: 
1.10    ! paf       334:        //r.write_assign_lang(*new(pool) VString(letter));
        !           335:        sendmail(r, method_name, letter, from, to);
1.6       paf       336: }
                    337: 
1.1       paf       338: // initialize
                    339: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
                    340:        // ^mail:send{hash}
                    341:        vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
                    342: }

E-mail: