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

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

E-mail: