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