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

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.48    ! paf         7:        $Id: mail.C,v 1.47 2001/12/19 12:26:11 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.48    ! paf       150:        const char *file_name_cstr=file_name->cstr();
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.3       paf       267:                        // body parts..
                    268:                        // ..collect
1.35      parser    269:                        Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());
1.28      paf       270:                        Mail_seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       271:                        // ..sort
1.28      paf       272:                        _qsort(seq, body_hash->size(), sizeof(Mail_seq_item), 
1.2       paf       273:                                sort_cmp_string_double_value);
1.48    ! paf       274: 
        !           275:                        bool multipart=body_hash->size()>1;
        !           276:                        // header
        !           277:                        char *boundary=0;
        !           278:                        if(multipart) {
        !           279:                                boundary=(char *)pool.malloc(MAX_NUMBER);
        !           280:                                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
        !           281:                                // multi-part
        !           282:                                result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
        !           283:                                result << "\n" 
        !           284:                                        "This is a multi-part message in MIME format.";
        !           285:                        }
        !           286: 
1.3       paf       287:                        // ..insert in 'seq' order
1.2       paf       288:                        for(int i=0; i<body_hash->size(); i++) {
1.48    ! paf       289:                                if(multipart) {
        !           290:                                        // intermediate boundary
        !           291:                                        result << "\n--" << boundary << "\n";
        !           292:                                }
1.2       paf       293: 
1.47      paf       294:                                if(Hash *part_hash=seq[i].value->get_hash(&method_name))
                    295:                                        if(seq[i].weight>=ATTACHMENT_WEIGHT)
                    296:                                                result << attach_hash_to_string(r, *seq[i].name, *part_hash);
1.7       paf       297:                                        else 
1.10      paf       298:                                                result << letter_hash_to_string(r, method_name, *part_hash, 
1.7       paf       299:                                                        level+1, 0, 0);
1.2       paf       300:                                else
1.41      parser    301:                                        throw Exception(0, 0,
1.47      paf       302:                                                seq[i].name,
1.2       paf       303:                                                "part is not hash");
                    304:                        }
                    305: 
                    306:                        // finish boundary
1.48    ! paf       307:                        if(multipart) {
        !           308:                                result << "\n--" << boundary << "--\n";
        !           309:                        }
1.2       paf       310:                } else {
1.10      paf       311:                        result << 
1.45      paf       312:                                "\n"; // header|body separator
                    313: 
                    314:                        const String& body=body_element->as_string();
                    315:                        const void *body_ptr=body.cstr();  // body
                    316:                        size_t body_size=body.size();  // body
                    317:                        const void *mail_ptr;
                    318:                        size_t mail_size;
                    319:                        Charset::transcode(pool, 
                    320:                                pool.get_source_charset(), body_ptr, body_size,
                    321:                                *charset/*always set - either mail.charset or $request:charset*/, mail_ptr, mail_size);
                    322:                        result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);
1.2       paf       323:                }
                    324:        } else 
1.41      parser    325:                throw Exception(0, 0,
1.2       paf       326:                        &method_name,
                    327:                        "has no $body");
                    328: 
                    329:        return result;
                    330: }
                    331: 
1.4       paf       332: static void sendmail(Request& r, const String& method_name, 
                    333:                                         const String& letter, 
                    334:                                         const String *from, const String *to) {
                    335:        Pool& pool=r.pool();
                    336: 
1.12      paf       337:        char *letter_cstr=letter.cstr();
1.25      paf       338:        Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_class->name()));
1.12      paf       339: 
1.19      paf       340: #ifdef _MSC_VER
1.4       paf       341:        if(!from)
1.41      parser    342:                throw Exception(0, 0,
1.4       paf       343:                        &method_name,
1.15      paf       344:                        "has no 'from' header specified");
1.4       paf       345:        if(!to)
1.41      parser    346:                throw Exception(0, 0,
1.4       paf       347:                        &method_name,
1.15      paf       348:                        "has no 'to' header specified");
1.4       paf       349: 
                    350:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       351:        Value *server_port;
1.29      parser    352:        // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.25      paf       353:        if(mail_conf && 
                    354:                (server_port=static_cast<Value *>(mail_conf->get(
1.5       paf       355:                        *new(pool) String(pool, "SMTP"))))) {
                    356:                char *server=server_port->as_string().cstr();
1.11      paf       357:                const char *port=rsplit(server, ':');
1.4       paf       358:                if(!port)
1.11      paf       359:                        port="25";
1.4       paf       360: 
                    361:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    362:        } else
1.41      parser    363:                throw Exception(0, 0,
1.4       paf       364:                        &method_name,
1.13      paf       365:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       366: #else
1.12      paf       367:        // unix
1.13      paf       368:        // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default
                    369:        // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default
1.36      parser    370:        char no_cstr[MAX_NUMBER];
                    371:        for(int no=-2; ; no++) {
                    372:                const String *prog_string;
                    373:                switch(no) {
                    374:                case -2: prog_string=new(pool) String(pool, "/usr/sbin/sendmail -t"); break;
                    375:                case -1: prog_string=new(pool) String(pool, "/usr/lib/sendmail -t"); break;
                    376:                default: 
                    377:                        {
                    378:                                String prog_key(pool, "prog");
                    379:                                snprintf(no_cstr, MAX_NUMBER, "%d", no);
                    380:                                prog_key << no_cstr;
                    381:                                if(mail_conf) {
1.25      paf       382:                                        if(Value *prog_value=static_cast<Value *>(mail_conf->get(prog_key)))
1.13      paf       383:                                                prog_string=&prog_value->as_string();
                    384:                                        else
                    385:                                                if(no==0)
                    386:                                                        continue;
                    387:                                                else
1.41      parser    388:                                                        throw Exception(0, 0,
1.13      paf       389:                                                                &method_name,
                    390:                                                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    391:                                                                prog_key.cstr());
1.36      parser    392:                                } else
1.41      parser    393:                                        throw Exception(0, 0,
1.36      parser    394:                                                &method_name,
                    395:                                                "$" MAIN_CLASS_NAME ":" MAIL_NAME " not defined");
1.13      paf       396:                        }
1.36      parser    397:                }
                    398:                // we know prog_string here
                    399:                Array argv(pool);
                    400:                const String *file_spec;
                    401:                int after_file_spec=prog_string->pos(" ", 1);
                    402:                if(after_file_spec<=0)
                    403:                        file_spec=prog_string;
                    404:                else {
                    405:                        size_t pos_after=after_file_spec;
                    406:                        file_spec=&prog_string->mid(0, pos_after);
                    407:                        prog_string->split(argv, &pos_after, " ", 1, String::UL_CLEAN);
                    408:                }
1.12      paf       409: 
1.36      parser    410:                // skip unavailable default programs
                    411:                if(no<0 && !file_executable(*file_spec))
                    412:                        continue;
                    413: 
                    414:                String in(pool, letter_cstr); String out(pool); String err(pool);
                    415:                int exit_status=pa_exec(*file_spec,
                    416:                        0/*default env*/,
                    417:                        &argv,
                    418:                        in, out, err);
                    419:                if(exit_status || err.size())
1.41      parser    420:                        throw Exception(0, 0,
1.36      parser    421:                                &method_name,
                    422:                                "'%s' reported problem: %s (%d)",
                    423:                                        file_spec->cstr(),
                    424:                                        err.size()?err.cstr():"UNKNOWN", 
                    425:                                        exit_status);
                    426:                break;
                    427:        }
1.4       paf       428: #endif
                    429: }
                    430: 
1.7       paf       431: 
                    432: // methods
                    433: 
1.18      paf       434: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       435:        Pool& pool=r.pool();
                    436: 
1.32      parser    437:        Value& vhash=params->as_no_junction(0, "message must not be code");
1.42      parser    438:        Hash *hash=vhash.get_hash(&method_name);
1.1       paf       439:        if(!hash)
1.41      parser    440:                throw Exception(0, 0,
1.1       paf       441:                        &method_name,
                    442:                        "message must be hash");
                    443: 
1.2       paf       444:        const String *from, *to;
1.4       paf       445:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       446: 
1.21      paf       447: //     r.write_assign_lang(*new(pool) VString(letter));
                    448:        sendmail(r, method_name, letter, from, to);
1.6       paf       449: }
                    450: 
1.24      paf       451: // constructor & configurator
1.23      paf       452: 
1.25      paf       453: MMail::MMail(Pool& apool) : Methoded(apool),
                    454:        mail_name(apool, MAIL_NAME),
                    455:        content_disposition_name(apool, CONTENT_DISPOSITION_NAME),
                    456:        content_disposition_filename_name(apool, CONTENT_DISPOSITION_FILENAME_NAME)
                    457: {
1.23      paf       458:        set_name(*NEW String(pool(), MAIL_CLASS_NAME));
                    459: 
1.27      paf       460:        // ^mail:send{hash}
1.23      paf       461:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.24      paf       462: }
                    463: 
                    464: void MMail::configure_user(Request& r) {
                    465:        Pool& pool=r.pool();
                    466: 
                    467:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.25      paf       468:        if(Value *mail_element=r.main_class->get_element(mail_name))
1.42      parser    469:                if(Hash *mail_conf=mail_element->get_hash(0))
1.25      paf       470:                        r.classes_conf.put(name(), mail_conf);
                    471:                else
1.41      parser    472:                        throw Exception(0, 0,
1.24      paf       473:                                0,
                    474:                                "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.23      paf       475: }
                    476: 
                    477: // creator
                    478: 
                    479: Methoded *MMail_create(Pool& pool) {
                    480:        return mail_class=new(pool) MMail(pool);
1.1       paf       481: }

E-mail: