Annotation of parser3/src/types/pa_vmail.C, revision 1.125
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.125 ! moko 21: volatile const char * IDENT_PA_VMAIL_C="$Id: pa_vmail.C,v 1.124 2017/02/07 22:00:49 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:
71: const int MAX_CHARS_IN_HEADER_LINE=500;
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.17 paf 383: throw Exception(exception_type,
1.9 paf 384: &result,
1.32 paf 385: "email contains bad characters (specials)");
1.9 paf 386: if(string_contains_char_which(email, (string_contains_char_which_check)isspace))
1.17 paf 387: throw Exception(exception_type,
1.9 paf 388: &result,
1.32 paf 389: "email contains bad characters (whitespace)");
1.9 paf 390: if(string_contains_char_which(email, (string_contains_char_which_check)iscntrl))
1.17 paf 391: throw Exception(exception_type,
1.9 paf 392: &result,
1.32 paf 393: "email contains bad characters (control)");
1.16 paf 394: if(result.is_empty())
1.17 paf 395: throw Exception(exception_type,
1.45 paf 396: 0,
1.16 paf 397: "email is empty");
1.39 paf 398: }
399:
1.45 paf 400: static const String& extractEmails(const String& string) {
401: char *emails=string.cstrm();
402: String& result=*new String;
1.39 paf 403: while(char *email=lsplit(&emails, ',')) {
404: rsplit(email, '>');
405: if(char *in_brackets=lsplit(email, '<'))
406: email=in_brackets;
407: if(!result.is_empty())
408: result<<",";
1.45 paf 409: extractEmail(result, email);
1.39 paf 410: }
1.9 paf 411:
412: return result;
413: }
1.45 paf 414:
415: #ifndef DOXYGEN
416: struct Store_message_element_info {
417: Request_charsets& charsets;
418: String& header;
419: const String* & from;
1.60 paf 420: bool extract_to; String* & to;
1.45 paf 421: bool mime_version_specified;
422: ArrayValue* parts[P_TYPES_COUNT];
423: int parts_count;
424: bool backward_compatibility;
425: Value* content_type;
1.66 paf 426: bool had_content_disposition;
1.45 paf 427:
428: Store_message_element_info(
429: Request_charsets& acharsets,
430: String& aheader,
431: const String* & afrom,
1.60 paf 432: bool aextract_to, String* & ato
1.45 paf 433: ):
434:
435: charsets(acharsets),
436: header(aheader),
437: from(afrom),
1.60 paf 438: extract_to(aextract_to), to(ato),
1.45 paf 439: mime_version_specified(false),
440: parts_count(0),
1.66 paf 441: backward_compatibility(false), content_type(0),
442: had_content_disposition(false){
1.45 paf 443: }
444: };
445: #endif
1.99 moko 446:
1.45 paf 447: static void store_message_element(HashStringValue::key_type raw_element_name,
1.92 misha 448: HashStringValue::value_type element_value,
449: Store_message_element_info *info) {
1.45 paf 450: const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
451: info->charsets.source(), String::CC_LOWER);
1.1 paf 452:
453: // exclude internals
1.52 paf 454: if(low_element_name==MAIL_OPTIONS_NAME
455: || low_element_name==CHARSET_NAME
1.5 paf 456: || low_element_name==VALUE_NAME
457: || low_element_name==RAW_NAME
1.64 paf 458: || low_element_name==FORMAT_NAME
1.76 misha 459: || low_element_name==NAME_NAME
1.94 misha 460: || low_element_name==CID_NAME
461: || low_element_name==MAIL_DEBUG_NAME)
1.1 paf 462: return;
463:
464: // grep parts
465: for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.45 paf 466: if(low_element_name.starts_with(part_name_begins[pt])) {
1.29 paf 467: // check that $.message# '#' is digit
1.45 paf 468: size_t start_len=strlen(part_name_begins[pt]);
469: if(low_element_name.length()>start_len) {
470: const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.63 paf 471: if(!isdigit((unsigned char)*at_num))
1.29 paf 472: continue;
473: }
1.45 paf 474: *info->parts[pt]+=element_value;
475: info->parts_count++;
1.1 paf 476: return;
477: }
478: }
479:
1.10 paf 480: // fetch some special headers
1.45 paf 481: if(low_element_name=="from")
482: info->from=&extractEmails(element_value->as_string());
1.90 misha 483: if(low_element_name==CONTENT_DISPOSITION)
1.66 paf 484: info->had_content_disposition=true;
1.60 paf 485: if(info->extract_to) { // defined only when SMTP used, see mail.C [collecting info for RCPT to-s]
1.39 paf 486: bool is_to=low_element_name=="to" ;
487: bool is_cc=low_element_name=="cc" ;
488: bool is_bcc=low_element_name=="bcc" ;
489: if(is_to||is_cc||is_bcc) {
1.45 paf 490: if(!info->to)
491: info->to=new String;
1.39 paf 492: else
1.45 paf 493: *info->to << ",";
494: *info->to << extractEmails(element_value->as_string());
1.39 paf 495: }
496:
497: if(is_bcc) // blinding it
1.45 paf 498: return;
1.39 paf 499: }
1.37 paf 500: if(low_element_name=="mime-version")
1.45 paf 501: info->mime_version_specified=true;
1.1 paf 502:
1.45 paf 503: // has content type?
504: if(low_element_name==CONTENT_TYPE_NAME) {
505: info->content_type=element_value;
506: if(info->backward_compatibility)
507: return;
1.39 paf 508: }
1.1 paf 509:
1.45 paf 510: // preparing header line
1.123 moko 511: const String& source_line=attributed_meaning_to_string(*element_value, String::L_AS_IS);
512:
1.66 paf 513: if(source_line.is_empty())
514: return; // we don't need empty headers here [used in clearing content-disposition]
515:
1.45 paf 516: const char* source_line_cstr=source_line.cstr();
517: String::C mail=Charset::transcode(
518: String::C(source_line_cstr, source_line.length()),
519: info->charsets.source(),
520: info->charsets.mail());
1.92 misha 521:
1.45 paf 522: String& mail_line=*new String;
1.73 paf 523: if(low_element_name=="to"
524: || low_element_name=="cc"
525: || low_element_name=="bcc")
526: {
527: // never wrap address lines, mailer can not handle wrapped properly
528: mail_line.append_strdup(mail.str, mail.length, String::L_MAIL_HEADER);
529: } else {
530: while(mail.length) {
531: bool too_long=mail.length>MAX_CHARS_IN_HEADER_LINE;
532: size_t length=too_long
533: ? MAX_CHARS_IN_HEADER_LINE
534: : mail.length;
535:
536: mail_line.append_strdup(mail.str, length, String::L_MAIL_HEADER);
537: mail.length-=length;
538:
539: if(too_long)
540: mail_line << "\n "; // break header and continue it on next line
541: }
542: }
1.45 paf 543:
544: // append header line
545: info->header
1.91 misha 546: << capitalize(raw_element_name.cstr())
1.96 misha 547: << ": " << mail_line.untaint_cstr(String::L_AS_IS, 0, &info->charsets)
1.45 paf 548: << "\n";
549: }
550:
551: static const String& file_value_to_string(Request& r, Value* send_value) {
1.64 paf 552: String& result=*new String;
553:
1.45 paf 554: VFile* vfile;
555: const String* file_name=0;
556: Value* vformat=0;
1.76 misha 557: Value* vcid=0;
1.66 paf 558: const String* dummy_from;
559: String* dummy_to;
1.95 misha 560: Store_message_element_info info(r.charsets, result, dummy_from, false, dummy_to);
561:
1.120 moko 562: HashStringValue *send_hash=send_value->get_hash();
563: if(send_hash && !send_value->as("file")) { // hash
1.74 paf 564: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.64 paf 565:
1.1 paf 566: // $.value
1.45 paf 567: if(Value* value=send_hash->get(value_name))
1.85 misha 568: vfile=value->as_vfile(String::L_AS_IS);
1.1 paf 569: else
1.120 moko 570: throw Exception(PARSER_RUNTIME, 0, "file part has no $value");
1.1 paf 571:
572: // $.format
1.45 paf 573: vformat=send_hash->get(format_name);
1.1 paf 574:
1.76 misha 575: // $.content-id
576: vcid=send_hash->get(cid_name);
577:
1.6 paf 578: // $.name
1.45 paf 579: if(Value* vfile_name=send_hash->get(name_name)) // $name specified
1.1 paf 580: file_name=&vfile_name->as_string();
1.28 paf 581: } else // must be VFile then
1.45 paf 582: vfile=send_value->as_vfile(String::L_AS_IS);
1.28 paf 583:
584: if(!file_name)
1.45 paf 585: file_name=&vfile->fields().get(name_name)->as_string();
1.28 paf 586:
1.95 misha 587: const char* file_name_cstr;
588: const char* quoted_file_name_cstr;
589: {
590: Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.97 misha 591: file_name_cstr=file_name->untaint_and_transcode_cstr(String::L_FILE_SPEC, &charsets);
1.95 misha 592: quoted_file_name_cstr=String(file_name_cstr).taint_cstr(String::L_MAIL_HEADER, 0, &charsets);
593: }
1.1 paf 594:
1.95 misha 595: // Content-Type: application/octet-stream
596: result
597: << HTTP_CONTENT_TYPE_CAPITALIZED ": "
598: << r.mime_type_of(file_name_cstr)
599: << "; name=\""
600: << quoted_file_name_cstr
601: << "\"\n";
1.66 paf 602:
1.95 misha 603: if(!info.had_content_disposition) // $.Content-Disposition wasn't specified by user
1.79 misha 604: result
1.91 misha 605: << CONTENT_DISPOSITION_CAPITALIZED ": "
1.79 misha 606: << ( vcid ? CONTENT_DISPOSITION_INLINE : CONTENT_DISPOSITION_ATTACHMENT )
607: << "; "
1.95 misha 608: << CONTENT_DISPOSITION_FILENAME_NAME"=\"" << quoted_file_name_cstr << "\"\n";
1.1 paf 609:
1.79 misha 610: if(vcid)
1.93 misha 611: result
612: << "Content-Id: <"
613: << vcid->as_string()
614: << ">\n"; // @todo: value must be escaped as %hh
1.79 misha 615:
1.45 paf 616: const String* type=vformat?&vformat->as_string():0;
1.93 misha 617: if(!type/*default*/ || *type=="base64") {
618: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": base64\n\n";
619: result << pa_base64_encode(vfile->value_ptr(), vfile->value_size());
1.75 paf 620: } else {
1.93 misha 621: if(*type=="uue") {
622: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": x-uuencode\n\n";
1.95 misha 623: result << pa_uuencode((const unsigned char*)vfile->value_ptr(), vfile->value_size(), file_name_cstr);
624: } else
1.82 misha 625: throw Exception(PARSER_RUNTIME,
626: type,
627: "unknown attachment encode format");
1.75 paf 628: }
1.95 misha 629:
1.1 paf 630: return result;
631: }
632:
1.45 paf 633: static const String& text_value_to_string(Request& r,
1.92 misha 634: PartType pt, Value* send_value,
635: Store_message_element_info& info) {
1.45 paf 636: String& result=*new String;
1.1 paf 637:
1.45 paf 638: Value* text_value;
1.81 misha 639: Value* content_transfer_encoding=0;
1.45 paf 640: if(HashStringValue* send_hash=send_value->get_hash()) {
1.1 paf 641: // $.USER-HEADERS
1.92 misha 642: info.content_type=0;
643: info.backward_compatibility=false; // reset
1.74 paf 644: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.1 paf 645: // $.value
1.45 paf 646: text_value=send_hash->get(value_name);
1.1 paf 647: if(!text_value)
1.78 misha 648: throw Exception(PARSER_RUNTIME,
1.45 paf 649: 0,
650: "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.81 misha 651: content_transfer_encoding=send_hash->get(content_transfer_encoding_name);
1.1 paf 652: } else
1.45 paf 653: text_value=send_value;
1.1 paf 654:
1.45 paf 655: if(!info.content_type) {
656: result
1.91 misha 657: << HTTP_CONTENT_TYPE_CAPITALIZED ": text/" << (pt==P_TEXT?"plain":"html")
1.45 paf 658: << "; charset=" << info.charsets.mail().NAME()
659: << "\n";
1.1 paf 660: }
1.81 misha 661: if(!content_transfer_encoding)
1.91 misha 662: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED << ": 8bit\n";
1.1 paf 663:
664: // header|body separator
665: result << "\n";
666:
667: // body
1.45 paf 668: const String* body;
1.122 moko 669: String::Language body_lang=String::L_AS_IS;
670:
1.1 paf 671: switch(pt) {
672: case P_TEXT:
1.92 misha 673: {
674: body=&text_value->as_string();
675: break;
676: }
1.122 moko 677: case P_HTML:
1.1 paf 678: {
1.122 moko 679: body_lang=String::Language(String::L_HTML | String::L_OPTIMIZE_BIT);
1.55 paf 680: if(text_value->get_junction())
1.41 paf 681: body=&r.process_to_string(*text_value);
1.92 misha 682: else
1.122 moko 683: throw Exception(PARSER_RUNTIME, 0, "html part value must be code");
1.1 paf 684: break;
685: }
1.53 paf 686: default:
1.122 moko 687: throw Exception(0, 0, "unhandled part type #%d", pt);
1.41 paf 688: }
1.122 moko 689:
1.41 paf 690: if(body) {
1.69 paf 691: Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.122 moko 692: const char* body_cstr=body->untaint_and_transcode_cstr(body_lang, &charsets);
1.95 misha 693: result.append_know_length(body_cstr, strlen(body_cstr), String::L_CLEAN);
1.1 paf 694: }
695:
696: return result;
1.111 moko 697: }
1.1 paf 698:
699: /// @todo files and messages in order (file, file2, ...)
1.45 paf 700: const String& VMail::message_hash_to_string(Request& r,
1.92 misha 701: HashStringValue* message_hash, int level,
702: const String* & from, bool extract_to, String* & to) {
1.34 paf 703:
1.1 paf 704: if(!message_hash)
1.78 misha 705: throw Exception(PARSER_RUNTIME,
1.45 paf 706: 0,
1.1 paf 707: "message must be hash");
708:
1.45 paf 709: String& result=*new String;
1.1 paf 710:
1.45 paf 711: if(Value* vrecodecharset_name=message_hash->get(charset_name))
1.121 moko 712: r.charsets.set_mail(pa_charsets.get(vrecodecharset_name->as_string()));
1.1 paf 713: else
1.45 paf 714: r.charsets.set_mail(r.charsets.source());
715: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 716:
1.45 paf 717: Store_message_element_info info(r.charsets,
1.60 paf 718: result, from, extract_to, to);
1.1 paf 719: {
1.45 paf 720: // for backward compatibilyty $.body+$.content-type ->
721: // $.text[$.value[] $.content-type[]]
722:
1.1 paf 723: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45 paf 724: info.parts[pt]=new ArrayValue(1);
725:
726: Value* body=message_hash->get("body");
727: if(body) {
728: message_hash->remove("body");
729: info.backward_compatibility=true;
730: }
1.74 paf 731: message_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.45 paf 732:
733: if(body) {
734: VHash& text_part=*new VHash();
735: HashStringValue& hash=text_part.hash();
736: hash.put(value_name, body);
737: if(info.content_type)
738: hash.put(content_type_name, info.content_type);
739:
740: *info.parts[P_TEXT]+=&text_part;
741: info.parts_count++;
742: }
743:
1.37 paf 744: if(!info.mime_version_specified)
745: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 746: }
747:
1.45 paf 748: int textCount=info.parts[P_TEXT]->count();
1.1 paf 749: if(textCount>1)
1.78 misha 750: throw Exception(PARSER_RUNTIME,
1.45 paf 751: 0,
1.1 paf 752: "multiple text parts not supported, use file part");
1.45 paf 753: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 754: if(htmlCount>1)
1.78 misha 755: throw Exception(PARSER_RUNTIME,
1.45 paf 756: 0,
1.1 paf 757: "multiple html parts not supported, use file part");
758:
759:
760: bool multipart=info.parts_count>1;
761: bool alternative=textCount && htmlCount;
762: // header
763: char *boundary=0;
764: if(multipart) {
1.118 moko 765: boundary=get_uuid_boundary();
1.76 misha 766:
767: bool is_inline = false;
768: {
769: ArrayValue& files=*info.parts[P_FILE];
770: for(size_t i=0; i<files.count(); i++) {
1.82 misha 771: HashStringValue* file;
772: if((file=files.get(i)->get_hash()) && file->get(cid_name)){
1.76 misha 773: is_inline = true;
774: break;
1.77 misha 775: }
1.76 misha 776: }
777: }
778:
1.91 misha 779: result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << ( is_inline ? HTTP_CONTENT_TYPE_MULTIPART_RELATED : HTTP_CONTENT_TYPE_MULTIPART_MIXED ) << ";";
1.76 misha 780:
1.1 paf 781: // multi-part
1.45 paf 782: result
1.76 misha 783: << " boundary=\"" << boundary << "\"\n"
1.45 paf 784: "\n"
785: "This is a multi-part message in MIME format.";
1.1 paf 786: }
787:
788: // alternative or not
789: {
790: if(alternative) {
1.45 paf 791: result << "\n\n--" << boundary << "\n" // intermediate boundary
1.91 misha 792: HTTP_CONTENT_TYPE_CAPITALIZED ": multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 793: }
794: for(int i=0; i<2; i++) {
795: PartType pt=i==0?P_TEXT:P_HTML;
1.45 paf 796: if(info.parts[pt]->count()) {
1.1 paf 797: if(alternative)
798: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
799: else if(boundary)
800: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 801: result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 802: }
803: }
804: if(alternative)
805: result << "\n\n--ALT" << boundary << "--\n";
806: }
807:
808: // files
809: {
1.45 paf 810: ArrayValue& files=*info.parts[P_FILE];
811: for(size_t i=0; i<files.count(); i++) {
1.1 paf 812: if(boundary)
813: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 814: result << file_value_to_string(r, files.get(i));
1.1 paf 815: }
816: }
817:
818: // messages
819: {
1.45 paf 820: ArrayValue& messages=*info.parts[P_MESSAGE];
821: for(size_t i=0; i<messages.count(); i++) {
1.1 paf 822: if(boundary)
823: result << "\n\n--" << boundary << "\n"; // intermediate boundary
824:
1.45 paf 825: const String* dummy_from;
826: String* dummy_to;
827: result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
1.60 paf 828: dummy_from, false, dummy_to);
1.1 paf 829: }
830: }
831:
832: // tailer
833: if(boundary)
834: result << "\n\n--" << boundary << "--\n"; // finish boundary
835:
836: // return
837: return result;
838: }
839:
840:
1.88 misha 841: Value* VMail::get_element(const String& aname) {
1.1 paf 842: // $fields
1.3 paf 843: #ifdef WITH_MAILRECEIVE
1.1 paf 844: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.48 paf 845: return &vreceived;
1.1 paf 846: #endif
847:
1.110 misha 848: // $method
1.88 misha 849: if(Value* result=VStateless_class::get_element(aname))
1.1 paf 850: return result;
851:
1.109 moko 852: return bark("%s field not found", &aname);
1.1 paf 853: }
E-mail: