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

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

E-mail: