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

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.22    ! paf         8:        $Id: mail.C,v 1.21 2001/04/23 09:38:40 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_config_includes.h"
                     12: 
1.19      paf        13: #ifdef _MSC_VER
1.6       paf        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.12      paf        21: #include "pa_exec.h"
1.4       paf        22: 
1.1       paf        23: // global var
                     24: 
                     25: VStateless_class *mail_class;
                     26: 
1.7       paf        27: // helpers
                     28: 
1.10      paf        29: // uuencode
                     30: 
                     31: static unsigned char uue_table[64] = {
                     32:   '`', '!', '"', '#', '$', '%', '&', '\'',
                     33:   '(', ')', '*', '+', ',', '-', '.', '/',
                     34:   '0', '1', '2', '3', '4', '5', '6', '7',
                     35:   '8', '9', ':', ';', '<', '=', '>', '?',
                     36:   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
                     37:   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
                     38:   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                     39:   'X', 'Y', 'Z', '[', '\\',']', '^', '_'
                     40: };
1.8       paf        41: static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
1.10      paf        42:        //header
                     43:        result << "content-transfer-encoding: x-uuencode\n" << "\n";
                     44:        result << "begin 644 " << file_name_cstr << "\n";
                     45: 
                     46:        //body
1.16      paf        47:        const unsigned char *in=(const unsigned char *)vfile.value_ptr();
                     48:        size_t in_length=vfile.value_size();
1.10      paf        49: 
                     50:        int count=45;
1.16      paf        51:        for(const unsigned char *itemp=in; itemp<(in+in_length); itemp+=count) {
                     52:                int index;      
1.10      paf        53: 
                     54:                if((itemp+count)>(in+in_length)) 
                     55:                        count=in_length-(itemp-in);
                     56: 
                     57:                char *buf=(char *)result.pool().malloc(MAX_STRING);
                     58:                char *optr=buf;
                     59:                
                     60:                /*
                     61:                * for UU and XX, encode the number of bytes as first character
                     62:                */
                     63:                *optr++ = uue_table[count];
                     64:                
                     65:                for (index=0; index<=count-3; index+=3) {
                     66:                        *optr++ = uue_table[itemp[index] >> 2];
                     67:                        *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | (itemp[index+1] >> 4)];
                     68:                        *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
                     69:                        *optr++ = uue_table[  itemp[index+2] & 0x3f];
                     70:                }
                     71:                
                     72:                /*
                     73:                * Special handlitempg for itempcomplete litempes
                     74:                */
                     75:                if (index != count) {
                     76:                        if (count - index == 2) {
                     77:                                *optr++ = uue_table[itemp[index] >> 2];
                     78:                                *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | 
                     79:                                        ( itemp[index+1] >> 4)];
                     80:                                *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
                     81:                                *optr++ = uue_table[0];
                     82:                        }
                     83:                        else if (count - index == 1) {
                     84:                                *optr++ = uue_table[ itemp[index] >> 2];
                     85:                                *optr++ = uue_table[(itemp[index] & 0x03) << 4];
                     86:                                *optr++ = uue_table[0];
                     87:                                *optr++ = uue_table[0];
                     88:                        }
                     89:                }
                     90:                /*
                     91:                * end of line
                     92:                */
                     93:                *optr++ = '\n'; 
                     94:                *optr = 0;
                     95:                result << buf;
                     96:        }
                     97:        
                     98:        //footer
1.21      paf        99:        result.APPEND_AS_IS((const char *)uue_table, 1/* one char */, 0, 0) << "\n"
1.10      paf       100:                "end\n";
1.8       paf       101: }
                    102: 
1.7       paf       103: /// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
                    104: static const String& attach_hash_to_string(Request& r, const String& origin_string, 
                    105:                                                                                   Hash& attach_hash) {
                    106:        Pool& pool=r.pool();
                    107: 
                    108:        Value *vtype=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "type")));
                    109:        if(!vtype)
                    110:                PTHROW(0, 0,
                    111:                        &origin_string,
                    112:                        "has no $type");
                    113: 
1.8       paf       114:        const VFile *vfile;
                    115:        if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
1.17      paf       116:                vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve
1.8       paf       117:        else
1.7       paf       118:                PTHROW(0, 0,
                    119:                        &origin_string,
                    120:                        "has no $value");
                    121: 
                    122:        const String *file_name;
                    123:        if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8       paf       124:                *new(pool) String(pool, "file-name")))) // specified $file-name
1.7       paf       125:                file_name=&vfile_name->as_string();
1.16      paf       126:        else // no $file-name, VFile surely knows name
                    127:                file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.8       paf       128:        const char *file_name_cstr=file_name->cstr(String::UL_FILE_NAME);
1.7       paf       129: 
                    130:        String& result=*new(pool) String(pool);
                    131: 
1.10      paf       132:        // content-type: application/octet-stream
                    133:        result << "content-type: " << r.mime_type_of(file_name_cstr) << "\n";
1.7       paf       134:        // content-disposition: attachment; filename="user_file_name"
1.10      paf       135:        result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7       paf       136: 
                    137:        const String& type=vtype->as_string();
                    138:        if(type=="uue") {
1.8       paf       139:                uuencode(result, file_name_cstr, *vfile);
1.7       paf       140:        } else 
                    141:                PTHROW(0, 0,
                    142:                        &type,
                    143:                        "unknown encode type");
                    144:        
                    145:        return result;
                    146: }
                    147: 
1.1       paf       148: 
1.20      paf       149: static bool find_content_type(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    150:                                                          void *) {
                    151:        return StrEqNc(aattribute.cstr(), CONTENT_TYPE_NAME);
                    152: }
                    153: static bool find_content_type_charset(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    154:                                                                          void *) {
                    155:        return StrEqNc(aattribute.cstr(), "charset");
                    156: }
                    157: 
1.22    ! paf       158: /// used by mail: _send / letter_hash_to_string / add_header_attribute
1.1       paf       159: struct Mail_info {
                    160:        String *attribute_to_exclude;
1.20      paf       161:        const char *charset;
1.1       paf       162:        String *header;
1.2       paf       163:        const String **from, **to;
1.1       paf       164: };
                    165: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    166:                                                                 void *info) {
                    167: 
                    168:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    169:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf       170: 
                    171:        // exclude one attribute [body]
1.1       paf       172:        if(aattribute==*mi.attribute_to_exclude)
                    173:                return;
                    174: 
1.2       paf       175:        // fetch from & to from header for SMTP
                    176:        if(mi.from && aattribute=="from")
                    177:                *mi.from=&lmeaning.as_string();
                    178:        if(mi.to && aattribute=="to")
                    179:                *mi.to=&lmeaning.as_string();
                    180: 
                    181:        // append header line
1.10      paf       182:        *mi.header << 
                    183:                aattribute << ":" << 
1.20      paf       184:                attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER).
                    185:                        cstr(String::UL_UNSPECIFIED, 0, mi.charset) << 
1.10      paf       186:                "\n";
1.2       paf       187: }
1.22    ! paf       188: 
        !           189: /// used in mail: _send / letter_hash_to_string / add_part
1.2       paf       190: struct Seq_item {
1.7       paf       191:        const String *part_name;
1.2       paf       192:        Value *part_value;
                    193: };
1.7       paf       194: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       195:                                         void *info) {
                    196:        Seq_item **seq_ref=static_cast<Seq_item **>(info);
1.7       paf       197:        (**seq_ref).part_name=&part_name;
1.2       paf       198:        (**seq_ref).part_value=static_cast<Value *>(part_value);
                    199:        (*seq_ref)++;
                    200: }
                    201: static double key_of_part(const void *item) {
1.7       paf       202:        const char *cstr=static_cast<const Seq_item *>(item)->part_name->cstr();
1.2       paf       203:        char *error_pos;
                    204:        return strtod(cstr, &error_pos);
                    205: }
                    206: static int sort_cmp_string_double_value(const void *a, const void *b) {
                    207:        double va=key_of_part(a);
                    208:        double vb=key_of_part(b);
1.7       paf       209: 
                    210:        // 0 logically equals infinity. so that attachments would go last
                    211:        if(va==0)
                    212:                return +1;
                    213:        if(vb==0)
                    214:                return -1;
                    215: 
1.2       paf       216:        if(va<vb)
                    217:                return -1;
                    218:        else if(va>vb)
                    219:                return +1;
                    220:        else 
                    221:                return 0;
1.1       paf       222: }
1.2       paf       223: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                    224:                                                                                   Hash& letter_hash, int level,
                    225:                                                                                   const String **from, const String **to) {
                    226:        Pool& pool=r.pool();
                    227: 
                    228:        // prepare header: 'hash' without "body"
                    229:        String& result=*new(pool) String(pool);
1.20      paf       230: 
                    231:        const char *charset=0;
                    232:        if(Value *content_type=
                    233:                static_cast<Value *>(letter_hash.first_that(find_content_type)))
                    234:                if(Hash *hash=content_type->get_hash())
                    235:                        if(Value *content_type_charset=
                    236:                                static_cast<Value *>(hash->first_that(find_content_type_charset)))
                    237:                                charset=content_type_charset->as_string().cstr();
                    238: 
1.15      paf       239:        *from=*to=0;
1.2       paf       240:        Mail_info mail_info={
                    241:                /*excluding*/ body_name,
1.20      paf       242:                charset,
1.2       paf       243:                &result,
                    244:                from, to
                    245:        };
                    246:        letter_hash.for_each(add_header_attribute, &mail_info);
                    247: 
                    248:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                    249:                if(Hash *body_hash=body_element->get_hash()) {
                    250:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.9       paf       251:                        snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.2       paf       252:                        // multi-part
1.10      paf       253:                        result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    254:                        result << "\n" 
1.2       paf       255:                                "This is a multi-part message in MIME format.";
                    256: 
1.3       paf       257:                        // body parts..
                    258:                        // ..collect
1.2       paf       259:                        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
                    260:                        Seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       261:                        // ..sort
1.2       paf       262:                        _qsort(seq, body_hash->size(), sizeof(Seq_item), 
                    263:                                sort_cmp_string_double_value);
1.3       paf       264:                        // ..insert in 'seq' order
1.2       paf       265:                        for(int i=0; i<body_hash->size(); i++) {
                    266:                                // intermediate boundary
1.10      paf       267:                                result << "\n--" << boundary << "\n";
1.2       paf       268: 
                    269:                                if(Hash *part_hash=seq[i].part_value->get_hash())
1.7       paf       270:                                        if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")
1.10      paf       271:                                                result << attach_hash_to_string(r, *seq[i].part_name, *part_hash);
1.7       paf       272:                                        else 
1.10      paf       273:                                                result << letter_hash_to_string(r, method_name, *part_hash, 
1.7       paf       274:                                                        level+1, 0, 0);
1.2       paf       275:                                else
                    276:                                        PTHROW(0, 0,
1.7       paf       277:                                                seq[i].part_name,
1.2       paf       278:                                                "part is not hash");
                    279:                        }
                    280: 
                    281:                        // finish boundary
1.10      paf       282:                        result << "\n--" << boundary << "--\n";
1.2       paf       283:                } else {
1.10      paf       284:                        result << 
                    285:                                "\n" << // header|body separator
                    286:                                body_element->as_string();  // body
1.2       paf       287:                }
                    288:        } else 
                    289:                PTHROW(0, 0,
                    290:                        &method_name,
                    291:                        "has no $body");
                    292: 
                    293:        return result;
                    294: }
                    295: 
1.4       paf       296: static void sendmail(Request& r, const String& method_name, 
                    297:                                         const String& letter, 
                    298:                                         const String *from, const String *to) {
                    299:        Pool& pool=r.pool();
                    300: 
1.12      paf       301:        char *letter_cstr=letter.cstr();
                    302: 
1.19      paf       303: #ifdef _MSC_VER
1.4       paf       304:        if(!from)
                    305:                PTHROW(0, 0,
                    306:                        &method_name,
1.15      paf       307:                        "has no 'from' header specified");
1.4       paf       308:        if(!to)
                    309:                PTHROW(0, 0,
                    310:                        &method_name,
1.15      paf       311:                        "has no 'to' header specified");
1.4       paf       312: 
                    313:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       314:        Value *server_port;
1.4       paf       315:        // $MAIN:MAIL.SMTP[mail.design.ru]
1.5       paf       316:        if(r.mail && 
                    317:                (server_port=static_cast<Value *>(r.mail->get(
                    318:                        *new(pool) String(pool, "SMTP"))))) {
                    319:                char *server=server_port->as_string().cstr();
1.11      paf       320:                const char *port=rsplit(server, ':');
1.4       paf       321:                if(!port)
1.11      paf       322:                        port="25";
1.4       paf       323: 
                    324:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    325:        } else
                    326:                PTHROW(0, 0,
                    327:                        &method_name,
1.13      paf       328:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       329: #else
1.12      paf       330:        // unix
1.13      paf       331:        // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default
                    332:        // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default
1.12      paf       333:        if(r.mail) {
                    334:                char no_cstr[MAX_NUMBER];
1.13      paf       335:                for(int no=-2; ; no++) {
                    336:                        const String *prog_string;
                    337:                        switch(no) {
                    338:                        case -2: prog_string=new(pool) String(pool, "/usr/sbin/sendmail -t"); break;
                    339:                        case -1: prog_string=new(pool) String(pool, "/usr/lib/sendmail -t"); break;
                    340:                        default: 
                    341:                                {
                    342:                                        String prog_key(pool, "prog");
                    343:                                        snprintf(no_cstr, MAX_NUMBER, "%d", no);
                    344:                                        prog_key << no_cstr;
                    345:                                        if(Value *prog_value=static_cast<Value *>(r.mail->get(prog_key)))
                    346:                                                prog_string=&prog_value->as_string();
                    347:                                        else
                    348:                                                if(no==0)
                    349:                                                        continue;
                    350:                                                else
                    351:                                                        PTHROW(0, 0,
                    352:                                                                &method_name,
                    353:                                                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    354:                                                                prog_key.cstr());
1.12      paf       355:                                }
1.13      paf       356:                        }
                    357:                        // we know prog_string here
                    358:                        Array argv(pool);
                    359:                        const String *file_spec;
                    360:                        int after_file_spec=prog_string->pos(" ", 1);
                    361:                        if(after_file_spec<=0)
                    362:                                file_spec=prog_string;
                    363:                        else {
                    364:                                size_t pos_after=after_file_spec;
                    365:                                file_spec=&prog_string->mid(0, pos_after);
                    366:                                prog_string->split(argv, &pos_after, " ", 1, String::UL_CLEAN);
                    367:                        }
1.12      paf       368: 
1.14      paf       369:                        // skip unavailable default programs
                    370:                        if(no<0 && !file_executable(*file_spec))
1.13      paf       371:                                continue;
                    372: 
                    373:                        String in(pool, letter_cstr); String out(pool); String err(pool);
                    374:                        int exit_status=pa_exec(*file_spec,
                    375:                                0/*default env*/,
                    376:                                &argv,
                    377:                                in, out, err);
                    378:                        if(exit_status || err.size())
1.12      paf       379:                                PTHROW(0, 0,
                    380:                                        &method_name,
1.13      paf       381:                                        "'%s' reported problem: %s (%d)",
                    382:                                                file_spec->cstr(),
                    383:                                                err.size()?err.cstr():"UNKNOWN", 
                    384:                                                exit_status);
                    385:                        break;
1.12      paf       386:                }
                    387:        } else
                    388:                PTHROW(0, 0,
                    389:                        &method_name,
1.13      paf       390:                        "$"MAIN_CLASS_NAME":"MAIL_NAME" not defined");
1.4       paf       391: #endif
                    392: }
                    393: 
1.7       paf       394: 
                    395: // methods
                    396: 
1.22    ! paf       397: /// ^mail:send{hash}
1.18      paf       398: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       399:        Pool& pool=r.pool();
                    400: 
1.18      paf       401:        Value& vhash=params->get_no_junction(0, "message must not be code");
1.1       paf       402:        Hash *hash=vhash.get_hash();
                    403:        if(!hash)
                    404:                PTHROW(0, 0,
                    405:                        &method_name,
                    406:                        "message must be hash");
                    407: 
1.2       paf       408:        const String *from, *to;
1.4       paf       409:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       410: 
1.21      paf       411: //     r.write_assign_lang(*new(pool) VString(letter));
                    412:        sendmail(r, method_name, letter, from, to);
1.6       paf       413: }
                    414: 
1.1       paf       415: // initialize
                    416: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
1.22    ! paf       417:        /// ^mail:send{hash}
1.1       paf       418:        vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
                    419: }

E-mail: