Annotation of parser3/src/types/pa_vmail.C, revision 1.46
1.1 paf 1: /** @file
2: Parser: @b mail class.
3: relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
4:
1.45 paf 5: Copyright(c) 2001-2003 ArtLebedev Group(http://www.artlebedev.com)
1.1 paf 6: Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
7: */
1.13 paf 8:
1.46 ! paf 9: static const char* IDENT_VMAIL_C="$Date: 2003/07/29 07:02:46 $";
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.45 paf 26:
27: #include "pa_charsets.h"
1.1 paf 28: #endif
29:
30: // defines
31:
32: #define RAW_NAME "raw"
33:
34: // internals
35:
36: enum PartType {
37: P_TEXT,
38: P_HTML,
39: P_FILE,
40: P_MESSAGE,
41:
42: P_TYPES_COUNT
43: };
44:
1.45 paf 45: static const char* const part_name_begins[P_TYPES_COUNT]={"text", "html", "file", "message"};
46:
47: // defines for statics
48:
49: #define FORMAT_NAME "format"
50: #define CHARSET_NAME "charset"
51:
52: // statics
53:
54: static const String format_name(FORMAT_NAME);
55: static const String charset_name(CHARSET_NAME);
1.1 paf 56:
57: // VMail
58:
1.45 paf 59: extern Methoded* mail_base_class;
1.1 paf 60:
1.45 paf 61: VMail::VMail(): VStateless_class(0, mail_base_class) {}
1.1 paf 62:
1.3 paf 63: #ifdef WITH_MAILRECEIVE
1.1 paf 64:
1.46 ! paf 65: static const String& maybeUpperCase(Charset& source_charset,
! 66: const String& src, bool toUpperCase) {
! 67: return toUpperCase?src.change_case(source_charset, String::CC_UPPER):src;
1.1 paf 68: }
69:
1.46 ! paf 70: static void UTF8toSource(Charset& source_charset,
! 71: const char* source_body, size_t source_content_length,
! 72: const void *& dest_body, size_t& dest_content_length) {
1.1 paf 73: if(source_body) {
74: if(!source_content_length)
75: source_content_length=strlen(source_body);
1.46 ! paf 76: String::C dest=Charset::transcode(String::C(source_body, source_content_length),
! 77: UTF8_charset, source_charset, dest_body, dest_content_length);
1.1 paf 78: } else {
79: dest_body=0;
80: dest_content_length=0;
81: }
82: }
83:
1.45 paf 84: static void putReceived(Charset& source_charset,
85: HashStringValue& received,
86: const char* name, Value* value,
87: bool nameToUpperCase=false) {
88:
1.1 paf 89: if(name && value) {
90: received.put(
1.45 paf 91: maybeUpperCase(source_charset,
92: String* (new String(pool.copy(name), 0, true/*tainted*/)), nameToUpperCase),
1.1 paf 93: value);
94: }
95: }
96:
1.45 paf 97: static void putReceived(Charset& source_charset,
98: HashStringValue& received,
99: const char* name, const char* value, size_t value_size=0,
100: bool nameToUpperCase=false) {
1.1 paf 101: if(value) {
102: const void *value_dest_body;
103: size_t value_dest_content_length;
1.45 paf 104: // UTF8toSource(value, value_size, value_dest_body, value_dest_content_length);
1.21 paf 105: value_dest_body=value;
106: value_dest_content_length=value_size;
1.1 paf 107:
1.45 paf 108: putReceived(source_charset,
109: received,
110: name,
111: *new VString(*new String( !remember string invariant!
112: pool.copy((const char* )value_dest_body), value_dest_content_length, true/*tainted*/))))
113: );
1.1 paf 114: }
115: }
116:
1.45 paf 117: static void putReceivedHashStringValue& received, const char* name, time_t value) {
1.1 paf 118: if(name)
1.45 paf 119: received.put(
120: String* (new String(pool.copy(name), 0, true/*tainted*/)),
121: Value*(new VDate(value))
122: );
1.1 paf 123: }
124:
1.45 paf 125: #ifndef DOXYGEN
126: struct MimeHeaderField2received_info {
127: ;
128: Charset* source_charset;
129: HashStringValue* received;
130: };
131: #endif
132: static void MimeHeaderField2received(const char* name, const char* value, gpointer data) {
133: MimeHeaderField2received_info& info=*static_cast<MimeHeaderField2received_info *>(data);
1.1 paf 134:
1.45 paf 135: putReceived(*info.*info.source_charset,
136: *info.received,
137: name, value, 0, true/*nameInUpperCase*/);
1.1 paf 138: }
139:
1.45 paf 140: static void parse(Request& r, GMimeStream *stream, HashStringValue& received);
1.1 paf 141:
142: #ifndef DOXYGEN
1.45 paf 143: struct MimePart2body_info {
144: Request* r;
145: HashStringValue* body;
1.1 paf 146: int partCounts[P_TYPES_COUNT];
147: };
148: #endif
1.27 paf 149: /// @test why no copy to global in P_HTML putReceived?
1.1 paf 150: static void MimePart2body(GMimePart *part,
1.45 paf 151: gpointer data) {
152: MimePart2body_info& info=*static_cast<MimePart2body_info *>(data);
153: =info.r->pool();
154: Charset& source_charset=info.r->charsets.source();
1.1 paf 155:
156: if(const GMimeContentType *type=g_mime_part_get_content_type(part)) {
157: if(g_mime_content_type_is_type(type, "multipart", "*"))
158: return; // skipping frames
159:
1.45 paf 160: PartType partType;
161: if(g_mime_content_type_is_type(type, "text", "plain"))
162: partType=P_TEXT;
163: else if(g_mime_content_type_is_type(type, "text", "html"))
164: partType=P_HTML;
165: else if(g_mime_content_type_is_type(type, "message", "*"))
166: partType=P_MESSAGE;
167: else
168: partType=P_FILE;
1.1 paf 169:
170: // partName
1.45 paf 171: const char* partName;
1.1 paf 172: char partNameBuf[MAX_STRING];
1.45 paf 173: const char* partNameStart=part_name_begins[partType];
174: if(int partNo=info.partCounts[partType]++) {
1.1 paf 175: snprintf(partNameBuf, MAX_STRING, "%s%d", partNameStart, partNo);
176: partName=partNameBuf;
177: } else
178: partName=partNameStart;
179:
180: // $.partX[
1.45 paf 181: VHash* vpartX(new VHash); putReceived(*info.body, partName, vpartX);
182: HashStringValue& partX=vpartX->hash();
1.1 paf 183: {
184: // $.raw[
1.45 paf 185: VHash* vraw(new VHash); putReceived(partX, RAW_NAME, vraw);
186: MimeHeaderField2received_info hfr_info={
187: &&source_charset, &vraw->hash()};
188: g_mime_header_foreach(part->headers, MimeHeaderField2received, &hfr_info);
1.1 paf 189: }
1.45 paf 190: const char* content_filename=0;
1.1 paf 191: {
192: // $.content-type[
1.45 paf 193: VHash* vcontent_type(new VHash); putReceived(partX, "content-type", vcontent_type);
194: HashStringValue& content_type=vcontent_type->hash();
1.1 paf 195: {
196: // $.value[text/plain]
197: char value[MAX_STRING];
198: snprintf(value, MAX_STRING, "%s/%s",
199: type->type?type->type:"x-unknown",
200: type->subtype?type->subtype:"x-unknown");
1.45 paf 201: putReceived(source_charset, content_type, VALUE_NAME, value);
1.1 paf 202: }
203: GMimeParam *param=type->params;
204: while(param) {
205: // $.charset[windows-1251] && co
1.45 paf 206: putReceived(source_charset, content_type, param->name, param->value, true);
1.1 paf 207: if(strcasecmp(param->name, "name")==0)
208: content_filename=param->value;
209: param=param->next;
210: }
211: }
212: // $.description
1.45 paf 213: putReceived(source_charset, partX, "description", part->description);
1.1 paf 214: // $.content-id
1.45 paf 215: putReceived(source_charset, partX, "content-id", part->content_id);
1.1 paf 216: // $.content-md5
1.45 paf 217: putReceived(source_charset, partX, "content-md5", part->content_md5);
1.1 paf 218: // $.content-location
1.45 paf 219: putReceived(source_charset, partX, "content-location", part->content_location);
1.1 paf 220:
221: // todo GMimePart:
222: // GMimePartEncodingType encoding;
223: // GMimeDisposition *disposition;
224: if(part->disposition) {
225: GMimeParam *param=part->disposition->params;
226: while(param) {
227: // $.charset[windows-1251] && co
228: if(strcasecmp(param->name, "filename")==0)
229: content_filename=param->value;
230: param=param->next;
231: }
232: }
233:
234: // MESSAGE
235: if(partType==P_MESSAGE) {
236: if(part->content)
237: if(GMimeStream *stream=part->content->stream)
1.45 paf 238: parse(*info.r, stream, partX);
1.1 paf 239: } else {
240: // $.value[string|file]
241: size_t buf_len;
1.45 paf 242: const char* local_buf=(const char*)g_mime_part_get_content(part, &buf_len);
1.1 paf 243: if(partType==P_FILE) {
1.45 paf 244: VFile* vfile(new VFile);
245: char *pooled_buf=pool.copy(local_buf, buf_len);
246: const VString& vcontent_type(content_filename?
247: new VString(info.r->mime_type_of(content_filename)):0);
248: vfile->set(true/*tainted*/, pooled_buf, buf_len, content_filename, vcontent_type);
249: putReceived(partX, VALUE_NAME, vfile);
1.1 paf 250: } else {
251: // P_TEXT, P_HTML
1.45 paf 252: putReceived(source_charset, partX, VALUE_NAME,(const char*)local_buf, buf_len);
1.1 paf 253: }
254: }
255: }
256: }
257:
1.30 paf 258: int gmt_offset() {
259: #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
1.31 paf 260: return timezone+(daylight?60*60*(timezone<0?-1:timezone>0?+1:0):0);
1.30 paf 261: #else
262: time_t t=time(0);
263: tm *tm=localtime(&t);
264: #if defined(HAVE_TM_GMTOFF)
265: return tm->tm_gmtoff;
266: #elif defined(HAVE_TM_TZADJ)
267: return tm->tm_tzadj;
268: #else
269: #error neither HAVE_TIMEZONE&HAVE_DAYIGHT nor HAVE_TM_GMTOFF nor HAVE_TM_TZADJ defined
270: #endif
271: #endif
272: }
273:
1.45 paf 274: static void parse(Request& r, GMimeStream *stream, HashStringValue& received) {
275: Charset& source_charset=r.charsets.source();
1.1 paf 276:
277: GMimeMessage *message=g_mime_parser_construct_message(stream);
278: try {
279: const GMimeMessageHeader *messageHeader=message->header;
280: if(!messageHeader)
281: return;
282:
283: // firstly user-defined strings go
284: // user headers
285: {
286: // $.raw[
1.45 paf 287: VHash* vraw(new VHash); putReceived(received, RAW_NAME, vraw);
288: MimeHeaderField2received_info hfr_info={
289: &&source_charset, &vraw->hash()};
290: g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &hfr_info);
1.1 paf 291: }
292:
293: // maybe-todo-recipients
294: // x(messageHeader->recipients)
295:
296: // secondly standard headers&body go
297: // standard header
298: // .from
1.45 paf 299: putReceived(source_charset, received, "from", messageHeader->from);
1.1 paf 300: // .reply-to
1.45 paf 301: putReceived(source_charset, received, "reply-to", messageHeader->reply_to);
1.1 paf 302: // .to
303: // todo: messageHeader->recipients
304: // .subject
1.45 paf 305: putReceived(source_charset, received, "subject", messageHeader->subject);
1.1 paf 306: // .date(date+gmt_offset)
307: int tt_offset =
308: ((messageHeader->gmt_offset / 100) *(60 * 60))
309: +(messageHeader->gmt_offset % 100) * 60;
1.30 paf 310:
1.1 paf 311: putReceived(received, "date",
312: messageHeader->date // local sender
313: -tt_offset // move local sender to GMT sender
1.30 paf 314: -gmt_offset() // move GMT sender to our local time
1.1 paf 315: );
316: // .message-id
1.45 paf 317: putReceived(source_charset, received, "message-id", messageHeader->message_id);
1.1 paf 318:
319: // .body[part/parts
320: GMimePart *part=message->mime_part;
321: const GMimeContentType *type=g_mime_part_get_content_type(part);
1.45 paf 322: MimePart2body_info info={&r, &received};
1.1 paf 323: g_mime_part_foreach(part, MimePart2body, &info);
324:
325: // normal unref
326: g_mime_object_unref(GMIME_OBJECT(message));
327: } catch(...) {
328: // abnormal unref
329: g_mime_object_unref(GMIME_OBJECT(message));
330: }
331: }
332: #endif
333:
334:
335:
1.23 paf 336: void VMail::fill_received(Request& r) {
1.1 paf 337: // store letter to received
1.3 paf 338: #ifdef WITH_MAILRECEIVE
1.45 paf 339: if(r.request_info.mail_received) {
1.1 paf 340: // init
341: g_mime_init(GMIME_INIT_FLAG_UTF8);
342:
343: // create stream with CRLF filter
344: GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
345: GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
346: GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
347: g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
1.45 paf 348: g_mime_stream_unref(stream);
1.1 paf 349: stream = istream;
350: try {
351: // parse incoming stream
1.45 paf 352: parse(r, stream, vreceived->hash());
1.1 paf 353: // normal stream free
1.45 paf 354: g_mime_stream_unref(stream);
1.1 paf 355: } catch(...) {
356: // abnormal stream free
1.45 paf 357: g_mime_stream_unref(stream);
1.1 paf 358: }
359: }
360: #endif
361: }
362:
1.9 paf 363: typedef int (*string_contains_char_which_check)(int);
1.45 paf 364: static bool string_contains_char_which(const char* string, string_contains_char_which_check check) {
1.9 paf 365: while(char c=*string++) {
366: if(check(c))
367: return true;
368: }
369: return false;
370: }
1.32 paf 371: static char *trimBoth(char *s) {
372: // sanity check
373: if(!s)
374: return 0;
375:
376: // trim head whitespace
377: while(*s && isspace(*s))
378: s++;
379: // trim tail whitespace
380: char *tail=s+strlen(s);
381: if(tail>s) {
382: do {
383: --tail;
384: if(isspace(*tail))
385: *tail=0;
386: } while(tail>s);
387: }
388: // return it
389: return s;
390: }
1.45 paf 391: static void extractEmail(String& result, char *email) {
1.32 paf 392: email=trimBoth(email);
1.45 paf 393: result.append_help_length(email, 0, String::L_TAINTED);
1.9 paf 394:
395: /*
396: http://www.faqs.org/rfcs/rfc822.html
397:
398: addr-spec = local-part "@" domain ; global address
399:
400: local-part = word *("." word) ; uninterpreted case-preserved
401: word = atom / quoted-string
402:
403: domain = sub-domain *("." sub-domain)
404: sub-domain = domain-ref / domain-literal
405: domain-ref = atom ; symbolic reference
406:
407: domain-literal << ignoring for now
408: quoted-string in word << ignoring for now
409:
410: atom = 1*<any CHAR except specials, SPACE and CTLs> << the ONLY to check
411:
412: specials = "(" / ")" / "<" / ">" / "@" ; Must be in quoted-
413: / "," / ";" / ":" / "\" / <"> ; string, to use
414: / "." / "[" / "]" ; within a word.
415:
416: */
1.45 paf 417: const char* exception_type="email.format";
1.9 paf 418: if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17 paf 419: throw Exception(exception_type,
1.9 paf 420: &result,
1.32 paf 421: "email contains bad characters (specials)");
1.9 paf 422: if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17 paf 423: throw Exception(exception_type,
1.9 paf 424: &result,
1.32 paf 425: "email contains bad characters (whitespace)");
1.9 paf 426: if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17 paf 427: throw Exception(exception_type,
1.9 paf 428: &result,
1.32 paf 429: "email contains bad characters (control)");
1.16 paf 430: if(result.is_empty())
1.17 paf 431: throw Exception(exception_type,
1.45 paf 432: 0,
1.16 paf 433: "email is empty");
1.39 paf 434: }
435:
1.45 paf 436: static const String& extractEmails(const String& string) {
437: char *emails=string.cstrm();
438: String& result=*new String;
1.39 paf 439: while(char *email=lsplit(&emails, ',')) {
440: rsplit(email, '>');
441: if(char *in_brackets=lsplit(email, '<'))
442: email=in_brackets;
443: if(!result.is_empty())
444: result<<",";
1.45 paf 445: extractEmail(result, email);
1.39 paf 446: }
1.9 paf 447:
448: return result;
449: }
1.45 paf 450:
451: #ifndef DOXYGEN
452: struct Store_message_element_info {
453: Request_charsets& charsets;
454: String& header;
455: const String* & from;
456: bool extract_to; String* & to;
457: const String* errors_to;
458: bool mime_version_specified;
459: ArrayValue* parts[P_TYPES_COUNT];
460: int parts_count;
461: bool backward_compatibility;
462: Value* content_type;
463:
464: Store_message_element_info(
465: Request_charsets& acharsets,
466: String& aheader,
467: const String* & afrom,
468: bool aextract_to, String* & ato
469: ):
470:
471: charsets(acharsets),
472: header(aheader),
473: from(afrom),
474: extract_to(aextract_to), to(ato),
475: errors_to(0),
476: mime_version_specified(false),
477: parts_count(0),
478: backward_compatibility(false), content_type(0) {
479: }
480: };
481: #endif
482: static void store_message_element(HashStringValue::key_type raw_element_name,
483: HashStringValue::value_type element_value,
484: Store_message_element_info *info) {
485: const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
486: info->charsets.source(), String::CC_LOWER);
1.1 paf 487:
488: // exclude internals
1.5 paf 489: if(low_element_name==CHARSET_NAME
490: || low_element_name==VALUE_NAME
491: || low_element_name==RAW_NAME
492: || low_element_name=="date")
1.1 paf 493: return;
494:
495: // grep parts
496: for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.45 paf 497: if(low_element_name.starts_with(part_name_begins[pt])) {
1.29 paf 498: // check that $.message# '#' is digit
1.45 paf 499: size_t start_len=strlen(part_name_begins[pt]);
500: if(low_element_name.length()>start_len) {
501: const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.29 paf 502: if(!isdigit(*at_num))
503: continue;
504: }
1.45 paf 505: *info->parts[pt]+=element_value;
506: info->parts_count++;
1.1 paf 507: return;
508: }
509: }
510:
1.10 paf 511: // fetch some special headers
1.45 paf 512: if(low_element_name=="from")
513: info->from=&extractEmails(element_value->as_string());
514: if(info->extract_to) { // defined only on WIN32, see mail.C [collecting info for RCPT to-s]
1.39 paf 515: bool is_to=low_element_name=="to" ;
516: bool is_cc=low_element_name=="cc" ;
517: bool is_bcc=low_element_name=="bcc" ;
518: if(is_to||is_cc||is_bcc) {
1.45 paf 519: if(!info->to)
520: info->to=new String;
1.39 paf 521: else
1.45 paf 522: *info->to << ",";
523: *info->to << extractEmails(element_value->as_string());
1.39 paf 524: }
525:
526: if(is_bcc) // blinding it
1.45 paf 527: return;
1.39 paf 528: }
1.12 paf 529: if(low_element_name=="errors-to")
1.45 paf 530: info->errors_to=&extractEmails(element_value->as_string());
1.37 paf 531: if(low_element_name=="mime-version")
1.45 paf 532: info->mime_version_specified=true;
1.1 paf 533:
1.45 paf 534: // has content type?
535: if(low_element_name==CONTENT_TYPE_NAME) {
536: info->content_type=element_value;
537: if(info->backward_compatibility)
538: return;
1.39 paf 539: }
1.1 paf 540:
1.45 paf 541: // preparing header line
542: const String& source_line=attributed_meaning_to_string(*element_value,
543: String::L_PASS_APPENDED/*does not matter, would cstr(AS_IS) right away*/);
544: const char* source_line_cstr=source_line.cstr();
545: String::C mail=Charset::transcode(
546: String::C(source_line_cstr, source_line.length()),
547: info->charsets.source(),
548: info->charsets.mail());
549: String& mail_line=*new String;
550: ///@todo can convert to append_help_length if transcode would return zero-terminated
551: mail_line.append_know_length(mail.str, mail.length, String::L_MAIL_HEADER);
552:
553: // append header line
554: info->header
555: << raw_element_name
556: << ":" << mail_line.cstr(String::L_UNSPECIFIED, 0, &info->charsets)
557: << "\n";
558: }
559:
560: static const String& file_value_to_string(Request& r, Value* send_value) {
561: VFile* vfile;
562: const String* file_name=0;
563: Value* vformat=0;
564: if(HashStringValue *send_hash=send_value->get_hash()) { // hash
1.1 paf 565: // $.value
1.45 paf 566: if(Value* value=send_hash->get(value_name))
567: vfile=value->as_vfile(String::L_AS_IS);
1.1 paf 568: else
569: throw Exception("parser.runtime",
1.45 paf 570: 0,
1.1 paf 571: "file part has no $value");
572:
573: // $.format
1.45 paf 574: vformat=send_hash->get(format_name);
1.1 paf 575:
1.6 paf 576: // $.name
1.45 paf 577: if(Value* vfile_name=send_hash->get(name_name)) // $name specified
1.1 paf 578: file_name=&vfile_name->as_string();
1.28 paf 579: } else // must be VFile then
1.45 paf 580: vfile=send_value->as_vfile(String::L_AS_IS);
1.28 paf 581:
582: if(!file_name)
1.45 paf 583: file_name=&vfile->fields().get(name_name)->as_string();
1.28 paf 584:
1.45 paf 585: const char* file_name_cstr=file_name->cstr();
1.1 paf 586:
1.45 paf 587: String& result=*new String;
1.1 paf 588:
589: // content-type: application/octet-stream
590: result << "content-type: " << r.mime_type_of(file_name_cstr)
591: << "; name=\"" << file_name_cstr << "\"\n";
592: // content-disposition: attachment; filename="user_file_name"
1.35 paf 593: result <<
594: CONTENT_DISPOSITION_NAME ": "CONTENT_DISPOSITION_VALUE"; "
595: CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.1 paf 596:
1.45 paf 597: const String* type=vformat?&vformat->as_string():0;
1.1 paf 598: if(!type/*default = uue*/ || *type=="uue") {
1.45 paf 599: pa_uuencode(result, *file_name, *vfile);
1.1 paf 600: } else // for now
601: throw Exception("parser.runtime",
602: type,
603: "unknown attachment encode format");
604:
605: return result;
606: }
607:
1.45 paf 608: static const String& text_value_to_string(Request& r,
609: PartType pt, Value* send_value,
610: Store_message_element_info& info) {
611: String& result=*new String;
1.1 paf 612:
1.45 paf 613: Value* text_value;
614: if(HashStringValue* send_hash=send_value->get_hash()) {
1.1 paf 615: // $.USER-HEADERS
1.45 paf 616: info.content_type=0; info.backward_compatibility=false; // reset
1.1 paf 617: send_hash->for_each(store_message_element, &info);
618: // $.value
1.45 paf 619: text_value=send_hash->get(value_name);
1.1 paf 620: if(!text_value)
621: throw Exception("parser.runtime",
1.45 paf 622: 0,
623: "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.1 paf 624: } else
1.45 paf 625: text_value=send_value;
1.1 paf 626:
1.45 paf 627: if(!info.content_type) {
628: result
629: << "content-type: text/" << (pt==P_TEXT?"plain":"html")
630: << "; charset=" << info.charsets.mail().NAME()
631: << "\n";
1.1 paf 632: }
633:
634: // header|body separator
635: result << "\n";
636:
637: // body
1.45 paf 638: const String* body;
1.1 paf 639: switch(pt) {
640: case P_TEXT:
1.41 paf 641: body=&text_value->as_string();
1.1 paf 642: break;
643: case P_HTML:
644: {
1.45 paf 645: Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
646: if(Junction* junction=text_value->get_junction())
1.41 paf 647: body=&r.process_to_string(*text_value);
1.26 paf 648: else
1.1 paf 649: throw Exception("parser.runtime",
1.45 paf 650: 0,
1.1 paf 651: "html part value must be code");
652:
653: break;
654: }
1.41 paf 655: }
656: if(body) {
1.45 paf 657: const char* body_cstr=strdup(body->cstr(String::L_UNSPECIFIED)); // body
658: String::C mail=Charset::transcode(
659: String::C(body_cstr, strlen(body_cstr)),
660: r.charsets.source(),
661: r.charsets.mail()/*always set - either mail.charset or $request:charset*/);
662: ///@todo
663: result.append_know_length(mail.str, mail.length, String::L_CLEAN);
1.1 paf 664: }
665:
666: return result;
667: };
668:
669: /// @todo files and messages in order (file, file2, ...)
1.45 paf 670: const String& VMail::message_hash_to_string(Request& r,
671: HashStringValue* message_hash, int level,
672: const String* & from, bool extract_to, String* & to) {
1.34 paf 673:
1.1 paf 674: if(!message_hash)
675: throw Exception("parser.runtime",
1.45 paf 676: 0,
1.1 paf 677: "message must be hash");
678:
1.45 paf 679: String& result=*new String;
1.1 paf 680:
1.45 paf 681: if(Value* vrecodecharset_name=message_hash->get(charset_name))
682: r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
683: .change_case(r.charsets.source(), String::CC_UPPER)));
1.1 paf 684: else
1.45 paf 685: r.charsets.set_mail(r.charsets.source());
686: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 687:
1.45 paf 688: Store_message_element_info info(r.charsets,
689: result, from, extract_to, to);
1.1 paf 690: {
1.45 paf 691: // for backward compatibilyty $.body+$.content-type ->
692: // $.text[$.value[] $.content-type[]]
693:
1.1 paf 694: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45 paf 695: info.parts[pt]=new ArrayValue(1);
696:
697: Value* body=message_hash->get("body");
698: if(body) {
699: message_hash->remove("body");
700: info.backward_compatibility=true;
701: }
1.1 paf 702: message_hash->for_each(store_message_element, &info);
1.45 paf 703:
704: if(body) {
705: VHash& text_part=*new VHash();
706: HashStringValue& hash=text_part.hash();
707: hash.put(value_name, body);
708: if(info.content_type)
709: hash.put(content_type_name, info.content_type);
710:
711: *info.parts[P_TEXT]+=&text_part;
712: info.parts_count++;
713: }
714:
1.10 paf 715: if(!info.errors_to)
716: result << "errors-to: postmaster\n"; // errors-to: default
1.37 paf 717: if(!info.mime_version_specified)
718: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 719: }
720:
1.45 paf 721: int textCount=info.parts[P_TEXT]->count();
1.1 paf 722: if(textCount>1)
723: throw Exception("parser.runtime",
1.45 paf 724: 0,
1.1 paf 725: "multiple text parts not supported, use file part");
1.45 paf 726: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 727: if(htmlCount>1)
728: throw Exception("parser.runtime",
1.45 paf 729: 0,
1.1 paf 730: "multiple html parts not supported, use file part");
731:
732:
733: bool multipart=info.parts_count>1;
734: bool alternative=textCount && htmlCount;
735: // header
736: char *boundary=0;
737: if(multipart) {
1.45 paf 738: boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1 paf 739: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
740: // multi-part
1.45 paf 741: result
742: << "content-type: multipart/mixed; boundary=\""
743: << boundary << "\"\n"
744: "\n"
745: "This is a multi-part message in MIME format.";
1.1 paf 746: }
747:
748: // alternative or not
749: {
750: if(alternative) {
1.45 paf 751: result << "\n\n--" << boundary << "\n" // intermediate boundary
752: "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 753: }
754: for(int i=0; i<2; i++) {
755: PartType pt=i==0?P_TEXT:P_HTML;
1.45 paf 756: if(info.parts[pt]->count()) {
1.1 paf 757: if(alternative)
758: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
759: else if(boundary)
760: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 761: result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 762: }
763: }
764: if(alternative)
765: result << "\n\n--ALT" << boundary << "--\n";
766: }
767:
768: // files
769: {
1.45 paf 770: ArrayValue& files=*info.parts[P_FILE];
771: for(size_t i=0; i<files.count(); i++) {
1.1 paf 772: if(boundary)
773: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 774: result << file_value_to_string(r, files.get(i));
1.1 paf 775: }
776: }
777:
778: // messages
779: {
1.45 paf 780: ArrayValue& messages=*info.parts[P_MESSAGE];
781: for(size_t i=0; i<messages.count(); i++) {
1.1 paf 782: if(boundary)
783: result << "\n\n--" << boundary << "\n"; // intermediate boundary
784:
1.45 paf 785: const String* dummy_from;
786: String* dummy_to;
787: result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
788: dummy_from, false, dummy_to);
1.1 paf 789: }
790: }
791:
792: // tailer
793: if(boundary)
794: result << "\n\n--" << boundary << "--\n"; // finish boundary
795:
796: // return
797: return result;
798: }
799:
800:
1.45 paf 801: Value* VMail::get_element(const String& aname, Value& aself, bool looking_up) {
1.1 paf 802: // $fields
1.3 paf 803: #ifdef WITH_MAILRECEIVE
1.1 paf 804: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.45 paf 805: return vreceived;
1.1 paf 806: #endif
807:
808: // $CLASS,$method
1.45 paf 809: if(Value* result=VStateless_class::get_element(aname, aself, looking_up))
1.1 paf 810: return result;
811:
812: return 0;
813: }
814:
1.3 paf 815: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1 paf 816: # define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
817: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
818: # ifdef _DEBUG
819: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
820: # else
821: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
822: # endif
823: #endif
E-mail: