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