|
|
| version 1.69, 2002/07/11 07:31:42 | version 1.78, 2002/08/13 15:55:41 |
|---|---|
| Line 3 | Line 3 |
| Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | |
| */ | */ |
| static const char* IDENT_MAIL_C="$Date$"; | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| Line 97 struct Mail_info { | Line 97 struct Mail_info { |
| Charset *charset; const char *content_charset_name; | Charset *charset; const char *content_charset_name; |
| String *header; | String *header; |
| const String **from, **to; | const String **from, **to; |
| const String *errors_to; | |
| }; | }; |
| #endif | #endif |
| static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, | |
| void *info) { | |
| Value& lmeaning=*static_cast<Value *>(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<Value *>(aelement_value); | |
| const String& low_element_name=raw_element_name.change_case(raw_element_name.pool(), String::CC_LOWER); | |
| Mail_info& mi=*static_cast<Mail_info *>(info); | Mail_info& mi=*static_cast<Mail_info *>(info); |
| // exclude one attribute [body] | // exclude one attribute [body] |
| if(aattribute==BODY_NAME | if(low_element_name==BODY_NAME |
| || aattribute==CHARSET_NAME) | || low_element_name==CHARSET_NAME) |
| return; | return; |
| // fetch from & to from header for SMTP | // fetch from & to from header for SMTP |
| if(mi.from && aattribute=="from") | if(mi.from && low_element_name=="from") |
| *mi.from=&lmeaning.as_string(); | *mi.from=&extractEmail(element_value.as_string()); |
| if(mi.to && aattribute=="to") | if(mi.to && low_element_name=="to") |
| *mi.to=&lmeaning.as_string(); | *mi.to=&extractEmail(element_value.as_string()); |
| if(low_element_name=="errors-to") | |
| mi.errors_to=&extractEmail(element_value.as_string()); | |
| // append header line | // append header line |
| *mi.header << | *mi.header << |
| aattribute << ":" << | low_element_name << ":" << |
| attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER). | attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER). |
| cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) << | cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) << |
| "\n"; | "\n"; |
| } | } |
| Line 191 static const String& message_hash_to_str | Line 196 static const String& message_hash_to_str |
| from, to | from, to |
| }; | }; |
| message_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<Value *>(message_hash.get(*body_name))) { | if(Value *body_element=static_cast<Value *>(message_hash.get(*body_name))) { |
| if(Hash *body_hash=body_element->get_hash(&method_name)) { | if(Hash *body_hash=body_element->get_hash(&method_name)) { |
| Line 242 static const String& message_hash_to_str | Line 249 static const String& message_hash_to_str |
| "\n"; // header|body separator | "\n"; // header|body separator |
| const String& body=body_element->as_string(); | const String& body=body_element->as_string(); |
| const void *body_ptr=body.cstr(); // body | const char *body_ptr=body.cstr(String::UL_UNSPECIFIED); // body |
| size_t body_size=body.size(); // body | size_t body_size=strlen(body_ptr); // body |
| const void *mail_ptr; | const void *mail_ptr; |
| size_t mail_size; | size_t mail_size; |
| Charset::transcode(pool, | Charset::transcode(pool, |
| Line 264 static void sendmail(Request& r, const S | Line 271 static void sendmail(Request& r, const S |
| const String *from, const String *to) { | const String *from, const String *to) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| char *message_cstr=message.cstr(); | char *message_cstr=message.cstr(String::UL_UNSPECIFIED); |
| Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_base_class->name())); | Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_base_class->name())); |
| #ifdef _MSC_VER | const char *exception_type="email.format"; |
| if(!from) | if(!from) |
| throw Exception("parser.runtime", | throw Exception(exception_type, |
| &method_name, | &method_name, |
| "has no 'from' header specified"); | "parameter does not specify 'from' header field"); |
| if(!to) | if(!to) |
| throw Exception("parser.runtime", | throw Exception(exception_type, |
| &method_name, | &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); | SMTP& smtp=*new(pool) SMTP(pool, method_name); |
| Value *server_port; | Value *server_port; |
| Line 295 static void sendmail(Request& r, const S | Line 303 static void sendmail(Request& r, const S |
| "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined"); | "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined"); |
| #else | #else |
| // unix | // unix |
| // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default | // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default |
| // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t"] default | // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default |
| const String *sendmail_command; | const String *sendmail_command; |
| #ifdef PA_FORCED_SENDMAIL | #ifdef PA_FORCED_SENDMAIL |
| Line 315 static void sendmail(Request& r, const S | Line 323 static void sendmail(Request& r, const S |
| String *test=new(pool) String(pool, "/usr/sbin/sendmail"); | String *test=new(pool) String(pool, "/usr/sbin/sendmail"); |
| if(!file_executable(*test)) | if(!file_executable(*test)) |
| test=new(pool) String(pool, "/usr/lib/sendmail"); | test=new(pool) String(pool, "/usr/lib/sendmail"); |
| test->APPEND_CONST(" -ti"); | test->APPEND_CONST(" -t -i -f postmaster"); |
| sendmail_command=test; | sendmail_command=test; |
| } | } |
| #endif | #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); | Array argv(pool); |
| const String *file_spec; | const String *file_spec; |
| int after_file_spec=sendmail_command->pos(" ", 1); | int after_file_spec=sendmail_command->pos(" ", 1); |
| Line 333 static void sendmail(Request& r, const S | Line 350 static void sendmail(Request& r, const S |
| } | } |
| if(!file_executable(*file_spec)) | if(!file_executable(*file_spec)) |
| throw Exception(0, | throw Exception("email.send", |
| file_spec, | file_spec, |
| "is not executable." | "is not executable." |
| #ifdef PA_FORCED_SENDMAIL | #ifdef PA_FORCED_SENDMAIL |
| " Use configure key \"--with-sendmail=appropriate sendmail command\"" | " Use configure key \"--with-sendmail=appropriate sendmail command\"" |
| #else | #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 | sendmailkey_cstr |
| #endif | #endif |
| ); | ); |
| Line 358 static void sendmail(Request& r, const S | Line 375 static void sendmail(Request& r, const S |
| &argv, | &argv, |
| in, out, err); | in, out, err); |
| if(exit_status || err.size()) | if(exit_status || err.size()) |
| throw Exception(0, | throw Exception("email.send", |
| &method_name, | &method_name, |
| "'%s' reported problem: %s (%d)", | "'%s' reported problem: %s (%d)", |
| file_spec->cstr(), | file_spec->cstr(), |
| Line 402 void MMail::configure_user(Request& r) { | Line 419 void MMail::configure_user(Request& r) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // $MAIN:MAIL[$SMTP[mail.design.ru]] | // $MAIN:MAIL[$SMTP[mail.design.ru]] |
| if(Value *mail_element=r.main_class->get_element(mail_name)) | if(Value *mail_element=r.main_class->get_element(mail_name, r.main_class, false)) |
| 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 |