Diff for /parser3/src/classes/mail.C between versions 1.9 and 1.15

version 1.9, 2001/04/07 17:33:04 version 1.15, 2001/04/10 07:47:38
Line 18 Line 18
 #include "pa_common.h"  #include "pa_common.h"
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_vfile.h"  #include "pa_vfile.h"
   #include "pa_exec.h"
   
 // global var  // global var
   
Line 25  VStateless_class *mail_class; Line 26  VStateless_class *mail_class;
   
 // helpers  // helpers
   
 /// @test uuencode  // 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) {  static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
         // content-transfer-encoding: x-uuencode          //header
         result+="content-transfer-encoding: x-uuencode\n";          result << "content-transfer-encoding: x-uuencode\n" << "\n";
         result+="\n";          result << "begin 644 " << file_name_cstr << "\n";
         result+="todo";  
           //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]]]   /// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
Line 65  static const String& attach_hash_to_stri Line 134  static const String& attach_hash_to_stri
   
         String& result=*new(pool) String(pool);          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"          // content-disposition: attachment; filename="user_file_name"
         result+="content-disposition: attachment; filename=\"";          result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
         result+=file_name_cstr;  
         result+="\"\n";  
   
         const String& type=vtype->as_string();          const String& type=vtype->as_string();
         if(type=="uue") {          if(type=="uue") {
Line 87  struct Mail_info { Line 156  struct Mail_info {
         String *header;          String *header;
         const String **from, **to;          const String **from, **to;
 };  };
   
 static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning,   static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                                                                  void *info) {                                                                   void *info) {
   
Line 105  static void add_header_attribute(const H Line 173  static void add_header_attribute(const H
                 *mi.to=&lmeaning.as_string();                  *mi.to=&lmeaning.as_string();
   
         // append header line          // append header line
         *mi.header+=aattribute;          *mi.header << 
         *mi.header+=":";                  aattribute << ":" << 
         *mi.header+=attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER);                  attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER) << 
         *mi.header+="\n";                  "\n";
 }  }
 struct Seq_item {  struct Seq_item {
         const String *part_name;          const String *part_name;
Line 150  static const String& letter_hash_to_stri Line 218  static const String& letter_hash_to_stri
   
         // prepare header: 'hash' without "body"          // prepare header: 'hash' without "body"
         String& result=*new(pool) String(pool);          String& result=*new(pool) String(pool);
           *from=*to=0;
         Mail_info mail_info={          Mail_info mail_info={
                 /*excluding*/ body_name,                  /*excluding*/ body_name,
                 &result,                  &result,
Line 162  static const String& letter_hash_to_stri Line 231  static const String& letter_hash_to_stri
                         char *boundary=(char *)pool.malloc(MAX_NUMBER);                          char *boundary=(char *)pool.malloc(MAX_NUMBER);
                         snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);                          snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
                         // multi-part                          // multi-part
                         ((result+=                          result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                                 "content-type: multipart/mixed; boundary=\"")+=boundary)+="\"\n"                          result << "\n" 
                                 "\n"  
                                 "This is a multi-part message in MIME format.";                                  "This is a multi-part message in MIME format.";
   
                         // body parts..                          // body parts..
Line 177  static const String& letter_hash_to_stri Line 245  static const String& letter_hash_to_stri
                         // ..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                                  // intermediate boundary
                                 ((result+="\n--")+=boundary)+="\n";                                  result << "\n--" << boundary << "\n";
   
                                 if(Hash *part_hash=seq[i].part_value->get_hash())                                  if(Hash *part_hash=seq[i].part_value->get_hash())
                                         if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")                                          if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")
                                                 result+=attach_hash_to_string(r, *seq[i].part_name, *part_hash);                                                  result << attach_hash_to_string(r, *seq[i].part_name, *part_hash);
                                         else                                           else 
                                                 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
                                         PTHROW(0, 0,                                          PTHROW(0, 0,
Line 192  static const String& letter_hash_to_stri Line 260  static const String& letter_hash_to_stri
                         }                          }
   
                         // finish boundary                          // finish boundary
                         ((result+="\n--")+=boundary)+="--\n";                          result << "\n--" << boundary << "--\n";
                 } else {                  } else {
                         result+="\n"; // header|body separator                          result << 
                         result+=body_element->as_string();  // body                                  "\n" << // header|body separator
                                   body_element->as_string();  // body
                 }                  }
         } else           } else 
                 PTHROW(0, 0,                  PTHROW(0, 0,
Line 205  static const String& letter_hash_to_stri Line 274  static const String& letter_hash_to_stri
         return result;          return result;
 }  }
   
 /// @test unix ver  
 static void sendmail(Request& r, const String& method_name,   static void sendmail(Request& r, const String& method_name, 
                                          const String& letter,                                            const String& letter, 
                                          const String *from, const String *to) {                                           const String *from, const String *to) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
   
           char *letter_cstr=letter.cstr();
   
 #ifdef WIN32  #ifdef WIN32
         if(!from)          if(!from)
                 PTHROW(0, 0,                  PTHROW(0, 0,
                         &method_name,                          &method_name,
                         "not specified 'from'");                          "has no 'from' header specified");
         if(!to)          if(!to)
                 PTHROW(0, 0,                  PTHROW(0, 0,
                         &method_name,                          &method_name,
                         "not specified 'to'");                          "has no 'to' header specified");
   
         char *letter_cstr=letter.cstr();  
         SMTP& smtp=*new(pool) SMTP(pool, method_name);          SMTP& smtp=*new(pool) SMTP(pool, method_name);
         Value *server_port;          Value *server_port;
         // $MAIN:MAIL.SMTP[mail.design.ru]          // $MAIN:MAIL.SMTP[mail.design.ru]
Line 229  static void sendmail(Request& r, const S Line 298  static void sendmail(Request& r, const S
                 (server_port=static_cast<Value *>(r.mail->get(                  (server_port=static_cast<Value *>(r.mail->get(
                         *new(pool) String(pool, "SMTP"))))) {                          *new(pool) String(pool, "SMTP"))))) {
                 char *server=server_port->as_string().cstr();                  char *server=server_port->as_string().cstr();
                 char *port=rsplit(server, ':');                  const char *port=rsplit(server, ':');
                 if(!port)                  if(!port)
                         port=const_cast<char *>("25");                          port="25";
   
                 smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());                  smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
         } else          } else
                 PTHROW(0, 0,                  PTHROW(0, 0,
                         &method_name,                          &method_name,
                         "$MAIN:MAIL.SMTP not defined");                          "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
 #else  #else
         PTHROW(0, 0,          // unix
                 &method_name,          // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default
                 "todo");          // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default
           if(r.mail) {
                   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(Value *prog_value=static_cast<Value *>(r.mail->get(prog_key)))
                                                   prog_string=&prog_value->as_string();
                                           else
                                                   if(no==0)
                                                           continue;
                                                   else
                                                           PTHROW(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);
                           }
   
                           // 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");
 #endif  #endif
 }  }
   
Line 264  static void _send(Request& r, const Stri Line 391  static void _send(Request& r, const Stri
         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));          //r.write_assign_lang(*new(pool) VString(letter));
         //sendmail(r, method_name, letter, from, to);          sendmail(r, method_name, letter, from, to);
 }  }
   
 // initialize  // initialize

Removed from v.1.9  
changed lines
  Added in v.1.15


E-mail: