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