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