--- parser3/src/classes/mail.C 2001/12/25 09:00:28 1.49 +++ parser3/src/classes/mail.C 2002/04/18 10:50:59 1.63 @@ -1,18 +1,14 @@ /** @file Parser: @b mail parser class. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: mail.C,v 1.49 2001/12/25 09:00:28 paf Exp $ + $Id: mail.C,v 1.63 2002/04/18 10:50:59 paf Exp $ */ #include "pa_config_includes.h" -#ifdef _MSC_VER -# include "smtp/smtp.h" -#endif - #include "pa_common.h" #include "pa_request.h" #include "pa_vfile.h" @@ -20,6 +16,10 @@ #include "pa_charsets.h" #include "pa_charset.h" +#ifdef _MSC_VER +# include "smtp/smtp.h" +#endif + // defines #define MAIL_CLASS_NAME "mail" @@ -137,7 +137,7 @@ static const String& attach_hash_to_stri if(Value *value=static_cast(attach_hash.get(*value_name))) vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve else - throw Exception(0, 0, + throw Exception("parser.runtime", &origin_string, "has no $value"); @@ -160,7 +160,7 @@ static const String& attach_hash_to_stri if(!type/*default = uue*/ || *type=="uue") { uuencode(result, file_name_cstr, *vfile); } else // for now - throw Exception(0, 0, + throw Exception("parser.runtime", type, "unknown attachment encode format"); @@ -170,7 +170,7 @@ static const String& attach_hash_to_stri #ifndef DOXYGEN struct Mail_info { - Charset *charset; + Charset *charset; const char *content_charset_name; String *header; const String **from, **to; }; @@ -196,7 +196,7 @@ static void add_header_attribute(const H *mi.header << aattribute << ":" << attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER). - cstr(String::UL_UNSPECIFIED, 0, mi.charset) << + cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) << "\n"; } @@ -216,7 +216,7 @@ static int get_part_name_weight(const Ha cstr+=6; offset=ATTACHMENT_WEIGHT; } else - throw Exception(0, 0, + throw Exception("parser.runtime", &part_name, "is neither text# nor attach#"); @@ -246,17 +246,23 @@ static const String& letter_hash_to_stri String& result=*new(pool) String(pool); Charset *charset; - if(Value *vcharset_name=static_cast(letter_hash.get(*charset_name))) - charset=&charsets->get_charset(vcharset_name->as_string()); + if(Value *vrecodecharset_name=static_cast(letter_hash.get(*charset_name))) + charset=&charsets->get_charset(vrecodecharset_name->as_string()); else charset=&pool.get_source_charset(); + const char *content_charset_name=0; + if(Value *vcontent_type=static_cast(letter_hash.get(*content_type_name))) + if(Hash *hcontent_type=vcontent_type->get_hash(0)) + if(Value *vcontentcharset_name=static_cast(hcontent_type->get(*charset_name))) + content_charset_name=vcontentcharset_name->as_string().cstr(); + if(from) *from=0; if(to) *to=0; Mail_info mail_info={ - charset, + charset, content_charset_name, &result, from, to }; @@ -298,7 +304,7 @@ static const String& letter_hash_to_stri result << letter_hash_to_string(r, method_name, *part_hash, level+1, 0, 0); else - throw Exception(0, 0, + throw Exception("parser.runtime", seq[i].name, "part is not hash"); } @@ -322,7 +328,7 @@ static const String& letter_hash_to_stri result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0); } } else - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "has no $body"); @@ -339,11 +345,11 @@ static void sendmail(Request& r, const S #ifdef _MSC_VER if(!from) - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "has no 'from' header specified"); if(!to) - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "has no 'to' header specified"); @@ -360,72 +366,81 @@ static void sendmail(Request& r, const S smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr()); } else - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined"); #else // unix - // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default - // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default - char no_cstr[MAX_NUMBER]; - for(int no=-2; ; no++) { - const String *prog_string; - switch(no) { - case -2: prog_string=new(pool) String(pool, "/usr/sbin/sendmail -t"); break; - case -1: prog_string=new(pool) String(pool, "/usr/lib/sendmail -t"); break; - default: - { - String prog_key(pool, "prog"); - snprintf(no_cstr, MAX_NUMBER, "%d", no); - prog_key << no_cstr; - if(mail_conf) { - if(Value *prog_value=static_cast(mail_conf->get(prog_key))) - prog_string=&prog_value->as_string(); - else - if(no==0) - continue; - else - throw Exception(0, 0, - &method_name, - "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", - prog_key.cstr()); - } else - throw Exception(0, 0, - &method_name, - "$" MAIN_CLASS_NAME ":" MAIL_NAME " not defined"); - } - } - // we know prog_string here - Array argv(pool); - const String *file_spec; - int after_file_spec=prog_string->pos(" ", 1); - if(after_file_spec<=0) - file_spec=prog_string; - else { - size_t pos_after=after_file_spec; - file_spec=&prog_string->mid(0, pos_after); - prog_string->split(argv, &pos_after, " ", 1, String::UL_AS_IS); - } + // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default + // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t"] default - // skip unavailable default programs - if(no<0 && !file_executable(*file_spec)) - continue; - - String in(pool, letter_cstr); String out(pool); String err(pool); - int exit_status=pa_exec(*file_spec, - 0/*default env*/, - &argv, - in, out, err); - if(exit_status || err.size()) - throw Exception(0, 0, + const String *sendmail_command; +#ifdef PA_FORCED_SENDMAIL + sendmail_command=new(pool) String(pool, PA_FORCED_SENDMAIL); +#else + const char *sendmailkey_cstr="sendmail"; + if(mail_conf) { + if(Value *sendmail_value=static_cast(mail_conf->get(String(pool, sendmailkey_cstr)))) + sendmail_command=&sendmail_value->as_string(); + else + throw Exception("parser.runtime", &method_name, - "'%s' reported problem: %s (%d)", - file_spec->cstr(), - err.size()?err.cstr():"UNKNOWN", - exit_status); - break; + "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", + sendmailkey_cstr); + } else { + String *test=new(pool) String(pool, "/usr/sbin/sendmail"); + if(!file_executable(*test)) + test=new(pool) String(pool, "/usr/lib/sendmail"); + test->APPEND_CONST(" -t"); + sendmail_command=test; } #endif + + // we know sendmail_command here + Array argv(pool); + const String *file_spec; + int after_file_spec=sendmail_command->pos(" ", 1); + if(after_file_spec<=0) + file_spec=sendmail_command; + else { + size_t pos_after=after_file_spec; + file_spec=&sendmail_command->mid(0, pos_after++); + sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS); + } + + if(!file_executable(*file_spec)) + throw Exception(0, + file_spec, + "is not executable." +#ifdef PA_FORCED_SENDMAIL + " Use configure key \"--with-sendmail=appropriate sendmail command\"" +#else + " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s with appropriate sendmail command", + sendmailkey_cstr +#endif + ); + + + String in(pool, letter_cstr); String out(pool); String err(pool); + int exit_status=pa_exec( + // forced_allow +#ifdef PA_FORCED_SENDMAIL + true +#else + false +#endif + , *file_spec, + 0/*default env*/, + &argv, + in, out, err); + if(exit_status || err.size()) + throw Exception(0, + &method_name, + "'%s' reported problem: %s (%d)", + file_spec->cstr(), + err.size()?err.cstr():"UNKNOWN", + exit_status); +#endif } @@ -437,26 +452,23 @@ static void _send(Request& r, const Stri Value& vhash=params->as_no_junction(0, "message must not be code"); Hash *hash=vhash.get_hash(&method_name); if(!hash) - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "message must be hash"); const String *from, *to; const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to); -// r.write_assign_lang(*new(pool) VString(letter)); sendmail(r, method_name, letter, from, to); } // constructor & configurator -MMail::MMail(Pool& apool) : Methoded(apool), +MMail::MMail(Pool& apool) : Methoded(apool, MAIL_CLASS_NAME), mail_name(apool, MAIL_NAME), content_disposition_name(apool, CONTENT_DISPOSITION_NAME), content_disposition_filename_name(apool, CONTENT_DISPOSITION_FILENAME_NAME) { - set_name(*NEW String(pool(), MAIL_CLASS_NAME)); - // ^mail:send{hash} add_native_method("send", Method::CT_STATIC, _send, 1, 1); } @@ -469,7 +481,7 @@ void MMail::configure_user(Request& r) { if(Hash *mail_conf=mail_element->get_hash(0)) r.classes_conf.put(name(), mail_conf); else - throw Exception(0, 0, + throw Exception("parser.runtime", 0, "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash"); }