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