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