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

1.1       paf         1: /** @file
                      2:        Parser: @b mail parser class.
                      3: 
1.100     paf         4:        Copyright (c) 2001-2004 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.105   ! paf         8: static const char * const IDENT_MAIL_C="$Date: 2004/05/11 15:03:49 $";
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.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.89      paf        65:                     const String& message, 
1.101     paf        66:                     const String* from, const String* to,
1.93      paf        67:                         const String* 
                     68: #ifndef _MSC_VER
                     69:                         options
                     70: #endif
                     71:                         ) {
1.89      paf        72:        const char* message_cstr=message.cstr(String::L_UNSPECIFIED);
1.2       paf        73: 
1.89      paf        74:        const char* exception_type="email.format";
1.85      paf        75:        if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from}
1.75      paf        76:                throw Exception(exception_type,
1.89      paf        77:                        0,
1.70      paf        78:                        "parameter does not specify 'from' header field");
1.104     paf        79:        if(smtp_server_port) {
                     80:                if(!to) // we use only in SMTP RCPT to: {to}
                     81:                        throw Exception(exception_type,
                     82:                                0,
                     83:                                "parameter does not specify 'to' header field");
1.4       paf        84: 
1.101     paf        85:                SMTP smtp;
                     86:                char* server=smtp_server_port->as_string().cstrm();
1.89      paf        87:                const char* port=rsplit(server, ':');
1.4       paf        88:                if(!port)
1.11      paf        89:                        port="25";
1.4       paf        90: 
1.89      paf        91:                smtp.Send(server, port, message_cstr, from->cstrm(), to->cstrm());
1.101     paf        92:                return;
                     93:        }
                     94: 
                     95: #if WIN32
                     96:        // win32 without SMTP server configured
                     97:        throw Exception("parser.runtime",
                     98:                0,
                     99:                "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       100: #else
1.12      paf       101:        // unix
1.70      paf       102:        // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
                    103:        // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i  -f postmaster"] default
1.12      paf       104: 
1.92      paf       105:        String* sendmail_command=new String;
1.89      paf       106:        if(vmail_conf) {
1.55      paf       107: #ifdef PA_FORCED_SENDMAIL
1.86      paf       108:                throw Exception("parser.runtime",
1.89      paf       109:                        0,
1.86      paf       110:                        "Parser was configured with --with-sendmail="PA_FORCED_SENDMAIL
1.87      paf       111:                        " key, to change sendmail you should reconfigure and recompie it");
1.55      paf       112: #else
1.89      paf       113:                if(Value* sendmail_value=vmail_conf->get_hash()->get(mail_sendmail_name))
1.92      paf       114:                        *sendmail_command<<sendmail_value->as_string();
1.51      paf       115:                else
1.60      paf       116:                        throw Exception("parser.runtime",
1.89      paf       117:                                0,
1.91      paf       118:                                "$"MAIN_CLASS_NAME":"MAIL_NAME"."SENDMAIL_NAME" not defined");
1.86      paf       119: #endif
1.51      paf       120:        } else {
1.86      paf       121: #ifdef PA_FORCED_SENDMAIL
1.96      paf       122:                *sendmail_command<<PA_FORCED_SENDMAIL;
1.86      paf       123: #else
1.89      paf       124:                String* test=new String("/usr/sbin/sendmail");
1.52      paf       125:                if(!file_executable(*test))
1.89      paf       126:                        test=new String("/usr/lib/sendmail");
1.92      paf       127:                *sendmail_command<<*test;
                    128:                *sendmail_command<<" -t -i -f postmaster";
1.86      paf       129: #endif
1.51      paf       130:        }
1.91      paf       131:        if(options)
1.92      paf       132:                *sendmail_command<<" "<<*options;
1.51      paf       133: 
1.70      paf       134:        // we know sendmail_command here, should replace "postmaster" with "$from" from message
1.89      paf       135:        size_t at_postmaster=sendmail_command->pos("postmaster");
                    136:        if(at_postmaster!=STRING_NOT_FOUND) {
1.70      paf       137:                String& reconstructed=sendmail_command->mid(0, at_postmaster);
1.71      paf       138:                reconstructed << *from;
1.89      paf       139:                reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->length());
1.70      paf       140:                sendmail_command=&reconstructed;
                    141:        }
                    142: 
                    143:        // execute it
1.89      paf       144:        ArrayString argv;
                    145:        const String* file_spec;
                    146:        size_t after_file_spec=sendmail_command->pos(' ');
                    147:        if(after_file_spec==STRING_NOT_FOUND || after_file_spec==0)
1.52      paf       148:                file_spec=sendmail_command;
1.51      paf       149:        else {
1.52      paf       150:                size_t pos_after=after_file_spec;
                    151:                file_spec=&sendmail_command->mid(0, pos_after++);
1.89      paf       152:                sendmail_command->split(argv, pos_after, " ", String::L_AS_IS);
1.36      parser    153:        }
1.51      paf       154: 
1.52      paf       155:        if(!file_executable(*file_spec))
1.75      paf       156:                throw Exception("email.send",
1.55      paf       157:                        file_spec, 
                    158:                        "is not executable."
                    159: #ifdef PA_FORCED_SENDMAIL
1.59      paf       160:                        " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58      paf       161: #else
1.91      paf       162:                        " Set $"MAIN_CLASS_NAME":"MAIL_NAME"."SENDMAIL_NAME" to appropriate sendmail command"
1.55      paf       163: #endif
                    164:                );
                    165: 
1.51      paf       166: 
1.95      paf       167:        String in(message_cstr);
1.89      paf       168:        PA_exec_result exec=pa_exec(
1.56      paf       169:                // forced_allow
                    170: #ifdef PA_FORCED_SENDMAIL
                    171:                true
                    172: #else
                    173:                false
                    174: #endif
1.57      paf       175:                , *file_spec,
1.89      paf       176:                0 /* pass env */,
                    177:                argv,
                    178:                in);
                    179:        if(exec.status || exec.err.length())
1.75      paf       180:                throw Exception("email.send",
1.89      paf       181:                        0,
1.51      paf       182:                        "'%s' reported problem: %s (%d)",
                    183:                                file_spec->cstr(),
1.89      paf       184:                                exec.err.length()?exec.err.cstr():"UNKNOWN", 
                    185:                                exec.status);
1.4       paf       186: #endif
                    187: }
                    188: 
1.7       paf       189: // methods
                    190: 
1.89      paf       191: static void _send(Request& r, MethodParams& params) {
                    192:        Value& vhash=params.as_no_junction(0, "message must not be code");
                    193:        HashStringValue* hash=vhash.get_hash();
1.1       paf       194:        if(!hash)
1.60      paf       195:                throw Exception("parser.runtime",
1.89      paf       196:                        0,
1.1       paf       197:                        "message must be hash");
                    198: 
1.91      paf       199:        const String* soptions=0;
                    200:        if(Value* voptions=hash->get(MAIL_OPTIONS_NAME))
                    201:                soptions=&voptions->as_string();
                    202: 
1.104     paf       203:        Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_base_class->name()));
                    204:        Value* smtp_server_port=0;
                    205:        if(vmail_conf) {
                    206:                // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
                    207:                smtp_server_port=vmail_conf->get_hash()->get(String::Body("SMTP"));
                    208:        }
                    209: 
                    210: 
1.89      paf       211:        const String* from=0;
                    212:        String* to=0;
                    213:        const String& message=
1.104     paf       214:                GET_SELF(r, VMail).message_hash_to_string(r, hash, 0, from, 
                    215:                        smtp_server_port?true:false /*send by SMTP=strip to?*/, to);
1.1       paf       216: 
1.105   ! paf       217:        r.write_pass_lang(message);
        !           218:        /*sendmail(vmail_conf, smtp_server_port, 
        !           219:                message, from, to, soptions);*/
1.6       paf       220: }
                    221: 
1.24      paf       222: // constructor & configurator
1.23      paf       223: 
1.89      paf       224: MMail::MMail(): Methoded(MAIL_CLASS_NAME) {
1.27      paf       225:        // ^mail:send{hash}
1.23      paf       226:        add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68      paf       227: }
                    228: 
                    229: void MMail::configure_user(Request& r) {
                    230: 
                    231:        // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.89      paf       232:        if(Value* mail_element=r.main_class.get_element(mail_name, r.main_class, false))
                    233:                if(mail_element->get_hash())
                    234:                        r.classes_conf.put(name(), mail_element);
1.68      paf       235:                else
1.69      paf       236:                        if( !mail_element->is_string() )
                    237:                                throw Exception("parser.runtime",
                    238:                                        0,
                    239:                                        "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.1       paf       240: }

E-mail: