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