Annotation of parser3/src/types/pa_vmail.C, revision 1.45
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.45 ! paf 9: static const char* IDENT_VMAIL_C="$Date: 2003/04/21 08:19:13 $";
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.45 ! paf 65: static const String& maybeUpperCaseCharset& source_charset,
! 66: const String& src, bool toUpperCase) {
! 67: return toUpperCase?src->change_case(source_charset, String::CC_UPPER):src;
1.1 paf 68: }
69:
1.45 ! paf 70: static void UTF8toSourceCharset& source_charset,
! 71: const char* source_body, size_t source_content_length,
1.1 paf 72: const void *& dest_body, size_t& dest_content_length) {
73: if(source_body) {
74: if(!source_content_length)
75: source_content_length=strlen(source_body);
1.45 ! paf 76: Charset::transcode(
! 77: *UTF8_charset, source_body, source_content_length,
! 78: source_charset, dest_body, dest_content_length);
1.1 paf 79: } else {
80: dest_body=0;
81: dest_content_length=0;
82: }
83: }
84:
1.45 ! paf 85: static void putReceived(Charset& source_charset,
! 86: HashStringValue& received,
! 87: const char* name, Value* value,
! 88: bool nameToUpperCase=false) {
! 89:
1.1 paf 90: if(name && value) {
91: received.put(
1.45 ! paf 92: maybeUpperCase(source_charset,
! 93: String* (new String(pool.copy(name), 0, true/*tainted*/)), nameToUpperCase),
1.1 paf 94: value);
95: }
96: }
97:
1.45 ! paf 98: static void putReceived(Charset& source_charset,
! 99: HashStringValue& received,
! 100: const char* name, const char* value, size_t value_size=0,
! 101: bool nameToUpperCase=false) {
1.1 paf 102: if(value) {
103: const void *value_dest_body;
104: size_t value_dest_content_length;
1.45 ! paf 105: // UTF8toSource(value, value_size, value_dest_body, value_dest_content_length);
1.21 paf 106: value_dest_body=value;
107: value_dest_content_length=value_size;
1.1 paf 108:
1.45 ! paf 109: putReceived(source_charset,
! 110: received,
! 111: name,
! 112: *new VString(*new String( !remember string invariant!
! 113: pool.copy((const char* )value_dest_body), value_dest_content_length, true/*tainted*/))))
! 114: );
1.1 paf 115: }
116: }
117:
1.45 ! paf 118: static void putReceivedHashStringValue& received, const char* name, time_t value) {
1.1 paf 119: if(name)
1.45 ! paf 120: received.put(
! 121: String* (new String(pool.copy(name), 0, true/*tainted*/)),
! 122: Value*(new VDate(value))
! 123: );
1.1 paf 124: }
125:
1.45 ! paf 126: #ifndef DOXYGEN
! 127: struct MimeHeaderField2received_info {
! 128: ;
! 129: Charset* source_charset;
! 130: HashStringValue* received;
! 131: };
! 132: #endif
! 133: static void MimeHeaderField2received(const char* name, const char* value, gpointer data) {
! 134: MimeHeaderField2received_info& info=*static_cast<MimeHeaderField2received_info *>(data);
1.1 paf 135:
1.45 ! paf 136: putReceived(*info.*info.source_charset,
! 137: *info.received,
! 138: name, value, 0, true/*nameInUpperCase*/);
1.1 paf 139: }
140:
1.45 ! paf 141: static void parse(Request& r, GMimeStream *stream, HashStringValue& received);
1.1 paf 142:
143: #ifndef DOXYGEN
1.45 ! paf 144: struct MimePart2body_info {
! 145: Request* r;
! 146: HashStringValue* body;
1.1 paf 147: int partCounts[P_TYPES_COUNT];
148: };
149: #endif
1.27 paf 150: /// @test why no copy to global in P_HTML putReceived?
1.1 paf 151: static void MimePart2body(GMimePart *part,
1.45 ! paf 152: gpointer data) {
! 153: MimePart2body_info& info=*static_cast<MimePart2body_info *>(data);
! 154: =info.r->pool();
! 155: Charset& source_charset=info.r->charsets.source();
1.1 paf 156:
157: if(const GMimeContentType *type=g_mime_part_get_content_type(part)) {
158: if(g_mime_content_type_is_type(type, "multipart", "*"))
159: return; // skipping frames
160:
1.45 ! paf 161: PartType partType;
! 162: if(g_mime_content_type_is_type(type, "text", "plain"))
! 163: partType=P_TEXT;
! 164: else if(g_mime_content_type_is_type(type, "text", "html"))
! 165: partType=P_HTML;
! 166: else if(g_mime_content_type_is_type(type, "message", "*"))
! 167: partType=P_MESSAGE;
! 168: else
! 169: partType=P_FILE;
1.1 paf 170:
171: // partName
1.45 ! paf 172: const char* partName;
1.1 paf 173: char partNameBuf[MAX_STRING];
1.45 ! paf 174: const char* partNameStart=part_name_begins[partType];
! 175: if(int partNo=info.partCounts[partType]++) {
1.1 paf 176: snprintf(partNameBuf, MAX_STRING, "%s%d", partNameStart, partNo);
177: partName=partNameBuf;
178: } else
179: partName=partNameStart;
180:
181: // $.partX[
1.45 ! paf 182: VHash* vpartX(new VHash); putReceived(*info.body, partName, vpartX);
! 183: HashStringValue& partX=vpartX->hash();
1.1 paf 184: {
185: // $.raw[
1.45 ! paf 186: VHash* vraw(new VHash); putReceived(partX, RAW_NAME, vraw);
! 187: MimeHeaderField2received_info hfr_info={
! 188: &&source_charset, &vraw->hash()};
! 189: g_mime_header_foreach(part->headers, MimeHeaderField2received, &hfr_info);
1.1 paf 190: }
1.45 ! paf 191: const char* content_filename=0;
1.1 paf 192: {
193: // $.content-type[
1.45 ! paf 194: VHash* vcontent_type(new VHash); putReceived(partX, "content-type", vcontent_type);
! 195: HashStringValue& content_type=vcontent_type->hash();
1.1 paf 196: {
197: // $.value[text/plain]
198: char value[MAX_STRING];
199: snprintf(value, MAX_STRING, "%s/%s",
200: type->type?type->type:"x-unknown",
201: type->subtype?type->subtype:"x-unknown");
1.45 ! paf 202: putReceived(source_charset, content_type, VALUE_NAME, value);
1.1 paf 203: }
204: GMimeParam *param=type->params;
205: while(param) {
206: // $.charset[windows-1251] && co
1.45 ! paf 207: putReceived(source_charset, content_type, param->name, param->value, true);
1.1 paf 208: if(strcasecmp(param->name, "name")==0)
209: content_filename=param->value;
210: param=param->next;
211: }
212: }
213: // $.description
1.45 ! paf 214: putReceived(source_charset, partX, "description", part->description);
1.1 paf 215: // $.content-id
1.45 ! paf 216: putReceived(source_charset, partX, "content-id", part->content_id);
1.1 paf 217: // $.content-md5
1.45 ! paf 218: putReceived(source_charset, partX, "content-md5", part->content_md5);
1.1 paf 219: // $.content-location
1.45 ! paf 220: putReceived(source_charset, partX, "content-location", part->content_location);
1.1 paf 221:
222: // todo GMimePart:
223: // GMimePartEncodingType encoding;
224: // GMimeDisposition *disposition;
225: if(part->disposition) {
226: GMimeParam *param=part->disposition->params;
227: while(param) {
228: // $.charset[windows-1251] && co
229: if(strcasecmp(param->name, "filename")==0)
230: content_filename=param->value;
231: param=param->next;
232: }
233: }
234:
235: // MESSAGE
236: if(partType==P_MESSAGE) {
237: if(part->content)
238: if(GMimeStream *stream=part->content->stream)
1.45 ! paf 239: parse(*info.r, stream, partX);
1.1 paf 240: } else {
241: // $.value[string|file]
242: size_t buf_len;
1.45 ! paf 243: const char* local_buf=(const char*)g_mime_part_get_content(part, &buf_len);
1.1 paf 244: if(partType==P_FILE) {
1.45 ! paf 245: VFile* vfile(new VFile);
! 246: char *pooled_buf=pool.copy(local_buf, buf_len);
! 247: const VString& vcontent_type(content_filename?
! 248: new VString(info.r->mime_type_of(content_filename)):0);
! 249: vfile->set(true/*tainted*/, pooled_buf, buf_len, content_filename, vcontent_type);
! 250: putReceived(partX, VALUE_NAME, vfile);
1.1 paf 251: } else {
252: // P_TEXT, P_HTML
1.45 ! paf 253: putReceived(source_charset, partX, VALUE_NAME,(const char*)local_buf, buf_len);
1.1 paf 254: }
255: }
256: }
257: }
258:
1.30 paf 259: int gmt_offset() {
260: #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
1.31 paf 261: return timezone+(daylight?60*60*(timezone<0?-1:timezone>0?+1:0):0);
1.30 paf 262: #else
263: time_t t=time(0);
264: tm *tm=localtime(&t);
265: #if defined(HAVE_TM_GMTOFF)
266: return tm->tm_gmtoff;
267: #elif defined(HAVE_TM_TZADJ)
268: return tm->tm_tzadj;
269: #else
270: #error neither HAVE_TIMEZONE&HAVE_DAYIGHT nor HAVE_TM_GMTOFF nor HAVE_TM_TZADJ defined
271: #endif
272: #endif
273: }
274:
1.45 ! paf 275: static void parse(Request& r, GMimeStream *stream, HashStringValue& received) {
! 276: Charset& source_charset=r.charsets.source();
1.1 paf 277:
278: GMimeMessage *message=g_mime_parser_construct_message(stream);
279: try {
280: const GMimeMessageHeader *messageHeader=message->header;
281: if(!messageHeader)
282: return;
283:
284: // firstly user-defined strings go
285: // user headers
286: {
287: // $.raw[
1.45 ! paf 288: VHash* vraw(new VHash); putReceived(received, RAW_NAME, vraw);
! 289: MimeHeaderField2received_info hfr_info={
! 290: &&source_charset, &vraw->hash()};
! 291: g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &hfr_info);
1.1 paf 292: }
293:
294: // maybe-todo-recipients
295: // x(messageHeader->recipients)
296:
297: // secondly standard headers&body go
298: // standard header
299: // .from
1.45 ! paf 300: putReceived(source_charset, received, "from", messageHeader->from);
1.1 paf 301: // .reply-to
1.45 ! paf 302: putReceived(source_charset, received, "reply-to", messageHeader->reply_to);
1.1 paf 303: // .to
304: // todo: messageHeader->recipients
305: // .subject
1.45 ! paf 306: putReceived(source_charset, received, "subject", messageHeader->subject);
1.1 paf 307: // .date(date+gmt_offset)
308: int tt_offset =
309: ((messageHeader->gmt_offset / 100) *(60 * 60))
310: +(messageHeader->gmt_offset % 100) * 60;
1.30 paf 311:
1.1 paf 312: putReceived(received, "date",
313: messageHeader->date // local sender
314: -tt_offset // move local sender to GMT sender
1.30 paf 315: -gmt_offset() // move GMT sender to our local time
1.1 paf 316: );
317: // .message-id
1.45 ! paf 318: putReceived(source_charset, received, "message-id", messageHeader->message_id);
1.1 paf 319:
320: // .body[part/parts
321: GMimePart *part=message->mime_part;
322: const GMimeContentType *type=g_mime_part_get_content_type(part);
1.45 ! paf 323: MimePart2body_info info={&r, &received};
1.1 paf 324: g_mime_part_foreach(part, MimePart2body, &info);
325:
326: // normal unref
327: g_mime_object_unref(GMIME_OBJECT(message));
328: } catch(...) {
329: // abnormal unref
330: g_mime_object_unref(GMIME_OBJECT(message));
331: }
332: }
333: #endif
334:
335:
336:
1.23 paf 337: void VMail::fill_received(Request& r) {
1.1 paf 338: // store letter to received
1.3 paf 339: #ifdef WITH_MAILRECEIVE
1.45 ! paf 340: if(r.request_info.mail_received) {
1.1 paf 341: // init
342: g_mime_init(GMIME_INIT_FLAG_UTF8);
343:
344: // create stream with CRLF filter
345: GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
346: GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
347: GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
348: g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
1.45 ! paf 349: g_mime_stream_unref(stream);
1.1 paf 350: stream = istream;
351: try {
352: // parse incoming stream
1.45 ! paf 353: parse(r, stream, vreceived->hash());
1.1 paf 354: // normal stream free
1.45 ! paf 355: g_mime_stream_unref(stream);
1.1 paf 356: } catch(...) {
357: // abnormal stream free
1.45 ! paf 358: g_mime_stream_unref(stream);
1.1 paf 359: }
360: }
361: #endif
362: }
363:
1.9 paf 364: typedef int (*string_contains_char_which_check)(int);
1.45 ! paf 365: static bool string_contains_char_which(const char* string, string_contains_char_which_check check) {
1.9 paf 366: while(char c=*string++) {
367: if(check(c))
368: return true;
369: }
370: return false;
371: }
1.32 paf 372: static char *trimBoth(char *s) {
373: // sanity check
374: if(!s)
375: return 0;
376:
377: // trim head whitespace
378: while(*s && isspace(*s))
379: s++;
380: // trim tail whitespace
381: char *tail=s+strlen(s);
382: if(tail>s) {
383: do {
384: --tail;
385: if(isspace(*tail))
386: *tail=0;
387: } while(tail>s);
388: }
389: // return it
390: return s;
391: }
1.45 ! paf 392: static void extractEmail(String& result, char *email) {
1.32 paf 393: email=trimBoth(email);
1.45 ! paf 394: result.append_help_length(email, 0, String::L_TAINTED);
1.9 paf 395:
396: /*
397: http://www.faqs.org/rfcs/rfc822.html
398:
399: addr-spec = local-part "@" domain ; global address
400:
401: local-part = word *("." word) ; uninterpreted case-preserved
402: word = atom / quoted-string
403:
404: domain = sub-domain *("." sub-domain)
405: sub-domain = domain-ref / domain-literal
406: domain-ref = atom ; symbolic reference
407:
408: domain-literal << ignoring for now
409: quoted-string in word << ignoring for now
410:
411: atom = 1*<any CHAR except specials, SPACE and CTLs> << the ONLY to check
412:
413: specials = "(" / ")" / "<" / ">" / "@" ; Must be in quoted-
414: / "," / ";" / ":" / "\" / <"> ; string, to use
415: / "." / "[" / "]" ; within a word.
416:
417: */
1.45 ! paf 418: const char* exception_type="email.format";
1.9 paf 419: if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.17 paf 420: throw Exception(exception_type,
1.9 paf 421: &result,
1.32 paf 422: "email contains bad characters (specials)");
1.9 paf 423: if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17 paf 424: throw Exception(exception_type,
1.9 paf 425: &result,
1.32 paf 426: "email contains bad characters (whitespace)");
1.9 paf 427: if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17 paf 428: throw Exception(exception_type,
1.9 paf 429: &result,
1.32 paf 430: "email contains bad characters (control)");
1.16 paf 431: if(result.is_empty())
1.17 paf 432: throw Exception(exception_type,
1.45 ! paf 433: 0,
1.16 paf 434: "email is empty");
1.39 paf 435: }
436:
1.45 ! paf 437: static const String& extractEmails(const String& string) {
! 438: char *emails=string.cstrm();
! 439: String& result=*new String;
1.39 paf 440: while(char *email=lsplit(&emails, ',')) {
441: rsplit(email, '>');
442: if(char *in_brackets=lsplit(email, '<'))
443: email=in_brackets;
444: if(!result.is_empty())
445: result<<",";
1.45 ! paf 446: extractEmail(result, email);
1.39 paf 447: }
1.9 paf 448:
449: return result;
450: }
1.45 ! paf 451:
! 452: #ifndef DOXYGEN
! 453: struct Store_message_element_info {
! 454: Request_charsets& charsets;
! 455: String& header;
! 456: const String* & from;
! 457: bool extract_to; String* & to;
! 458: const String* errors_to;
! 459: bool mime_version_specified;
! 460: ArrayValue* parts[P_TYPES_COUNT];
! 461: int parts_count;
! 462: bool backward_compatibility;
! 463: Value* content_type;
! 464:
! 465: Store_message_element_info(
! 466: Request_charsets& acharsets,
! 467: String& aheader,
! 468: const String* & afrom,
! 469: bool aextract_to, String* & ato
! 470: ):
! 471:
! 472: charsets(acharsets),
! 473: header(aheader),
! 474: from(afrom),
! 475: extract_to(aextract_to), to(ato),
! 476: errors_to(0),
! 477: mime_version_specified(false),
! 478: parts_count(0),
! 479: backward_compatibility(false), content_type(0) {
! 480: }
! 481: };
! 482: #endif
! 483: static void store_message_element(HashStringValue::key_type raw_element_name,
! 484: HashStringValue::value_type element_value,
! 485: Store_message_element_info *info) {
! 486: const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
! 487: info->charsets.source(), String::CC_LOWER);
1.1 paf 488:
489: // exclude internals
1.5 paf 490: if(low_element_name==CHARSET_NAME
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));
! 647: if(Junction* junction=text_value->get_junction())
1.41 paf 648: body=&r.process_to_string(*text_value);
1.26 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");
653:
654: break;
655: }
1.41 paf 656: }
657: if(body) {
1.45 ! paf 658: const char* body_cstr=strdup(body->cstr(String::L_UNSPECIFIED)); // body
! 659: String::C mail=Charset::transcode(
! 660: String::C(body_cstr, strlen(body_cstr)),
! 661: r.charsets.source(),
! 662: r.charsets.mail()/*always set - either mail.charset or $request:charset*/);
! 663: ///@todo
! 664: result.append_know_length(mail.str, mail.length, String::L_CLEAN);
1.1 paf 665: }
666:
667: return result;
668: };
669:
670: /// @todo files and messages in order (file, file2, ...)
1.45 ! paf 671: const String& VMail::message_hash_to_string(Request& r,
! 672: HashStringValue* message_hash, int level,
! 673: const String* & from, bool extract_to, String* & to) {
1.34 paf 674:
1.1 paf 675: if(!message_hash)
676: throw Exception("parser.runtime",
1.45 ! paf 677: 0,
1.1 paf 678: "message must be hash");
679:
1.45 ! paf 680: String& result=*new String;
1.1 paf 681:
1.45 ! paf 682: if(Value* vrecodecharset_name=message_hash->get(charset_name))
! 683: r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
! 684: .change_case(r.charsets.source(), String::CC_UPPER)));
1.1 paf 685: else
1.45 ! paf 686: r.charsets.set_mail(r.charsets.source());
! 687: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 688:
1.45 ! paf 689: Store_message_element_info info(r.charsets,
! 690: result, from, extract_to, to);
1.1 paf 691: {
1.45 ! paf 692: // for backward compatibilyty $.body+$.content-type ->
! 693: // $.text[$.value[] $.content-type[]]
! 694:
1.1 paf 695: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45 ! paf 696: info.parts[pt]=new ArrayValue(1);
! 697:
! 698: Value* body=message_hash->get("body");
! 699: if(body) {
! 700: message_hash->remove("body");
! 701: info.backward_compatibility=true;
! 702: }
1.1 paf 703: message_hash->for_each(store_message_element, &info);
1.45 ! paf 704:
! 705: if(body) {
! 706: VHash& text_part=*new VHash();
! 707: HashStringValue& hash=text_part.hash();
! 708: hash.put(value_name, body);
! 709: if(info.content_type)
! 710: hash.put(content_type_name, info.content_type);
! 711:
! 712: *info.parts[P_TEXT]+=&text_part;
! 713: info.parts_count++;
! 714: }
! 715:
1.10 paf 716: if(!info.errors_to)
717: result << "errors-to: postmaster\n"; // errors-to: default
1.37 paf 718: if(!info.mime_version_specified)
719: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 720: }
721:
1.45 ! paf 722: int textCount=info.parts[P_TEXT]->count();
1.1 paf 723: if(textCount>1)
724: throw Exception("parser.runtime",
1.45 ! paf 725: 0,
1.1 paf 726: "multiple text parts not supported, use file part");
1.45 ! paf 727: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 728: if(htmlCount>1)
729: throw Exception("parser.runtime",
1.45 ! paf 730: 0,
1.1 paf 731: "multiple html parts not supported, use file part");
732:
733:
734: bool multipart=info.parts_count>1;
735: bool alternative=textCount && htmlCount;
736: // header
737: char *boundary=0;
738: if(multipart) {
1.45 ! paf 739: boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1 paf 740: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
741: // multi-part
1.45 ! paf 742: result
! 743: << "content-type: multipart/mixed; boundary=\""
! 744: << boundary << "\"\n"
! 745: "\n"
! 746: "This is a multi-part message in MIME format.";
1.1 paf 747: }
748:
749: // alternative or not
750: {
751: if(alternative) {
1.45 ! paf 752: result << "\n\n--" << boundary << "\n" // intermediate boundary
! 753: "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 754: }
755: for(int i=0; i<2; i++) {
756: PartType pt=i==0?P_TEXT:P_HTML;
1.45 ! paf 757: if(info.parts[pt]->count()) {
1.1 paf 758: if(alternative)
759: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
760: else if(boundary)
761: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 ! paf 762: result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 763: }
764: }
765: if(alternative)
766: result << "\n\n--ALT" << boundary << "--\n";
767: }
768:
769: // files
770: {
1.45 ! paf 771: ArrayValue& files=*info.parts[P_FILE];
! 772: for(size_t i=0; i<files.count(); i++) {
1.1 paf 773: if(boundary)
774: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 ! paf 775: result << file_value_to_string(r, files.get(i));
1.1 paf 776: }
777: }
778:
779: // messages
780: {
1.45 ! paf 781: ArrayValue& messages=*info.parts[P_MESSAGE];
! 782: for(size_t i=0; i<messages.count(); i++) {
1.1 paf 783: if(boundary)
784: result << "\n\n--" << boundary << "\n"; // intermediate boundary
785:
1.45 ! paf 786: const String* dummy_from;
! 787: String* dummy_to;
! 788: result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
! 789: dummy_from, false, dummy_to);
1.1 paf 790: }
791: }
792:
793: // tailer
794: if(boundary)
795: result << "\n\n--" << boundary << "--\n"; // finish boundary
796:
797: // return
798: return result;
799: }
800:
801:
1.45 ! paf 802: Value* VMail::get_element(const String& aname, Value& aself, bool looking_up) {
1.1 paf 803: // $fields
1.3 paf 804: #ifdef WITH_MAILRECEIVE
1.1 paf 805: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.45 ! paf 806: return vreceived;
1.1 paf 807: #endif
808:
809: // $CLASS,$method
1.45 ! paf 810: if(Value* result=VStateless_class::get_element(aname, aself, looking_up))
1.1 paf 811: return result;
812:
813: return 0;
814: }
815:
1.3 paf 816: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1 paf 817: # define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
818: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
819: # ifdef _DEBUG
820: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
821: # else
822: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
823: # endif
824: #endif
E-mail: