--- parser3/src/classes/mail.C 2002/06/12 12:50:32 1.65 +++ parser3/src/classes/mail.C 2002/08/08 10:04:38 1.76 @@ -3,10 +3,10 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: mail.C,v 1.65 2002/06/12 12:50:32 paf Exp $ */ +static const char* IDENT_MAIL_C="$Date: 2002/08/08 10:04:38 $"; + #include "pa_config_includes.h" #include "pa_common.h" @@ -15,6 +15,7 @@ #include "pa_exec.h" #include "pa_charsets.h" #include "pa_charset.h" +#include "pa_uue.h" #ifdef _MSC_VER # include "smtp/smtp.h" @@ -26,102 +27,26 @@ #define MAIL_NAME "MAIL" -// global variable - -Methoded *mail_class; - // consts const int ATTACHMENT_WEIGHT=100; +// global variable + +Methoded *mail_base_class; + // class class MMail : public Methoded { public: MMail(Pool& pool); public: // Methoded - bool used_directly() { return true; } + bool used_directly() { return false; } void configure_user(Request& r); private: String mail_name; }; -// helpers - -// uuencode - -static unsigned char uue_table[64] = { - '`', '!', '"', '#', '$', '%', '&', '\'', - '(', ')', '*', '+', ',', '-', '.', '/', - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', ':', ';', '<', '=', '>', '?', - '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', - 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', - 'X', 'Y', 'Z', '[', '\\',']', '^', '_' -}; -static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) { - //header - result << "content-transfer-encoding: x-uuencode\n" << "\n"; - result << "begin 644 " << file_name_cstr << "\n"; - - //body - const unsigned char *in=(const unsigned char *)vfile.value_ptr(); - size_t in_length=vfile.value_size(); - - int count=45; - for(const unsigned char *itemp=in; itemp<(in+in_length); itemp+=count) { - int index; - - if((itemp+count)>(in+in_length)) - count=in_length-(itemp-in); - - char *buf=(char *)result.pool().malloc(MAX_STRING); - char *optr=buf; - - /* - * for UU and XX, encode the number of bytes as first character - */ - *optr++ = uue_table[count]; - - for (index=0; index<=count-3; index+=3) { - *optr++ = uue_table[itemp[index] >> 2]; - *optr++ = uue_table[((itemp[index ] & 0x03) << 4) | (itemp[index+1] >> 4)]; - *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)]; - *optr++ = uue_table[ itemp[index+2] & 0x3f]; - } - - /* - * Special handlitempg for itempcomplete litempes - */ - if (index != count) { - if (count - index == 2) { - *optr++ = uue_table[itemp[index] >> 2]; - *optr++ = uue_table[((itemp[index ] & 0x03) << 4) | - ( itemp[index+1] >> 4)]; - *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)]; - *optr++ = uue_table[0]; - } - else if (count - index == 1) { - *optr++ = uue_table[ itemp[index] >> 2]; - *optr++ = uue_table[(itemp[index] & 0x03) << 4]; - *optr++ = uue_table[0]; - *optr++ = uue_table[0]; - } - } - /* - * end of line - */ - *optr++ = '\n'; - *optr = 0; - result << buf; - } - - //footer - result.APPEND_AS_IS((const char *)uue_table, 1/* one char */, 0, 0) << "\n" - "end\n"; -} - /** ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] @todo solve - bad with html attaches */ @@ -157,7 +82,7 @@ static const String& attach_hash_to_stri const String *type=vformat?&vformat->as_string():0; if(!type/*default = uue*/ || *type=="uue") { - uuencode(result, file_name_cstr, *vfile); + pa_uuencode(result, file_name_cstr, *vfile); } else // for now throw Exception("parser.runtime", type, @@ -172,29 +97,34 @@ struct Mail_info { Charset *charset; const char *content_charset_name; String *header; const String **from, **to; + const String *errors_to; }; #endif -static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, - void *info) { - Value& lmeaning=*static_cast(ameaning); +const String& extractEmail(const String& string); +static void add_header_attribute(const Hash::Key& raw_element_name, Hash::Val *aelement_value, + void *info) { + Value& element_value=*static_cast(aelement_value); + const String& low_element_name=raw_element_name.change_case(raw_element_name.pool(), String::CC_LOWER); Mail_info& mi=*static_cast(info); // exclude one attribute [body] - if(aattribute==BODY_NAME - || aattribute==CHARSET_NAME) + if(low_element_name==BODY_NAME + || low_element_name==CHARSET_NAME) return; // fetch from & to from header for SMTP - if(mi.from && aattribute=="from") - *mi.from=&lmeaning.as_string(); - if(mi.to && aattribute=="to") - *mi.to=&lmeaning.as_string(); + if(mi.from && low_element_name=="from") + *mi.from=&extractEmail(element_value.as_string()); + if(mi.to && low_element_name=="to") + *mi.to=&extractEmail(element_value.as_string()); + if(low_element_name=="errors-to") + mi.errors_to=&extractEmail(element_value.as_string()); // append header line *mi.header << - aattribute << ":" << - attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER). + low_element_name << ":" << + attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER). cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) << "\n"; } @@ -236,8 +166,8 @@ static int key_of_part(const void *item) static int sort_cmp_string_double_value(const void *a, const void *b) { return key_of_part(a)-key_of_part(b); } -static const String& letter_hash_to_string(Request& r, const String& method_name, - Hash& letter_hash, int level, +static const String& message_hash_to_string(Request& r, const String& method_name, + Hash& message_hash, int level, const String **from, const String **to) { Pool& pool=r.pool(); @@ -245,13 +175,13 @@ static const String& letter_hash_to_stri String& result=*new(pool) String(pool); Charset *charset; - if(Value *vrecodecharset_name=static_cast(letter_hash.get(*charset_name))) + if(Value *vrecodecharset_name=static_cast(message_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(Value *vcontent_type=static_cast(message_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(); @@ -265,9 +195,11 @@ static const String& letter_hash_to_stri &result, from, to }; - letter_hash.for_each(add_header_attribute, &mail_info); + message_hash.for_each(add_header_attribute, &mail_info); + if(!mail_info.errors_to) + result << "errors-to: postmaster\n"; // errors-to: default - if(Value *body_element=static_cast(letter_hash.get(*body_name))) { + if(Value *body_element=static_cast(message_hash.get(*body_name))) { if(Hash *body_hash=body_element->get_hash(&method_name)) { // body parts.. // ..collect @@ -300,7 +232,7 @@ static const String& letter_hash_to_stri if(seq[i].weight>=ATTACHMENT_WEIGHT) result << attach_hash_to_string(r, *seq[i].name, *part_hash); else - result << letter_hash_to_string(r, method_name, *part_hash, + result << message_hash_to_string(r, method_name, *part_hash, level+1, 0, 0); else throw Exception("parser.runtime", @@ -317,8 +249,8 @@ static const String& letter_hash_to_stri "\n"; // header|body separator const String& body=body_element->as_string(); - const void *body_ptr=body.cstr(); // body - size_t body_size=body.size(); // body + const char *body_ptr=body.cstr(String::UL_UNSPECIFIED); // body + size_t body_size=strlen(body_ptr); // body const void *mail_ptr; size_t mail_size; Charset::transcode(pool, @@ -335,22 +267,23 @@ static const String& letter_hash_to_stri } static void sendmail(Request& r, const String& method_name, - const String& letter, + const String& message, const String *from, const String *to) { Pool& pool=r.pool(); - char *letter_cstr=letter.cstr(); - Hash *mail_conf=static_cast(r.classes_conf.get(mail_class->name())); + char *message_cstr=message.cstr(String::UL_UNSPECIFIED); + Hash *mail_conf=static_cast(r.classes_conf.get(mail_base_class->name())); -#ifdef _MSC_VER + const char *exception_type="email.format"; if(!from) - throw Exception("parser.runtime", + throw Exception(exception_type, &method_name, - "has no 'from' header specified"); + "parameter does not specify 'from' header field"); if(!to) - throw Exception("parser.runtime", + throw Exception(exception_type, &method_name, - "has no 'to' header specified"); + "parameter does not specify 'to' header field"); +#ifdef _MSC_VER SMTP& smtp=*new(pool) SMTP(pool, method_name); Value *server_port; @@ -363,15 +296,15 @@ static void sendmail(Request& r, const S if(!port) port="25"; - smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr()); + smtp.Send(server, port, message_cstr, from->cstr(), to->cstr()); } else throw Exception("parser.runtime", &method_name, "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined"); #else // unix - // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default - // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t"] default + // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default + // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default const String *sendmail_command; #ifdef PA_FORCED_SENDMAIL @@ -390,12 +323,21 @@ static void sendmail(Request& r, const S 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"); + test->APPEND_CONST(" -t -i -f postmaster"); sendmail_command=test; } #endif - // we know sendmail_command here + // we know sendmail_command here, should replace "postmaster" with "$from" from message + int at_postmaster=sendmail_command->pos("postmaster"); + if(at_postmaster>0) { + String& reconstructed=sendmail_command->mid(0, at_postmaster); + reconstructed << *from; + reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->size()); + sendmail_command=&reconstructed; + } + + // execute it Array argv(pool); const String *file_spec; int after_file_spec=sendmail_command->pos(" ", 1); @@ -408,19 +350,19 @@ static void sendmail(Request& r, const S } if(!file_executable(*file_spec)) - throw Exception(0, + throw Exception("email.send", 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", + " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s to appropriate sendmail command", sendmailkey_cstr #endif ); - String in(pool, letter_cstr); String out(pool); String err(pool); + String in(pool, message_cstr); String out(pool); String err(pool); int exit_status=pa_exec( // forced_allow #ifdef PA_FORCED_SENDMAIL @@ -433,7 +375,7 @@ static void sendmail(Request& r, const S &argv, in, out, err); if(exit_status || err.size()) - throw Exception(0, + throw Exception("email.send", &method_name, "'%s' reported problem: %s (%d)", file_spec->cstr(), @@ -456,9 +398,12 @@ static void _send(Request& r, const Stri "message must be hash"); const String *from, *to; - const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to); + const String& message=hash->get(*body_name)/*old format*/? + message_hash_to_string(r, method_name, *hash, 0, &from, &to) : // old + static_cast(r.self)->message_hash_to_string(r, &method_name, hash, 0, &from, &to); // new - sendmail(r, method_name, letter, from, to); + //r.write_pass_lang(message); + sendmail(r, method_name, message, from, to); } // constructor & configurator @@ -477,14 +422,15 @@ void MMail::configure_user(Request& r) { if(Value *mail_element=r.main_class->get_element(mail_name)) if(Hash *mail_conf=mail_element->get_hash(0)) r.classes_conf.put(name(), mail_conf); - else if(!mail_element->get_string()) - throw Exception("parser.runtime", - 0, - "$" MAIN_CLASS_NAME ":" MAIL_NAME " is not hash"); + else + if( !mail_element->is_string() ) + throw Exception("parser.runtime", + 0, + "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash"); } // creator Methoded *MMail_create(Pool& pool) { - return mail_class=new(pool) MMail(pool); + return mail_base_class=new(pool) MMail(pool); }