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