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

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

E-mail: