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