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