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