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