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