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