|
|
| version 1.88.2.6, 2003/02/06 16:05:23 | version 1.134, 2020/05/22 21:14:01 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b mail parser class. | Parser: @b mail parser class. |
| Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_MAIL_C="$Date$"; | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "pa_vmethod_frame.h" | #include "pa_vmethod_frame.h" |
| Line 17 static const char* IDENT_MAIL_C="$Date$" | Line 15 static const char* IDENT_MAIL_C="$Date$" |
| #include "pa_charsets.h" | #include "pa_charsets.h" |
| #include "pa_charset.h" | #include "pa_charset.h" |
| #include "pa_uue.h" | #include "pa_uue.h" |
| #include "pa_vmail.h" | |
| #ifdef _MSC_VER | #include "smtp.h" |
| # include "smtp/smtp.h" | |
| #endif | volatile const char * IDENT_MAIL_C="$Id$"; |
| // defines | // defines |
| #define MAIL_CLASS_NAME "mail" | #define MAIL_CLASS_NAME "mail" |
| #define SENDMAIL_NAME "sendmail" | |
| // consts | // consts |
| Line 32 const int ATTACHMENT_WEIGHT=100; | Line 32 const int ATTACHMENT_WEIGHT=100; |
| // class | // class |
| class MMail : public Methoded { | class MMail: public Methoded { |
| public: // Methoded | public: // Methoded |
| bool used_directly() { return false; } | bool used_directly() { return false; } |
| void configure_user(Request& r); | void configure_user(Request& r); |
| Line 43 public: | Line 43 public: |
| // global variable | // global variable |
| MethodedPtr mail_base_class(new MMail); | DECLARE_CLASS_VAR(mail, new MMail); |
| // defines for statics | // defines for statics |
| Line 51 MethodedPtr mail_base_class(new MMail); | Line 51 MethodedPtr mail_base_class(new MMail); |
| // statics | // statics |
| static StringPtr mail_name(new String(MAIL_NAME)); | static const String mail_name(MAIL_NAME); |
| static const String mail_sendmail_name(SENDMAIL_NAME); | |
| // helpers | // helpers |
| static void sendmail(Request& r, StringPtr method_name, | static void sendmail( |
| StringPtr message, | Value* |
| StringPtr from, StringPtr to) { | #ifndef WIN32 |
| Pool& pool=r.pool(); | vmail_conf |
| #endif | |
| , Value* smtp_server_port, | |
| const String& message, | |
| const String* from, | |
| const String* to, | |
| const String* | |
| #ifndef WIN32 | |
| options | |
| #endif | |
| ) { | |
| const char* exception_type="email.format"; | |
| if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from} | |
| throw Exception(exception_type, 0, "parameter does not specify 'from' header field"); | |
| const char* message_cstr=message.untaint_cstr(String::L_AS_IS); | |
| CharPtr message_cstr=message->cstr(String::UL_UNSPECIFIED); | if(smtp_server_port) { |
| ValuePtr vmail_conf=r.classes_conf.get(mail_base_class->name()); | if(!to) // we use only in SMTP RCPT to: {to} |
| throw Exception(exception_type, 0, "parameter does not specify 'to' header field"); | |
| const char *exception_type="email.format"; | SMTP smtp; |
| if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from} | char* server=smtp_server_port->as_string().cstrm(); |
| throw Exception(exception_type, | const char* port=rsplit(server, ':'); |
| method_name, | |
| "parameter does not specify 'from' header field"); | |
| #ifdef _MSC_VER | |
| if(!to) // we use only in SMTP RCPT to: {to} | |
| throw Exception(exception_type, | |
| method_name, | |
| "parameter does not specify 'to' header field"); | |
| SMTPPtr smtp(new SMTP(method_name)); | |
| ValuePtr server_port; | |
| // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]] | |
| if(vmail_conf && | |
| (server_port=vmail_conf->get_hash(method_name)->get(StringPtr(new String("SMTP"))))) { | |
| CharPtr server=server_port->as_string(&pool)->cstr(); | |
| const char *port=rsplit(server, ':'); | |
| if(!port) | if(!port) |
| port="25"; | port="25"; |
| smtp->Send(server, port, message_cstr, from->cstr(), to->cstr()); | smtp.Send(server, port, message_cstr, from->cstrm(), to->cstrm()); |
| } else | return; |
| throw Exception("parser.runtime", | } |
| method_name, | |
| "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined"); | #ifdef WIN32 |
| // win32 without SMTP server configured | |
| throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" MAIL_NAME ".SMTP not defined"); | |
| #else | #else |
| // unix | // unix |
| // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default | // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default |
| // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default | // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default |
| const String *sendmail_command; | String* sendmail_command=new String; |
| const char *sendmailkey_cstr="sendmail"; | if(vmail_conf) { |
| if(mail_conf) { | |
| #ifdef PA_FORCED_SENDMAIL | #ifdef PA_FORCED_SENDMAIL |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| method_name, | 0, |
| "Parser was configured with --with-sendmail="PA_FORCED_SENDMAIL | "Parser was configured with --with-sendmail=" PA_FORCED_SENDMAIL |
| " key, to change sendmail you should reconfigure and recompie it"); | " key, to change sendmail you should reconfigure and recompie it"); |
| #else | #else |
| if(Value *sendmail_value=static_cast<Value *>(mail_conf->get(String(pool, sendmailkey_cstr)))) | if(Value* sendmail_value=vmail_conf->get_hash()->get(mail_sendmail_name)) |
| sendmail_command=&sendmail_value->as_string(); | *sendmail_command<<sendmail_value->as_string(); |
| else | else |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " not defined"); |
| method_name, | |
| "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", | |
| sendmailkey_cstr); | |
| #endif | #endif |
| } else { | } else { |
| #ifdef PA_FORCED_SENDMAIL | #ifdef PA_FORCED_SENDMAIL |
| sendmail_command=new(pool) String(pool, PA_FORCED_SENDMAIL); | *sendmail_command<<PA_FORCED_SENDMAIL; |
| #else | #else |
| String *test=new(pool) String(pool, "/usr/sbin/sendmail"); | String* test=new String("/usr/sbin/sendmail"); |
| if(!file_executable(*test)) | if(!file_executable(*test)) |
| test=new(pool) String(pool, "/usr/lib/sendmail"); | test=new String("/usr/lib/sendmail"); |
| test->APPEND_CONST(" -t -i -f postmaster"); | *sendmail_command<<*test; |
| sendmail_command=test; | *sendmail_command<<" -t -i -f postmaster"; |
| #endif | #endif |
| } | } |
| if(options) | |
| *sendmail_command<<" "<<*options; | |
| // we know sendmail_command here, should replace "postmaster" with "$from" from message | // we know sendmail_command here, should replace "postmaster" with "$from" from message |
| int at_postmaster=sendmail_command->pos("postmaster"); | size_t at_postmaster=sendmail_command->pos("postmaster"); |
| if(at_postmaster>0) { | if(at_postmaster!=STRING_NOT_FOUND) { |
| String& reconstructed=sendmail_command->mid(0, at_postmaster); | String& reconstructed=sendmail_command->mid(0, at_postmaster); |
| reconstructed << *from; | reconstructed << *from; |
| reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->size()); | reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->length()); |
| sendmail_command=&reconstructed; | sendmail_command=&reconstructed; |
| } | } |
| // execute it | // execute it |
| Array argv(pool); | ArrayString argv; |
| const String *file_spec; | const String* file_spec; |
| int after_file_spec=sendmail_command->pos(" ", 1); | size_t after_file_spec=sendmail_command->pos(' '); |
| if(after_file_spec<=0) | if(after_file_spec==STRING_NOT_FOUND || after_file_spec==0) |
| file_spec=sendmail_command; | file_spec=sendmail_command; |
| else { | else { |
| size_t pos_after=after_file_spec; | file_spec=&sendmail_command->mid(0, after_file_spec); |
| file_spec=&sendmail_command->mid(0, pos_after++); | sendmail_command->split(argv, after_file_spec+1, " ", String::L_AS_IS); |
| sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS); | |
| } | } |
| if(!file_executable(*file_spec)) | if(!file_executable(*file_spec)) |
| Line 152 static void sendmail(Request& r, StringP | Line 152 static void sendmail(Request& r, StringP |
| #ifdef PA_FORCED_SENDMAIL | #ifdef PA_FORCED_SENDMAIL |
| " Use configure key \"--with-sendmail=appropriate sendmail command\"" | " Use configure key \"--with-sendmail=appropriate sendmail command\"" |
| #else | #else |
| " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s to appropriate sendmail command", | " Set $" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " to appropriate sendmail command" |
| sendmailkey_cstr | |
| #endif | #endif |
| ); | ); |
| PA_exec_result exec=pa_exec( | |
| String in(pool, message_cstr); String out(pool); String err(pool); | |
| int exit_status=pa_exec( | |
| // forced_allow | // forced_allow |
| #ifdef PA_FORCED_SENDMAIL | #ifdef PA_FORCED_SENDMAIL |
| true | true |
| Line 167 static void sendmail(Request& r, StringP | Line 164 static void sendmail(Request& r, StringP |
| false | false |
| #endif | #endif |
| , *file_spec, | , *file_spec, |
| 0/*default env*/, | 0 /* pass env */, |
| &argv, | argv, |
| in, out, err); | String::C(message_cstr, strlen(message_cstr))); |
| if(exit_status || err.size()) | |
| throw Exception("email.send", | if(exec.status || exec.err.length()) |
| method_name, | throw Exception("email.send", 0, "'%s' reported problem: %s (%d)", file_spec->cstr(), exec.err.length() ? exec.err.cstr() : "UNKNOWN", exec.status); |
| "'%s' reported problem: %s (%d)", | #endif //WIN32 |
| file_spec->cstr(), | |
| err.size()?err.cstr():"UNKNOWN", | |
| exit_status); | |
| #endif | |
| } | } |
| // methods | // methods |
| static void _send(Request& r, StringPtr method_name, MethodParams& params) { | static void _send(Request& r, MethodParams& params) { |
| Pool& pool=r.pool(); | HashStringValue* hash=params.as_hash(0, "message"); |
| if(!hash || !hash->count()) | |
| ValuePtr vhash=params.as_no_junction(0, "message must not be code"); | return; |
| HashStringValue* hash=vhash->get_hash(method_name); | // todo@ check if enough options are specified. |
| if(!hash) | // now ^mail:send[^hash::create[]] and ^mail:send[$.print-debug(1)] "work". |
| throw Exception("parser.runtime", | |
| method_name, | const String* soptions=0; |
| "message must be hash"); | if(Value* voptions=hash->get(MAIL_OPTIONS_NAME)) |
| soptions=&voptions->as_string(); | |
| StringPtr from(0); | |
| StringPtr to(0); | bool print_debug=false; |
| StringPtr message= | if(Value* vdebug=hash->get(MAIL_DEBUG_NAME)) |
| GET_SELF(r, VMail).message_hash_to_string(r, method_name, hash, 0, from, to); | print_debug=vdebug->as_bool(); |
| Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_class->type())); | |
| Value* smtp_server_port=0; | |
| if(vmail_conf) { | |
| // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]] | |
| smtp_server_port=vmail_conf->get_hash()->get("SMTP"); | |
| } | |
| //r.write_pass_lang(message); | const String* from=0; |
| sendmail(r, method_name, message, from, to); | String* to=0; |
| const String& message = GET_SELF(r, VMail).message_hash_to_string(r, hash, from, smtp_server_port ? true : false /*send by SMTP=strip to?*/, to); | |
| if(print_debug) | |
| r.write(message); | |
| else | |
| sendmail(vmail_conf, smtp_server_port, message, from, to, soptions); | |
| } | } |
| // constructor & configurator | // constructor & configurator |
| Line 209 MMail::MMail(): Methoded(MAIL_CLASS_NAME | Line 215 MMail::MMail(): Methoded(MAIL_CLASS_NAME |
| } | } |
| void MMail::configure_user(Request& r) { | void MMail::configure_user(Request& r) { |
| Pool& pool=r.pool(); | |
| // $MAIN:MAIL[$SMTP[mail.design.ru]] | // $MAIN:MAIL[$SMTP[mail.design.ru]] |
| if(ValuePtr mail_element=r.main_class->get_element(mail_name, *r.main_class, false)) | if(Value* mail_element=r.main_class.get_element(mail_name)) { |
| if(mail_element->get_hash(Exception::undefined_source)) | if(mail_element->get_hash()) |
| r.classes_conf.put(name(), mail_element); | r.classes_conf.put(type(), mail_element); |
| else | else |
| if( !mail_element->is_string() ) | if( !mail_element->is_string() ) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, 0, "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash"); |
| Exception::undefined_source, | } |
| "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash"); | |
| } | } |