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

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

E-mail: