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