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

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.68    ! paf         7:        $Id: mail.C,v 1.67.2.1 2002/06/30 07:52:47 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.67      paf        18: #include "pa_uue.h"
1.50      paf        19: 
                     20: #ifdef _MSC_VER
                     21: #      include "smtp/smtp.h"
                     22: #endif
1.4       paf        23: 
1.23      paf        24: // defines
1.1       paf        25: 
1.23      paf        26: #define MAIL_CLASS_NAME "mail"
                     27: 
1.25      paf        28: #define MAIL_NAME "MAIL"
                     29: 
1.67      paf        30: // consts
1.25      paf        31: 
1.67      paf        32: const int ATTACHMENT_WEIGHT=100;
1.25      paf        33: 
1.67      paf        34: // global variable
1.47      paf        35: 
1.67      paf        36: Methoded *mail_base_class;
1.47      paf        37: 
1.23      paf        38: // class
                     39: 
                     40: class MMail : public Methoded {
                     41: public:
                     42:        MMail(Pool& pool);
1.24      paf        43: public: // Methoded
1.67      paf        44:        bool used_directly() { return false; }
1.68    ! paf        45:        void configure_user(Request& r);
1.25      paf        46: private:
                     47:        String mail_name;
1.23      paf        48: };
1.1       paf        49: 
1.33      parser     50: /** ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
                     51:        @todo solve - bad with html attaches
                     52: */
1.7       paf        53: static const String& attach_hash_to_string(Request& r, const String& origin_string, 
                     54:                                                                                   Hash& attach_hash) {
                     55:        Pool& pool=r.pool();
                     56: 
1.46      paf        57:        Value *vformat=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "format")));
1.7       paf        58: 
1.8       paf        59:        const VFile *vfile;
                     60:        if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
1.17      paf        61:                vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve
1.8       paf        62:        else
1.60      paf        63:                throw Exception("parser.runtime",
1.7       paf        64:                        &origin_string,
                     65:                        "has no $value");
                     66: 
                     67:        const String *file_name;
                     68:        if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8       paf        69:                *new(pool) String(pool, "file-name")))) // specified $file-name
1.7       paf        70:                file_name=&vfile_name->as_string();
1.16      paf        71:        else // no $file-name, VFile surely knows name
                     72:                file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.48      paf        73:        const char *file_name_cstr=file_name->cstr();
1.7       paf        74: 
                     75:        String& result=*new(pool) String(pool);
                     76: 
1.10      paf        77:        // content-type: application/octet-stream
1.64      paf        78:        result << "content-type: " << r.mime_type_of(file_name_cstr) 
                     79:                << "; name=\"" << file_name_cstr << "\"\n";
1.7       paf        80:        // content-disposition: attachment; filename="user_file_name"
1.10      paf        81:        result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7       paf        82: 
1.46      paf        83:        const String *type=vformat?&vformat->as_string():0;
                     84:        if(!type/*default = uue*/ || *type=="uue") {
1.67      paf        85:                pa_uuencode(result, file_name_cstr, *vfile);
1.46      paf        86:        } else // for now
1.60      paf        87:                throw Exception("parser.runtime",
1.46      paf        88:                        type,
                     89:                        "unknown attachment encode format");
1.7       paf        90:        
                     91:        return result;
                     92: }
                     93: 
1.1       paf        94: 
1.36      parser     95: #ifndef DOXYGEN
1.1       paf        96: struct Mail_info {
1.61      paf        97:        Charset *charset; const char *content_charset_name;
1.1       paf        98:        String *header;
1.2       paf        99:        const String **from, **to;
1.1       paf       100: };
1.36      parser    101: #endif
1.1       paf       102: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    103:                                                                 void *info) {
                    104: 
                    105:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    106:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf       107: 
                    108:        // exclude one attribute [body]
1.45      paf       109:        if(aattribute==BODY_NAME
                    110:                || aattribute==CHARSET_NAME)
1.1       paf       111:                return;
                    112: 
1.2       paf       113:        // fetch from & to from header for SMTP
                    114:        if(mi.from && aattribute=="from")
                    115:                *mi.from=&lmeaning.as_string();
                    116:        if(mi.to && aattribute=="to")
                    117:                *mi.to=&lmeaning.as_string();
                    118: 
                    119:        // append header line
1.10      paf       120:        *mi.header << 
                    121:                aattribute << ":" << 
1.20      paf       122:                attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER).
1.61      paf       123:                        cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) << 
1.10      paf       124:                "\n";
1.2       paf       125: }
1.22      paf       126: 
1.36      parser    127: #ifndef DOXYGEN
1.28      paf       128: struct Mail_seq_item {
1.47      paf       129:        int weight;
                    130:        const String *name;
                    131:        Value *value;
1.2       paf       132: };
1.36      parser    133: #endif
1.47      paf       134: static int get_part_name_weight(const Hash::Key& part_name) {
                    135:        const char *cstr=part_name.cstr();
                    136:        int offset=0;
                    137:        if(strncmp(cstr, "text", 4)==0) {
                    138:                cstr+=4;
                    139:        } else if(strncmp(cstr, "attach", 6)==0) {
                    140:                cstr+=6;
                    141:                offset=ATTACHMENT_WEIGHT;
                    142:        } else
1.60      paf       143:                throw Exception("parser.runtime",
1.47      paf       144:                        &part_name,
                    145:                        "is neither text# nor attach#");
                    146: 
                    147:        char *error_pos;
                    148:        return strtol(cstr, &error_pos, 10)+offset;
                    149: }
1.7       paf       150: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       151:                                         void *info) {
1.28      paf       152:        Mail_seq_item **seq_ref=static_cast<Mail_seq_item **>(info);
1.47      paf       153:        (**seq_ref).weight=get_part_name_weight(part_name);
                    154:        (**seq_ref).name=&part_name;
                    155:        (**seq_ref).value=static_cast<Value *>(part_value);
1.2       paf       156:        (*seq_ref)++;
                    157: }
1.47      paf       158: static int key_of_part(const void *item) {
                    159:        return static_cast<const Mail_seq_item *>(item)->weight;
1.2       paf       160: }
                    161: static int sort_cmp_string_double_value(const void *a, const void *b) {
1.47      paf       162:        return key_of_part(a)-key_of_part(b);
1.1       paf       163: }
1.67      paf       164: static const String& message_hash_to_string(Request& r, const String& method_name, 
                    165:                                                                                   Hash& message_hash, int level,
1.2       paf       166:                                                                                   const String **from, const String **to) {
                    167:        Pool& pool=r.pool();
                    168: 
                    169:        // prepare header: 'hash' without "body"
                    170:        String& result=*new(pool) String(pool);
1.20      paf       171: 
1.45      paf       172:        Charset *charset;
1.67      paf       173:        if(Value *vrecodecharset_name=static_cast<Value *>(message_hash.get(*charset_name)))
1.61      paf       174:                charset=&charsets->get_charset(vrecodecharset_name->as_string());
1.45      paf       175:        else
                    176:                charset=&pool.get_source_charset();
1.20      paf       177: 
1.61      paf       178:        const char *content_charset_name=0;
1.67      paf       179:        if(Value *vcontent_type=static_cast<Value *>(message_hash.get(*content_type_name)))
1.61      paf       180:                if(Hash *hcontent_type=vcontent_type->get_hash(0))
                    181:                        if(Value *vcontentcharset_name=static_cast<Value *>(hcontent_type->get(*charset_name)))
                    182:                                content_charset_name=vcontentcharset_name->as_string().cstr();
                    183: 
1.46      paf       184:        if(from)
                    185:                *from=0;
                    186:        if(to)
                    187:                *to=0;
1.2       paf       188:        Mail_info mail_info={
1.61      paf       189:                charset, content_charset_name,
1.2       paf       190:                &result,
                    191:                from, to
                    192:        };
1.67      paf       193:        message_hash.for_each(add_header_attribute, &mail_info);
1.2       paf       194: 
1.67      paf       195:        if(Value *body_element=static_cast<Value *>(message_hash.get(*body_name))) {
1.42      parser    196:                if(Hash *body_hash=body_element->get_hash(&method_name)) {
1.3       paf       197:                        // body parts..
                    198:                        // ..collect
1.35      parser    199:                        Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());
1.28      paf       200:                        Mail_seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       201:                        // ..sort
1.28      paf       202:                        _qsort(seq, body_hash->size(), sizeof(Mail_seq_item), 
1.2       paf       203:                                sort_cmp_string_double_value);
1.48      paf       204: 
                    205:                        bool multipart=body_hash->size()>1;
                    206:                        // header
                    207:                        char *boundary=0;
                    208:                        if(multipart) {
                    209:                                boundary=(char *)pool.malloc(MAX_NUMBER);
                    210:                                snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                    211:                                // multi-part
                    212:                                result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    213:                                result << "\n" 
                    214:                                        "This is a multi-part message in MIME format.";
                    215:                        }
                    216: 
1.3       paf       217:                        // ..insert in 'seq' order
1.2       paf       218:                        for(int i=0; i<body_hash->size(); i++) {
1.48      paf       219:                                if(multipart) {
                    220:                                        // intermediate boundary
                    221:                                        result << "\n--" << boundary << "\n";
                    222:                                }
1.2       paf       223: 
1.47      paf       224:                                if(Hash *part_hash=seq[i].value->get_hash(&method_name))
                    225:                                        if(seq[i].weight>=ATTACHMENT_WEIGHT)
                    226:                                                result << attach_hash_to_string(r, *seq[i].name, *part_hash);
1.7       paf       227:                                        else 
1.67      paf       228:                                                result << message_hash_to_string(r, method_name, *part_hash, 
1.7       paf       229:                                                        level+1, 0, 0);
1.2       paf       230:                                else
1.60      paf       231:                                        throw Exception("parser.runtime",
1.47      paf       232:                                                seq[i].name,
1.2       paf       233:                                                "part is not hash");
                    234:                        }
                    235: 
                    236:                        // finish boundary
1.48      paf       237:                        if(multipart) {
                    238:                                result << "\n--" << boundary << "--\n";
                    239:                        }
1.2       paf       240:                } else {
1.10      paf       241:                        result << 
1.45      paf       242:                                "\n"; // header|body separator
                    243: 
                    244:                        const String& body=body_element->as_string();
                    245:                        const void *body_ptr=body.cstr();  // body
                    246:                        size_t body_size=body.size();  // body
                    247:                        const void *mail_ptr;
                    248:                        size_t mail_size;
                    249:                        Charset::transcode(pool, 
                    250:                                pool.get_source_charset(), body_ptr, body_size,
                    251:                                *charset/*always set - either mail.charset or $request:charset*/, mail_ptr, mail_size);
                    252:                        result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);
1.2       paf       253:                }
                    254:        } else 
1.60      paf       255:                throw Exception("parser.runtime",
1.2       paf       256:                        &method_name,
                    257:                        "has no $body");
                    258: 
                    259:        return result;
                    260: }
                    261: 
1.4       paf       262: static void sendmail(Request& r, const String& method_name, 
1.67      paf       263:                                         const String& message, 
1.4       paf       264:                                         const String *from, const String *to) {
                    265:        Pool& pool=r.pool();
                    266: 
1.67      paf       267:        char *message_cstr=message.cstr();
                    268:        Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_base_class->name()));
1.12      paf       269: 
1.19      paf       270: #ifdef _MSC_VER
1.4       paf       271:        if(!from)
1.60      paf       272:                throw Exception("parser.runtime",
1.4       paf       273:                        &method_name,
1.15      paf       274:                        "has no 'from' header specified");
1.4       paf       275:        if(!to)
1.60      paf       276:                throw Exception("parser.runtime",
1.4       paf       277:                        &method_name,
1.15      paf       278:                        "has no 'to' header specified");
1.4       paf       279: 
                    280:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       281:        Value *server_port;
1.29      parser    282:        // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.25      paf       283:        if(mail_conf && 
                    284:                (server_port=static_cast<Value *>(mail_conf->get(
1.5       paf       285:                        *new(pool) String(pool, "SMTP"))))) {
                    286:                char *server=server_port->as_string().cstr();
1.11      paf       287:                const char *port=rsplit(server, ':');
1.4       paf       288:                if(!port)
1.11      paf       289:                        port="25";
1.4       paf       290: 
1.67      paf       291:                smtp.Send(server, port, message_cstr, from->cstr(), to->cstr());
1.4       paf       292:        } else
1.60      paf       293:                throw Exception("parser.runtime",
1.4       paf       294:                        &method_name,
1.13      paf       295:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       296: #else
1.12      paf       297:        // unix
1.51      paf       298:        // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default
                    299:        // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t"] default
1.12      paf       300: 
1.55      paf       301:        const String *sendmail_command;
                    302: #ifdef PA_FORCED_SENDMAIL
1.58      paf       303:        sendmail_command=new(pool) String(pool, PA_FORCED_SENDMAIL);
1.55      paf       304: #else
1.51      paf       305:        const char *sendmailkey_cstr="sendmail";
                    306:        if(mail_conf) {
                    307:                if(Value *sendmail_value=static_cast<Value *>(mail_conf->get(String(pool, sendmailkey_cstr))))
1.52      paf       308:                        sendmail_command=&sendmail_value->as_string();
1.51      paf       309:                else
1.60      paf       310:                        throw Exception("parser.runtime",
1.36      parser    311:                                &method_name,
1.51      paf       312:                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    313:                                sendmailkey_cstr);
                    314:        } else {
1.52      paf       315:                String *test=new(pool) String(pool, "/usr/sbin/sendmail");
                    316:                if(!file_executable(*test))
                    317:                        test=new(pool) String(pool, "/usr/lib/sendmail");
1.66      paf       318:                test->APPEND_CONST(" -ti");
1.52      paf       319:                sendmail_command=test;
1.51      paf       320:        }
1.55      paf       321: #endif
1.51      paf       322: 
1.52      paf       323:        // we know sendmail_command here
1.51      paf       324:        Array argv(pool);
                    325:        const String *file_spec;
1.52      paf       326:        int after_file_spec=sendmail_command->pos(" ", 1);
1.51      paf       327:        if(after_file_spec<=0)
1.52      paf       328:                file_spec=sendmail_command;
1.51      paf       329:        else {
1.52      paf       330:                size_t pos_after=after_file_spec;
                    331:                file_spec=&sendmail_command->mid(0, pos_after++);
                    332:                sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS);
1.36      parser    333:        }
1.51      paf       334: 
1.52      paf       335:        if(!file_executable(*file_spec))
1.60      paf       336:                throw Exception(0,
1.55      paf       337:                        file_spec, 
                    338:                        "is not executable."
                    339: #ifdef PA_FORCED_SENDMAIL
1.59      paf       340:                        " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58      paf       341: #else
1.55      paf       342:                        " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s with appropriate sendmail command", 
                    343:                                sendmailkey_cstr
                    344: #endif
                    345:                );
                    346: 
1.51      paf       347: 
1.67      paf       348:        String in(pool, message_cstr); String out(pool); String err(pool);
1.56      paf       349:        int exit_status=pa_exec(
                    350:                // forced_allow
                    351: #ifdef PA_FORCED_SENDMAIL
                    352:                true
                    353: #else
                    354:                false
                    355: #endif
1.57      paf       356:                , *file_spec,
1.51      paf       357:                0/*default env*/,
                    358:                &argv,
                    359:                in, out, err);
                    360:        if(exit_status || err.size())
1.60      paf       361:                throw Exception(0,
1.51      paf       362:                        &method_name,
                    363:                        "'%s' reported problem: %s (%d)",
                    364:                                file_spec->cstr(),
                    365:                                err.size()?err.cstr():"UNKNOWN", 
                    366:                                exit_status);
1.4       paf       367: #endif
                    368: }
                    369: 
1.7       paf       370: 
                    371: // methods
                    372: 
1.18      paf       373: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       374:        Pool& pool=r.pool();
                    375: 
1.32      parser    376:        Value& vhash=params->as_no_junction(0, "message must not be code");
1.42      parser    377:        Hash *hash=vhash.get_hash(&method_name);
1.1       paf       378:        if(!hash)
1.60      paf       379:                throw Exception("parser.runtime",
1.1       paf       380:                        &method_name,
                    381:                        "message must be hash");
                    382: 
1.2       paf       383:        const String *from, *to;
1.67      paf       384:        const String& message=hash->get(*body_name)/*old format*/?
                    385:                message_hash_to_string(r, method_name, *hash, 0, &from, &to) : // old
                    386:                static_cast<VMail *>(r.self)->message_hash_to_string(r, &method_name, hash, 0, &from, &to); // new
1.1       paf       387: 
1.67      paf       388:        //r.write_pass_lang(message);
                    389:        sendmail(r, method_name, message, from, to);
1.6       paf       390: }
                    391: 
1.24      paf       392: // constructor & configurator
1.23      paf       393: 
1.63      paf       394: MMail::MMail(Pool& apool) : Methoded(apool, MAIL_CLASS_NAME),
1.64      paf       395:        mail_name(apool, MAIL_NAME)
1.25      paf       396: {
1.27      paf       397:        // ^mail:send{hash}
1.23      paf       398:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68    ! paf       399: }
        !           400: 
        !           401: void MMail::configure_user(Request& r) {
        !           402:        Pool& pool=r.pool();
        !           403: 
        !           404:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
        !           405:        if(Value *mail_element=r.main_class->get_element(mail_name))
        !           406:                if(Hash *mail_conf=mail_element->get_hash(0))
        !           407:                        r.classes_conf.put(name(), mail_conf);
        !           408:                else
        !           409:                        throw Exception("parser.runtime",
        !           410:                                0,
        !           411:                                "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.24      paf       412: }
                    413: 
1.23      paf       414: // creator
                    415: 
                    416: Methoded *MMail_create(Pool& pool) {
1.67      paf       417:        return mail_base_class=new(pool) MMail(pool);
1.1       paf       418: }

E-mail: