Annotation of parser3/src/types/pa_vmail.C, revision 1.22
1.1 paf 1: /** @file
2: Parser: @b mail class.
3: relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
4:
5: Copyright(c) 2001, 2002 ArtLebedev Group(http://www.artlebedev.com)
6: Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
7: */
1.13 paf 8:
1.22 ! paf 9: static const char* IDENT_VMAIL_C="$Date: 2002/08/14 13:00:41 $";
1.1 paf 10:
11: #include "pa_sapi.h"
12: #include "pa_vmail.h"
13: #include "pa_vstring.h"
14: #include "pa_request.h"
15: #include "pa_common.h"
16: #include "pa_charset.h"
17: #include "pa_charsets.h"
18: #include "pa_vdate.h"
19: #include "pa_vfile.h"
20: #include "pa_uue.h"
21:
1.3 paf 22: #ifdef WITH_MAILRECEIVE
1.4 paf 23: extern "C" {
1.1 paf 24: #include "gmime.h"
1.4 paf 25: }
1.1 paf 26: #endif
27:
28: // defines
29:
30: #define RAW_NAME "raw"
31:
32: // internals
33:
34: enum PartType {
35: P_TEXT,
36: P_HTML,
37: P_FILE,
38: P_MESSAGE,
39:
40: P_TYPES_COUNT
41: };
42:
43: static const char * const part_name_starts[P_TYPES_COUNT]={"text", "html", "file", "message"};
44:
45: // VMail
46:
47: extern Methoded *mail_base_class;
48:
49: VMail::VMail(Pool& apool) : VStateless_class(apool, 0, mail_base_class),
50: vreceived(apool) {
51: }
52:
1.3 paf 53: #ifdef WITH_MAILRECEIVE
1.1 paf 54:
55: static const String& maybeUpperCase(Pool& pool, const String& src, bool toUpperCase) {
56: return toUpperCase?src.change_case(pool, String::CC_UPPER):src;
57: }
58:
59: static void UTF8toSource(Pool& pool, const char *source_body, size_t source_content_length,
60: const void *& dest_body, size_t& dest_content_length) {
61: if(source_body) {
62: if(!source_content_length)
63: source_content_length=strlen(source_body);
64: Charset::transcode(pool,
65: *utf8_charset, source_body, source_content_length,
66: pool.get_source_charset(), dest_body, dest_content_length);
67: } else {
68: dest_body=0;
69: dest_content_length=0;
70: }
71: }
72:
73: static void putReceived(Hash& received, const char *name, Value *value, bool nameToUpperCase=false) {
74: Pool& pool=received.pool();
75: if(name && value) {
76: received.put(
77: maybeUpperCase(pool, String::OnPool(pool, name, 0, true/*tainted*/), nameToUpperCase),
78: value);
79: }
80: }
81:
82: static void putReceived(Hash& received, const char *name, const char *value, size_t value_size=0, bool nameToUpperCase=false) {
83: if(value) {
84: Pool& pool=received.pool();
85:
86: const void *value_dest_body;
87: size_t value_dest_content_length;
1.21 paf 88: // UTF8toSource(pool, value, value_size, value_dest_body, value_dest_content_length);
89: value_dest_body=value;
90: value_dest_content_length=value_size;
1.1 paf 91:
92: putReceived(received, name,
93: new(pool) VString(
94: String::OnPool(pool,(const char *)value_dest_body, value_dest_content_length, true/*tainted*/)));
95: }
96: }
97:
98: static void putReceived(Hash& received, const char *name, time_t value) {
99: Pool& pool=received.pool();
100: if(name)
101: received.put(String::OnPool(pool, name, 0, true/*tainted*/), new(pool) VDate(pool, value));
102: }
103:
104: static void MimeHeaderField2received(const char *name, const char *value, gpointer data) {
105: Hash& received=*static_cast<Hash *>(data);
106:
107: putReceived(received, name, value, 0, true/*nameInUpperCase*/);
108: }
109:
110: static void parse(GMimeStream *stream, Hash& received);
111:
112: #ifndef DOXYGEN
113: struct MimePart2bodyInfo {
114: Hash *body;
115: int partCounts[P_TYPES_COUNT];
116: };
117: #endif
118: static void MimePart2body(GMimePart *part,
119: gpointer data) {
120: MimePart2bodyInfo& i=*static_cast<MimePart2bodyInfo *>(data);
121: Pool& pool=i.body->pool();
122:
123: if(const GMimeContentType *type=g_mime_part_get_content_type(part)) {
124: if(g_mime_content_type_is_type(type, "multipart", "*"))
125: return; // skipping frames
126:
127: PartType partType;
128: if(g_mime_content_type_is_type(type, "text", "plain"))
129: partType=P_TEXT;
130: else if(g_mime_content_type_is_type(type, "text", "html"))
131: partType=P_HTML;
132: else if(g_mime_content_type_is_type(type, "message", "*"))
133: partType=P_MESSAGE;
134: else
135: partType=P_FILE;
136:
137: // partName
138: const char *partName;
139: char partNameBuf[MAX_STRING];
140: const char *partNameStart=part_name_starts[partType];
141: if(int partNo=i.partCounts[partType]++) {
142: snprintf(partNameBuf, MAX_STRING, "%s%d", partNameStart, partNo);
143: partName=partNameBuf;
144: } else
145: partName=partNameStart;
146:
147: // $.partX[
148: VHash& vpartX=*new(pool) VHash(pool); putReceived(*i.body, partName, &vpartX);
149: Hash& partX=vpartX.hash(0);
150: {
151: // $.raw[
152: VHash& vraw=*new(pool) VHash(pool); putReceived(partX, RAW_NAME, &vraw);
153: g_mime_header_foreach(part->headers, MimeHeaderField2received, &vraw.hash(0));
154: }
155: const char *content_filename=0;
156: {
157: // $.content-type[
158: VHash& vcontent_type=*new(pool) VHash(pool); putReceived(partX, "content-type", &vcontent_type);
159: Hash& content_type=vcontent_type.hash(0);
160: {
161: // $.value[text/plain]
162: char value[MAX_STRING];
163: snprintf(value, MAX_STRING, "%s/%s",
164: type->type?type->type:"x-unknown",
165: type->subtype?type->subtype:"x-unknown");
166: putReceived(content_type, VALUE_NAME, value);
167: }
168: GMimeParam *param=type->params;
169: while(param) {
170: // $.charset[windows-1251] && co
171: putReceived(content_type, param->name, param->value, true);
172: if(strcasecmp(param->name, "name")==0)
173: content_filename=param->value;
174: param=param->next;
175: }
176: }
177: // $.description
178: putReceived(partX, "description", part->description);
179: // $.content-id
180: putReceived(partX, "content-id", part->content_id);
181: // $.content-md5
182: putReceived(partX, "content-md5", part->content_md5);
183: // $.content-location
184: putReceived(partX, "content-location", part->content_location);
185:
186: // todo GMimePart:
187: // GMimePartEncodingType encoding;
188: // GMimeDisposition *disposition;
189: if(part->disposition) {
190: GMimeParam *param=part->disposition->params;
191: while(param) {
192: // $.charset[windows-1251] && co
193: if(strcasecmp(param->name, "filename")==0)
194: content_filename=param->value;
195: param=param->next;
196: }
197: }
198:
199: // MESSAGE
200: if(partType==P_MESSAGE) {
201: if(part->content)
202: if(GMimeStream *stream=part->content->stream)
203: parse(stream, partX);
204: } else {
205: // $.value[string|file]
206: size_t buf_len;
1.15 paf 207: const void *local_buf=g_mime_part_get_content(part, &buf_len);
1.1 paf 208: if(partType==P_FILE) {
209: VFile& vfile=*new(pool) VFile(pool);
1.15 paf 210: char *global_buf=(char *)pool.malloc(buf_len);
211: memcpy(global_buf, local_buf, buf_len);
212: vfile.set(true/*tainted*/, global_buf, buf_len, content_filename);
1.1 paf 213: putReceived(partX, VALUE_NAME, &vfile);
214: } else {
215: // P_TEXT, P_HTML
1.15 paf 216: putReceived(partX, VALUE_NAME,(const char*)local_buf, buf_len);
1.1 paf 217: }
218: }
219: }
220: }
221:
222: static void parse(GMimeStream *stream, Hash& received) {
223: Pool& pool=received.pool();
224:
225: GMimeMessage *message=g_mime_parser_construct_message(stream);
226: try {
227: const GMimeMessageHeader *messageHeader=message->header;
228: if(!messageHeader)
229: return;
230:
231: // firstly user-defined strings go
232: // user headers
233: {
234: // $.raw[
1.5 paf 235: VHash& vraw=*new(pool) VHash(pool); putReceived(received, RAW_NAME, &vraw);
1.1 paf 236: g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &vraw.hash(0));
237: }
238:
239: // maybe-todo-recipients
240: // x(messageHeader->recipients)
241:
242: // secondly standard headers&body go
243: // standard header
244: // .from
245: putReceived(received, "from", messageHeader->from);
246: // .reply-to
247: putReceived(received, "reply-to", messageHeader->reply_to);
248: // .to
249: // todo: messageHeader->recipients
250: // .subject
251: putReceived(received, "subject", messageHeader->subject);
252: // .date(date+gmt_offset)
253: int tt_offset =
254: ((messageHeader->gmt_offset / 100) *(60 * 60))
255: +(messageHeader->gmt_offset % 100) * 60;
256: putReceived(received, "date",
257: messageHeader->date // local sender
258: -tt_offset // move local sender to GMT sender
259: -(timezone+(daylight?60*60*sign(timezone):0)) // move GMT sender to our local time
260: );
261: // .message-id
262: putReceived(received, "message-id", messageHeader->message_id);
263:
264: // .body[part/parts
265: GMimePart *part=message->mime_part;
266: const GMimeContentType *type=g_mime_part_get_content_type(part);
267: MimePart2bodyInfo info={&received};
268: g_mime_part_foreach(part, MimePart2body, &info);
269:
270: // normal unref
271: g_mime_object_unref(GMIME_OBJECT(message));
272: } catch(...) {
273: // abnormal unref
274: g_mime_object_unref(GMIME_OBJECT(message));
275: }
276: }
277: #endif
278:
279:
280:
281: void VMail::fill_received(Request& request) {
282: // store letter to received
1.3 paf 283: #ifdef WITH_MAILRECEIVE
1.1 paf 284: if(request.info.mail_received) {
285: // init
286: g_mime_init(GMIME_INIT_FLAG_UTF8);
287:
288: // create stream with CRLF filter
289: GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
290: GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
291: GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
292: g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
293: g_mime_stream_unref(stream);
294: stream = istream;
295: try {
296: // parse incoming stream
297: parse(stream, vreceived.hash(0));
298: // normal stream free
299: g_mime_stream_unref(stream);
300: } catch(...) {
301: // abnormal stream free
302: g_mime_stream_unref(stream);
303: }
304: }
305: #endif
306: }
307:
308: #ifndef DOXYGEN
309: struct Store_message_element_info {
310: Charset *charset;
311: String *header;
1.11 paf 312: const String **from, **to;
313: const String *errors_to;
1.1 paf 314: Array *parts[P_TYPES_COUNT];
315: int parts_count;
316: bool has_content_type;
317: };
318: #endif
1.9 paf 319: typedef int (*string_contains_char_which_check)(int);
320: static bool string_contains_char_which(const char *string, string_contains_char_which_check check) {
321: while(char c=*string++) {
322: if(check(c))
323: return true;
324: }
325: return false;
326: }
327: const String& extractEmail(const String& string) {
328: Pool& pool=string.pool();
329:
330: char *email=string.cstr();
331: lsplit(email, '>'); lsplit(email, '\x0D');lsplit(email, '\x0A');
332: char *next=rsplit(email, '<');
333: if(next) email=next;
334:
335: String& result=*new(pool) String(pool);
336: result.APPEND_TAINTED(email, 0, string.origin().file, string.origin().line);
337:
338: /*
339: http://www.faqs.org/rfcs/rfc822.html
340:
341: addr-spec = local-part "@" domain ; global address
342:
343: local-part = word *("." word) ; uninterpreted case-preserved
344: word = atom / quoted-string
345:
346: domain = sub-domain *("." sub-domain)
347: sub-domain = domain-ref / domain-literal
348: domain-ref = atom ; symbolic reference
349:
350: domain-literal << ignoring for now
351: quoted-string in word << ignoring for now
352:
353: atom = 1*<any CHAR except specials, SPACE and CTLs> << the ONLY to check
354:
355: specials = "(" / ")" / "<" / ">" / "@" ; Must be in quoted-
356: / "," / ";" / ":" / "\" / <"> ; string, to use
357: / "." / "[" / "]" ; within a word.
358:
359: */
1.17 paf 360: const char *exception_type="email.format";
1.9 paf 361: if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17 paf 362: throw Exception(exception_type,
1.9 paf 363: &result,
364: "email contains characters (specials)");
365: if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17 paf 366: throw Exception(exception_type,
1.9 paf 367: &result,
368: "email contains characters (whitespace)");
369: if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17 paf 370: throw Exception(exception_type,
1.9 paf 371: &result,
372: "email contains characters (control)");
1.16 paf 373: if(result.is_empty())
1.17 paf 374: throw Exception(exception_type,
1.16 paf 375: &string,
376: "email is empty");
1.9 paf 377:
378: return result;
379: }
1.5 paf 380: static void store_message_element(const Hash::Key& raw_element_name, Hash::Val *aelement_value,
1.1 paf 381: void *info) {
382: Value& element_value=*static_cast<Value *>(aelement_value);
1.5 paf 383: const String& low_element_name=raw_element_name.change_case(raw_element_name.pool(), String::CC_LOWER);
1.1 paf 384: Store_message_element_info& i=*static_cast<Store_message_element_info *>(info);
385:
386: // exclude internals
1.5 paf 387: if(low_element_name==CHARSET_NAME
388: || low_element_name==VALUE_NAME
389: || low_element_name==RAW_NAME
390: || low_element_name=="date")
1.1 paf 391: return;
392:
393: // grep parts
394: for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.5 paf 395: if(low_element_name.starts_with(part_name_starts[pt])) {
1.1 paf 396: *i.parts[pt]+=&element_value;
397: i.parts_count++;
398: return;
399: }
400: }
401:
1.10 paf 402: // fetch some special headers
1.5 paf 403: if(i.from && low_element_name=="from")
1.9 paf 404: *i.from=&extractEmail(element_value.as_string());
1.5 paf 405: if(i.to && low_element_name=="to")
1.9 paf 406: *i.to=&extractEmail(element_value.as_string());
1.12 paf 407: if(low_element_name=="errors-to")
1.11 paf 408: i.errors_to=&extractEmail(element_value.as_string());
1.1 paf 409:
410: // append header line
411: *i.header <<
1.5 paf 412: raw_element_name << ":" <<
1.1 paf 413: attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER).
414: cstr(String::UL_UNSPECIFIED, 0, i.charset, i.charset?i.charset->name().cstr():0) <<
415: "\n";
416:
417: // has content type?
1.5 paf 418: if(low_element_name==CONTENT_TYPE_NAME)
1.1 paf 419: i.has_content_type=true;
420: }
421:
422: static const String& file_value_to_string(Request& r, const String *source,
423: Value& send_value) {
424: Pool& pool=r.pool();
425: const VFile *vfile;
426: const String *file_name;
427: Value *vformat;
428: if(Hash *send_hash=send_value.get_hash(source)) { // hash
429: // $.value
430: if(Value *value=static_cast<Value *>(send_hash->get(*value_name)))
431: vfile=value->as_vfile(String::UL_AS_IS);
432: else
433: throw Exception("parser.runtime",
434: source,
435: "file part has no $value");
436:
437: // $.format
438: vformat=static_cast<Value *>(send_hash->get(*new(pool) String(pool, "format")));
439:
1.6 paf 440: // $.name
1.1 paf 441: if(Value *vfile_name=static_cast<Value *>(send_hash->get(
1.6 paf 442: *new(pool) String(pool, "name")))) // specified $name
1.1 paf 443: file_name=&vfile_name->as_string();
444: } else { // must be VFile
445: vfile=send_value.as_vfile(String::UL_AS_IS);
446: file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
447: vformat=0;
448: }
449: const char *file_name_cstr=file_name->cstr();
450:
451: String& result=*new(pool) String(pool);
452:
453: // content-type: application/octet-stream
454: result << "content-type: " << r.mime_type_of(file_name_cstr)
455: << "; name=\"" << file_name_cstr << "\"\n";
456: // content-disposition: attachment; filename="user_file_name"
457: result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
458:
459: const String *type=vformat?&vformat->as_string():0;
460: if(!type/*default = uue*/ || *type=="uue") {
461: pa_uuencode(result, file_name_cstr, *vfile);
462: } else // for now
463: throw Exception("parser.runtime",
464: type,
465: "unknown attachment encode format");
466:
467: return result;
468: }
469:
470: static const String& text_value_to_string(Request& r, const String *source,
471: PartType pt, Value& send_value,
472: Store_message_element_info& info) {
473: Pool& pool=r.pool();
474: String& result=*new(pool) String(pool);
475:
476: Value *text_value;
477: if(Hash *send_hash=send_value.get_hash(source)) {
478: // $.USER-HEADERS
479: info.has_content_type=false; // reset
480: send_hash->for_each(store_message_element, &info);
481: // $.value
482: text_value=static_cast<Value *>(send_hash->get(*value_name));
483: if(!text_value)
484: throw Exception("parser.runtime",
485: source,
486: "%s part has no $" VALUE_NAME, part_name_starts[pt]);
487: } else
488: text_value=&send_value;
489:
490: if(!info.has_content_type) {
491: result << "content-type: text/" << (pt==P_TEXT?"plain":"html");
492: if(info.charset)
493: result << "; charset=" << info.charset->name();
494: result << "\n";
495: }
496:
497: // header|body separator
498: result << "\n";
499:
500: // body
501: switch(pt) {
502: case P_TEXT:
1.18 paf 503: result<<text_value->as_string();
1.1 paf 504: break;
505: case P_HTML:
506: {
507: Temp_lang temp_lang(r, String::UL_HTML);
508: if(Junction *junction=text_value->get_junction()) {
509: // execution of found $.html{code} must be in context of ^send[...]
510: // setting code context, would execute in ^.send[>>context<<]
511: //junction->change_context(?.get_junction());
512: junction->root=r.root;
513: junction->rcontext=r.rcontext;
514: junction->wcontext=r.wcontext;
515:
516: result << r.process_to_string(*text_value);
517: } else
518: throw Exception("parser.runtime",
519: source,
520: "html part value must be code");
521:
522: break;
523: }
524: }
525:
526: return result;
527: };
528:
529: /// @todo files and messages in order (file, file2, ...)
530: const String& VMail::message_hash_to_string(Request& r, const String *source,
531: Hash *message_hash, int level,
532: const String **from, const String **to) {
533: if(!message_hash)
534: throw Exception("parser.runtime",
535: source,
536: "message must be hash");
537:
538: String& result=*NEW String(pool());
539:
540: Charset *charset;
541: if(Value *vrecodecharset_name=static_cast<Value *>(message_hash->get(*charset_name)))
542: charset=&charsets->get_charset(vrecodecharset_name->as_string());
543: else
544: charset=&pool().get_source_charset();
545:
546: Store_message_element_info info={
547: charset,
548: &result,
549: from, to
550: };
551: {
552: if(from)
553: *from=0;
554: if(to)
555: *to=0;
556: for(int pt=0; pt<P_TYPES_COUNT; pt++)
557: info.parts[pt]=NEW Array(pool());
558: message_hash->for_each(store_message_element, &info);
1.10 paf 559: if(!info.errors_to)
560: result << "errors-to: postmaster\n"; // errors-to: default
1.1 paf 561: }
562:
563: int textCount=info.parts[P_TEXT]->size();
564: if(textCount>1)
565: throw Exception("parser.runtime",
566: source,
567: "multiple text parts not supported, use file part");
568: int htmlCount=info.parts[P_HTML]->size();
569: if(htmlCount>1)
570: throw Exception("parser.runtime",
571: source,
572: "multiple html parts not supported, use file part");
573:
574:
575: bool multipart=info.parts_count>1;
576: bool alternative=textCount && htmlCount;
577: // header
578: char *boundary=0;
579: if(multipart) {
580: boundary=(char *)malloc(MAX_NUMBER);
581: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
582: // multi-part
583: result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
584: result << "\n"
585: "This is a multi-part message in MIME format.";
586: }
587:
588: // alternative or not
589: {
590: if(alternative) {
591: result << "\n\n--" << boundary << "\n"; // intermediate boundary
592: result << "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
593: }
594: for(int i=0; i<2; i++) {
595: PartType pt=i==0?P_TEXT:P_HTML;
596: if(info.parts[pt]->size()) {
597: if(alternative)
598: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
599: else if(boundary)
600: result << "\n\n--" << boundary << "\n"; // intermediate boundary
601: result << text_value_to_string(r, source, pt,
602: *static_cast<Value *>(info.parts[pt]->get(0)), info);
603: }
604: }
605: if(alternative)
606: result << "\n\n--ALT" << boundary << "--\n";
607: }
608:
609: // files
610: {
611: Array& files=*info.parts[P_FILE];
612: for(int i=0; i<files.size(); i++) {
613: if(boundary)
614: result << "\n\n--" << boundary << "\n"; // intermediate boundary
615: result << file_value_to_string(r, source, *static_cast<Value *>(files.get(i)));
616: }
617: }
618:
619: // messages
620: {
621: Array& messages=*info.parts[P_MESSAGE];
622: for(int i=0; i<messages.size(); i++) {
623: if(boundary)
624: result << "\n\n--" << boundary << "\n"; // intermediate boundary
625:
626: result << message_hash_to_string(r, source,
627: static_cast<Value *>(messages.get(i))->get_hash(source),
628: level+1);
629: }
630: }
631:
632: // tailer
633: if(boundary)
634: result << "\n\n--" << boundary << "--\n"; // finish boundary
635:
636: // return
637: return result;
638: }
639:
640:
1.22 ! paf 641: Value *VMail::get_element(const String& aname, Value *aself, bool looking_up) {
1.1 paf 642: // $fields
1.3 paf 643: #ifdef WITH_MAILRECEIVE
1.1 paf 644: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
645: return &vreceived;
646: #endif
647:
648: // $CLASS,$method
1.22 ! paf 649: if(Value *result=VStateless_class::get_element(aname, aself, looking_up))
1.1 paf 650: return result;
651:
652: return 0;
653: }
654:
1.3 paf 655: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1 paf 656: # define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
657: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
658: # ifdef _DEBUG
659: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
660: # else
661: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
662: # endif
663: #endif
E-mail: