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

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.21    ! paf         8:        $Id: mail.C,v 1.20 2001/04/23 08:52:15 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.1       paf       158: struct Mail_info {
                    159:        String *attribute_to_exclude;
1.20      paf       160:        const char *charset;
1.1       paf       161:        String *header;
1.2       paf       162:        const String **from, **to;
1.1       paf       163: };
                    164: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    165:                                                                 void *info) {
                    166: 
                    167:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    168:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf       169: 
                    170:        // exclude one attribute [body]
1.1       paf       171:        if(aattribute==*mi.attribute_to_exclude)
                    172:                return;
                    173: 
1.2       paf       174:        // fetch from & to from header for SMTP
                    175:        if(mi.from && aattribute=="from")
                    176:                *mi.from=&lmeaning.as_string();
                    177:        if(mi.to && aattribute=="to")
                    178:                *mi.to=&lmeaning.as_string();
                    179: 
                    180:        // append header line
1.10      paf       181:        *mi.header << 
                    182:                aattribute << ":" << 
1.20      paf       183:                attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER).
                    184:                        cstr(String::UL_UNSPECIFIED, 0, mi.charset) << 
1.10      paf       185:                "\n";
1.2       paf       186: }
                    187: struct Seq_item {
1.7       paf       188:        const String *part_name;
1.2       paf       189:        Value *part_value;
                    190: };
1.7       paf       191: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       192:                                         void *info) {
                    193:        Seq_item **seq_ref=static_cast<Seq_item **>(info);
1.7       paf       194:        (**seq_ref).part_name=&part_name;
1.2       paf       195:        (**seq_ref).part_value=static_cast<Value *>(part_value);
                    196:        (*seq_ref)++;
                    197: }
                    198: static double key_of_part(const void *item) {
1.7       paf       199:        const char *cstr=static_cast<const Seq_item *>(item)->part_name->cstr();
1.2       paf       200:        char *error_pos;
                    201:        return strtod(cstr, &error_pos);
                    202: }
                    203: static int sort_cmp_string_double_value(const void *a, const void *b) {
                    204:        double va=key_of_part(a);
                    205:        double vb=key_of_part(b);
1.7       paf       206: 
                    207:        // 0 logically equals infinity. so that attachments would go last
                    208:        if(va==0)
                    209:                return +1;
                    210:        if(vb==0)
                    211:                return -1;
                    212: 
1.2       paf       213:        if(va<vb)
                    214:                return -1;
                    215:        else if(va>vb)
                    216:                return +1;
                    217:        else 
                    218:                return 0;
1.1       paf       219: }
1.2       paf       220: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                    221:                                                                                   Hash& letter_hash, int level,
                    222:                                                                                   const String **from, const String **to) {
                    223:        Pool& pool=r.pool();
                    224: 
                    225:        // prepare header: 'hash' without "body"
                    226:        String& result=*new(pool) String(pool);
1.20      paf       227: 
                    228:        const char *charset=0;
                    229:        if(Value *content_type=
                    230:                static_cast<Value *>(letter_hash.first_that(find_content_type)))
                    231:                if(Hash *hash=content_type->get_hash())
                    232:                        if(Value *content_type_charset=
                    233:                                static_cast<Value *>(hash->first_that(find_content_type_charset)))
                    234:                                charset=content_type_charset->as_string().cstr();
                    235: 
1.15      paf       236:        *from=*to=0;
1.2       paf       237:        Mail_info mail_info={
                    238:                /*excluding*/ body_name,
1.20      paf       239:                charset,
1.2       paf       240:                &result,
                    241:                from, to
                    242:        };
                    243:        letter_hash.for_each(add_header_attribute, &mail_info);
                    244: 
                    245:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                    246:                if(Hash *body_hash=body_element->get_hash()) {
                    247:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.9       paf       248:                        snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.2       paf       249:                        // multi-part
1.10      paf       250:                        result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    251:                        result << "\n" 
1.2       paf       252:                                "This is a multi-part message in MIME format.";
                    253: 
1.3       paf       254:                        // body parts..
                    255:                        // ..collect
1.2       paf       256:                        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
                    257:                        Seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       258:                        // ..sort
1.2       paf       259:                        _qsort(seq, body_hash->size(), sizeof(Seq_item), 
                    260:                                sort_cmp_string_double_value);
1.3       paf       261:                        // ..insert in 'seq' order
1.2       paf       262:                        for(int i=0; i<body_hash->size(); i++) {
                    263:                                // intermediate boundary
1.10      paf       264:                                result << "\n--" << boundary << "\n";
1.2       paf       265: 
                    266:                                if(Hash *part_hash=seq[i].part_value->get_hash())
1.7       paf       267:                                        if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")
1.10      paf       268:                                                result << attach_hash_to_string(r, *seq[i].part_name, *part_hash);
1.7       paf       269:                                        else 
1.10      paf       270:                                                result << letter_hash_to_string(r, method_name, *part_hash, 
1.7       paf       271:                                                        level+1, 0, 0);
1.2       paf       272:                                else
                    273:                                        PTHROW(0, 0,
1.7       paf       274:                                                seq[i].part_name,
1.2       paf       275:                                                "part is not hash");
                    276:                        }
                    277: 
                    278:                        // finish boundary
1.10      paf       279:                        result << "\n--" << boundary << "--\n";
1.2       paf       280:                } else {
1.10      paf       281:                        result << 
                    282:                                "\n" << // header|body separator
                    283:                                body_element->as_string();  // body
1.2       paf       284:                }
                    285:        } else 
                    286:                PTHROW(0, 0,
                    287:                        &method_name,
                    288:                        "has no $body");
                    289: 
                    290:        return result;
                    291: }
                    292: 
1.4       paf       293: static void sendmail(Request& r, const String& method_name, 
                    294:                                         const String& letter, 
                    295:                                         const String *from, const String *to) {
                    296:        Pool& pool=r.pool();
                    297: 
1.12      paf       298:        char *letter_cstr=letter.cstr();
                    299: 
1.19      paf       300: #ifdef _MSC_VER
1.4       paf       301:        if(!from)
                    302:                PTHROW(0, 0,
                    303:                        &method_name,
1.15      paf       304:                        "has no 'from' header specified");
1.4       paf       305:        if(!to)
                    306:                PTHROW(0, 0,
                    307:                        &method_name,
1.15      paf       308:                        "has no 'to' header specified");
1.4       paf       309: 
                    310:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       311:        Value *server_port;
1.4       paf       312:        // $MAIN:MAIL.SMTP[mail.design.ru]
1.5       paf       313:        if(r.mail && 
                    314:                (server_port=static_cast<Value *>(r.mail->get(
                    315:                        *new(pool) String(pool, "SMTP"))))) {
                    316:                char *server=server_port->as_string().cstr();
1.11      paf       317:                const char *port=rsplit(server, ':');
1.4       paf       318:                if(!port)
1.11      paf       319:                        port="25";
1.4       paf       320: 
                    321:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    322:        } else
                    323:                PTHROW(0, 0,
                    324:                        &method_name,
1.13      paf       325:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       326: #else
1.12      paf       327:        // unix
1.13      paf       328:        // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default
                    329:        // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default
1.12      paf       330:        if(r.mail) {
                    331:                char no_cstr[MAX_NUMBER];
1.13      paf       332:                for(int no=-2; ; no++) {
                    333:                        const String *prog_string;
                    334:                        switch(no) {
                    335:                        case -2: prog_string=new(pool) String(pool, "/usr/sbin/sendmail -t"); break;
                    336:                        case -1: prog_string=new(pool) String(pool, "/usr/lib/sendmail -t"); break;
                    337:                        default: 
                    338:                                {
                    339:                                        String prog_key(pool, "prog");
                    340:                                        snprintf(no_cstr, MAX_NUMBER, "%d", no);
                    341:                                        prog_key << no_cstr;
                    342:                                        if(Value *prog_value=static_cast<Value *>(r.mail->get(prog_key)))
                    343:                                                prog_string=&prog_value->as_string();
                    344:                                        else
                    345:                                                if(no==0)
                    346:                                                        continue;
                    347:                                                else
                    348:                                                        PTHROW(0, 0,
                    349:                                                                &method_name,
                    350:                                                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    351:                                                                prog_key.cstr());
1.12      paf       352:                                }
1.13      paf       353:                        }
                    354:                        // we know prog_string here
                    355:                        Array argv(pool);
                    356:                        const String *file_spec;
                    357:                        int after_file_spec=prog_string->pos(" ", 1);
                    358:                        if(after_file_spec<=0)
                    359:                                file_spec=prog_string;
                    360:                        else {
                    361:                                size_t pos_after=after_file_spec;
                    362:                                file_spec=&prog_string->mid(0, pos_after);
                    363:                                prog_string->split(argv, &pos_after, " ", 1, String::UL_CLEAN);
                    364:                        }
1.12      paf       365: 
1.14      paf       366:                        // skip unavailable default programs
                    367:                        if(no<0 && !file_executable(*file_spec))
1.13      paf       368:                                continue;
                    369: 
                    370:                        String in(pool, letter_cstr); String out(pool); String err(pool);
                    371:                        int exit_status=pa_exec(*file_spec,
                    372:                                0/*default env*/,
                    373:                                &argv,
                    374:                                in, out, err);
                    375:                        if(exit_status || err.size())
1.12      paf       376:                                PTHROW(0, 0,
                    377:                                        &method_name,
1.13      paf       378:                                        "'%s' reported problem: %s (%d)",
                    379:                                                file_spec->cstr(),
                    380:                                                err.size()?err.cstr():"UNKNOWN", 
                    381:                                                exit_status);
                    382:                        break;
1.12      paf       383:                }
                    384:        } else
                    385:                PTHROW(0, 0,
                    386:                        &method_name,
1.13      paf       387:                        "$"MAIN_CLASS_NAME":"MAIL_NAME" not defined");
1.4       paf       388: #endif
                    389: }
                    390: 
1.7       paf       391: 
                    392: // methods
                    393: 
1.18      paf       394: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       395:        Pool& pool=r.pool();
                    396: 
1.18      paf       397:        Value& vhash=params->get_no_junction(0, "message must not be code");
1.1       paf       398:        Hash *hash=vhash.get_hash();
                    399:        if(!hash)
                    400:                PTHROW(0, 0,
                    401:                        &method_name,
                    402:                        "message must be hash");
                    403: 
1.2       paf       404:        const String *from, *to;
1.4       paf       405:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       406: 
1.21    ! paf       407: //     r.write_assign_lang(*new(pool) VString(letter));
        !           408:        sendmail(r, method_name, letter, from, to);
1.6       paf       409: }
                    410: 
1.1       paf       411: // initialize
                    412: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
                    413:        // ^mail:send{hash}
                    414:        vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
                    415: }

E-mail: