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