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

1.1       paf         1: /** @file
                      2:        Parser: @b mail parser class.
                      3: 
1.125   ! moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (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: 
                      8: #include "pa_config_includes.h"
1.89      paf         9: #include "pa_vmethod_frame.h"
1.1       paf        10: 
                     11: #include "pa_common.h"
                     12: #include "pa_request.h"
1.6       paf        13: #include "pa_vfile.h"
1.12      paf        14: #include "pa_exec.h"
1.45      paf        15: #include "pa_charsets.h"
                     16: #include "pa_charset.h"
1.67      paf        17: #include "pa_uue.h"
1.89      paf        18: #include "pa_vmail.h"
1.50      paf        19: 
1.103     paf        20: #include "smtp.h"
1.4       paf        21: 
1.125   ! moko       22: volatile const char * IDENT_MAIL_C="$Id: mail.C,v 1.124 2015/10/09 11:42:38 moko Exp $";
1.118     moko       23: 
1.23      paf        24: // defines
1.1       paf        25: 
1.23      paf        26: #define MAIL_CLASS_NAME "mail"
1.91      paf        27: #define SENDMAIL_NAME "sendmail"
1.25      paf        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.89      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.89      paf        39: 
                     40: public:
                     41:        MMail();
1.23      paf        42: };
1.1       paf        43: 
1.89      paf        44: // global variable
1.7       paf        45: 
1.89      paf        46: DECLARE_CLASS_VAR(mail, 0/*fictive*/, new MMail);
1.7       paf        47: 
1.89      paf        48: // defines for statics
1.1       paf        49: 
1.89      paf        50: #define MAIL_NAME "MAIL"
1.70      paf        51: 
1.89      paf        52: // statics
                     53:        
                     54: static const String mail_name(MAIL_NAME);
1.91      paf        55: static const String mail_sendmail_name(SENDMAIL_NAME);
1.22      paf        56: 
1.89      paf        57: // helpers
1.47      paf        58: 
1.104     paf        59: static void sendmail(
                     60:                        Value* 
                     61: #ifndef WIN32
                     62:                        vmail_conf
                     63: #endif
                     64:                        , Value* smtp_server_port,
1.114     misha      65:                        const String& message, 
                     66:                        const String* from,
                     67:                        const String* to,
                     68:                        const String* 
1.121     moko       69: #ifndef WIN32
1.93      paf        70:                         options
                     71: #endif
                     72:                         ) {
1.89      paf        73:        const char* exception_type="email.format";
1.85      paf        74:        if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from}
1.75      paf        75:                throw Exception(exception_type,
1.89      paf        76:                        0,
1.70      paf        77:                        "parameter does not specify 'from' header field");
1.116     misha      78: 
                     79:        const char* message_cstr=message.untaint_cstr(String::L_AS_IS);
                     80: 
1.104     paf        81:        if(smtp_server_port) {
                     82:                if(!to) // we use only in SMTP RCPT to: {to}
                     83:                        throw Exception(exception_type,
                     84:                                0,
                     85:                                "parameter does not specify 'to' header field");
1.4       paf        86: 
1.101     paf        87:                SMTP smtp;
                     88:                char* server=smtp_server_port->as_string().cstrm();
1.89      paf        89:                const char* port=rsplit(server, ':');
1.4       paf        90:                if(!port)
1.11      paf        91:                        port="25";
1.4       paf        92: 
1.89      paf        93:                smtp.Send(server, port, message_cstr, from->cstrm(), to->cstrm());
1.101     paf        94:                return;
                     95:        }
                     96: 
1.120     moko       97: #ifdef WIN32
1.101     paf        98:        // win32 without SMTP server configured
1.111     misha      99:        throw Exception(PARSER_RUNTIME,
1.101     paf       100:                0,
                    101:                "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       102: #else
1.12      paf       103:        // unix
1.70      paf       104:        // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
                    105:        // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i  -f postmaster"] default
1.12      paf       106: 
1.92      paf       107:        String* sendmail_command=new String;
1.89      paf       108:        if(vmail_conf) {
1.55      paf       109: #ifdef PA_FORCED_SENDMAIL
1.111     misha     110:                throw Exception(PARSER_RUNTIME,
1.89      paf       111:                        0,
1.122     moko      112:                        "Parser was configured with --with-sendmail=" PA_FORCED_SENDMAIL
1.87      paf       113:                        " key, to change sendmail you should reconfigure and recompie it");
1.55      paf       114: #else
1.89      paf       115:                if(Value* sendmail_value=vmail_conf->get_hash()->get(mail_sendmail_name))
1.92      paf       116:                        *sendmail_command<<sendmail_value->as_string();
1.51      paf       117:                else
1.111     misha     118:                        throw Exception(PARSER_RUNTIME,
1.89      paf       119:                                0,
1.122     moko      120:                                "$" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " not defined");
1.86      paf       121: #endif
1.51      paf       122:        } else {
1.86      paf       123: #ifdef PA_FORCED_SENDMAIL
1.96      paf       124:                *sendmail_command<<PA_FORCED_SENDMAIL;
1.86      paf       125: #else
1.89      paf       126:                String* test=new String("/usr/sbin/sendmail");
1.52      paf       127:                if(!file_executable(*test))
1.89      paf       128:                        test=new String("/usr/lib/sendmail");
1.92      paf       129:                *sendmail_command<<*test;
                    130:                *sendmail_command<<" -t -i -f postmaster";
1.86      paf       131: #endif
1.51      paf       132:        }
1.91      paf       133:        if(options)
1.92      paf       134:                *sendmail_command<<" "<<*options;
1.51      paf       135: 
1.70      paf       136:        // we know sendmail_command here, should replace "postmaster" with "$from" from message
1.89      paf       137:        size_t at_postmaster=sendmail_command->pos("postmaster");
                    138:        if(at_postmaster!=STRING_NOT_FOUND) {
1.70      paf       139:                String& reconstructed=sendmail_command->mid(0, at_postmaster);
1.71      paf       140:                reconstructed << *from;
1.89      paf       141:                reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->length());
1.70      paf       142:                sendmail_command=&reconstructed;
                    143:        }
                    144: 
                    145:        // execute it
1.89      paf       146:        ArrayString argv;
                    147:        const String* file_spec;
                    148:        size_t after_file_spec=sendmail_command->pos(' ');
                    149:        if(after_file_spec==STRING_NOT_FOUND || after_file_spec==0)
1.52      paf       150:                file_spec=sendmail_command;
1.51      paf       151:        else {
1.52      paf       152:                size_t pos_after=after_file_spec;
                    153:                file_spec=&sendmail_command->mid(0, pos_after++);
1.89      paf       154:                sendmail_command->split(argv, pos_after, " ", String::L_AS_IS);
1.36      parser    155:        }
1.51      paf       156: 
1.52      paf       157:        if(!file_executable(*file_spec))
1.75      paf       158:                throw Exception("email.send",
1.55      paf       159:                        file_spec, 
                    160:                        "is not executable."
                    161: #ifdef PA_FORCED_SENDMAIL
1.59      paf       162:                        " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58      paf       163: #else
1.122     moko      164:                        " Set $" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " to appropriate sendmail command"
1.55      paf       165: #endif
                    166:                );
                    167: 
1.51      paf       168: 
1.95      paf       169:        String in(message_cstr);
1.89      paf       170:        PA_exec_result exec=pa_exec(
1.56      paf       171:                // forced_allow
                    172: #ifdef PA_FORCED_SENDMAIL
                    173:                true
                    174: #else
                    175:                false
                    176: #endif
1.57      paf       177:                , *file_spec,
1.89      paf       178:                0 /* pass env */,
                    179:                argv,
                    180:                in);
                    181:        if(exec.status || exec.err.length())
1.75      paf       182:                throw Exception("email.send",
1.89      paf       183:                        0,
1.51      paf       184:                        "'%s' reported problem: %s (%d)",
                    185:                                file_spec->cstr(),
1.89      paf       186:                                exec.err.length()?exec.err.cstr():"UNKNOWN", 
                    187:                                exec.status);
1.121     moko      188: #endif //WIN32
1.4       paf       189: }
                    190: 
1.7       paf       191: // methods
                    192: 
1.89      paf       193: static void _send(Request& r, MethodParams& params) {
1.119     misha     194:        HashStringValue* hash=params.as_hash(0, "message");
                    195:        if(!hash || !hash->count())
                    196:                return;
                    197:        // todo@ check if enough options are specified.
                    198:        // now ^mail:send[^hash::create[]] and ^mail:send[$.print-debug(1)] "work".
1.1       paf       199: 
1.91      paf       200:        const String* soptions=0;
                    201:        if(Value* voptions=hash->get(MAIL_OPTIONS_NAME))
                    202:                soptions=&voptions->as_string();
                    203: 
1.115     misha     204:        bool print_debug=false;
                    205:        if(Value* vdebug=hash->get(MAIL_DEBUG_NAME))
                    206:                print_debug=vdebug->as_bool();
                    207: 
1.104     paf       208:        Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_base_class->name()));
                    209:        Value* smtp_server_port=0;
                    210:        if(vmail_conf) {
                    211:                // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.124     moko      212:                smtp_server_port=vmail_conf->get_hash()->get("SMTP");
1.104     paf       213:        }
                    214: 
                    215: 
1.89      paf       216:        const String* from=0;
                    217:        String* to=0;
                    218:        const String& message=
1.104     paf       219:                GET_SELF(r, VMail).message_hash_to_string(r, hash, 0, from, 
                    220:                        smtp_server_port?true:false /*send by SMTP=strip to?*/, to);
1.1       paf       221: 
1.115     misha     222:        if(print_debug)
                    223:                r.write_pass_lang(message);
                    224:        else
                    225:                sendmail(vmail_conf, smtp_server_port, message, from, to, soptions);
1.6       paf       226: }
                    227: 
1.24      paf       228: // constructor & configurator
1.23      paf       229: 
1.89      paf       230: MMail::MMail(): Methoded(MAIL_CLASS_NAME) {
1.27      paf       231:        // ^mail:send{hash}
1.23      paf       232:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68      paf       233: }
                    234: 
                    235: void MMail::configure_user(Request& r) {
                    236: 
                    237:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.123     moko      238:        if(Value* mail_element=r.main_class.get_element(mail_name)) {
1.89      paf       239:                if(mail_element->get_hash())
                    240:                        r.classes_conf.put(name(), mail_element);
1.68      paf       241:                else
1.69      paf       242:                        if( !mail_element->is_string() )
1.111     misha     243:                                throw Exception(PARSER_RUNTIME,
1.69      paf       244:                                        0,
                    245:                                        "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.123     moko      246:        }
1.1       paf       247: }

E-mail: