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

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

E-mail: