Annotation of parser3/src/classes/mail.C, revision 1.66
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.1 paf 6:
1.66 ! paf 7: $Id: mail.C,v 1.65 2002/06/12 12:50:32 paf Exp $
1.1 paf 8: */
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.50 paf 18:
19: #ifdef _MSC_VER
20: # include "smtp/smtp.h"
21: #endif
1.4 paf 22:
1.23 paf 23: // defines
1.1 paf 24:
1.23 paf 25: #define MAIL_CLASS_NAME "mail"
26:
1.25 paf 27: #define MAIL_NAME "MAIL"
28:
29: // global variable
30:
31: Methoded *mail_class;
32:
1.47 paf 33: // consts
34:
35: const int ATTACHMENT_WEIGHT=100;
36:
1.23 paf 37: // class
38:
39: class MMail : public Methoded {
40: public:
41: MMail(Pool& pool);
1.24 paf 42: public: // Methoded
1.23 paf 43: bool used_directly() { return true; }
1.24 paf 44: void configure_user(Request& r);
1.25 paf 45: private:
46: String mail_name;
1.23 paf 47: };
1.1 paf 48:
1.7 paf 49: // helpers
50:
1.10 paf 51: // uuencode
52:
53: static unsigned char uue_table[64] = {
54: '`', '!', '"', '#', '$', '%', '&', '\'',
55: '(', ')', '*', '+', ',', '-', '.', '/',
56: '0', '1', '2', '3', '4', '5', '6', '7',
57: '8', '9', ':', ';', '<', '=', '>', '?',
58: '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
59: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
60: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
61: 'X', 'Y', 'Z', '[', '\\',']', '^', '_'
62: };
1.8 paf 63: static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
1.10 paf 64: //header
65: result << "content-transfer-encoding: x-uuencode\n" << "\n";
66: result << "begin 644 " << file_name_cstr << "\n";
67:
68: //body
1.16 paf 69: const unsigned char *in=(const unsigned char *)vfile.value_ptr();
70: size_t in_length=vfile.value_size();
1.10 paf 71:
72: int count=45;
1.16 paf 73: for(const unsigned char *itemp=in; itemp<(in+in_length); itemp+=count) {
74: int index;
1.10 paf 75:
76: if((itemp+count)>(in+in_length))
77: count=in_length-(itemp-in);
78:
79: char *buf=(char *)result.pool().malloc(MAX_STRING);
80: char *optr=buf;
81:
82: /*
83: * for UU and XX, encode the number of bytes as first character
84: */
85: *optr++ = uue_table[count];
86:
87: for (index=0; index<=count-3; index+=3) {
88: *optr++ = uue_table[itemp[index] >> 2];
89: *optr++ = uue_table[((itemp[index ] & 0x03) << 4) | (itemp[index+1] >> 4)];
90: *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
91: *optr++ = uue_table[ itemp[index+2] & 0x3f];
92: }
93:
94: /*
95: * Special handlitempg for itempcomplete litempes
96: */
97: if (index != count) {
98: if (count - index == 2) {
99: *optr++ = uue_table[itemp[index] >> 2];
100: *optr++ = uue_table[((itemp[index ] & 0x03) << 4) |
101: ( itemp[index+1] >> 4)];
102: *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
103: *optr++ = uue_table[0];
104: }
105: else if (count - index == 1) {
106: *optr++ = uue_table[ itemp[index] >> 2];
107: *optr++ = uue_table[(itemp[index] & 0x03) << 4];
108: *optr++ = uue_table[0];
109: *optr++ = uue_table[0];
110: }
111: }
112: /*
113: * end of line
114: */
115: *optr++ = '\n';
116: *optr = 0;
117: result << buf;
118: }
119:
120: //footer
1.21 paf 121: result.APPEND_AS_IS((const char *)uue_table, 1/* one char */, 0, 0) << "\n"
1.10 paf 122: "end\n";
1.8 paf 123: }
124:
1.33 parser 125: /** ^mail:send[$attach[$type[uue|mime64] $value[DATA]]]
126: @todo solve - bad with html attaches
127: */
1.7 paf 128: static const String& attach_hash_to_string(Request& r, const String& origin_string,
129: Hash& attach_hash) {
130: Pool& pool=r.pool();
131:
1.46 paf 132: Value *vformat=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "format")));
1.7 paf 133:
1.8 paf 134: const VFile *vfile;
135: if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
1.17 paf 136: vfile=value->as_vfile(String::UL_AS_IS); // bad with html attaches. todo: solve
1.8 paf 137: else
1.60 paf 138: throw Exception("parser.runtime",
1.7 paf 139: &origin_string,
140: "has no $value");
141:
142: const String *file_name;
143: if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8 paf 144: *new(pool) String(pool, "file-name")))) // specified $file-name
1.7 paf 145: file_name=&vfile_name->as_string();
1.16 paf 146: else // no $file-name, VFile surely knows name
147: file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
1.48 paf 148: const char *file_name_cstr=file_name->cstr();
1.7 paf 149:
150: String& result=*new(pool) String(pool);
151:
1.10 paf 152: // content-type: application/octet-stream
1.64 paf 153: result << "content-type: " << r.mime_type_of(file_name_cstr)
154: << "; name=\"" << file_name_cstr << "\"\n";
1.7 paf 155: // content-disposition: attachment; filename="user_file_name"
1.10 paf 156: result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7 paf 157:
1.46 paf 158: const String *type=vformat?&vformat->as_string():0;
159: if(!type/*default = uue*/ || *type=="uue") {
1.8 paf 160: uuencode(result, file_name_cstr, *vfile);
1.46 paf 161: } else // for now
1.60 paf 162: throw Exception("parser.runtime",
1.46 paf 163: type,
164: "unknown attachment encode format");
1.7 paf 165:
166: return result;
167: }
168:
1.1 paf 169:
1.36 parser 170: #ifndef DOXYGEN
1.1 paf 171: struct Mail_info {
1.61 paf 172: Charset *charset; const char *content_charset_name;
1.1 paf 173: String *header;
1.2 paf 174: const String **from, **to;
1.1 paf 175: };
1.36 parser 176: #endif
1.1 paf 177: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning,
178: void *info) {
179:
180: Value& lmeaning=*static_cast<Value *>(ameaning);
181: Mail_info& mi=*static_cast<Mail_info *>(info);
1.2 paf 182:
183: // exclude one attribute [body]
1.45 paf 184: if(aattribute==BODY_NAME
185: || aattribute==CHARSET_NAME)
1.1 paf 186: return;
187:
1.2 paf 188: // fetch from & to from header for SMTP
189: if(mi.from && aattribute=="from")
190: *mi.from=&lmeaning.as_string();
191: if(mi.to && aattribute=="to")
192: *mi.to=&lmeaning.as_string();
193:
194: // append header line
1.10 paf 195: *mi.header <<
196: aattribute << ":" <<
1.20 paf 197: attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER).
1.61 paf 198: cstr(String::UL_UNSPECIFIED, 0, mi.charset, mi.content_charset_name) <<
1.10 paf 199: "\n";
1.2 paf 200: }
1.22 paf 201:
1.36 parser 202: #ifndef DOXYGEN
1.28 paf 203: struct Mail_seq_item {
1.47 paf 204: int weight;
205: const String *name;
206: Value *value;
1.2 paf 207: };
1.36 parser 208: #endif
1.47 paf 209: static int get_part_name_weight(const Hash::Key& part_name) {
210: const char *cstr=part_name.cstr();
211: int offset=0;
212: if(strncmp(cstr, "text", 4)==0) {
213: cstr+=4;
214: } else if(strncmp(cstr, "attach", 6)==0) {
215: cstr+=6;
216: offset=ATTACHMENT_WEIGHT;
217: } else
1.60 paf 218: throw Exception("parser.runtime",
1.47 paf 219: &part_name,
220: "is neither text# nor attach#");
221:
222: char *error_pos;
223: return strtol(cstr, &error_pos, 10)+offset;
224: }
1.7 paf 225: static void add_part(const Hash::Key& part_name, Hash::Val *part_value,
1.2 paf 226: void *info) {
1.28 paf 227: Mail_seq_item **seq_ref=static_cast<Mail_seq_item **>(info);
1.47 paf 228: (**seq_ref).weight=get_part_name_weight(part_name);
229: (**seq_ref).name=&part_name;
230: (**seq_ref).value=static_cast<Value *>(part_value);
1.2 paf 231: (*seq_ref)++;
232: }
1.47 paf 233: static int key_of_part(const void *item) {
234: return static_cast<const Mail_seq_item *>(item)->weight;
1.2 paf 235: }
236: static int sort_cmp_string_double_value(const void *a, const void *b) {
1.47 paf 237: return key_of_part(a)-key_of_part(b);
1.1 paf 238: }
1.2 paf 239: static const String& letter_hash_to_string(Request& r, const String& method_name,
240: Hash& letter_hash, int level,
241: const String **from, const String **to) {
242: Pool& pool=r.pool();
243:
244: // prepare header: 'hash' without "body"
245: String& result=*new(pool) String(pool);
1.20 paf 246:
1.45 paf 247: Charset *charset;
1.61 paf 248: if(Value *vrecodecharset_name=static_cast<Value *>(letter_hash.get(*charset_name)))
249: charset=&charsets->get_charset(vrecodecharset_name->as_string());
1.45 paf 250: else
251: charset=&pool.get_source_charset();
1.20 paf 252:
1.61 paf 253: const char *content_charset_name=0;
254: if(Value *vcontent_type=static_cast<Value *>(letter_hash.get(*content_type_name)))
255: if(Hash *hcontent_type=vcontent_type->get_hash(0))
256: if(Value *vcontentcharset_name=static_cast<Value *>(hcontent_type->get(*charset_name)))
257: content_charset_name=vcontentcharset_name->as_string().cstr();
258:
1.46 paf 259: if(from)
260: *from=0;
261: if(to)
262: *to=0;
1.2 paf 263: Mail_info mail_info={
1.61 paf 264: charset, content_charset_name,
1.2 paf 265: &result,
266: from, to
267: };
268: letter_hash.for_each(add_header_attribute, &mail_info);
269:
270: if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
1.42 parser 271: if(Hash *body_hash=body_element->get_hash(&method_name)) {
1.3 paf 272: // body parts..
273: // ..collect
1.35 parser 274: Mail_seq_item *seq=(Mail_seq_item *)pool.malloc(sizeof(Mail_seq_item)*body_hash->size());
1.28 paf 275: Mail_seq_item *seq_ref=seq; body_hash->for_each(add_part, &seq_ref);
1.3 paf 276: // ..sort
1.28 paf 277: _qsort(seq, body_hash->size(), sizeof(Mail_seq_item),
1.2 paf 278: sort_cmp_string_double_value);
1.48 paf 279:
280: bool multipart=body_hash->size()>1;
281: // header
282: char *boundary=0;
283: if(multipart) {
284: boundary=(char *)pool.malloc(MAX_NUMBER);
285: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
286: // multi-part
287: result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
288: result << "\n"
289: "This is a multi-part message in MIME format.";
290: }
291:
1.3 paf 292: // ..insert in 'seq' order
1.2 paf 293: for(int i=0; i<body_hash->size(); i++) {
1.48 paf 294: if(multipart) {
295: // intermediate boundary
296: result << "\n--" << boundary << "\n";
297: }
1.2 paf 298:
1.47 paf 299: if(Hash *part_hash=seq[i].value->get_hash(&method_name))
300: if(seq[i].weight>=ATTACHMENT_WEIGHT)
301: result << attach_hash_to_string(r, *seq[i].name, *part_hash);
1.7 paf 302: else
1.10 paf 303: result << letter_hash_to_string(r, method_name, *part_hash,
1.7 paf 304: level+1, 0, 0);
1.2 paf 305: else
1.60 paf 306: throw Exception("parser.runtime",
1.47 paf 307: seq[i].name,
1.2 paf 308: "part is not hash");
309: }
310:
311: // finish boundary
1.48 paf 312: if(multipart) {
313: result << "\n--" << boundary << "--\n";
314: }
1.2 paf 315: } else {
1.10 paf 316: result <<
1.45 paf 317: "\n"; // header|body separator
318:
319: const String& body=body_element->as_string();
320: const void *body_ptr=body.cstr(); // body
321: size_t body_size=body.size(); // body
322: const void *mail_ptr;
323: size_t mail_size;
324: Charset::transcode(pool,
325: pool.get_source_charset(), body_ptr, body_size,
326: *charset/*always set - either mail.charset or $request:charset*/, mail_ptr, mail_size);
327: result.APPEND_CLEAN((const char*)mail_ptr, mail_size, 0, 0);
1.2 paf 328: }
329: } else
1.60 paf 330: throw Exception("parser.runtime",
1.2 paf 331: &method_name,
332: "has no $body");
333:
334: return result;
335: }
336:
1.4 paf 337: static void sendmail(Request& r, const String& method_name,
338: const String& letter,
339: const String *from, const String *to) {
340: Pool& pool=r.pool();
341:
1.12 paf 342: char *letter_cstr=letter.cstr();
1.25 paf 343: Hash *mail_conf=static_cast<Hash *>(r.classes_conf.get(mail_class->name()));
1.12 paf 344:
1.19 paf 345: #ifdef _MSC_VER
1.4 paf 346: if(!from)
1.60 paf 347: throw Exception("parser.runtime",
1.4 paf 348: &method_name,
1.15 paf 349: "has no 'from' header specified");
1.4 paf 350: if(!to)
1.60 paf 351: throw Exception("parser.runtime",
1.4 paf 352: &method_name,
1.15 paf 353: "has no 'to' header specified");
1.4 paf 354:
355: SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5 paf 356: Value *server_port;
1.29 parser 357: // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.25 paf 358: if(mail_conf &&
359: (server_port=static_cast<Value *>(mail_conf->get(
1.5 paf 360: *new(pool) String(pool, "SMTP"))))) {
361: char *server=server_port->as_string().cstr();
1.11 paf 362: const char *port=rsplit(server, ':');
1.4 paf 363: if(!port)
1.11 paf 364: port="25";
1.4 paf 365:
366: smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
367: } else
1.60 paf 368: throw Exception("parser.runtime",
1.4 paf 369: &method_name,
1.13 paf 370: "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4 paf 371: #else
1.12 paf 372: // unix
1.51 paf 373: // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t"] default
374: // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t"] default
1.12 paf 375:
1.55 paf 376: const String *sendmail_command;
377: #ifdef PA_FORCED_SENDMAIL
1.58 paf 378: sendmail_command=new(pool) String(pool, PA_FORCED_SENDMAIL);
1.55 paf 379: #else
1.51 paf 380: const char *sendmailkey_cstr="sendmail";
381: if(mail_conf) {
382: if(Value *sendmail_value=static_cast<Value *>(mail_conf->get(String(pool, sendmailkey_cstr))))
1.52 paf 383: sendmail_command=&sendmail_value->as_string();
1.51 paf 384: else
1.60 paf 385: throw Exception("parser.runtime",
1.36 parser 386: &method_name,
1.51 paf 387: "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined",
388: sendmailkey_cstr);
389: } else {
1.52 paf 390: String *test=new(pool) String(pool, "/usr/sbin/sendmail");
391: if(!file_executable(*test))
392: test=new(pool) String(pool, "/usr/lib/sendmail");
1.66 ! paf 393: test->APPEND_CONST(" -ti");
1.52 paf 394: sendmail_command=test;
1.51 paf 395: }
1.55 paf 396: #endif
1.51 paf 397:
1.52 paf 398: // we know sendmail_command here
1.51 paf 399: Array argv(pool);
400: const String *file_spec;
1.52 paf 401: int after_file_spec=sendmail_command->pos(" ", 1);
1.51 paf 402: if(after_file_spec<=0)
1.52 paf 403: file_spec=sendmail_command;
1.51 paf 404: else {
1.52 paf 405: size_t pos_after=after_file_spec;
406: file_spec=&sendmail_command->mid(0, pos_after++);
407: sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS);
1.36 parser 408: }
1.51 paf 409:
1.52 paf 410: if(!file_executable(*file_spec))
1.60 paf 411: throw Exception(0,
1.55 paf 412: file_spec,
413: "is not executable."
414: #ifdef PA_FORCED_SENDMAIL
1.59 paf 415: " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58 paf 416: #else
1.55 paf 417: " Set $"MAIN_CLASS_NAME":"MAIL_NAME".%s with appropriate sendmail command",
418: sendmailkey_cstr
419: #endif
420: );
421:
1.51 paf 422:
423: String in(pool, letter_cstr); String out(pool); String err(pool);
1.56 paf 424: int exit_status=pa_exec(
425: // forced_allow
426: #ifdef PA_FORCED_SENDMAIL
427: true
428: #else
429: false
430: #endif
1.57 paf 431: , *file_spec,
1.51 paf 432: 0/*default env*/,
433: &argv,
434: in, out, err);
435: if(exit_status || err.size())
1.60 paf 436: throw Exception(0,
1.51 paf 437: &method_name,
438: "'%s' reported problem: %s (%d)",
439: file_spec->cstr(),
440: err.size()?err.cstr():"UNKNOWN",
441: exit_status);
1.4 paf 442: #endif
443: }
444:
1.7 paf 445:
446: // methods
447:
1.18 paf 448: static void _send(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 449: Pool& pool=r.pool();
450:
1.32 parser 451: Value& vhash=params->as_no_junction(0, "message must not be code");
1.42 parser 452: Hash *hash=vhash.get_hash(&method_name);
1.1 paf 453: if(!hash)
1.60 paf 454: throw Exception("parser.runtime",
1.1 paf 455: &method_name,
456: "message must be hash");
457:
1.2 paf 458: const String *from, *to;
1.4 paf 459: const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1 paf 460:
1.21 paf 461: sendmail(r, method_name, letter, from, to);
1.6 paf 462: }
463:
1.24 paf 464: // constructor & configurator
1.23 paf 465:
1.63 paf 466: MMail::MMail(Pool& apool) : Methoded(apool, MAIL_CLASS_NAME),
1.64 paf 467: mail_name(apool, MAIL_NAME)
1.25 paf 468: {
1.27 paf 469: // ^mail:send{hash}
1.23 paf 470: add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.24 paf 471: }
472:
473: void MMail::configure_user(Request& r) {
474: Pool& pool=r.pool();
475:
476: // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.25 paf 477: if(Value *mail_element=r.main_class->get_element(mail_name))
1.42 parser 478: if(Hash *mail_conf=mail_element->get_hash(0))
1.25 paf 479: r.classes_conf.put(name(), mail_conf);
1.65 paf 480: else if(!mail_element->get_string())
1.60 paf 481: throw Exception("parser.runtime",
1.24 paf 482: 0,
1.65 paf 483: "$" MAIN_CLASS_NAME ":" MAIL_NAME " is not hash");
1.23 paf 484: }
485:
486: // creator
487:
488: Methoded *MMail_create(Pool& pool) {
489: return mail_class=new(pool) MMail(pool);
1.1 paf 490: }
E-mail: