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

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.13! paf         8: static const char* IDENT_MAIL_C="$Date: 2003/02/26 11:29:55 $";
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.67      paf        29: // consts
1.25      paf        30: 
1.67      paf        31: const int ATTACHMENT_WEIGHT=100;
1.25      paf        32: 
1.23      paf        33: // class
                     34: 
1.88.2.9  paf        35: class MMail: public Methoded {
1.24      paf        36: public: // Methoded
1.67      paf        37:        bool used_directly() { return false; }
1.68      paf        38:        void configure_user(Request& r);
1.7       paf        39: 
1.88.2.6  paf        40: public:
                     41:        MMail();
1.1       paf        42: };
1.70      paf        43: 
1.88.2.6  paf        44: // global variable
1.22      paf        45: 
1.88.2.12  paf        46: DECLARE_CLASS_VAR(mail, 0/*fictive*/, new MMail);
1.47      paf        47: 
1.88.2.6  paf        48: // defines for statics
1.2       paf        49: 
1.88.2.6  paf        50: #define MAIL_NAME "MAIL"
1.20      paf        51: 
1.88.2.6  paf        52: // statics
                     53:        
                     54: static StringPtr mail_name(new String(MAIL_NAME));
1.2       paf        55: 
1.88.2.6  paf        56: // helpers
1.2       paf        57: 
1.88.2.6  paf        58: static void sendmail(Request& r, StringPtr method_name, 
                     59:                                         StringPtr message, 
                     60:                                         StringPtr from, StringPtr to) {
1.4       paf        61:        Pool& pool=r.pool();
                     62: 
1.88.2.6  paf        63:        CharPtr message_cstr=message->cstr(String::UL_UNSPECIFIED);
1.88.2.7  paf        64:        Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_base_class->name()).get());
1.12      paf        65: 
1.88.2.6  paf        66:        const char *exception_type="email.format";
1.85      paf        67:        if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from}
1.75      paf        68:                throw Exception(exception_type,
1.88.2.5  paf        69:                        method_name,
1.70      paf        70:                        "parameter does not specify 'from' header field");
1.85      paf        71: 
                     72: #ifdef _MSC_VER
                     73:        if(!to) // we use only in SMTP RCPT to: {to}
1.75      paf        74:                throw Exception(exception_type,
1.88.2.5  paf        75:                        method_name,
1.70      paf        76:                        "parameter does not specify 'to' header field");
1.4       paf        77: 
1.88.2.6  paf        78:        SMTPPtr smtp(new SMTP(method_name));
                     79:        ValuePtr server_port;
1.29      parser     80:        // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.88.2.6  paf        81:        if(vmail_conf && 
                     82:                (server_port=vmail_conf->get_hash(method_name)->get(StringPtr(new String("SMTP"))))) {
                     83:                CharPtr server=server_port->as_string(&pool)->cstr();
                     84:                const char *port=rsplit(server, ':');
1.4       paf        85:                if(!port)
1.11      paf        86:                        port="25";
1.4       paf        87: 
1.88.2.6  paf        88:                smtp->Send(server, port, message_cstr, from->cstr(), to->cstr());
1.4       paf        89:        } else
1.60      paf        90:                throw Exception("parser.runtime",
1.88.2.5  paf        91:                        method_name,
1.13      paf        92:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf        93: #else
1.12      paf        94:        // unix
1.70      paf        95:        // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
                     96:        // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i  -f postmaster"] default
1.12      paf        97: 
1.88.2.13! paf        98:        StringPtr sendmail_command;
        !            99:        const char* sendmailkey_cstr="sendmail";
        !           100:        if(vmail_conf) {
1.55      paf       101: #ifdef PA_FORCED_SENDMAIL
1.86      paf       102:                throw Exception("parser.runtime",
1.88.2.5  paf       103:                        method_name,
1.86      paf       104:                        "Parser was configured with --with-sendmail="PA_FORCED_SENDMAIL
1.87      paf       105:                        " key, to change sendmail you should reconfigure and recompie it");
1.55      paf       106: #else
1.88.2.13! paf       107:                if(ValuePtr sendmail_value=vmail_conf->get_hash(method_name)->get(String(pool, sendmailkey_cstr)))
1.52      paf       108:                        sendmail_command=&sendmail_value->as_string();
1.51      paf       109:                else
1.60      paf       110:                        throw Exception("parser.runtime",
1.88.2.5  paf       111:                                method_name,
1.51      paf       112:                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    113:                                sendmailkey_cstr);
1.86      paf       114: #endif
1.51      paf       115:        } else {
1.86      paf       116: #ifdef PA_FORCED_SENDMAIL
1.88.2.13! paf       117:                sendmail_command=StringPtr(new String(PA_FORCED_SENDMAIL));
1.86      paf       118: #else
1.88.2.13! paf       119:                StringPtr test(new String("/usr/sbin/sendmail");
1.52      paf       120:                if(!file_executable(*test))
1.88.2.13! paf       121:                        test=StringPtr(new String("/usr/lib/sendmail"));
1.70      paf       122:                test->APPEND_CONST(" -t -i -f postmaster");
1.52      paf       123:                sendmail_command=test;
1.86      paf       124: #endif
1.51      paf       125:        }
                    126: 
1.70      paf       127:        // we know sendmail_command here, should replace "postmaster" with "$from" from message
                    128:        int at_postmaster=sendmail_command->pos("postmaster");
                    129:        if(at_postmaster>0) {
1.88.2.13! paf       130:                StringPtr reconstructed=sendmail_command->mid(0, at_postmaster);
        !           131:                *reconstructed << *from;
        !           132:                *reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->size());
        !           133:                sendmail_command=reconstructed;
1.70      paf       134:        }
                    135: 
                    136:        // execute it
1.51      paf       137:        Array argv(pool);
1.88.2.13! paf       138:        StringPtr file_spec;
1.52      paf       139:        int after_file_spec=sendmail_command->pos(" ", 1);
1.51      paf       140:        if(after_file_spec<=0)
1.52      paf       141:                file_spec=sendmail_command;
1.51      paf       142:        else {
1.52      paf       143:                size_t pos_after=after_file_spec;
1.88.2.13! paf       144:                file_spec=sendmail_command->mid(0, pos_after++);
1.52      paf       145:                sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS);
1.36      parser    146:        }
1.51      paf       147: 
1.88.2.13! paf       148:        if(!file_executable(file_spec))
1.75      paf       149:                throw Exception("email.send",
1.55      paf       150:                        file_spec, 
                    151:                        "is not executable."
                    152: #ifdef PA_FORCED_SENDMAIL
1.59      paf       153:                        " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58      paf       154: #else
1.70      paf       155:                        " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s to appropriate sendmail command", 
1.55      paf       156:                                sendmailkey_cstr
                    157: #endif
                    158:                );
                    159: 
1.51      paf       160: 
1.88.2.13! paf       161:        String in(message_cstr); String out; String err;
        !           162:        PA_exec_result executed=pa_exec(
        !           163:        //int exit_status
1.56      paf       164:                // forced_allow
                    165: #ifdef PA_FORCED_SENDMAIL
                    166:                true
                    167: #else
                    168:                false
                    169: #endif
1.57      paf       170:                , *file_spec,
1.51      paf       171:                0/*default env*/,
                    172:                &argv,
                    173:                in, out, err);
                    174:        if(exit_status || err.size())
1.75      paf       175:                throw Exception("email.send",
1.88.2.5  paf       176:                        method_name,
1.51      paf       177:                        "'%s' reported problem: %s (%d)",
1.88.2.11  paf       178:                                file_spec->cstr().get(),
                    179:                                err.size()?err.cstr().get():"UNKNOWN", 
1.51      paf       180:                                exit_status);
1.4       paf       181: #endif
                    182: }
                    183: 
1.7       paf       184: // methods
                    185: 
1.88.2.10  paf       186: static void _send(Request& r, StringPtr method_name, MethodParams* params) {
1.1       paf       187:        Pool& pool=r.pool();
                    188: 
1.88.2.10  paf       189:        ValuePtr vhash=params->as_no_junction(0, "message must not be code");
1.88.2.6  paf       190:        HashStringValue* hash=vhash->get_hash(method_name);
1.1       paf       191:        if(!hash)
1.60      paf       192:                throw Exception("parser.runtime",
1.88.2.5  paf       193:                        method_name,
1.1       paf       194:                        "message must be hash");
                    195: 
1.88.2.6  paf       196:        StringPtr from(0);
                    197:        StringPtr to(0);
                    198:        StringPtr message=
                    199:                GET_SELF(r, VMail).message_hash_to_string(r, method_name, hash, 0, from, to);
1.1       paf       200: 
1.67      paf       201:        //r.write_pass_lang(message);
                    202:        sendmail(r, method_name, message, from, to);
1.6       paf       203: }
                    204: 
1.24      paf       205: // constructor & configurator
1.23      paf       206: 
1.88.2.6  paf       207: MMail::MMail(): Methoded(MAIL_CLASS_NAME) {
1.27      paf       208:        // ^mail:send{hash}
1.23      paf       209:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68      paf       210: }
                    211: 
                    212: void MMail::configure_user(Request& r) {
                    213:        Pool& pool=r.pool();
                    214: 
                    215:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.88.2.6  paf       216:        if(ValuePtr mail_element=r.main_class->get_element(mail_name, *r.main_class, false))
                    217:                if(mail_element->get_hash(Exception::undefined_source))
                    218:                        r.classes_conf.put(name(), mail_element);
1.68      paf       219:                else
1.69      paf       220:                        if( !mail_element->is_string() )
                    221:                                throw Exception("parser.runtime",
1.88.2.6  paf       222:                                        Exception::undefined_source,
1.69      paf       223:                                        "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.1       paf       224: }

E-mail: