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