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