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