Diff for /parser3/src/classes/mail.C between versions 1.47 and 1.65

version 1.47, 2001/12/19 12:26:11 version 1.65, 2002/06/12 12:50:32
Line 1 Line 1
 /** @file  /** @file
         Parser: @b mail parser class.          Parser: @b mail parser class.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
   
         $Id$          $Id$
 */  */
   
 #include "pa_config_includes.h"  #include "pa_config_includes.h"
   
 #ifdef _MSC_VER  
 #       include "smtp/smtp.h"  
 #endif  
   
 #include "pa_common.h"  #include "pa_common.h"
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_vfile.h"  #include "pa_vfile.h"
Line 20 Line 16
 #include "pa_charsets.h"  #include "pa_charsets.h"
 #include "pa_charset.h"  #include "pa_charset.h"
   
   #ifdef _MSC_VER
   #       include "smtp/smtp.h"
   #endif
   
 // defines  // defines
   
 #define MAIL_CLASS_NAME "mail"  #define MAIL_CLASS_NAME "mail"
Line 44  public: // Methoded Line 44  public: // Methoded
         void configure_user(Request& r);          void configure_user(Request& r);
 private:  private:
         String mail_name;          String mail_name;
         String content_disposition_name;  
         String content_disposition_filename_name;  
 };  };
   
 // helpers  // helpers
Line 137  static const String& attach_hash_to_stri Line 135  static const String& attach_hash_to_stri
         if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))          if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
                 vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve                  vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve
         else          else
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &origin_string,                          &origin_string,
                         "has no $value");                          "has no $value");
   
Line 147  static const String& attach_hash_to_stri Line 145  static const String& attach_hash_to_stri
                 file_name=&vfile_name->as_string();                  file_name=&vfile_name->as_string();
         else // no $file-name, VFile surely knows name          else // no $file-name, VFile surely knows name
                 file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();                  file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
         const char *file_name_cstr=file_name->cstr(String::UL_FILE_SPEC);          const char *file_name_cstr=file_name->cstr();
   
         String& result=*new(pool) String(pool);          String& result=*new(pool) String(pool);
   
         // content-type: application/octet-stream          // content-type: application/octet-stream
         result << "content-type: " << r.mime_type_of(file_name_cstr) << "\n";          result << "content-type: " << r.mime_type_of(file_name_cstr) 
                   << "; name=\"" << file_name_cstr << "\"\n";
         // content-disposition: attachment; filename="user_file_name"          // content-disposition: attachment; filename="user_file_name"
         result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";          result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
   
Line 160  static const String& attach_hash_to_stri Line 159  static const String& attach_hash_to_stri
         if(!type/*default = uue*/ || *type=="uue") {          if(!type/*default = uue*/ || *type=="uue") {
                 uuencode(result, file_name_cstr, *vfile);                  uuencode(result, file_name_cstr, *vfile);
         } else // for now          } else // for now
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         type,                          type,
                         "unknown attachment encode format");                          "unknown attachment encode format");
                   
Line 170  static const String& attach_hash_to_stri Line 169  static const String& attach_hash_to_stri
   
 #ifndef DOXYGEN  #ifndef DOXYGEN
 struct Mail_info {  struct Mail_info {
         Charset *charset;          Charset *charset; const char *content_charset_name;
         String *header;          String *header;
         const String **from, **to;          const String **from, **to;
 };  };
Line 196  static void add_header_attribute(const H Line 195  static void add_header_attribute(const H
         *mi.header <<           *mi.header << 
                 aattribute << ":" <<                   aattribute << ":" << 
                 attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER).                  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";                  "\n";
 }  }
   
Line 216  static int get_part_name_weight(const Ha Line 215  static int get_part_name_weight(const Ha
                 cstr+=6;                  cstr+=6;
                 offset=ATTACHMENT_WEIGHT;                  offset=ATTACHMENT_WEIGHT;
         } else          } else
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &part_name,                          &part_name,
                         "is neither text# nor attach#");                          "is neither text# nor attach#");
   
Line 246  static const String& letter_hash_to_stri Line 245  static const String& letter_hash_to_stri
         String& result=*new(pool) String(pool);          String& result=*new(pool) String(pool);
   
         Charset *charset;          Charset *charset;
         if(Value *vcharset_name=static_cast<Value *>(letter_hash.get(*charset_name)))          if(Value *vrecodecharset_name=static_cast<Value *>(letter_hash.get(*charset_name)))
                 charset=&charsets->get_charset(vcharset_name->as_string());                  charset=&charsets->get_charset(vrecodecharset_name->as_string());
         else          else
                 charset=&pool.get_source_charset();                  charset=&pool.get_source_charset();
   
           const char *content_charset_name=0;
           if(Value *vcontent_type=static_cast<Value *>(letter_hash.get(*content_type_name)))
                   if(Hash *hcontent_type=vcontent_type->get_hash(0))
                           if(Value *vcontentcharset_name=static_cast<Value *>(hcontent_type->get(*charset_name)))
                                   content_charset_name=vcontentcharset_name->as_string().cstr();
   
         if(from)          if(from)
                 *from=0;                  *from=0;
         if(to)          if(to)
                 *to=0;                  *to=0;
         Mail_info mail_info={          Mail_info mail_info={
                 charset,                  charset, content_charset_name,
                 &result,                  &result,
                 from, to                  from, to
         };          };
Line 264  static const String& letter_hash_to_stri Line 269  static const String& letter_hash_to_stri
   
         if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {          if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                 if(Hash *body_hash=body_element->get_hash(&method_name)) {                  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  
                         result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";  
                         result << "\n"   
                                 "This is a multi-part message in MIME format.";  
   
                         // body parts..                          // body parts..
                         // ..collect                          // ..collect
                         Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());                          Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());
Line 278  static const String& letter_hash_to_stri Line 276  static const String& letter_hash_to_stri
                         // ..sort                          // ..sort
                         _qsort(seq, body_hash->size(), sizeof(Mail_seq_item),                           _qsort(seq, body_hash->size(), sizeof(Mail_seq_item), 
                                 sort_cmp_string_double_value);                                  sort_cmp_string_double_value);
   
                           bool multipart=body_hash->size()>1;
                           // header
                           char *boundary=0;
                           if(multipart) {
                                   boundary=(char *)pool.malloc(MAX_NUMBER);
                                   snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                                   // multi-part
                                   result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                                   result << "\n" 
                                           "This is a multi-part message in MIME format.";
                           }
   
                         // ..insert in 'seq' order                          // ..insert in 'seq' order
                         for(int i=0; i<body_hash->size(); i++) {                          for(int i=0; i<body_hash->size(); i++) {
                                 // intermediate boundary                                  if(multipart) {
                                 result << "\n--" << boundary << "\n";                                          // intermediate boundary
                                           result << "\n--" << boundary << "\n";
                                   }
   
                                 if(Hash *part_hash=seq[i].value->get_hash(&method_name))                                  if(Hash *part_hash=seq[i].value->get_hash(&method_name))
                                         if(seq[i].weight>=ATTACHMENT_WEIGHT)                                          if(seq[i].weight>=ATTACHMENT_WEIGHT)
Line 290  static const String& letter_hash_to_stri Line 303  static const String& letter_hash_to_stri
                                                 result << letter_hash_to_string(r, method_name, *part_hash,                                                   result << letter_hash_to_string(r, method_name, *part_hash, 
                                                         level+1, 0, 0);                                                          level+1, 0, 0);
                                 else                                  else
                                         throw Exception(0, 0,                                          throw Exception("parser.runtime",
                                                 seq[i].name,                                                  seq[i].name,
                                                 "part is not hash");                                                  "part is not hash");
                         }                          }
   
                         // finish boundary                          // finish boundary
                         result << "\n--" << boundary << "--\n";                          if(multipart) {
                                   result << "\n--" << boundary << "--\n";
                           }
                 } else {                  } else {
                         result <<                           result << 
                                 "\n"; // header|body separator                                  "\n"; // header|body separator
Line 312  static const String& letter_hash_to_stri Line 327  static const String& letter_hash_to_stri
                         result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);                          result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);
                 }                  }
         } else           } else 
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         "has no $body");                          "has no $body");
   
Line 329  static void sendmail(Request& r, const S Line 344  static void sendmail(Request& r, const S
   
 #ifdef _MSC_VER  #ifdef _MSC_VER
         if(!from)          if(!from)
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         "has no 'from' header specified");                          "has no 'from' header specified");
         if(!to)          if(!to)
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         "has no 'to' header specified");                          "has no 'to' header specified");
   
Line 350  static void sendmail(Request& r, const S Line 365  static void sendmail(Request& r, const S
   
                 smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());                  smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
         } else          } else
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");                          "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
 #else  #else
         // unix          // unix
         // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default          // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default
         // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default          // $MAIN:MAIL.sendmail["/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<Value *>(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_CLEAN);  
                 }  
   
                 // skip unavailable default programs          const String *sendmail_command;
                 if(no<0 && !file_executable(*file_spec))  #ifdef PA_FORCED_SENDMAIL
                         continue;          sendmail_command=new(pool) String(pool, PA_FORCED_SENDMAIL);
   #else
                 String in(pool, letter_cstr); String out(pool); String err(pool);          const char *sendmailkey_cstr="sendmail";
                 int exit_status=pa_exec(*file_spec,          if(mail_conf) {
                         0/*default env*/,                  if(Value *sendmail_value=static_cast<Value *>(mail_conf->get(String(pool, sendmailkey_cstr))))
                         &argv,                          sendmail_command=&sendmail_value->as_string();
                         in, out, err);                  else
                 if(exit_status || err.size())                          throw Exception("parser.runtime",
                         throw Exception(0, 0,  
                                 &method_name,                                  &method_name,
                                 "'%s' reported problem: %s (%d)",                                  "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                                         file_spec->cstr(),                                  sendmailkey_cstr);
                                         err.size()?err.cstr():"UNKNOWN",           } else {
                                         exit_status);                  String *test=new(pool) String(pool, "/usr/sbin/sendmail");
                 break;                  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  #endif
 }  }
   
Line 427  static void _send(Request& r, const Stri Line 451  static void _send(Request& r, const Stri
         Value& vhash=params->as_no_junction(0, "message must not be code");          Value& vhash=params->as_no_junction(0, "message must not be code");
         Hash *hash=vhash.get_hash(&method_name);          Hash *hash=vhash.get_hash(&method_name);
         if(!hash)          if(!hash)
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         "message must be hash");                          "message must be hash");
   
         const String *from, *to;          const String *from, *to;
         const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &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);          sendmail(r, method_name, letter, from, to);
 }  }
   
 // constructor & configurator  // constructor & configurator
   
 MMail::MMail(Pool& apool) : Methoded(apool),  MMail::MMail(Pool& apool) : Methoded(apool, MAIL_CLASS_NAME),
         mail_name(apool, MAIL_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}          // ^mail:send{hash}
         add_native_method("send", Method::CT_STATIC, _send, 1, 1);          add_native_method("send", Method::CT_STATIC, _send, 1, 1);
 }  }
Line 458  void MMail::configure_user(Request& r) { Line 477  void MMail::configure_user(Request& r) {
         if(Value *mail_element=r.main_class->get_element(mail_name))          if(Value *mail_element=r.main_class->get_element(mail_name))
                 if(Hash *mail_conf=mail_element->get_hash(0))                  if(Hash *mail_conf=mail_element->get_hash(0))
                         r.classes_conf.put(name(), mail_conf);                          r.classes_conf.put(name(), mail_conf);
                 else                  else if(!mail_element->get_string())
                         throw Exception(0, 0,                          throw Exception("parser.runtime",
                                 0,                                  0,
                                 "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");                                  "$" MAIN_CLASS_NAME ":" MAIL_NAME " is not hash");
 }  }
   
 // creator  // creator

Removed from v.1.47  
changed lines
  Added in v.1.65


E-mail: