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