Annotation of parser3/src/classes/mail.C, revision 1.83
1.1 paf 1: /** @file
2: Parser: @b mail parser class.
3:
1.53 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.54 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.73 paf 6: */
1.1 paf 7:
1.83 ! paf 8: static const char* IDENT_MAIL_C="$Date: 2002/12/02 10:07:38 $";
1.1 paf 9:
10: #include "pa_config_includes.h"
11:
12: #include "pa_common.h"
13: #include "pa_request.h"
1.6 paf 14: #include "pa_vfile.h"
1.12 paf 15: #include "pa_exec.h"
1.45 paf 16: #include "pa_charsets.h"
17: #include "pa_charset.h"
1.67 paf 18: #include "pa_uue.h"
1.50 paf 19:
20: #ifdef _MSC_VER
21: # include "smtp/smtp.h"
22: #endif
1.4 paf 23:
1.23 paf 24: // defines
1.1 paf 25:
1.23 paf 26: #define MAIL_CLASS_NAME "mail"
27:
1.25 paf 28: #define MAIL_NAME "MAIL"
29:
1.67 paf 30: // consts
1.25 paf 31:
1.67 paf 32: const int ATTACHMENT_WEIGHT=100;
1.25 paf 33:
1.67 paf 34: // global variable
1.47 paf 35:
1.67 paf 36: Methoded *mail_base_class;
1.47 paf 37:
1.23 paf 38: // class
39:
40: class MMail : public Methoded {
41: public:
42: MMail(Pool& pool);
1.24 paf 43: public: // Methoded
1.67 paf 44: bool used_directly() { return false; }
1.68 paf 45: void configure_user(Request& r);
1.25 paf 46: private:
47: String mail_name;
1.23 paf 48: };
1.1 paf 49:
1.33 parser 50: /** ^mail:send[$attach[$type[uue|mime64] $value[DATA]]]
51: @todo solve - bad with html attaches
52: */
1.7 paf 53: static const String& attach_hash_to_string(Request& r, const String& origin_string,
54: Hash& attach_hash) {
55: Pool& pool=r.pool();
56:
1.46 paf 57: Value *vformat=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "format")));
1.7 paf 58:
1.8 paf 59: const VFile *vfile;
60: if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
1.17 paf 61: vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve
1.8 paf 62: else
1.60 paf 63: throw Exception("parser.runtime",
1.7 paf 64: &origin_string,
65: "has no $value");
66:
67: const String *file_name;
68: if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8 paf 69: *new(pool) String(pool, "file-name")))) // specified $file-name
1.7 paf 70: file_name=&vfile_name->as_string();
1.16 paf 71: else // no $file-name, VFile surely knows name
72: file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.48 paf 73: const char *file_name_cstr=file_name->cstr();
1.7 paf 74:
75: String& result=*new(pool) String(pool);
76:
1.10 paf 77: // content-type: application/octet-stream
1.64 paf 78: result << "content-type: " << r.mime_type_of(file_name_cstr)
79: << "; name=\"" << file_name_cstr << "\"\n";
1.7 paf 80: // content-disposition: attachment; filename="user_file_name"
1.10 paf 81: result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7 paf 82:
1.46 paf 83: const String *type=vformat?&vformat->as_string():0;
84: if(!type/*default = uue*/ || *type=="uue") {
1.67 paf 85: pa_uuencode(result, file_name_cstr, *vfile);
1.46 paf 86: } else // for now
1.60 paf 87: throw Exception("parser.runtime",
1.46 paf 88: type,
89: "unknown attachment encode format");
1.7 paf 90:
91: return result;
92: }
93:
1.1 paf 94:
1.36 parser 95: #ifndef DOXYGEN
1.1 paf 96: struct Mail_info {
1.61 paf 97: Charset *charset; const char *content_charset_name;
1.1 paf 98: String *header;
1.2 paf 99: const String **from, **to;
1.72 paf 100: const String *errors_to;
1.83 ! paf 101: bool mime_version_specified;
1.1 paf 102: };
1.36 parser 103: #endif
1.70 paf 104:
105: const String& extractEmail(const String& string);
1.72 paf 106: static void add_header_attribute(const Hash::Key& raw_element_name, Hash::Val *aelement_value,
1.1 paf 107: void *info) {
1.72 paf 108: Value& element_value=*static_cast<Value *>(aelement_value);
109: const String& low_element_name=raw_element_name.change_case(raw_element_name.pool(), String::CC_LOWER);
1.1 paf 110: Mail_info& mi=*static_cast<Mail_info *>(info);
1.2 paf 111:
112: // exclude one attribute [body]
1.72 paf 113: if(low_element_name==BODY_NAME
114: || low_element_name==CHARSET_NAME)
1.1 paf 115: return;
116:
1.2 paf 117: // fetch from & to from header for SMTP
1.72 paf 118: if(mi.from && low_element_name=="from")
119: *mi.from=&extractEmail(element_value.as_string());
120: if(mi.to && low_element_name=="to")
121: *mi.to=&extractEmail(element_value.as_string());
122: if(low_element_name=="errors-to")
123: mi.errors_to=&extractEmail(element_value.as_string());
1.83 ! paf 124: if(low_element_name=="mime-version")
! 125: mi.mime_version_specified=true;
1.2 paf 126:
127: // append header line
1.10 paf 128: *mi.header <<
1.72 paf 129: low_element_name << ":" <<
1.82 paf 130: attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER, true).
1.61 paf 131: cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) <<
1.10 paf 132: "\n";
1.2 paf 133: }
1.22 paf 134:
1.36 parser 135: #ifndef DOXYGEN
1.28 paf 136: struct Mail_seq_item {
1.47 paf 137: int weight;
138: const String *name;
139: Value *value;
1.2 paf 140: };
1.36 parser 141: #endif
1.47 paf 142: static int get_part_name_weight(const Hash::Key& part_name) {
143: const char *cstr=part_name.cstr();
144: int offset=0;
145: if(strncmp(cstr, "text", 4)==0) {
146: cstr+=4;
147: } else if(strncmp(cstr, "attach", 6)==0) {
148: cstr+=6;
149: offset=ATTACHMENT_WEIGHT;
150: } else
1.60 paf 151: throw Exception("parser.runtime",
1.47 paf 152: &part_name,
153: "is neither text# nor attach#");
154:
155: char *error_pos;
156: return strtol(cstr, &error_pos, 10)+offset;
157: }
1.7 paf 158: static void add_part(const Hash::Key& part_name, Hash::Val *part_value,
1.2 paf 159: void *info) {
1.28 paf 160: Mail_seq_item **seq_ref=static_cast<Mail_seq_item **>(info);
1.47 paf 161: (**seq_ref).weight=get_part_name_weight(part_name);
162: (**seq_ref).name=&part_name;
163: (**seq_ref).value=static_cast<Value *>(part_value);
1.2 paf 164: (*seq_ref)++;
165: }
1.47 paf 166: static int key_of_part(const void *item) {
167: return static_cast<const Mail_seq_item *>(item)->weight;
1.2 paf 168: }
169: static int sort_cmp_string_double_value(const void *a, const void *b) {
1.47 paf 170: return key_of_part(a)-key_of_part(b);
1.1 paf 171: }
1.67 paf 172: static const String& message_hash_to_string(Request& r, const String& method_name,
173: Hash& message_hash, int level,
1.2 paf 174: const String **from, const String **to) {
175: Pool& pool=r.pool();
176:
177: // prepare header: 'hash' without "body"
178: String& result=*new(pool) String(pool);
1.20 paf 179:
1.45 paf 180: Charset *charset;
1.67 paf 181: if(Value *vrecodecharset_name=static_cast<Value *>(message_hash.get(*charset_name)))
1.61 paf 182: charset=&charsets->get_charset(vrecodecharset_name->as_string());
1.45 paf 183: else
184: charset=&pool.get_source_charset();
1.20 paf 185:
1.61 paf 186: const char *content_charset_name=0;
1.67 paf 187: if(Value *vcontent_type=static_cast<Value *>(message_hash.get(*content_type_name)))
1.61 paf 188: if(Hash *hcontent_type=vcontent_type->get_hash(0))
189: if(Value *vcontentcharset_name=static_cast<Value *>(hcontent_type->get(*charset_name)))
190: content_charset_name=vcontentcharset_name->as_string().cstr();
191:
1.46 paf 192: if(from)
193: *from=0;
194: if(to)
195: *to=0;
1.2 paf 196: Mail_info mail_info={
1.61 paf 197: charset, content_charset_name,
1.2 paf 198: &result,
199: from, to
200: };
1.67 paf 201: message_hash.for_each(add_header_attribute, &mail_info);
1.72 paf 202: if(!mail_info.errors_to)
203: result << "errors-to: postmaster\n"; // errors-to: default
1.83 ! paf 204: if(!mail_info.mime_version_specified)
! 205: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.2 paf 206:
1.67 paf 207: if(Value *body_element=static_cast<Value *>(message_hash.get(*body_name))) {
1.42 parser 208: if(Hash *body_hash=body_element->get_hash(&method_name)) {
1.3 paf 209: // body parts..
210: // ..collect
1.35 parser 211: Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());
1.28 paf 212: Mail_seq_item *seq_ref=seq; body_hash->for_each(add_part, &seq_ref);
1.3 paf 213: // ..sort
1.28 paf 214: _qsort(seq, body_hash->size(), sizeof(Mail_seq_item),
1.2 paf 215: sort_cmp_string_double_value);
1.48 paf 216:
217: bool multipart=body_hash->size()>1;
218: // header
219: char *boundary=0;
220: if(multipart) {
221: boundary=(char *)pool.malloc(MAX_NUMBER);
222: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
223: // multi-part
224: result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
225: result << "\n"
226: "This is a multi-part message in MIME format.";
227: }
228:
1.3 paf 229: // ..insert in 'seq' order
1.2 paf 230: for(int i=0; i<body_hash->size(); i++) {
1.48 paf 231: if(multipart) {
232: // intermediate boundary
233: result << "\n--" << boundary << "\n";
234: }
1.2 paf 235:
1.47 paf 236: if(Hash *part_hash=seq[i].value->get_hash(&method_name))
237: if(seq[i].weight>=ATTACHMENT_WEIGHT)
238: result << attach_hash_to_string(r, *seq[i].name, *part_hash);
1.7 paf 239: else
1.67 paf 240: result << message_hash_to_string(r, method_name, *part_hash,
1.7 paf 241: level+1, 0, 0);
1.2 paf 242: else
1.60 paf 243: throw Exception("parser.runtime",
1.47 paf 244: seq[i].name,
1.2 paf 245: "part is not hash");
246: }
247:
248: // finish boundary
1.48 paf 249: if(multipart) {
250: result << "\n--" << boundary << "--\n";
251: }
1.2 paf 252: } else {
1.10 paf 253: result <<
1.45 paf 254: "\n"; // header|body separator
255:
256: const String& body=body_element->as_string();
1.76 paf 257: const char *body_ptr=body.cstr(String::UL_UNSPECIFIED); // body
258: size_t body_size=strlen(body_ptr); // body
1.45 paf 259: const void *mail_ptr;
260: size_t mail_size;
261: Charset::transcode(pool,
262: pool.get_source_charset(), body_ptr, body_size,
263: *charset/*always set - either mail.charset or $request:charset*/, mail_ptr, mail_size);
264: result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);
1.2 paf 265: }
266: } else
1.60 paf 267: throw Exception("parser.runtime",
1.2 paf 268: &method_name,
269: "has no $body");
270:
271: return result;
272: }
273:
1.4 paf 274: static void sendmail(Request& r, const String& method_name,
1.67 paf 275: const String& message,
1.4 paf 276: const String *from, const String *to) {
277: Pool& pool=r.pool();
278:
1.76 paf 279: char *message_cstr=message.cstr(String::UL_UNSPECIFIED);
1.67 paf 280: Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_base_class->name()));
1.12 paf 281:
1.75 paf 282: const char *exception_type="email.format";
1.4 paf 283: if(!from)
1.75 paf 284: throw Exception(exception_type,
1.4 paf 285: &method_name,
1.70 paf 286: "parameter does not specify 'from' header field");
1.4 paf 287: if(!to)
1.75 paf 288: throw Exception(exception_type,
1.4 paf 289: &method_name,
1.70 paf 290: "parameter does not specify 'to' header field");
291: #ifdef _MSC_VER
1.4 paf 292:
293: SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5 paf 294: Value *server_port;
1.29 parser 295: // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.25 paf 296: if(mail_conf &&
297: (server_port=static_cast<Value *>(mail_conf->get(
1.5 paf 298: *new(pool) String(pool, "SMTP"))))) {
299: char *server=server_port->as_string().cstr();
1.11 paf 300: const char *port=rsplit(server, ':');
1.4 paf 301: if(!port)
1.11 paf 302: port="25";
1.4 paf 303:
1.67 paf 304: smtp.Send(server, port, message_cstr, from->cstr(), to->cstr());
1.4 paf 305: } else
1.60 paf 306: throw Exception("parser.runtime",
1.4 paf 307: &method_name,
1.13 paf 308: "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4 paf 309: #else
1.12 paf 310: // unix
1.70 paf 311: // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
312: // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default
1.12 paf 313:
1.55 paf 314: const String *sendmail_command;
315: #ifdef PA_FORCED_SENDMAIL
1.58 paf 316: sendmail_command=new(pool) String(pool, PA_FORCED_SENDMAIL);
1.55 paf 317: #else
1.51 paf 318: const char *sendmailkey_cstr="sendmail";
319: if(mail_conf) {
320: if(Value *sendmail_value=static_cast<Value *>(mail_conf->get(String(pool, sendmailkey_cstr))))
1.52 paf 321: sendmail_command=&sendmail_value->as_string();
1.51 paf 322: else
1.60 paf 323: throw Exception("parser.runtime",
1.36 parser 324: &method_name,
1.51 paf 325: "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined",
326: sendmailkey_cstr);
327: } else {
1.52 paf 328: String *test=new(pool) String(pool, "/usr/sbin/sendmail");
329: if(!file_executable(*test))
330: test=new(pool) String(pool, "/usr/lib/sendmail");
1.70 paf 331: test->APPEND_CONST(" -t -i -f postmaster");
1.52 paf 332: sendmail_command=test;
1.51 paf 333: }
1.55 paf 334: #endif
1.51 paf 335:
1.70 paf 336: // we know sendmail_command here, should replace "postmaster" with "$from" from message
337: int at_postmaster=sendmail_command->pos("postmaster");
338: if(at_postmaster>0) {
339: String& reconstructed=sendmail_command->mid(0, at_postmaster);
1.71 paf 340: reconstructed << *from;
341: reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->size());
1.70 paf 342: sendmail_command=&reconstructed;
343: }
344:
345: // execute it
1.51 paf 346: Array argv(pool);
347: const String *file_spec;
1.52 paf 348: int after_file_spec=sendmail_command->pos(" ", 1);
1.51 paf 349: if(after_file_spec<=0)
1.52 paf 350: file_spec=sendmail_command;
1.51 paf 351: else {
1.52 paf 352: size_t pos_after=after_file_spec;
353: file_spec=&sendmail_command->mid(0, pos_after++);
354: sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS);
1.36 parser 355: }
1.51 paf 356:
1.52 paf 357: if(!file_executable(*file_spec))
1.75 paf 358: throw Exception("email.send",
1.55 paf 359: file_spec,
360: "is not executable."
361: #ifdef PA_FORCED_SENDMAIL
1.59 paf 362: " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58 paf 363: #else
1.70 paf 364: " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s to appropriate sendmail command",
1.55 paf 365: sendmailkey_cstr
366: #endif
367: );
368:
1.51 paf 369:
1.67 paf 370: String in(pool, message_cstr); String out(pool); String err(pool);
1.56 paf 371: int exit_status=pa_exec(
372: // forced_allow
373: #ifdef PA_FORCED_SENDMAIL
374: true
375: #else
376: false
377: #endif
1.57 paf 378: , *file_spec,
1.51 paf 379: 0/*default env*/,
380: &argv,
381: in, out, err);
382: if(exit_status || err.size())
1.75 paf 383: throw Exception("email.send",
1.51 paf 384: &method_name,
385: "'%s' reported problem: %s (%d)",
386: file_spec->cstr(),
387: err.size()?err.cstr():"UNKNOWN",
388: exit_status);
1.4 paf 389: #endif
390: }
391:
1.7 paf 392:
393: // methods
394:
1.18 paf 395: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 396: Pool& pool=r.pool();
397:
1.32 parser 398: Value& vhash=params->as_no_junction(0, "message must not be code");
1.42 parser 399: Hash *hash=vhash.get_hash(&method_name);
1.1 paf 400: if(!hash)
1.60 paf 401: throw Exception("parser.runtime",
1.1 paf 402: &method_name,
403: "message must be hash");
404:
1.2 paf 405: const String *from, *to;
1.67 paf 406: const String& message=hash->get(*body_name)/*old format*/?
407: message_hash_to_string(r, method_name, *hash, 0, &from, &to) : // old
1.79 paf 408: static_cast<VMail *>(r.get_self())->message_hash_to_string(r, &method_name, hash, 0, &from, &to); // new
1.1 paf 409:
1.67 paf 410: //r.write_pass_lang(message);
411: sendmail(r, method_name, message, from, to);
1.6 paf 412: }
413:
1.24 paf 414: // constructor & configurator
1.23 paf 415:
1.63 paf 416: MMail::MMail(Pool& apool) : Methoded(apool, MAIL_CLASS_NAME),
1.64 paf 417: mail_name(apool, MAIL_NAME)
1.25 paf 418: {
1.27 paf 419: // ^mail:send{hash}
1.23 paf 420: add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68 paf 421: }
422:
423: void MMail::configure_user(Request& r) {
424: Pool& pool=r.pool();
425:
426: // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.81 paf 427: if(Value *mail_element=r.main_class.get_element(mail_name, r.main_class, false))
1.68 paf 428: if(Hash *mail_conf=mail_element->get_hash(0))
429: r.classes_conf.put(name(), mail_conf);
430: else
1.69 paf 431: if( !mail_element->is_string() )
432: throw Exception("parser.runtime",
433: 0,
434: "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.24 paf 435: }
436:
1.23 paf 437: // creator
438:
439: Methoded *MMail_create(Pool& pool) {
1.67 paf 440: return mail_base_class=new(pool) MMail(pool);
1.1 paf 441: }
E-mail: