Annotation of parser3/src/types/pa_vmail.C, revision 1.94
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.94 ! misha 9: static const char * const IDENT_VMAIL_C="$Date: 2009-09-26 12:32:23 $";
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;
607: Store_message_element_info info(r.charsets,
608: result, dummy_from, false, dummy_to);
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.45 paf 635: const char* file_name_cstr=file_name->cstr();
1.1 paf 636:
637: // content-type: application/octet-stream
1.91 misha 638: result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << r.mime_type_of(file_name_cstr) << "; name=\"" << file_name_cstr << "\"\n";
1.66 paf 639:
640: if(!info.had_content_disposition) {
1.92 misha 641: // $.Content-Disposition wasn't specified by user
1.79 misha 642: result
1.91 misha 643: << CONTENT_DISPOSITION_CAPITALIZED ": "
1.79 misha 644: << ( vcid ? CONTENT_DISPOSITION_INLINE : CONTENT_DISPOSITION_ATTACHMENT )
645: << "; "
646: << CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.66 paf 647: }
1.1 paf 648:
1.79 misha 649: if(vcid)
1.93 misha 650: result
651: << "Content-Id: <"
652: << vcid->as_string()
653: << ">\n"; // @todo: value must be escaped as %hh
1.79 misha 654:
1.45 paf 655: const String* type=vformat?&vformat->as_string():0;
1.93 misha 656: if(!type/*default*/ || *type=="base64") {
657: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": base64\n\n";
658: result << pa_base64_encode(vfile->value_ptr(), vfile->value_size());
1.75 paf 659: } else {
1.93 misha 660: if(*type=="uue") {
661: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED ": x-uuencode\n\n";
1.94 ! misha 662: result << pa_uuencode(*file_name, *vfile);
1.75 paf 663: } else {
1.82 misha 664: throw Exception(PARSER_RUNTIME,
665: type,
666: "unknown attachment encode format");
1.75 paf 667: }
668: }
1.1 paf 669:
670: return result;
671: }
672:
1.45 paf 673: static const String& text_value_to_string(Request& r,
1.92 misha 674: PartType pt, Value* send_value,
675: Store_message_element_info& info) {
1.45 paf 676: String& result=*new String;
1.1 paf 677:
1.45 paf 678: Value* text_value;
1.81 misha 679: Value* content_transfer_encoding=0;
1.45 paf 680: if(HashStringValue* send_hash=send_value->get_hash()) {
1.1 paf 681: // $.USER-HEADERS
1.92 misha 682: info.content_type=0;
683: info.backward_compatibility=false; // reset
1.74 paf 684: send_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.1 paf 685: // $.value
1.45 paf 686: text_value=send_hash->get(value_name);
1.1 paf 687: if(!text_value)
1.78 misha 688: throw Exception(PARSER_RUNTIME,
1.45 paf 689: 0,
690: "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.81 misha 691: content_transfer_encoding=send_hash->get(content_transfer_encoding_name);
1.1 paf 692: } else
1.45 paf 693: text_value=send_value;
1.1 paf 694:
1.45 paf 695: if(!info.content_type) {
696: result
1.91 misha 697: << HTTP_CONTENT_TYPE_CAPITALIZED ": text/" << (pt==P_TEXT?"plain":"html")
1.45 paf 698: << "; charset=" << info.charsets.mail().NAME()
699: << "\n";
1.1 paf 700: }
1.81 misha 701: if(!content_transfer_encoding)
1.91 misha 702: result << CONTENT_TRANSFER_ENCODING_CAPITALIZED << ": 8bit\n";
1.1 paf 703:
704: // header|body separator
705: result << "\n";
706:
707: // body
1.45 paf 708: const String* body;
1.1 paf 709: switch(pt) {
710: case P_TEXT:
1.92 misha 711: {
712: body=&text_value->as_string();
713: break;
714: }
1.1 paf 715: case P_HTML:
716: {
1.45 paf 717: Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
1.55 paf 718: if(text_value->get_junction())
1.41 paf 719: body=&r.process_to_string(*text_value);
1.92 misha 720: else
1.78 misha 721: throw Exception(PARSER_RUNTIME,
1.45 paf 722: 0,
1.1 paf 723: "html part value must be code");
724:
725: break;
726: }
1.53 paf 727: default:
728: throw Exception(0,
729: 0,
730: "unhandled part type #%d", pt);
1.41 paf 731: }
732: if(body) {
1.69 paf 733: Request_charsets charsets(r.charsets.source(), r.charsets.mail()/*uri!*/, r.charsets.mail());
1.87 misha 734: const char* body_cstr=body->untaint_cstr(String::L_AS_IS, 0, &charsets); // body
1.45 paf 735: String::C mail=Charset::transcode(
736: String::C(body_cstr, strlen(body_cstr)),
737: r.charsets.source(),
738: r.charsets.mail()/*always set - either mail.charset or $request:charset*/);
739: ///@todo
740: result.append_know_length(mail.str, mail.length, String::L_CLEAN);
1.1 paf 741: }
742:
743: return result;
744: };
745:
746: /// @todo files and messages in order (file, file2, ...)
1.45 paf 747: const String& VMail::message_hash_to_string(Request& r,
1.92 misha 748: HashStringValue* message_hash, int level,
749: const String* & from, bool extract_to, String* & to) {
1.34 paf 750:
1.1 paf 751: if(!message_hash)
1.78 misha 752: throw Exception(PARSER_RUNTIME,
1.45 paf 753: 0,
1.1 paf 754: "message must be hash");
755:
1.45 paf 756: String& result=*new String;
1.1 paf 757:
1.45 paf 758: if(Value* vrecodecharset_name=message_hash->get(charset_name))
759: r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()
760: .change_case(r.charsets.source(), String::CC_UPPER)));
1.1 paf 761: else
1.45 paf 762: r.charsets.set_mail(r.charsets.source());
763: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 764:
1.45 paf 765: Store_message_element_info info(r.charsets,
1.60 paf 766: result, from, extract_to, to);
1.1 paf 767: {
1.45 paf 768: // for backward compatibilyty $.body+$.content-type ->
769: // $.text[$.value[] $.content-type[]]
770:
1.1 paf 771: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.45 paf 772: info.parts[pt]=new ArrayValue(1);
773:
774: Value* body=message_hash->get("body");
775: if(body) {
776: message_hash->remove("body");
777: info.backward_compatibility=true;
778: }
1.74 paf 779: message_hash->for_each<Store_message_element_info*>(store_message_element, &info);
1.45 paf 780:
781: if(body) {
782: VHash& text_part=*new VHash();
783: HashStringValue& hash=text_part.hash();
784: hash.put(value_name, body);
785: if(info.content_type)
786: hash.put(content_type_name, info.content_type);
787:
788: *info.parts[P_TEXT]+=&text_part;
789: info.parts_count++;
790: }
791:
1.10 paf 792: if(!info.errors_to)
1.91 misha 793: result << "Errors-To: postmaster\n"; // errors-to: default
1.37 paf 794: if(!info.mime_version_specified)
795: result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 796: }
797:
1.45 paf 798: int textCount=info.parts[P_TEXT]->count();
1.1 paf 799: if(textCount>1)
1.78 misha 800: throw Exception(PARSER_RUNTIME,
1.45 paf 801: 0,
1.1 paf 802: "multiple text parts not supported, use file part");
1.45 paf 803: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 804: if(htmlCount>1)
1.78 misha 805: throw Exception(PARSER_RUNTIME,
1.45 paf 806: 0,
1.1 paf 807: "multiple html parts not supported, use file part");
808:
809:
810: bool multipart=info.parts_count>1;
811: bool alternative=textCount && htmlCount;
812: // header
813: char *boundary=0;
814: if(multipart) {
1.45 paf 815: boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1 paf 816: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.76 misha 817:
818: bool is_inline = false;
819: {
820: ArrayValue& files=*info.parts[P_FILE];
821: for(size_t i=0; i<files.count(); i++) {
1.82 misha 822: HashStringValue* file;
823: if((file=files.get(i)->get_hash()) && file->get(cid_name)){
1.76 misha 824: is_inline = true;
825: break;
1.77 misha 826: }
1.76 misha 827: }
828: }
829:
1.91 misha 830: result << HTTP_CONTENT_TYPE_CAPITALIZED ": " << ( is_inline ? HTTP_CONTENT_TYPE_MULTIPART_RELATED : HTTP_CONTENT_TYPE_MULTIPART_MIXED ) << ";";
1.76 misha 831:
1.1 paf 832: // multi-part
1.45 paf 833: result
1.76 misha 834: << " boundary=\"" << boundary << "\"\n"
1.45 paf 835: "\n"
836: "This is a multi-part message in MIME format.";
1.1 paf 837: }
838:
839: // alternative or not
840: {
841: if(alternative) {
1.45 paf 842: result << "\n\n--" << boundary << "\n" // intermediate boundary
1.91 misha 843: HTTP_CONTENT_TYPE_CAPITALIZED ": multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 844: }
845: for(int i=0; i<2; i++) {
846: PartType pt=i==0?P_TEXT:P_HTML;
1.45 paf 847: if(info.parts[pt]->count()) {
1.1 paf 848: if(alternative)
849: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
850: else if(boundary)
851: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 852: result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 853: }
854: }
855: if(alternative)
856: result << "\n\n--ALT" << boundary << "--\n";
857: }
858:
859: // files
860: {
1.45 paf 861: ArrayValue& files=*info.parts[P_FILE];
862: for(size_t i=0; i<files.count(); i++) {
1.1 paf 863: if(boundary)
864: result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.45 paf 865: result << file_value_to_string(r, files.get(i));
1.1 paf 866: }
867: }
868:
869: // messages
870: {
1.45 paf 871: ArrayValue& messages=*info.parts[P_MESSAGE];
872: for(size_t i=0; i<messages.count(); i++) {
1.1 paf 873: if(boundary)
874: result << "\n\n--" << boundary << "\n"; // intermediate boundary
875:
1.45 paf 876: const String* dummy_from;
877: String* dummy_to;
878: result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
1.60 paf 879: dummy_from, false, dummy_to);
1.1 paf 880: }
881: }
882:
883: // tailer
884: if(boundary)
885: result << "\n\n--" << boundary << "--\n"; // finish boundary
886:
887: // return
888: return result;
889: }
890:
891:
1.88 misha 892: Value* VMail::get_element(const String& aname) {
1.1 paf 893: // $fields
1.3 paf 894: #ifdef WITH_MAILRECEIVE
1.1 paf 895: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.48 paf 896: return &vreceived;
1.1 paf 897: #endif
898:
899: // $CLASS,$method
1.88 misha 900: if(Value* result=VStateless_class::get_element(aname))
1.1 paf 901: return result;
902:
903: return 0;
904: }
905:
1.3 paf 906: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.48 paf 907: # define GNOME_LIBS "../../../../win32/gnome"
1.1 paf 908: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
909: # ifdef _DEBUG
910: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
911: # else
912: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
913: # endif
914: #endif
E-mail: