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