Annotation of parser3/src/types/pa_vmail.C, revision 1.92
1.1 paf 1: /** @file
2: Parser: @b mail class.
3: relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
4:
1.84 misha 5: Copyright(c) 2001-2009 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.92 ! misha 9: static const char * const IDENT_VMAIL_C="$Date: 2009-09-25 13:00:19 $";
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:
1.84 misha 212: // @todo GMimePart:
1.1 paf 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
1.84 misha 293: // @todo: messageHeader->recipients
1.1 paf 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
1.92 ! misha 333: r
1.53 paf 334: #endif
1.92 ! misha 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:
1.92 ! misha 415: domain-literal << ignoring for now
1.9 paf 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-
1.92 ! misha 421: / "," / ";" / ":" / "\" / <"> ; string, to use
! 422: / "." / "[" / "]" ; within a word.
1.9 paf 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,
1.92 ! misha 493: HashStringValue::value_type element_value,
! 494: Store_message_element_info *info) {
1.45 paf 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.90 misha 527: if(low_element_name==CONTENT_DISPOSITION)
1.66 paf 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());
1.92 ! misha 567:
1.45 paf 568: String& mail_line=*new String;
1.73 paf 569: if(low_element_name=="to"
570: || low_element_name=="cc"
571: || low_element_name=="bcc")
572: {
573: // never wrap address lines, mailer can not handle wrapped properly
574: mail_line.append_strdup(mail.str, mail.length, String::L_MAIL_HEADER);
575: } else {
576: while(mail.length) {
577: bool too_long=mail.length>MAX_CHARS_IN_HEADER_LINE;
578: size_t length=too_long
579: ? MAX_CHARS_IN_HEADER_LINE
580: : mail.length;
581:
582: mail_line.append_strdup(mail.str, length, String::L_MAIL_HEADER);
583: mail.length-=length;
584:
585: if(too_long)
586: mail_line << "\n "; // break header and continue it on next line
587: }
588: }
1.45 paf 589:
590: // append header line
591: info->header
1.91 misha 592: << capitalize(raw_element_name.cstr())
1.86 misha 593: << ":" << mail_line.untaint_cstr(String::L_AS_IS, 0, &info->charsets)
1.45 paf 594: << "\n";
595: }
596:
597: static const String& file_value_to_string(Request& r, Value* send_value) {
1.64 paf 598: String& result=*new String;
599:
1.45 paf 600: VFile* vfile;
601: const String* file_name=0;
602: Value* vformat=0;
1.76 misha 603: Value* vcid=0;
1.66 paf 604: const String* dummy_from;
605: String* dummy_to;
606: Store_message_element_info info(r.charsets,
607: result, dummy_from, false, dummy_to);
1.45 paf 608: if(HashStringValue *send_hash=send_value->get_hash()) { // hash
1.74 paf 609: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.64 paf 610:
1.1 paf 611: // $.value
1.45 paf 612: if(Value* value=send_hash->get(value_name))
1.85 misha 613: vfile=value->as_vfile(String::L_AS_IS);
1.1 paf 614: else
1.78 misha 615: throw Exception(PARSER_RUNTIME,
1.45 paf 616: 0,
1.1 paf 617: "file part has no $value");
618:
619: // $.format
1.45 paf 620: vformat=send_hash->get(format_name);
1.1 paf 621:
1.76 misha 622: // $.content-id
623: vcid=send_hash->get(cid_name);
624:
1.6 paf 625: // $.name
1.45 paf 626: if(Value* vfile_name=send_hash->get(name_name)) // $name specified
1.1 paf 627: file_name=&vfile_name->as_string();
1.28 paf 628: } else // must be VFile then
1.45 paf 629: vfile=send_value->as_vfile(String::L_AS_IS);
1.28 paf 630:
631: if(!file_name)
1.45 paf 632: file_name=&vfile->fields().get(name_name)->as_string();
1.28 paf 633:
1.45 paf 634: const char* file_name_cstr=file_name->cstr();
1.1 paf 635:
636: // content-type: application/octet-stream
1.91 misha 637: result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << r.mime_type_of(file_name_cstr) << "; name=\"" << file_name_cstr << "\"\n";
1.66 paf 638:
639: if(!info.had_content_disposition) {
1.92 ! misha 640: // $.Content-Disposition wasn't specified by user
1.79 misha 641: result
1.91 misha 642: << CONTENT_DISPOSITION_CAPITALIZED ": "
1.79 misha 643: << ( vcid ? CONTENT_DISPOSITION_INLINE : CONTENT_DISPOSITION_ATTACHMENT )
644: << "; "
645: << CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.66 paf 646: }
1.1 paf 647:
1.79 misha 648: if(vcid)
1.84 misha 649: result << CID_NAME ": <" << vcid->as_string() << ">\n"; // @todo: value must be escaped as %hh
1.79 misha 650:
1.45 paf 651: const String* type=vformat?&vformat->as_string():0;
1.1 paf 652: if(!type/*default = uue*/ || *type=="uue") {
1.91 misha 653: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": x-uuencode\n" << "\n";
1.45 paf 654: pa_uuencode(result, *file_name, *vfile);
1.75 paf 655: } else {
656: if(*type=="base64") {
1.91 misha 657: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": base64\n" << "\n";
1.80 misha 658: result << pa_base64_encode(vfile->value_ptr(), vfile->value_size());
1.75 paf 659: } else {
660: // for now
1.82 misha 661: throw Exception(PARSER_RUNTIME,
662: type,
663: "unknown attachment encode format");
1.75 paf 664: }
665: }
1.1 paf 666:
667: return result;
668: }
669:
1.45 paf 670: static const String& text_value_to_string(Request& r,
1.92 ! misha 671: PartType pt, Value* send_value,
! 672: Store_message_element_info& info) {
1.45 paf 673: String& result=*new String;
1.1 paf 674:
1.45 paf 675: Value* text_value;
1.81 misha 676: Value* content_transfer_encoding=0;
1.45 paf 677: if(HashStringValue* send_hash=send_value->get_hash()) {
1.1 paf 678: // $.USER-HEADERS
1.92 ! misha 679: info.content_type=0;
! 680: info.backward_compatibility=false; // reset
1.74 paf 681: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.1 paf 682: // $.value
1.45 paf 683: text_value=send_hash->get(value_name);
1.1 paf 684: if(!text_value)
1.78 misha 685: throw Exception(PARSER_RUNTIME,
1.45 paf 686: 0,
687: "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.81 misha 688: content_transfer_encoding=send_hash->get(content_transfer_encoding_name);
1.1 paf 689: } else
1.45 paf 690: text_value=send_value;
1.1 paf 691:
1.45 paf 692: if(!info.content_type) {
693: result
1.91 misha 694: << HTTP_CONTENT_TYPE_CAPITALIZED ": text/" << (pt==P_TEXT?"plain":"html")
1.45 paf 695: << "; charset=" << info.charsets.mail().NAME()
696: << "\n";
1.1 paf 697: }
1.81 misha 698: if(!content_transfer_encoding)
1.91 misha 699: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED << ": 8bit\n";
1.1 paf 700:
701: // header|body separator
702: result << "\n";
703:
704: // body
1.45 paf 705: const String* body;
1.1 paf 706: switch(pt) {
707: case P_TEXT:
1.92 ! misha 708: {
! 709: body=&text_value->as_string();
! 710: break;
! 711: }
1.1 paf 712: case P_HTML:
713: {
1.45 paf 714: Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
1.55 paf 715: if(text_value->get_junction())
1.41 paf 716: body=&r.process_to_string(*text_value);
1.92 ! misha 717: else
1.78 misha 718: throw Exception(PARSER_RUNTIME,
1.45 paf 719: 0,
1.1 paf 720: "html part value must be code");
721:
722: break;
723: }
1.53 paf 724: default:
725: throw Exception(0,
726: 0,
727: "unhandled part type #%d", pt);
1.41 paf 728: }
729: if(body) {
1.69 paf 730: Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.87 misha 731: const char* body_cstr=body->untaint_cstr(String::L_AS_IS, 0, &charsets); // body
1.45 paf 732: String::C mail=Charset::transcode(
733: String::C(body_cstr, strlen(body_cstr)),
734: r.charsets.source(),
735: r.charsets.mail()/*always set - either mail.charset or $request:charset*/);
736: ///@todo
737: result.append_know_length(mail.str, mail.length, String::L_CLEAN);
1.1 paf 738: }
739:
740: return result;
741: };
742:
743: /// @todo files and messages in order (file, file2, ...)
1.45 paf 744: const String& VMail::message_hash_to_string(Request& r,
1.92 ! misha 745: HashStringValue* message_hash, int level,
! 746: const String* & from, bool extract_to, String* & to) {
1.34 paf 747:
1.1 paf 748: if(!message_hash)
1.78 misha 749: throw Exception(PARSER_RUNTIME,
1.45 paf 750: 0,
1.1 paf 751: "message must be hash");
752:
1.45 paf 753: String& result=*new String;
1.1 paf 754:
1.45 paf 755: if(Value* vrecodecharset_name=message_hash->get(charset_name))
756: r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
757: .change_case(r.charsets.source(), String::CC_UPPER)));
1.1 paf 758: else
1.45 paf 759: r.charsets.set_mail(r.charsets.source());
760: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 761:
1.45 paf 762: Store_message_element_info info(r.charsets,
1.60 paf 763: result, from, extract_to, to);
1.1 paf 764: {
1.45 paf 765: // for backward compatibilyty $.body+$.content-type ->
766: // $.text[$.value[] $.content-type[]]
767:
1.1 paf 768: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45 paf 769: info.parts[pt]=new ArrayValue(1);
770:
771: Value* body=message_hash->get("body");
772: if(body) {
773: message_hash->remove("body");
774: info.backward_compatibility=true;
775: }
1.74 paf 776: message_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.45 paf 777:
778: if(body) {
779: VHash& text_part=*new VHash();
780: HashStringValue& hash=text_part.hash();
781: hash.put(value_name, body);
782: if(info.content_type)
783: hash.put(content_type_name, info.content_type);
784:
785: *info.parts[P_TEXT]+=&text_part;
786: info.parts_count++;
787: }
788:
1.10 paf 789: if(!info.errors_to)
1.91 misha 790: result << "Errors-To: postmaster\n"; // errors-to: default
1.37 paf 791: if(!info.mime_version_specified)
792: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 793: }
794:
1.45 paf 795: int textCount=info.parts[P_TEXT]->count();
1.1 paf 796: if(textCount>1)
1.78 misha 797: throw Exception(PARSER_RUNTIME,
1.45 paf 798: 0,
1.1 paf 799: "multiple text parts not supported, use file part");
1.45 paf 800: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 801: if(htmlCount>1)
1.78 misha 802: throw Exception(PARSER_RUNTIME,
1.45 paf 803: 0,
1.1 paf 804: "multiple html parts not supported, use file part");
805:
806:
807: bool multipart=info.parts_count>1;
808: bool alternative=textCount && htmlCount;
809: // header
810: char *boundary=0;
811: if(multipart) {
1.45 paf 812: boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1 paf 813: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.76 misha 814:
815: bool is_inline = false;
816: {
817: ArrayValue& files=*info.parts[P_FILE];
818: for(size_t i=0; i<files.count(); i++) {
1.82 misha 819: HashStringValue* file;
820: if((file=files.get(i)->get_hash()) && file->get(cid_name)){
1.76 misha 821: is_inline = true;
822: break;
1.77 misha 823: }
1.76 misha 824: }
825: }
826:
1.91 misha 827: result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << ( is_inline ? HTTP_CONTENT_TYPE_MULTIPART_RELATED : HTTP_CONTENT_TYPE_MULTIPART_MIXED ) << ";";
1.76 misha 828:
1.1 paf 829: // multi-part
1.45 paf 830: result
1.76 misha 831: << " boundary=\"" << boundary << "\"\n"
1.45 paf 832: "\n"
833: "This is a multi-part message in MIME format.";
1.1 paf 834: }
835:
836: // alternative or not
837: {
838: if(alternative) {
1.45 paf 839: result << "\n\n--" << boundary << "\n" // intermediate boundary
1.91 misha 840: HTTP_CONTENT_TYPE_CAPITALIZED ": multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 841: }
842: for(int i=0; i<2; i++) {
843: PartType pt=i==0?P_TEXT:P_HTML;
1.45 paf 844: if(info.parts[pt]->count()) {
1.1 paf 845: if(alternative)
846: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
847: else if(boundary)
848: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 849: result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 850: }
851: }
852: if(alternative)
853: result << "\n\n--ALT" << boundary << "--\n";
854: }
855:
856: // files
857: {
1.45 paf 858: ArrayValue& files=*info.parts[P_FILE];
859: for(size_t i=0; i<files.count(); i++) {
1.1 paf 860: if(boundary)
861: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 862: result << file_value_to_string(r, files.get(i));
1.1 paf 863: }
864: }
865:
866: // messages
867: {
1.45 paf 868: ArrayValue& messages=*info.parts[P_MESSAGE];
869: for(size_t i=0; i<messages.count(); i++) {
1.1 paf 870: if(boundary)
871: result << "\n\n--" << boundary << "\n"; // intermediate boundary
872:
1.45 paf 873: const String* dummy_from;
874: String* dummy_to;
875: result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
1.60 paf 876: dummy_from, false, dummy_to);
1.1 paf 877: }
878: }
879:
880: // tailer
881: if(boundary)
882: result << "\n\n--" << boundary << "--\n"; // finish boundary
883:
884: // return
885: return result;
886: }
887:
888:
1.88 misha 889: Value* VMail::get_element(const String& aname) {
1.1 paf 890: // $fields
1.3 paf 891: #ifdef WITH_MAILRECEIVE
1.1 paf 892: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.48 paf 893: return &vreceived;
1.1 paf 894: #endif
895:
896: // $CLASS,$method
1.88 misha 897: if(Value* result=VStateless_class::get_element(aname))
1.1 paf 898: return result;
899:
900: return 0;
901: }
902:
1.3 paf 903: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.48 paf 904: # define GNOME_LIBS "../../../../win32/gnome"
1.1 paf 905: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
906: # ifdef _DEBUG
907: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
908: # else
909: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
910: # endif
911: #endif
E-mail: