--- parser3/src/classes/mail.C 2001/04/07 15:16:26 1.6 +++ parser3/src/classes/mail.C 2001/04/08 13:11:15 1.10 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: mail.C,v 1.6 2001/04/07 15:16:26 paf Exp $ + $Id: mail.C,v 1.10 2001/04/08 13:11:15 paf Exp $ */ #include "pa_config_includes.h" @@ -23,14 +23,138 @@ VStateless_class *mail_class; -// methods +// 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 *itemp; + + int index; + int count=45; + + const unsigned char *in=(const unsigned char *)vfile.value_ptr(); + size_t in_length=vfile.value_size(); + + for(itemp=in; itemp<(in+in_length); itemp+=count) { + 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_CLEAN((const char *)uue_table, 1/* one char */, 0, 0) << "\n" + "end\n"; +} + +/// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] +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, + &origin_string, + "has no $type"); + + const VFile *vfile; + if(Value *value=static_cast(attach_hash.get(*value_name))) + vfile=value->as_vfile(); + else + PTHROW(0, 0, + &origin_string, + "has no $value"); + + const String *file_name; + if(Value *vfile_name=static_cast(attach_hash.get( + *new(pool) String(pool, "file-name")))) // specified $file-name + file_name=&vfile_name->as_string(); + else // no $file-name + if(Value *vfile_name=static_cast(vfile->fields().get(*name_name))) + file_name=&vfile_name->as_string(); // VFile knows name + else // vfile doesn't know name + file_name=new(pool) String(pool, "noname.dat"); + const char *file_name_cstr=file_name->cstr(String::UL_FILE_NAME); + + String& result=*new(pool) String(pool); + + // content-type: application/octet-stream + result << "content-type: " << r.mime_type_of(file_name_cstr) << "\n"; + // content-disposition: attachment; filename="user_file_name" + result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n"; + + const String& type=vtype->as_string(); + if(type=="uue") { + uuencode(result, file_name_cstr, *vfile); + } else + PTHROW(0, 0, + &type, + "unknown encode type"); + + return result; +} + struct Mail_info { String *attribute_to_exclude; String *header; const String **from, **to; }; - static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, void *info) { @@ -48,30 +172,37 @@ static void add_header_attribute(const H *mi.to=&lmeaning.as_string(); // append header line - *mi.header+=aattribute; - *mi.header+=":"; - *mi.header+=attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER); - *mi.header+="\n"; + *mi.header << + aattribute << ":" << + attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER) << + "\n"; } struct Seq_item { - const String *part_number; + const String *part_name; Value *part_value; }; -static void add_part(const Hash::Key& part_number, Hash::Val *part_value, +static void add_part(const Hash::Key& part_name, Hash::Val *part_value, void *info) { Seq_item **seq_ref=static_cast(info); - (**seq_ref).part_number=&part_number; + (**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_number->cstr(); + const char *cstr=static_cast(item)->part_name->cstr(); char *error_pos; return strtod(cstr, &error_pos); } static int sort_cmp_string_double_value(const void *a, const void *b) { double va=key_of_part(a); double vb=key_of_part(b); + + // 0 logically equals infinity. so that attachments would go last + if(va==0) + return +1; + if(vb==0) + return -1; + if(vavb) @@ -96,12 +227,10 @@ static const String& letter_hash_to_stri if(Value *body_element=static_cast(letter_hash.get(*body_name))) { if(Hash *body_hash=body_element->get_hash()) { char *boundary=(char *)pool.malloc(MAX_NUMBER); - snprintf(boundary, MAX_NUMBER-6/*level_*/, "level_%d", level); + snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level); // multi-part - ((result+= - "content-type: multipart/mixed;\n" - " boundary=\"----=")+=boundary)+="\"\n" - "\n" + result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n"; + result << "\n" "This is a multi-part message in MIME format."; // body parts.. @@ -114,22 +243,26 @@ static const String& letter_hash_to_stri // ..insert in 'seq' order for(int i=0; isize(); i++) { // intermediate boundary - ((result+="\n------=")+=boundary)+="\n"; + result << "\n--" << boundary << "\n"; if(Hash *part_hash=seq[i].part_value->get_hash()) - result+=letter_hash_to_string(r, method_name, *part_hash, - level+1, 0, 0); + 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, - seq[i].part_number, + seq[i].part_name, "part is not hash"); } // finish boundary - ((result+="\n------=")+=boundary)+="--\n"; + result << "\n--" << boundary << "--\n"; } else { - result+="\n"; // header|body separator - result+=body_element->as_string(); // body + result << + "\n" << // header|body separator + body_element->as_string(); // body } } else PTHROW(0, 0, @@ -139,7 +272,6 @@ static const String& letter_hash_to_stri return result; } - /// @test unix ver static void sendmail(Request& r, const String& method_name, const String& letter, @@ -180,6 +312,9 @@ static void sendmail(Request& r, const S #endif } + +// methods + static void _send(Request& r, const String& method_name, Array *params) { Pool& pool=r.pool(); @@ -196,73 +331,12 @@ static void _send(Request& r, const Stri 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); -} - -/// ^mail:attach[uue|base64;DATA] -/// ^mail:attach[uue|base64;DATA;user-file-name] -static void _attach(Request& r, const String& method_name, Array *params) { - Pool& pool=r.pool(); - - Value& vtype=*static_cast(params->get(0)); - // forcing [this vtype] - r.fail_if_junction_(true, vtype, method_name, "type must not be code"); - - Value& vdata=*static_cast(params->get(1)); - // forcing [this vtype] - r.fail_if_junction_(true, vdata, method_name, "data must not be code"); - const VFile& vfile=*vdata.as_vfile(); - - Value *user_file_name; - if(params->size()>2) { - user_file_name=static_cast(params->get(0)); - // forcing [this vtype] - r.fail_if_junction_(true, *user_file_name, method_name, - "user file name must not be code"); - } else { - user_file_name=static_cast(vfile.fields().get(*name_name)); - } - - VHash& result=*new(pool) VHash(pool); - - { // content-disposition: attachment; filename='user_file_name' - VHash& content_disposition=*new(pool) VHash(pool); - content_disposition.hash().put(*value_name, - new(pool) VString(*new(pool) String(pool, "attachment"))); - content_disposition.hash().put( - *new(pool) String(pool, "filename"), - user_file_name); - result.hash().put(*new(pool) String(pool, "content-disposition"), - &content_disposition); - } - - const String& type=vtype.as_string(); - if(type=="uue") { - { // content-transfer-encoding: x-uuencode - VString& content_transfer_encoding=*new(pool) VString( - *new(pool) String(pool, "x-uuencode")); - result.hash().put(*new(pool) String(pool, "content-transfer-encoding"), - &content_transfer_encoding); - } - - result.hash().put(*body_name, - new(pool) String(pool, "todo")); - } else - PTHROW(0, 0, - &type, - "unknown encode type"); - - result.set_name(*new(pool) String(pool, "100")); // so that would go last - r.write_no_lang(result); + //r.write_assign_lang(*new(pool) VString(letter)); + sendmail(r, method_name, letter, from, to); } // initialize void initialize_mail_class(Pool& pool, VStateless_class& vclass) { // ^mail:send{hash} vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1); - - // ^mail:encode[uue|base64;DATA] - // ^mail:encode[uue|base64;DATA;user-file-name] - vclass.add_native_method("attach", Method::CT_ANY, _attach, 2, 3); }