Annotation of parser3/src/types/pa_vmail.C, revision 1.127
1.1 paf 1: /** @file
2: Parser: @b mail class.
3: relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
4:
1.124 moko 5: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 paf 6: Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
7: */
1.13 paf 8:
1.1 paf 9: #include "pa_sapi.h"
10: #include "pa_vmail.h"
11: #include "pa_vstring.h"
12: #include "pa_request.h"
13: #include "pa_common.h"
1.127 ! moko 14: #include "pa_base64.h"
1.1 paf 15: #include "pa_charset.h"
16: #include "pa_charsets.h"
1.118 moko 17: #include "pa_random.h"
1.1 paf 18: #include "pa_vdate.h"
19: #include "pa_vfile.h"
20: #include "pa_uue.h"
21:
1.127 ! moko 22: volatile const char * IDENT_PA_VMAIL_C="$Id: pa_vmail.C,v 1.126 2017/05/29 11:03:23 moko Exp $" IDENT_PA_VMAIL_H;
1.98 moko 23:
1.3 paf 24: #ifdef WITH_MAILRECEIVE
1.4 paf 25: extern "C" {
1.99 moko 26: #include "gmime/gmime.h"
1.4 paf 27: }
1.45 paf 28:
29: #include "pa_charsets.h"
1.1 paf 30: #endif
31:
32: // defines
33:
34: #define RAW_NAME "raw"
35:
36: // internals
37:
38: enum PartType {
39: P_TEXT,
40: P_HTML,
41: P_FILE,
42: P_MESSAGE,
43: P_TYPES_COUNT
44: };
45:
1.99 moko 46: static const char* const part_name_begins[P_TYPES_COUNT] = {
47: "text",
48: "html",
49: "file",
50: "message"
51: };
1.45 paf 52:
53: // defines for statics
54:
55: #define FORMAT_NAME "format"
56: #define CHARSET_NAME "charset"
1.76 misha 57: #define CID_NAME "content-id"
1.45 paf 58:
1.112 moko 59: #define CONTENT_TRANSFER_ENCODING_NAME "content-transfer-encoding"
60: #define CONTENT_TRANSFER_ENCODING_CAPITALIZED "Content-Transfer-Encoding"
61:
1.45 paf 62: // statics
63:
64: static const String format_name(FORMAT_NAME);
65: static const String charset_name(CHARSET_NAME);
1.76 misha 66: static const String cid_name(CID_NAME);
1.1 paf 67:
1.112 moko 68: static const String content_transfer_encoding_name(CONTENT_TRANSFER_ENCODING_NAME);
69:
1.61 paf 70: // consts
71:
1.126 moko 72: const int MAX_CHARS_IN_HEADER_LINE = (991 - 9 /* Subject: */ - 19 /* =?Windows-1251?Q?...?= */ - 128 /* just in case */) / 3 /* quote-printable */ - 5 /* maximum part of trancated UTF-8 char */;
1.61 paf 73:
1.1 paf 74: // VMail
75:
1.116 moko 76: extern Methoded* mail_class;
1.1 paf 77:
1.117 moko 78: VMail::VMail(): VStateless_class(mail_class) {}
1.1 paf 79:
1.3 paf 80: #ifdef WITH_MAILRECEIVE
1.1 paf 81:
1.48 paf 82: #define EXCEPTION_VALUE "x-exception"
83:
1.99 moko 84: static Charset* source_charset;
85:
1.107 moko 86: static const char *transcode(const char *value) {
87: if(value && !source_charset->isUTF8()){
1.121 moko 88: String::C transcoded=Charset::transcode(String::C(value, strlen(value)), pa_UTF8_charset, *source_charset);
1.107 moko 89: value=transcoded.str;
90: }
91: return value;
92: }
93:
1.99 moko 94: static void putReceived(HashStringValue& received, const char* name, Value* value, bool capitalizeName=false) {
95: if(name && value)
96: received.put(capitalizeName ? capitalize(pa_strdup(name)) : pa_strdup(name), value);
1.1 paf 97: }
98:
1.99 moko 99: static void putReceived(HashStringValue& received, const char* name, const char* value, bool capitalizeName=false) {
100: if(name && value)
1.108 moko 101: putReceived(received, name, new VString(*new String(pa_strdup(value), String::L_TAINTED)), capitalizeName);
1.1 paf 102: }
103:
1.99 moko 104: static void putReceived(HashStringValue& received, const char* name, time_t value) {
105: if(name)
106: received.put(pa_strdup(name), new VDate(value) );
1.1 paf 107: }
108:
1.99 moko 109: static void MimeHeaderField2received(const char* name, const char* value, gpointer data) {
110: HashStringValue* received=static_cast<HashStringValue*>(data);
111: putReceived(*received, name, value, true /*capitalizeName*/);
1.1 paf 112: }
113:
1.99 moko 114: static void parse(Request& r, GMimeMessage *message, HashStringValue& received);
1.1 paf 115:
116: #ifndef DOXYGEN
1.45 paf 117: struct MimePart2body_info {
118: Request* r;
119: HashStringValue* body;
1.1 paf 120: int partCounts[P_TYPES_COUNT];
121: };
122: #endif
1.99 moko 123:
124:
125: static char *readStream(GMimeStream* gstream, size_t &length){
126: length=MAX_STRING;
127: char *result=(char*)pa_malloc_atomic(length+1);
128: char *ptr=result;
129:
130: while(true) {
131: size_t current_size=ptr-result;
132: ssize_t todo_size=length-current_size;
133: ssize_t received_size=g_mime_stream_read (gstream, ptr, todo_size);
134:
135: if(received_size<0)
136: throw Exception(PARSER_RUNTIME, 0,"mail content stream read error");
137: if(received_size==0)
138: break;
139: if(received_size==todo_size) {
140: length=length*2;
141: result=(char *)pa_realloc(result, length+1);
142: ptr=result+current_size+received_size;
143: } else {
144: ptr+=received_size;
145: }
146: }
147:
148: length=ptr-result;
149: result[length]='\0';
150: return result;
151: }
152:
153: static void MimePart2body(GMimeObject *parent, GMimeObject *part, gpointer data) {
1.45 paf 154: MimePart2body_info& info=*static_cast<MimePart2body_info *>(data);
1.1 paf 155:
1.99 moko 156: // skipping message/partial & frames
1.101 moko 157: if (GMIME_IS_MESSAGE_PARTIAL (part) || GMIME_IS_MULTIPART (part))
1.99 moko 158: return;
159:
160: if (GMimeContentType *type=g_mime_object_get_content_type(part)) {
161: PartType partType=P_FILE;
162:
1.107 moko 163: if (GMIME_IS_MESSAGE_PART(part)){
1.99 moko 164: partType=P_MESSAGE;
1.107 moko 165: } else {
166: const char *disposition=g_mime_object_get_disposition(part);
167: if(!disposition || strcmp(disposition, GMIME_DISPOSITION_ATTACHMENT)){
168: if(g_mime_content_type_is_type(type, "text", "plain"))
169: partType=P_TEXT;
170: else if(g_mime_content_type_is_type(type, "text", "html"))
171: partType=P_HTML;
172: }
173: }
1.99 moko 174:
1.1 paf 175: // partName
1.99 moko 176: int partNumber=++info.partCounts[partType];
177: const char *partName=part_name_begins[partType];
1.1 paf 178:
1.99 moko 179: char partNameNumbered[MAX_STRING];
180: snprintf(partNameNumbered, MAX_STRING, "%s%d", partName, partNumber);
1.1 paf 181:
1.99 moko 182: // $.partN[
183: VHash* vpartHash(new VHash);
184: if(partNumber==1)
185: putReceived(*info.body, partName, vpartHash);
186: putReceived(*info.body, partNameNumbered, vpartHash);
187:
188: HashStringValue& partHash=vpartHash->hash();
189:
190: // $.raw[
191: VHash* vraw(new VHash);
192: putReceived(partHash, RAW_NAME, vraw);
193: g_mime_header_list_foreach(part->headers, MimeHeaderField2received, &vraw->hash());
194:
195: // $.content-type[
196: VHash* vcontent_type(new VHash);
197: putReceived(partHash, "content-type", vcontent_type);
198:
199: // $.value[text/plain]
200: char value[MAX_STRING];
201: snprintf(value, MAX_STRING, "%s/%s", type->type ? type->type : "x-unknown", type->subtype ? type->subtype : "x-unknown");
202: putReceived(vcontent_type->hash(), VALUE_NAME, value);
203:
204: const GMimeParam *param=g_mime_content_type_get_params(type);
205: while(param) {
206: // $.charset[windows-1251] && co
1.107 moko 207: putReceived(vcontent_type->hash(), g_mime_param_get_name(param), transcode(g_mime_param_get_value(param)), true /*capitalizeName*/);
1.99 moko 208: param=g_mime_param_next(param);
209: }
210:
211: if (GMIME_IS_MESSAGE_PART (part)) {
1.102 moko 212: /* message/rfc822, $.raw[] will be overwitten */
1.99 moko 213: GMimeMessage *message = g_mime_message_part_get_message ((GMimeMessagePart *) part);
214: parse(*info.r, message, partHash);
1.1 paf 215: } else {
1.101 moko 216: GMimePart *gpart = (GMimePart *)part;
217:
218: putReceived(partHash, "description", g_mime_part_get_content_description(gpart));
219: putReceived(partHash, "content-id", g_mime_part_get_content_id(gpart));
220: putReceived(partHash, "content-md5", g_mime_part_get_content_md5(gpart));
221: putReceived(partHash, "content-location", g_mime_part_get_content_location(gpart));
222:
1.1 paf 223: // $.value[string|file]
1.103 moko 224: if(GMimeDataWrapper* gcontent=g_mime_part_get_content_object(gpart)){
225: GMimeStream* gstream=g_mime_stream_filter_new(g_mime_data_wrapper_get_stream(gcontent));
226:
227: if(GMimeFilter* filter=g_mime_filter_basic_new(g_mime_part_get_content_encoding(gpart), false))
228: g_mime_stream_filter_add(GMIME_STREAM_FILTER(gstream), filter);
229:
230: size_t length;
1.99 moko 231:
1.103 moko 232: if(partType==P_FILE) {
233: char *content=readStream(gstream, length);
1.107 moko 234: const char* content_filename=transcode(g_mime_part_get_filename(gpart));
1.103 moko 235: VFile* vfile(new VFile);
236: vfile->set_binary(true/*tainted*/, content, length, new String(content_filename), content_filename ? new VString(info.r->mime_type_of(content_filename)) : 0);
237: putReceived(partHash, VALUE_NAME, vfile);
238: } else {
239: // P_TEXT, P_HTML
240: if(Value *charset=vcontent_type->hash().get("Charset"))
241: if(GMimeFilter* filter=g_mime_filter_charset_new(charset->get_string()->cstr(), source_charset->NAME_CSTR()))
242: g_mime_stream_filter_add(GMIME_STREAM_FILTER(gstream), filter);
243:
244: char *content=readStream(gstream, length);
245: putReceived(partHash, VALUE_NAME,new VString(*new String(content)));
246: }
1.1 paf 247: }
248: }
249: }
250: }
251:
1.99 moko 252: static void parse(Request& r, GMimeMessage *message, HashStringValue& received) {
1.1 paf 253: try {
254: // firstly user-defined strings go
255: // user headers
256: {
257: // $.raw[
1.99 moko 258: VHash* vraw(new VHash); putReceived( received, RAW_NAME, vraw);
259: g_mime_header_list_foreach(g_mime_object_get_header_list(GMIME_OBJECT(message)), MimeHeaderField2received, &vraw->hash());
260: }
261:
262: // secondly standard headers
263: putReceived(received, "message-id", g_mime_message_get_message_id(message));
1.107 moko 264: putReceived(received, "from", transcode(g_mime_message_get_sender(message)));
265:
266: const char *msg_to=internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_TO), false);
267: putReceived(received, "to", transcode(msg_to));
268: const char *msg_cc=internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_CC), false);
269: putReceived(received, "cc", transcode(msg_cc));
270:
271: putReceived(received, "reply-to", transcode(g_mime_message_get_reply_to(message)));
272: putReceived(received, "subject", transcode(g_mime_message_get_subject(message)));
273:
1.99 moko 274: // @todo: g_mime_message_get_recipients(message)
275:
1.113 moko 276: // .date(time_t in UTC)
1.99 moko 277: time_t date;
1.113 moko 278: g_mime_message_get_date(message, &date, 0);
1.114 moko 279: putReceived(received, "date", date);
1.1 paf 280:
281: // .body[part/parts
1.55 paf 282: MimePart2body_info info={&r, &received, {0}};
1.99 moko 283: g_mime_message_foreach(message, MimePart2body, &info);
1.1 paf 284:
1.48 paf 285: } catch(const Exception& e) {
1.125 moko 286: putReceived(received, VALUE_NAME, "<exception occurred while parsing message>");
1.99 moko 287: putReceived(received, EXCEPTION_VALUE, e.comment());
1.1 paf 288: } catch(...) {
1.125 moko 289: putReceived(received, VALUE_NAME, "<exception occurred while parsing message>");
1.1 paf 290: }
291: }
292:
1.99 moko 293: void VMail::fill_received(Request& r) {
1.45 paf 294: if(r.request_info.mail_received) {
1.99 moko 295: source_charset=&r.charsets.source();
1.50 paf 296: g_mime_init(0);
1.1 paf 297:
298: // create stream with CRLF filter
1.104 moko 299: GMimeStream *stream = g_mime_stream_filter_new(g_mime_stream_file_new(stdin));
1.99 moko 300: g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream), g_mime_filter_crlf_new(false, false));
1.1 paf 301: try {
1.99 moko 302: // parse incoming message
303: GMimeMessage *message=g_mime_parser_construct_message(g_mime_parser_new_with_stream(stream));
304: parse(r, message, vreceived.hash());
1.101 moko 305: g_object_unref(GMIME_OBJECT(message));
1.48 paf 306: } catch(const Exception& e) {
307: HashStringValue& received=vreceived.hash();
1.125 moko 308: putReceived(received, VALUE_NAME, "<exception occurred while parsing message>");
1.99 moko 309: putReceived(received, EXCEPTION_VALUE, e.comment());
1.1 paf 310: } catch(...) {
1.99 moko 311: // abnormal stream free
312: g_object_unref(stream);
1.48 paf 313: rethrow;
1.1 paf 314: }
1.99 moko 315: g_object_unref(stream);
316:
317: g_mime_shutdown();
1.1 paf 318: }
319: }
320:
1.99 moko 321: #else // WITH_MAILRECEIVE
322: void VMail::fill_received(Request&){}
323: #endif // WITH_MAILRECEIVE
324:
1.9 paf 325: typedef int (*string_contains_char_which_check)(int);
1.99 moko 326:
1.45 paf 327: static bool string_contains_char_which(const char* string, string_contains_char_which_check check) {
1.9 paf 328: while(char c=*string++) {
1.62 paf 329: if(check((unsigned char)c))
1.9 paf 330: return true;
331: }
332: return false;
333: }
1.99 moko 334:
1.32 paf 335: static char *trimBoth(char *s) {
336: // sanity check
337: if(!s)
338: return 0;
339:
340: // trim head whitespace
1.62 paf 341: while(*s && isspace((unsigned char)*s))
1.32 paf 342: s++;
343: // trim tail whitespace
344: char *tail=s+strlen(s);
345: if(tail>s) {
346: do {
347: --tail;
1.62 paf 348: if(isspace((unsigned char)*tail))
1.32 paf 349: *tail=0;
350: } while(tail>s);
351: }
352: // return it
353: return s;
354: }
1.99 moko 355:
1.45 paf 356: static void extractEmail(String& result, char *email) {
1.32 paf 357: email=trimBoth(email);
1.45 paf 358: result.append_help_length(email, 0, String::L_TAINTED);
1.9 paf 359:
360: /*
361: http://www.faqs.org/rfcs/rfc822.html
362:
363: addr-spec = local-part "@" domain ; global address
364:
365: local-part = word *("." word) ; uninterpreted case-preserved
366: word = atom / quoted-string
367:
368: domain = sub-domain *("." sub-domain)
369: sub-domain = domain-ref / domain-literal
370: domain-ref = atom ; symbolic reference
371:
1.92 misha 372: domain-literal << ignoring for now
1.9 paf 373: quoted-string in word << ignoring for now
374:
375: atom = 1*<any CHAR except specials, SPACE and CTLs> << the ONLY to check
376:
377: specials = "(" / ")" / "<" / ">" / "@" ; Must be in quoted-
1.92 misha 378: / "," / ";" / ":" / "\" / <"> ; string, to use
379: / "." / "[" / "]" ; within a word.
1.9 paf 380:
381: */
1.45 paf 382: const char* exception_type="email.format";
1.9 paf 383: if(strpbrk(email, "()<>,;:\\\"[]"/*specials minus @ and . */))
1.126 moko 384: throw Exception(exception_type, &result, "email contains bad characters (specials)");
1.9 paf 385: if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.126 moko 386: throw Exception(exception_type, &result, "email contains bad characters (whitespace)");
1.9 paf 387: if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.126 moko 388: throw Exception(exception_type, &result, "email contains bad characters (control)");
1.16 paf 389: if(result.is_empty())
1.126 moko 390: throw Exception(exception_type, 0, "email is empty");
1.39 paf 391: }
392:
1.45 paf 393: static const String& extractEmails(const String& string) {
394: char *emails=string.cstrm();
395: String& result=*new String;
1.39 paf 396: while(char *email=lsplit(&emails, ',')) {
397: rsplit(email, '>');
398: if(char *in_brackets=lsplit(email, '<'))
399: email=in_brackets;
400: if(!result.is_empty())
401: result<<",";
1.45 paf 402: extractEmail(result, email);
1.39 paf 403: }
1.9 paf 404:
405: return result;
406: }
1.45 paf 407:
408: #ifndef DOXYGEN
409: struct Store_message_element_info {
410: Request_charsets& charsets;
411: String& header;
412: const String* & from;
1.60 paf 413: bool extract_to; String* & to;
1.45 paf 414: bool mime_version_specified;
415: ArrayValue* parts[P_TYPES_COUNT];
416: int parts_count;
417: bool backward_compatibility;
418: Value* content_type;
1.66 paf 419: bool had_content_disposition;
1.45 paf 420:
1.126 moko 421: Store_message_element_info(Request_charsets& acharsets, String& aheader, const String* & afrom, bool aextract_to, String* & ato):
1.45 paf 422: charsets(acharsets),
423: header(aheader),
424: from(afrom),
1.60 paf 425: extract_to(aextract_to), to(ato),
1.45 paf 426: mime_version_specified(false),
427: parts_count(0),
1.66 paf 428: backward_compatibility(false), content_type(0),
429: had_content_disposition(false){
1.45 paf 430: }
431: };
432: #endif
1.99 moko 433:
1.126 moko 434: size_t mail_header_utf8_substring(const char *mail, size_t sub_length, size_t length){
435: int error_offset;
436: if(int error_code=pa_pcre_valid_utf((unsigned char *)mail, sub_length, &error_offset)){
437: if(error_code<PCRE_UTF8_ERR6){ // Missing X byte at the end of the string errors
438: sub_length+=error_code; // adding X bytes
439: return sub_length < length ? sub_length : length;
440: }
441: }
442:
443: return sub_length;
444: }
445:
446: static void store_message_element(HashStringValue::key_type raw_element_name, HashStringValue::value_type element_value, Store_message_element_info *info) {
447: const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(info->charsets.source(), String::CC_LOWER);
1.1 paf 448:
449: // exclude internals
1.52 paf 450: if(low_element_name==MAIL_OPTIONS_NAME
451: || low_element_name==CHARSET_NAME
1.5 paf 452: || low_element_name==VALUE_NAME
453: || low_element_name==RAW_NAME
1.64 paf 454: || low_element_name==FORMAT_NAME
1.76 misha 455: || low_element_name==NAME_NAME
1.94 misha 456: || low_element_name==CID_NAME
457: || low_element_name==MAIL_DEBUG_NAME)
1.1 paf 458: return;
459:
460: // grep parts
461: for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.45 paf 462: if(low_element_name.starts_with(part_name_begins[pt])) {
1.29 paf 463: // check that $.message# '#' is digit
1.45 paf 464: size_t start_len=strlen(part_name_begins[pt]);
465: if(low_element_name.length()>start_len) {
466: const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.63 paf 467: if(!isdigit((unsigned char)*at_num))
1.29 paf 468: continue;
469: }
1.45 paf 470: *info->parts[pt]+=element_value;
471: info->parts_count++;
1.1 paf 472: return;
473: }
474: }
475:
1.10 paf 476: // fetch some special headers
1.45 paf 477: if(low_element_name=="from")
478: info->from=&extractEmails(element_value->as_string());
1.90 misha 479: if(low_element_name==CONTENT_DISPOSITION)
1.66 paf 480: info->had_content_disposition=true;
1.60 paf 481: if(info->extract_to) { // defined only when SMTP used, see mail.C [collecting info for RCPT to-s]
1.39 paf 482: bool is_to=low_element_name=="to" ;
483: bool is_cc=low_element_name=="cc" ;
484: bool is_bcc=low_element_name=="bcc" ;
485: if(is_to||is_cc||is_bcc) {
1.45 paf 486: if(!info->to)
487: info->to=new String;
1.39 paf 488: else
1.45 paf 489: *info->to << ",";
490: *info->to << extractEmails(element_value->as_string());
1.39 paf 491: }
492:
493: if(is_bcc) // blinding it
1.45 paf 494: return;
1.39 paf 495: }
1.37 paf 496: if(low_element_name=="mime-version")
1.45 paf 497: info->mime_version_specified=true;
1.1 paf 498:
1.45 paf 499: // has content type?
500: if(low_element_name==CONTENT_TYPE_NAME) {
501: info->content_type=element_value;
502: if(info->backward_compatibility)
503: return;
1.39 paf 504: }
1.1 paf 505:
1.45 paf 506: // preparing header line
1.123 moko 507: const String& source_line=attributed_meaning_to_string(*element_value, String::L_AS_IS);
508:
1.66 paf 509: if(source_line.is_empty())
510: return; // we don't need empty headers here [used in clearing content-disposition]
511:
1.45 paf 512: const char* source_line_cstr=source_line.cstr();
1.126 moko 513: String::C mail=Charset::transcode(String::C(source_line_cstr, source_line.length()), info->charsets.source(), info->charsets.mail());
1.92 misha 514:
1.45 paf 515: String& mail_line=*new String;
1.126 moko 516: if(low_element_name=="to" || low_element_name=="cc" || low_element_name=="bcc")
1.73 paf 517: {
518: // never wrap address lines, mailer can not handle wrapped properly
519: mail_line.append_strdup(mail.str, mail.length, String::L_MAIL_HEADER);
520: } else {
521: while(mail.length) {
1.126 moko 522: bool too_long=mail.length > MAX_CHARS_IN_HEADER_LINE;
523: size_t length=too_long ? info->charsets.mail().isUTF8() ? mail_header_utf8_substring(mail.str, MAX_CHARS_IN_HEADER_LINE, mail.length) : MAX_CHARS_IN_HEADER_LINE : mail.length;
1.73 paf 524:
525: mail_line.append_strdup(mail.str, length, String::L_MAIL_HEADER);
1.126 moko 526:
1.73 paf 527: mail.length-=length;
1.126 moko 528: mail.str+=length;
1.73 paf 529:
530: if(too_long)
531: mail_line << "\n "; // break header and continue it on next line
1.126 moko 532: }
1.73 paf 533: }
1.45 paf 534:
535: // append header line
536: info->header
1.91 misha 537: << capitalize(raw_element_name.cstr())
1.96 misha 538: << ": " << mail_line.untaint_cstr(String::L_AS_IS, 0, &info->charsets)
1.45 paf 539: << "\n";
540: }
541:
542: static const String& file_value_to_string(Request& r, Value* send_value) {
1.64 paf 543: String& result=*new String;
544:
1.45 paf 545: VFile* vfile;
546: const String* file_name=0;
547: Value* vformat=0;
1.76 misha 548: Value* vcid=0;
1.66 paf 549: const String* dummy_from;
550: String* dummy_to;
1.95 misha 551: Store_message_element_info info(r.charsets, result, dummy_from, false, dummy_to);
552:
1.120 moko 553: HashStringValue *send_hash=send_value->get_hash();
554: if(send_hash && !send_value->as("file")) { // hash
1.74 paf 555: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.64 paf 556:
1.1 paf 557: // $.value
1.45 paf 558: if(Value* value=send_hash->get(value_name))
1.85 misha 559: vfile=value->as_vfile(String::L_AS_IS);
1.1 paf 560: else
1.120 moko 561: throw Exception(PARSER_RUNTIME, 0, "file part has no $value");
1.1 paf 562:
563: // $.format
1.45 paf 564: vformat=send_hash->get(format_name);
1.1 paf 565:
1.76 misha 566: // $.content-id
567: vcid=send_hash->get(cid_name);
568:
1.6 paf 569: // $.name
1.45 paf 570: if(Value* vfile_name=send_hash->get(name_name)) // $name specified
1.1 paf 571: file_name=&vfile_name->as_string();
1.28 paf 572: } else // must be VFile then
1.45 paf 573: vfile=send_value->as_vfile(String::L_AS_IS);
1.28 paf 574:
575: if(!file_name)
1.45 paf 576: file_name=&vfile->fields().get(name_name)->as_string();
1.28 paf 577:
1.95 misha 578: const char* file_name_cstr;
579: const char* quoted_file_name_cstr;
580: {
581: Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.97 misha 582: file_name_cstr=file_name->untaint_and_transcode_cstr(String::L_FILE_SPEC, &charsets);
1.95 misha 583: quoted_file_name_cstr=String(file_name_cstr).taint_cstr(String::L_MAIL_HEADER, 0, &charsets);
584: }
1.1 paf 585:
1.95 misha 586: // Content-Type: application/octet-stream
587: result
588: << HTTP_CONTENT_TYPE_CAPITALIZED ": "
589: << r.mime_type_of(file_name_cstr)
590: << "; name=\""
591: << quoted_file_name_cstr
592: << "\"\n";
1.66 paf 593:
1.95 misha 594: if(!info.had_content_disposition) // $.Content-Disposition wasn't specified by user
1.79 misha 595: result
1.91 misha 596: << CONTENT_DISPOSITION_CAPITALIZED ": "
1.79 misha 597: << ( vcid ? CONTENT_DISPOSITION_INLINE : CONTENT_DISPOSITION_ATTACHMENT )
598: << "; "
1.95 misha 599: << CONTENT_DISPOSITION_FILENAME_NAME"=\"" << quoted_file_name_cstr << "\"\n";
1.1 paf 600:
1.79 misha 601: if(vcid)
1.93 misha 602: result
603: << "Content-Id: <"
604: << vcid->as_string()
605: << ">\n"; // @todo: value must be escaped as %hh
1.79 misha 606:
1.45 paf 607: const String* type=vformat?&vformat->as_string():0;
1.93 misha 608: if(!type/*default*/ || *type=="base64") {
609: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": base64\n\n";
610: result << pa_base64_encode(vfile->value_ptr(), vfile->value_size());
1.75 paf 611: } else {
1.93 misha 612: if(*type=="uue") {
613: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": x-uuencode\n\n";
1.95 misha 614: result << pa_uuencode((const unsigned char*)vfile->value_ptr(), vfile->value_size(), file_name_cstr);
615: } else
1.82 misha 616: throw Exception(PARSER_RUNTIME,
617: type,
618: "unknown attachment encode format");
1.75 paf 619: }
1.95 misha 620:
1.1 paf 621: return result;
622: }
623:
1.45 paf 624: static const String& text_value_to_string(Request& r,
1.92 misha 625: PartType pt, Value* send_value,
626: Store_message_element_info& info) {
1.45 paf 627: String& result=*new String;
1.1 paf 628:
1.45 paf 629: Value* text_value;
1.81 misha 630: Value* content_transfer_encoding=0;
1.45 paf 631: if(HashStringValue* send_hash=send_value->get_hash()) {
1.1 paf 632: // $.USER-HEADERS
1.92 misha 633: info.content_type=0;
634: info.backward_compatibility=false; // reset
1.74 paf 635: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.1 paf 636: // $.value
1.45 paf 637: text_value=send_hash->get(value_name);
1.1 paf 638: if(!text_value)
1.78 misha 639: throw Exception(PARSER_RUNTIME,
1.45 paf 640: 0,
641: "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.81 misha 642: content_transfer_encoding=send_hash->get(content_transfer_encoding_name);
1.1 paf 643: } else
1.45 paf 644: text_value=send_value;
1.1 paf 645:
1.45 paf 646: if(!info.content_type) {
647: result
1.91 misha 648: << HTTP_CONTENT_TYPE_CAPITALIZED ": text/" << (pt==P_TEXT?"plain":"html")
1.45 paf 649: << "; charset=" << info.charsets.mail().NAME()
650: << "\n";
1.1 paf 651: }
1.81 misha 652: if(!content_transfer_encoding)
1.91 misha 653: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED << ": 8bit\n";
1.1 paf 654:
655: // header|body separator
656: result << "\n";
657:
658: // body
1.45 paf 659: const String* body;
1.122 moko 660: String::Language body_lang=String::L_AS_IS;
661:
1.1 paf 662: switch(pt) {
663: case P_TEXT:
1.92 misha 664: {
665: body=&text_value->as_string();
666: break;
667: }
1.122 moko 668: case P_HTML:
1.1 paf 669: {
1.122 moko 670: body_lang=String::Language(String::L_HTML | String::L_OPTIMIZE_BIT);
1.55 paf 671: if(text_value->get_junction())
1.41 paf 672: body=&r.process_to_string(*text_value);
1.92 misha 673: else
1.122 moko 674: throw Exception(PARSER_RUNTIME, 0, "html part value must be code");
1.1 paf 675: break;
676: }
1.53 paf 677: default:
1.122 moko 678: throw Exception(0, 0, "unhandled part type #%d", pt);
1.41 paf 679: }
1.122 moko 680:
1.41 paf 681: if(body) {
1.69 paf 682: Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.122 moko 683: const char* body_cstr=body->untaint_and_transcode_cstr(body_lang, &charsets);
1.95 misha 684: result.append_know_length(body_cstr, strlen(body_cstr), String::L_CLEAN);
1.1 paf 685: }
686:
687: return result;
1.111 moko 688: }
1.1 paf 689:
690: /// @todo files and messages in order (file, file2, ...)
1.45 paf 691: const String& VMail::message_hash_to_string(Request& r,
1.92 misha 692: HashStringValue* message_hash, int level,
693: const String* & from, bool extract_to, String* & to) {
1.34 paf 694:
1.1 paf 695: if(!message_hash)
1.78 misha 696: throw Exception(PARSER_RUNTIME,
1.45 paf 697: 0,
1.1 paf 698: "message must be hash");
699:
1.45 paf 700: String& result=*new String;
1.1 paf 701:
1.45 paf 702: if(Value* vrecodecharset_name=message_hash->get(charset_name))
1.121 moko 703: r.charsets.set_mail(pa_charsets.get(vrecodecharset_name->as_string()));
1.1 paf 704: else
1.45 paf 705: r.charsets.set_mail(r.charsets.source());
706: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 707:
1.45 paf 708: Store_message_element_info info(r.charsets,
1.60 paf 709: result, from, extract_to, to);
1.1 paf 710: {
1.45 paf 711: // for backward compatibilyty $.body+$.content-type ->
712: // $.text[$.value[] $.content-type[]]
713:
1.1 paf 714: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45 paf 715: info.parts[pt]=new ArrayValue(1);
716:
717: Value* body=message_hash->get("body");
718: if(body) {
719: message_hash->remove("body");
720: info.backward_compatibility=true;
721: }
1.74 paf 722: message_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.45 paf 723:
724: if(body) {
725: VHash& text_part=*new VHash();
726: HashStringValue& hash=text_part.hash();
727: hash.put(value_name, body);
728: if(info.content_type)
729: hash.put(content_type_name, info.content_type);
730:
731: *info.parts[P_TEXT]+=&text_part;
732: info.parts_count++;
733: }
734:
1.37 paf 735: if(!info.mime_version_specified)
736: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 737: }
738:
1.45 paf 739: int textCount=info.parts[P_TEXT]->count();
1.1 paf 740: if(textCount>1)
1.78 misha 741: throw Exception(PARSER_RUNTIME,
1.45 paf 742: 0,
1.1 paf 743: "multiple text parts not supported, use file part");
1.45 paf 744: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 745: if(htmlCount>1)
1.78 misha 746: throw Exception(PARSER_RUNTIME,
1.45 paf 747: 0,
1.1 paf 748: "multiple html parts not supported, use file part");
749:
750:
751: bool multipart=info.parts_count>1;
752: bool alternative=textCount && htmlCount;
753: // header
754: char *boundary=0;
755: if(multipart) {
1.118 moko 756: boundary=get_uuid_boundary();
1.76 misha 757:
758: bool is_inline = false;
759: {
760: ArrayValue& files=*info.parts[P_FILE];
761: for(size_t i=0; i<files.count(); i++) {
1.82 misha 762: HashStringValue* file;
763: if((file=files.get(i)->get_hash()) && file->get(cid_name)){
1.76 misha 764: is_inline = true;
765: break;
1.77 misha 766: }
1.76 misha 767: }
768: }
769:
1.91 misha 770: result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << ( is_inline ? HTTP_CONTENT_TYPE_MULTIPART_RELATED : HTTP_CONTENT_TYPE_MULTIPART_MIXED ) << ";";
1.76 misha 771:
1.1 paf 772: // multi-part
1.45 paf 773: result
1.76 misha 774: << " boundary=\"" << boundary << "\"\n"
1.45 paf 775: "\n"
776: "This is a multi-part message in MIME format.";
1.1 paf 777: }
778:
779: // alternative or not
780: {
781: if(alternative) {
1.45 paf 782: result << "\n\n--" << boundary << "\n" // intermediate boundary
1.91 misha 783: HTTP_CONTENT_TYPE_CAPITALIZED ": multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 784: }
785: for(int i=0; i<2; i++) {
786: PartType pt=i==0?P_TEXT:P_HTML;
1.45 paf 787: if(info.parts[pt]->count()) {
1.1 paf 788: if(alternative)
789: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
790: else if(boundary)
791: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 792: result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 793: }
794: }
795: if(alternative)
796: result << "\n\n--ALT" << boundary << "--\n";
797: }
798:
799: // files
800: {
1.45 paf 801: ArrayValue& files=*info.parts[P_FILE];
802: for(size_t i=0; i<files.count(); i++) {
1.1 paf 803: if(boundary)
804: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 805: result << file_value_to_string(r, files.get(i));
1.1 paf 806: }
807: }
808:
809: // messages
810: {
1.45 paf 811: ArrayValue& messages=*info.parts[P_MESSAGE];
812: for(size_t i=0; i<messages.count(); i++) {
1.1 paf 813: if(boundary)
814: result << "\n\n--" << boundary << "\n"; // intermediate boundary
815:
1.45 paf 816: const String* dummy_from;
817: String* dummy_to;
818: result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
1.60 paf 819: dummy_from, false, dummy_to);
1.1 paf 820: }
821: }
822:
823: // tailer
824: if(boundary)
825: result << "\n\n--" << boundary << "--\n"; // finish boundary
826:
827: // return
828: return result;
829: }
830:
831:
1.88 misha 832: Value* VMail::get_element(const String& aname) {
1.1 paf 833: // $fields
1.3 paf 834: #ifdef WITH_MAILRECEIVE
1.1 paf 835: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.48 paf 836: return &vreceived;
1.1 paf 837: #endif
838:
1.110 misha 839: // $method
1.88 misha 840: if(Value* result=VStateless_class::get_element(aname))
1.1 paf 841: return result;
842:
1.109 moko 843: return bark("%s field not found", &aname);
1.1 paf 844: }
E-mail: