--- parser3/src/classes/mail.C 2001/05/04 10:42:36 1.26 +++ parser3/src/classes/mail.C 2001/11/05 11:46:21 1.44 @@ -2,10 +2,9 @@ Parser: @b mail parser class. Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://paf.design.ru) - Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: mail.C,v 1.26 2001/05/04 10:42:36 paf Exp $ + $Id: mail.C,v 1.44 2001/11/05 11:46:21 paf Exp $ */ #include "pa_config_includes.h" @@ -119,14 +118,16 @@ static void uuencode(String& result, con "end\n"; } -/// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] +/** ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] + @todo solve - bad with html attaches +*/ static const String& attach_hash_to_string(Request& r, const String& origin_string, Hash& attach_hash) { Pool& pool=r.pool(); Value *vtype=static_cast(attach_hash.get(*new(pool) String(pool, "type"))); if(!vtype) - PTHROW(0, 0, + throw Exception(0, 0, &origin_string, "has no $type"); @@ -134,7 +135,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 - PTHROW(0, 0, + throw Exception(0, 0, &origin_string, "has no $value"); @@ -144,7 +145,7 @@ static const String& attach_hash_to_stri file_name=&vfile_name->as_string(); else // no $file-name, VFile surely knows name file_name=&static_cast(vfile->fields().get(*name_name))->as_string(); - const char *file_name_cstr=file_name->cstr(String::UL_FILE_NAME); + const char *file_name_cstr=file_name->cstr(String::UL_FILE_SPEC); String& result=*new(pool) String(pool); @@ -157,7 +158,7 @@ static const String& attach_hash_to_stri if(type=="uue") { uuencode(result, file_name_cstr, *vfile); } else - PTHROW(0, 0, + throw Exception(0, 0, &type, "unknown encode type"); @@ -165,22 +166,23 @@ static const String& attach_hash_to_stri } -static bool find_content_type(const Hash::Key& aattribute, Hash::Val *ameaning, +static void *find_content_type(const Hash::Key& aattribute, Hash::Val *ameaning, void *) { - return StrEqNc(aattribute.cstr(), CONTENT_TYPE_NAME); + return StrEqNc(aattribute.cstr(), CONTENT_TYPE_NAME)?ameaning:0; } -static bool find_content_type_charset(const Hash::Key& aattribute, Hash::Val *ameaning, +static void *find_content_type_charset(const Hash::Key& aattribute, Hash::Val *ameaning, void *) { - return StrEqNc(aattribute.cstr(), "charset"); + return StrEqNc(aattribute.cstr(), "charset")?ameaning:0; } -/// used by mail: _send / letter_hash_to_string / add_header_attribute +#ifndef DOXYGEN struct Mail_info { String *attribute_to_exclude; const char *charset; String *header; const String **from, **to; }; +#endif static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, void *info) { @@ -205,20 +207,21 @@ static void add_header_attribute(const H "\n"; } -/// used in mail: _send / letter_hash_to_string / add_part -struct Seq_item { +#ifndef DOXYGEN +struct Mail_seq_item { const String *part_name; Value *part_value; }; +#endif static void add_part(const Hash::Key& part_name, Hash::Val *part_value, void *info) { - Seq_item **seq_ref=static_cast(info); + Mail_seq_item **seq_ref=static_cast(info); (**seq_ref).part_name=&part_name; (**seq_ref).part_value=static_cast(part_value); (*seq_ref)++; } static double key_of_part(const void *item) { - const char *cstr=static_cast(item)->part_name->cstr(); + const char *cstr=static_cast(item)->part_name->cstr(); char *error_pos; return strtod(cstr, &error_pos); } @@ -250,7 +253,7 @@ static const String& letter_hash_to_stri const char *charset=0; if(Value *content_type= static_cast(letter_hash.first_that(find_content_type))) - if(Hash *hash=content_type->get_hash()) + if(Hash *hash=content_type->get_hash(&method_name)) if(Value *content_type_charset= static_cast(hash->first_that(find_content_type_charset))) charset=content_type_charset->as_string().cstr(); @@ -265,7 +268,7 @@ static const String& letter_hash_to_stri letter_hash.for_each(add_header_attribute, &mail_info); if(Value *body_element=static_cast(letter_hash.get(*body_name))) { - if(Hash *body_hash=body_element->get_hash()) { + if(Hash *body_hash=body_element->get_hash(&method_name)) { char *boundary=(char *)pool.malloc(MAX_NUMBER); snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level); // multi-part @@ -275,24 +278,24 @@ static const String& letter_hash_to_stri // body parts.. // ..collect - Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size()); - Seq_item *seq_ref=seq; body_hash->for_each(add_part, &seq_ref); + Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size()); + Mail_seq_item *seq_ref=seq; body_hash->for_each(add_part, &seq_ref); // ..sort - _qsort(seq, body_hash->size(), sizeof(Seq_item), + _qsort(seq, body_hash->size(), sizeof(Mail_seq_item), sort_cmp_string_double_value); // ..insert in 'seq' order for(int i=0; isize(); i++) { // intermediate boundary result << "\n--" << boundary << "\n"; - if(Hash *part_hash=seq[i].part_value->get_hash()) + if(Hash *part_hash=seq[i].part_value->get_hash(&method_name)) if(seq[i].part_name->mid(0, 6/*attach*/)=="attach") result << attach_hash_to_string(r, *seq[i].part_name, *part_hash); else result << letter_hash_to_string(r, method_name, *part_hash, level+1, 0, 0); else - PTHROW(0, 0, + throw Exception(0, 0, seq[i].part_name, "part is not hash"); } @@ -302,10 +305,10 @@ static const String& letter_hash_to_stri } else { result << "\n" << // header|body separator - body_element->as_string(); // body + body_element->as_string().cstr(); // body } } else - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "has no $body"); @@ -322,17 +325,17 @@ static void sendmail(Request& r, const S #ifdef _MSC_VER if(!from) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "has no 'from' header specified"); if(!to) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "has no 'to' header specified"); SMTP& smtp=*new(pool) SMTP(pool, method_name); Value *server_port; - // $MAIN:MAIL.SMTP[mail.design.ru] + // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]] if(mail_conf && (server_port=static_cast(mail_conf->get( *new(pool) String(pool, "SMTP"))))) { @@ -343,71 +346,71 @@ static void sendmail(Request& r, const S smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr()); } else - PTHROW(0, 0, + throw Exception(0, 0, &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 - if(mail_conf) { - 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; + 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 - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", prog_key.cstr()); - } - } - // 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_CLEAN); + } else + throw Exception(0, 0, + &method_name, + "$" MAIN_CLASS_NAME ":" MAIL_NAME " not defined"); } - - // 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()) - PTHROW(0, 0, - &method_name, - "'%s' reported problem: %s (%d)", - file_spec->cstr(), - err.size()?err.cstr():"UNKNOWN", - exit_status); - break; } - } else - PTHROW(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_CLEAN); + } + + // 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, + &method_name, + "'%s' reported problem: %s (%d)", + file_spec->cstr(), + err.size()?err.cstr():"UNKNOWN", + exit_status); + break; + } #endif } @@ -417,10 +420,10 @@ static void sendmail(Request& r, const S static void _send(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Value& vhash=params->get_no_junction(0, "message must not be code"); - Hash *hash=vhash.get_hash(); + Value& vhash=params->as_no_junction(0, "message must not be code"); + Hash *hash=vhash.get_hash(&method_name); if(!hash) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "message must be hash"); @@ -440,7 +443,7 @@ MMail::MMail(Pool& apool) : Methoded(apo { set_name(*NEW String(pool(), MAIL_CLASS_NAME)); - /// ^mail:send{hash} + // ^mail:send{hash} add_native_method("send", Method::CT_STATIC, _send, 1, 1); } @@ -449,10 +452,10 @@ void MMail::configure_user(Request& r) { // $MAIN:MAIL[$SMTP[mail.design.ru]] if(Value *mail_element=r.main_class->get_element(mail_name)) - if(Hash *mail_conf=mail_element->get_hash()) + if(Hash *mail_conf=mail_element->get_hash(0)) r.classes_conf.put(name(), mail_conf); else - PTHROW(0, 0, + throw Exception(0, 0, 0, "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash"); }