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

1.1       paf         1: /** @file
                      2:        Parser: @b mail parser class.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.44      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: 
1.47    ! paf         7:        $Id: mail.C,v 1.46 2001/12/19 09:53:28 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #include "pa_config_includes.h"
                     11: 
1.19      paf        12: #ifdef _MSC_VER
1.6       paf        13: #      include "smtp/smtp.h"
                     14: #endif
                     15: 
1.1       paf        16: #include "pa_common.h"
                     17: #include "pa_request.h"
1.6       paf        18: #include "pa_vfile.h"
1.12      paf        19: #include "pa_exec.h"
1.45      paf        20: #include "pa_charsets.h"
                     21: #include "pa_charset.h"
1.4       paf        22: 
1.23      paf        23: // defines
1.1       paf        24: 
1.23      paf        25: #define MAIL_CLASS_NAME "mail"
                     26: 
1.25      paf        27: #define MAIL_NAME "MAIL"
                     28: 
                     29: // global variable
                     30: 
                     31: Methoded *mail_class;
                     32: 
1.47    ! paf        33: // consts
        !            34: 
        !            35: const int ATTACHMENT_WEIGHT=100;
        !            36: 
1.23      paf        37: // class
                     38: 
                     39: class MMail : public Methoded {
                     40: public:
                     41:        MMail(Pool& pool);
1.24      paf        42: public: // Methoded
1.23      paf        43:        bool used_directly() { return true; }
1.24      paf        44:        void configure_user(Request& r);
1.25      paf        45: private:
                     46:        String mail_name;
                     47:        String content_disposition_name;
                     48:        String content_disposition_filename_name;
1.23      paf        49: };
1.1       paf        50: 
1.7       paf        51: // helpers
                     52: 
1.10      paf        53: // uuencode
                     54: 
                     55: static unsigned char uue_table[64] = {
                     56:   '`', '!', '"', '#', '$', '%', '&', '\'',
                     57:   '(', ')', '*', '+', ',', '-', '.', '/',
                     58:   '0', '1', '2', '3', '4', '5', '6', '7',
                     59:   '8', '9', ':', ';', '<', '=', '>', '?',
                     60:   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
                     61:   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
                     62:   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                     63:   'X', 'Y', 'Z', '[', '\\',']', '^', '_'
                     64: };
1.8       paf        65: static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
1.10      paf        66:        //header
                     67:        result << "content-transfer-encoding: x-uuencode\n" << "\n";
                     68:        result << "begin 644 " << file_name_cstr << "\n";
                     69: 
                     70:        //body
1.16      paf        71:        const unsigned char *in=(const unsigned char *)vfile.value_ptr();
                     72:        size_t in_length=vfile.value_size();
1.10      paf        73: 
                     74:        int count=45;
1.16      paf        75:        for(const unsigned char *itemp=in; itemp<(in+in_length); itemp+=count) {
                     76:                int index;      
1.10      paf        77: 
                     78:                if((itemp+count)>(in+in_length)) 
                     79:                        count=in_length-(itemp-in);
                     80: 
                     81:                char *buf=(char *)result.pool().malloc(MAX_STRING);
                     82:                char *optr=buf;
                     83:                
                     84:                /*
                     85:                * for UU and XX, encode the number of bytes as first character
                     86:                */
                     87:                *optr++ = uue_table[count];
                     88:                
                     89:                for (index=0; index<=count-3; index+=3) {
                     90:                        *optr++ = uue_table[itemp[index] >> 2];
                     91:                        *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | (itemp[index+1] >> 4)];
                     92:                        *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
                     93:                        *optr++ = uue_table[  itemp[index+2] & 0x3f];
                     94:                }
                     95:                
                     96:                /*
                     97:                * Special handlitempg for itempcomplete litempes
                     98:                */
                     99:                if (index != count) {
                    100:                        if (count - index == 2) {
                    101:                                *optr++ = uue_table[itemp[index] >> 2];
                    102:                                *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | 
                    103:                                        ( itemp[index+1] >> 4)];
                    104:                                *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
                    105:                                *optr++ = uue_table[0];
                    106:                        }
                    107:                        else if (count - index == 1) {
                    108:                                *optr++ = uue_table[ itemp[index] >> 2];
                    109:                                *optr++ = uue_table[(itemp[index] & 0x03) << 4];
                    110:                                *optr++ = uue_table[0];
                    111:                                *optr++ = uue_table[0];
                    112:                        }
                    113:                }
                    114:                /*
                    115:                * end of line
                    116:                */
                    117:                *optr++ = '\n'; 
                    118:                *optr = 0;
                    119:                result << buf;
                    120:        }
                    121:        
                    122:        //footer
1.21      paf       123:        result.APPEND_AS_IS((const char *)uue_table, 1/* one char */, 0, 0) << "\n"
1.10      paf       124:                "end\n";
1.8       paf       125: }
                    126: 
1.33      parser    127: /** ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
                    128:        @todo solve - bad with html attaches
                    129: */
1.7       paf       130: static const String& attach_hash_to_string(Request& r, const String& origin_string, 
                    131:                                                                                   Hash& attach_hash) {
                    132:        Pool& pool=r.pool();
                    133: 
1.46      paf       134:        Value *vformat=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "format")));
1.7       paf       135: 
1.8       paf       136:        const VFile *vfile;
                    137:        if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
1.17      paf       138:                vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve
1.8       paf       139:        else
1.41      parser    140:                throw Exception(0, 0,
1.7       paf       141:                        &origin_string,
                    142:                        "has no $value");
                    143: 
                    144:        const String *file_name;
                    145:        if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8       paf       146:                *new(pool) String(pool, "file-name")))) // specified $file-name
1.7       paf       147:                file_name=&vfile_name->as_string();
1.16      paf       148:        else // no $file-name, VFile surely knows name
                    149:                file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.37      parser    150:        const char *file_name_cstr=file_name->cstr(String::UL_FILE_SPEC);
1.7       paf       151: 
                    152:        String& result=*new(pool) String(pool);
                    153: 
1.10      paf       154:        // content-type: application/octet-stream
                    155:        result << "content-type: " << r.mime_type_of(file_name_cstr) << "\n";
1.7       paf       156:        // content-disposition: attachment; filename="user_file_name"
1.10      paf       157:        result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7       paf       158: 
1.46      paf       159:        const String *type=vformat?&vformat->as_string():0;
                    160:        if(!type/*default = uue*/ || *type=="uue") {
1.8       paf       161:                uuencode(result, file_name_cstr, *vfile);
1.46      paf       162:        } else // for now
1.41      parser    163:                throw Exception(0, 0,
1.46      paf       164:                        type,
                    165:                        "unknown attachment encode format");
1.7       paf       166:        
                    167:        return result;
                    168: }
                    169: 
1.1       paf       170: 
1.36      parser    171: #ifndef DOXYGEN
1.1       paf       172: struct Mail_info {
1.45      paf       173:        Charset *charset;
1.1       paf       174:        String *header;
1.2       paf       175:        const String **from, **to;
1.1       paf       176: };
1.36      parser    177: #endif
1.1       paf       178: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    179:                                                                 void *info) {
                    180: 
                    181:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    182:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf       183: 
                    184:        // exclude one attribute [body]
1.45      paf       185:        if(aattribute==BODY_NAME
                    186:                || aattribute==CHARSET_NAME)
1.1       paf       187:                return;
                    188: 
1.2       paf       189:        // fetch from & to from header for SMTP
                    190:        if(mi.from && aattribute=="from")
                    191:                *mi.from=&lmeaning.as_string();
                    192:        if(mi.to && aattribute=="to")
                    193:                *mi.to=&lmeaning.as_string();
                    194: 
                    195:        // append header line
1.10      paf       196:        *mi.header << 
                    197:                aattribute << ":" << 
1.20      paf       198:                attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER).
                    199:                        cstr(String::UL_UNSPECIFIED, 0, mi.charset) << 
1.10      paf       200:                "\n";
1.2       paf       201: }
1.22      paf       202: 
1.36      parser    203: #ifndef DOXYGEN
1.28      paf       204: struct Mail_seq_item {
1.47    ! paf       205:        int weight;
        !           206:        const String *name;
        !           207:        Value *value;
1.2       paf       208: };
1.36      parser    209: #endif
1.47    ! paf       210: static int get_part_name_weight(const Hash::Key& part_name) {
        !           211:        const char *cstr=part_name.cstr();
        !           212:        int offset=0;
        !           213:        if(strncmp(cstr, "text", 4)==0) {
        !           214:                cstr+=4;
        !           215:        } else if(strncmp(cstr, "attach", 6)==0) {
        !           216:                cstr+=6;
        !           217:                offset=ATTACHMENT_WEIGHT;
        !           218:        } else
        !           219:                throw Exception(0, 0,
        !           220:                        &part_name,
        !           221:                        "is neither text# nor attach#");
        !           222: 
        !           223:        char *error_pos;
        !           224:        return strtol(cstr, &error_pos, 10)+offset;
        !           225: }
1.7       paf       226: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       227:                                         void *info) {
1.28      paf       228:        Mail_seq_item **seq_ref=static_cast<Mail_seq_item **>(info);
1.47    ! paf       229:        (**seq_ref).weight=get_part_name_weight(part_name);
        !           230:        (**seq_ref).name=&part_name;
        !           231:        (**seq_ref).value=static_cast<Value *>(part_value);
1.2       paf       232:        (*seq_ref)++;
                    233: }
1.47    ! paf       234: static int key_of_part(const void *item) {
        !           235:        return static_cast<const Mail_seq_item *>(item)->weight;
1.2       paf       236: }
                    237: static int sort_cmp_string_double_value(const void *a, const void *b) {
1.47    ! paf       238:        return key_of_part(a)-key_of_part(b);
1.1       paf       239: }
1.2       paf       240: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                    241:                                                                                   Hash& letter_hash, int level,
                    242:                                                                                   const String **from, const String **to) {
                    243:        Pool& pool=r.pool();
                    244: 
                    245:        // prepare header: 'hash' without "body"
                    246:        String& result=*new(pool) String(pool);
1.20      paf       247: 
1.45      paf       248:        Charset *charset;
                    249:        if(Value *vcharset_name=static_cast<Value *>(letter_hash.get(*charset_name)))
                    250:                charset=&charsets->get_charset(vcharset_name->as_string());
                    251:        else
                    252:                charset=&pool.get_source_charset();
1.20      paf       253: 
1.46      paf       254:        if(from)
                    255:                *from=0;
                    256:        if(to)
                    257:                *to=0;
1.2       paf       258:        Mail_info mail_info={
1.20      paf       259:                charset,
1.2       paf       260:                &result,
                    261:                from, to
                    262:        };
                    263:        letter_hash.for_each(add_header_attribute, &mail_info);
                    264: 
                    265:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
1.42      parser    266:                if(Hash *body_hash=body_element->get_hash(&method_name)) {
1.2       paf       267:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.9       paf       268:                        snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.2       paf       269:                        // multi-part
1.10      paf       270:                        result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    271:                        result << "\n" 
1.2       paf       272:                                "This is a multi-part message in MIME format.";
                    273: 
1.3       paf       274:                        // body parts..
                    275:                        // ..collect
1.35      parser    276:                        Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());
1.28      paf       277:                        Mail_seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       278:                        // ..sort
1.28      paf       279:                        _qsort(seq, body_hash->size(), sizeof(Mail_seq_item), 
1.2       paf       280:                                sort_cmp_string_double_value);
1.3       paf       281:                        // ..insert in 'seq' order
1.2       paf       282:                        for(int i=0; i<body_hash->size(); i++) {
                    283:                                // intermediate boundary
1.10      paf       284:                                result << "\n--" << boundary << "\n";
1.2       paf       285: 
1.47    ! paf       286:                                if(Hash *part_hash=seq[i].value->get_hash(&method_name))
        !           287:                                        if(seq[i].weight>=ATTACHMENT_WEIGHT)
        !           288:                                                result << attach_hash_to_string(r, *seq[i].name, *part_hash);
1.7       paf       289:                                        else 
1.10      paf       290:                                                result << letter_hash_to_string(r, method_name, *part_hash, 
1.7       paf       291:                                                        level+1, 0, 0);
1.2       paf       292:                                else
1.41      parser    293:                                        throw Exception(0, 0,
1.47    ! paf       294:                                                seq[i].name,
1.2       paf       295:                                                "part is not hash");
                    296:                        }
                    297: 
                    298:                        // finish boundary
1.10      paf       299:                        result << "\n--" << boundary << "--\n";
1.2       paf       300:                } else {
1.10      paf       301:                        result << 
1.45      paf       302:                                "\n"; // header|body separator
                    303: 
                    304:                        const String& body=body_element->as_string();
                    305:                        const void *body_ptr=body.cstr();  // body
                    306:                        size_t body_size=body.size();  // body
                    307:                        const void *mail_ptr;
                    308:                        size_t mail_size;
                    309:                        Charset::transcode(pool, 
                    310:                                pool.get_source_charset(), body_ptr, body_size,
                    311:                                *charset/*always set - either mail.charset or $request:charset*/, mail_ptr, mail_size);
                    312:                        result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);
1.2       paf       313:                }
                    314:        } else 
1.41      parser    315:                throw Exception(0, 0,
1.2       paf       316:                        &method_name,
                    317:                        "has no $body");
                    318: 
                    319:        return result;
                    320: }
                    321: 
1.4       paf       322: static void sendmail(Request& r, const String& method_name, 
                    323:                                         const String& letter, 
                    324:                                         const String *from, const String *to) {
                    325:        Pool& pool=r.pool();
                    326: 
1.12      paf       327:        char *letter_cstr=letter.cstr();
1.25      paf       328:        Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_class->name()));
1.12      paf       329: 
1.19      paf       330: #ifdef _MSC_VER
1.4       paf       331:        if(!from)
1.41      parser    332:                throw Exception(0, 0,
1.4       paf       333:                        &method_name,
1.15      paf       334:                        "has no 'from' header specified");
1.4       paf       335:        if(!to)
1.41      parser    336:                throw Exception(0, 0,
1.4       paf       337:                        &method_name,
1.15      paf       338:                        "has no 'to' header specified");
1.4       paf       339: 
                    340:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       341:        Value *server_port;
1.29      parser    342:        // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.25      paf       343:        if(mail_conf && 
                    344:                (server_port=static_cast<Value *>(mail_conf->get(
1.5       paf       345:                        *new(pool) String(pool, "SMTP"))))) {
                    346:                char *server=server_port->as_string().cstr();
1.11      paf       347:                const char *port=rsplit(server, ':');
1.4       paf       348:                if(!port)
1.11      paf       349:                        port="25";
1.4       paf       350: 
                    351:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    352:        } else
1.41      parser    353:                throw Exception(0, 0,
1.4       paf       354:                        &method_name,
1.13      paf       355:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       356: #else
1.12      paf       357:        // unix
1.13      paf       358:        // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default
                    359:        // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default
1.36      parser    360:        char no_cstr[MAX_NUMBER];
                    361:        for(int no=-2; ; no++) {
                    362:                const String *prog_string;
                    363:                switch(no) {
                    364:                case -2: prog_string=new(pool) String(pool, "/usr/sbin/sendmail -t"); break;
                    365:                case -1: prog_string=new(pool) String(pool, "/usr/lib/sendmail -t"); break;
                    366:                default: 
                    367:                        {
                    368:                                String prog_key(pool, "prog");
                    369:                                snprintf(no_cstr, MAX_NUMBER, "%d", no);
                    370:                                prog_key << no_cstr;
                    371:                                if(mail_conf) {
1.25      paf       372:                                        if(Value *prog_value=static_cast<Value *>(mail_conf->get(prog_key)))
1.13      paf       373:                                                prog_string=&prog_value->as_string();
                    374:                                        else
                    375:                                                if(no==0)
                    376:                                                        continue;
                    377:                                                else
1.41      parser    378:                                                        throw Exception(0, 0,
1.13      paf       379:                                                                &method_name,
                    380:                                                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    381:                                                                prog_key.cstr());
1.36      parser    382:                                } else
1.41      parser    383:                                        throw Exception(0, 0,
1.36      parser    384:                                                &method_name,
                    385:                                                "$" MAIN_CLASS_NAME ":" MAIL_NAME " not defined");
1.13      paf       386:                        }
1.36      parser    387:                }
                    388:                // we know prog_string here
                    389:                Array argv(pool);
                    390:                const String *file_spec;
                    391:                int after_file_spec=prog_string->pos(" ", 1);
                    392:                if(after_file_spec<=0)
                    393:                        file_spec=prog_string;
                    394:                else {
                    395:                        size_t pos_after=after_file_spec;
                    396:                        file_spec=&prog_string->mid(0, pos_after);
                    397:                        prog_string->split(argv, &pos_after, " ", 1, String::UL_CLEAN);
                    398:                }
1.12      paf       399: 
1.36      parser    400:                // skip unavailable default programs
                    401:                if(no<0 && !file_executable(*file_spec))
                    402:                        continue;
                    403: 
                    404:                String in(pool, letter_cstr); String out(pool); String err(pool);
                    405:                int exit_status=pa_exec(*file_spec,
                    406:                        0/*default env*/,
                    407:                        &argv,
                    408:                        in, out, err);
                    409:                if(exit_status || err.size())
1.41      parser    410:                        throw Exception(0, 0,
1.36      parser    411:                                &method_name,
                    412:                                "'%s' reported problem: %s (%d)",
                    413:                                        file_spec->cstr(),
                    414:                                        err.size()?err.cstr():"UNKNOWN", 
                    415:                                        exit_status);
                    416:                break;
                    417:        }
1.4       paf       418: #endif
                    419: }
                    420: 
1.7       paf       421: 
                    422: // methods
                    423: 
1.18      paf       424: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       425:        Pool& pool=r.pool();
                    426: 
1.32      parser    427:        Value& vhash=params->as_no_junction(0, "message must not be code");
1.42      parser    428:        Hash *hash=vhash.get_hash(&method_name);
1.1       paf       429:        if(!hash)
1.41      parser    430:                throw Exception(0, 0,
1.1       paf       431:                        &method_name,
                    432:                        "message must be hash");
                    433: 
1.2       paf       434:        const String *from, *to;
1.4       paf       435:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       436: 
1.21      paf       437: //     r.write_assign_lang(*new(pool) VString(letter));
                    438:        sendmail(r, method_name, letter, from, to);
1.6       paf       439: }
                    440: 
1.24      paf       441: // constructor & configurator
1.23      paf       442: 
1.25      paf       443: MMail::MMail(Pool& apool) : Methoded(apool),
                    444:        mail_name(apool, MAIL_NAME),
                    445:        content_disposition_name(apool, CONTENT_DISPOSITION_NAME),
                    446:        content_disposition_filename_name(apool, CONTENT_DISPOSITION_FILENAME_NAME)
                    447: {
1.23      paf       448:        set_name(*NEW String(pool(), MAIL_CLASS_NAME));
                    449: 
1.27      paf       450:        // ^mail:send{hash}
1.23      paf       451:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.24      paf       452: }
                    453: 
                    454: void MMail::configure_user(Request& r) {
                    455:        Pool& pool=r.pool();
                    456: 
                    457:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.25      paf       458:        if(Value *mail_element=r.main_class->get_element(mail_name))
1.42      parser    459:                if(Hash *mail_conf=mail_element->get_hash(0))
1.25      paf       460:                        r.classes_conf.put(name(), mail_conf);
                    461:                else
1.41      parser    462:                        throw Exception(0, 0,
1.24      paf       463:                                0,
                    464:                                "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.23      paf       465: }
                    466: 
                    467: // creator
                    468: 
                    469: Methoded *MMail_create(Pool& pool) {
                    470:        return mail_class=new(pool) MMail(pool);
1.1       paf       471: }

E-mail: