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

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

E-mail: