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

1.1       paf         1: /** @file
                      2:        Parser: @b mail parser class.
                      3: 
1.53      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.54      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: 
1.57    ! paf         7:        $Id: mail.C,v 1.56 2002/03/25 11:55:26 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #include "pa_config_includes.h"
                     11: 
                     12: #include "pa_common.h"
                     13: #include "pa_request.h"
1.6       paf        14: #include "pa_vfile.h"
1.12      paf        15: #include "pa_exec.h"
1.45      paf        16: #include "pa_charsets.h"
                     17: #include "pa_charset.h"
1.50      paf        18: 
                     19: #ifdef _MSC_VER
                     20: #      include "smtp/smtp.h"
                     21: #endif
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.51      paf       368:        // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default
                    369:        // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t"] default
1.12      paf       370: 
1.55      paf       371:        const String *sendmail_command;
                    372: #ifdef PA_FORCED_SENDMAIL
                    373:        sendmail_command=PA_FORCED_SENDMAIL;
                    374: #else
1.51      paf       375:        const char *sendmailkey_cstr="sendmail";
                    376:        if(mail_conf) {
                    377:                if(Value *sendmail_value=static_cast<Value *>(mail_conf->get(String(pool, sendmailkey_cstr))))
1.52      paf       378:                        sendmail_command=&sendmail_value->as_string();
1.51      paf       379:                else
1.41      parser    380:                        throw Exception(0, 0,
1.36      parser    381:                                &method_name,
1.51      paf       382:                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    383:                                sendmailkey_cstr);
                    384:        } else {
1.52      paf       385:                String *test=new(pool) String(pool, "/usr/sbin/sendmail");
                    386:                if(!file_executable(*test))
                    387:                        test=new(pool) String(pool, "/usr/lib/sendmail");
                    388:                test->APPEND_CONST(" -t");
                    389:                sendmail_command=test;
1.51      paf       390:        }
1.55      paf       391: #endif
1.51      paf       392: 
1.52      paf       393:        // we know sendmail_command here
1.51      paf       394:        Array argv(pool);
                    395:        const String *file_spec;
1.52      paf       396:        int after_file_spec=sendmail_command->pos(" ", 1);
1.51      paf       397:        if(after_file_spec<=0)
1.52      paf       398:                file_spec=sendmail_command;
1.51      paf       399:        else {
1.52      paf       400:                size_t pos_after=after_file_spec;
                    401:                file_spec=&sendmail_command->mid(0, pos_after++);
                    402:                sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS);
1.36      parser    403:        }
1.51      paf       404: 
1.52      paf       405:        if(!file_executable(*file_spec))
1.51      paf       406:                throw Exception(0, 0,
1.55      paf       407:                        file_spec, 
                    408:                        "is not executable."
                    409: #ifdef PA_FORCED_SENDMAIL
                    410:                        " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s with appropriate sendmail command", 
                    411:                                sendmailkey_cstr
                    412: #else
                    413:                        " Use configure key --with-sendmail=\"appropriate sendmail command\""
                    414: #endif
                    415:                );
                    416: 
1.51      paf       417: 
                    418:        String in(pool, letter_cstr); String out(pool); String err(pool);
1.56      paf       419:        int exit_status=pa_exec(
                    420:                // forced_allow
                    421: #ifdef PA_FORCED_SENDMAIL
                    422:                true
                    423: #else
                    424:                false
                    425: #endif
1.57    ! paf       426:                , *file_spec,
1.51      paf       427:                0/*default env*/,
                    428:                &argv,
                    429:                in, out, err);
                    430:        if(exit_status || err.size())
                    431:                throw Exception(0, 0,
                    432:                        &method_name,
                    433:                        "'%s' reported problem: %s (%d)",
                    434:                                file_spec->cstr(),
                    435:                                err.size()?err.cstr():"UNKNOWN", 
                    436:                                exit_status);
1.4       paf       437: #endif
                    438: }
                    439: 
1.7       paf       440: 
                    441: // methods
                    442: 
1.18      paf       443: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       444:        Pool& pool=r.pool();
                    445: 
1.32      parser    446:        Value& vhash=params->as_no_junction(0, "message must not be code");
1.42      parser    447:        Hash *hash=vhash.get_hash(&method_name);
1.1       paf       448:        if(!hash)
1.41      parser    449:                throw Exception(0, 0,
1.1       paf       450:                        &method_name,
                    451:                        "message must be hash");
                    452: 
1.2       paf       453:        const String *from, *to;
1.4       paf       454:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       455: 
1.21      paf       456: //     r.write_assign_lang(*new(pool) VString(letter));
                    457:        sendmail(r, method_name, letter, from, to);
1.6       paf       458: }
                    459: 
1.24      paf       460: // constructor & configurator
1.23      paf       461: 
1.25      paf       462: MMail::MMail(Pool& apool) : Methoded(apool),
                    463:        mail_name(apool, MAIL_NAME),
                    464:        content_disposition_name(apool, CONTENT_DISPOSITION_NAME),
                    465:        content_disposition_filename_name(apool, CONTENT_DISPOSITION_FILENAME_NAME)
                    466: {
1.23      paf       467:        set_name(*NEW String(pool(), MAIL_CLASS_NAME));
                    468: 
1.27      paf       469:        // ^mail:send{hash}
1.23      paf       470:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.24      paf       471: }
                    472: 
                    473: void MMail::configure_user(Request& r) {
                    474:        Pool& pool=r.pool();
                    475: 
                    476:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.25      paf       477:        if(Value *mail_element=r.main_class->get_element(mail_name))
1.42      parser    478:                if(Hash *mail_conf=mail_element->get_hash(0))
1.25      paf       479:                        r.classes_conf.put(name(), mail_conf);
                    480:                else
1.41      parser    481:                        throw Exception(0, 0,
1.24      paf       482:                                0,
                    483:                                "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.23      paf       484: }
                    485: 
                    486: // creator
                    487: 
                    488: Methoded *MMail_create(Pool& pool) {
                    489:        return mail_class=new(pool) MMail(pool);
1.1       paf       490: }

E-mail: