Annotation of parser3/src/types/pa_vmail.C, revision 1.40.2.12.2.15
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.1 (paf 9:): static const char* IDENT_VMAIL_C="$Date: 2003/03/27 14:51:29 $";
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.12.2.7 (paf 45:: static const char* const part_name_begins[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(FORMAT_NAME);
55:): static const String charset_name(CHARSET_NAME);
1.40.2.5 paf 56:
1.1 paf 57: // VMail
58:
1.40.2.12.2.8 (paf 59:: extern Methoded* mail_base_class;
1.1 paf 60:
1.40.2.12.2.1 (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.40.2.12.2.1 (paf 65:: static const String& maybeUpperCaseCharset& source_charset,
66:: const String& src, bool toUpperCase) {
1.40.2.12.2.4 (paf 67:: return toUpperCase?src->change_case(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.2.4 (paf 76:: Charset::transcode(
1.40.2.12 paf 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,
1.40.2.12.2.8 (paf 87:: const char* name, Value* value,
1.40.2.12 paf 88: bool nameToUpperCase=false) {
89:
1.1 paf 90: if(name && value) {
91: received.put(
1.40.2.12.2.4 (paf 92:: maybeUpperCase(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.40.2.12.2.4 (paf 105:: // UTF8toSource(value, value_size, value_dest_body, value_dest_content_length);
1.21 paf 106: value_dest_body=value;
107: value_dest_content_length=value_size;
1.1 paf 108:
1.40.2.12.2.4 (paf 109:: putReceived(source_charset,
1.40.2.12 paf 110: received,
111: name,
1.40.2.12.2.8 (paf 112:: Value*(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.2.8 (paf 122:: Value*(new VDate(value))
1.40.2.12 paf 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.2.4 (paf 136:: putReceived(*info.*info.source_charset,
1.40.2.12 paf 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.12.2.7 (paf 174:: const char* partNameStart=part_name_begins[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.2.8 (paf 182:: VHash* vpartX(new VHash); putReceived(*info.body, partName, vpartX);
1.40.2.12.2.9 (paf 183:: HashStringValue& partX=vpartX->hash();
1.1 paf 184: {
185: // $.raw[
1.40.2.12.2.8 (paf 186:: VHash* vraw(new VHash); putReceived(partX, RAW_NAME, vraw);
1.40.2.12 paf 187: MimeHeaderField2received_info hfr_info={
1.40.2.12.2.9 (paf 188:: &&source_charset, &vraw->hash()};
1.40.2.12 paf 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.2.8 (paf 194:: VHash* vcontent_type(new VHash); putReceived(partX, "content-type", vcontent_type);
1.40.2.12.2.9 (paf 195:: HashStringValue& content_type=vcontent_type->hash();
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.2.4 (paf 202:: putReceived(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.2.4 (paf 207:: putReceived(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.2.4 (paf 214:: putReceived(source_charset, partX, "description", part->description);
1.1 paf 215: // $.content-id
1.40.2.12.2.4 (paf 216:: putReceived(source_charset, partX, "content-id", part->content_id);
1.1 paf 217: // $.content-md5
1.40.2.12.2.4 (paf 218:: putReceived(source_charset, partX, "content-md5", part->content_md5);
1.1 paf 219: // $.content-location
1.40.2.12.2.4 (paf 220:: putReceived(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.2.8 (paf 245:: VFile* vfile(new VFile);
1.40.2.12 paf 246: char *pooled_buf=pool.copy(local_buf, buf_len);
1.40.2.12.2.8 (paf 247:: const VString& vcontent_type(content_filename?
1.40.2.12 paf 248: new VString(info.r->mime_type_of(content_filename)):0);
1.40.2.12.2.4 (paf 249:: vfile->set(true/*tainted*/, pooled_buf, buf_len, content_filename, vcontent_type);
250:: putReceived(partX, VALUE_NAME, vfile);
1.1 paf 251: } else {
252: // P_TEXT, P_HTML
1.40.2.12.2.4 (paf 253:: putReceived(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.2.8 (paf 288:: VHash* vraw(new VHash); putReceived(received, RAW_NAME, vraw);
1.40.2.12 paf 289: MimeHeaderField2received_info hfr_info={
1.40.2.12.2.9 (paf 290:: &&source_charset, &vraw->hash()};
1.40.2.12 paf 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.2.4 (paf 300:: putReceived(source_charset, received, "from", messageHeader->from);
1.1 paf 301: // .reply-to
1.40.2.12.2.4 (paf 302:: putReceived(source_charset, received, "reply-to", messageHeader->reply_to);
1.1 paf 303: // .to
304: // todo: messageHeader->recipients
305: // .subject
1.40.2.12.2.4 (paf 306:: putReceived(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.2.4 (paf 312:: putReceived(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.2.4 (paf 318:: putReceived(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.2.9 (paf 353:: parse(r, stream, vreceived->hash());
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(String& result, char *email) {
1.32 paf 393: email=trimBoth(email);
1.40.2.12.2.1 (paf 394:): result.append_help_length(email, 0, String::L_TAINTED);
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.12.2.1 (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.12.2.1 (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.12.2.1 (paf 429:): &result,
1.32 paf 430: "email contains bad characters (control)");
1.40.2.12.2.1 (paf 431:): if(result.is_empty())
1.17 paf 432: throw Exception(exception_type,
1.40.2.12.2.1 (paf 433:): 0,
1.16 paf 434: "email is empty");
1.39 paf 435: }
436:
1.40.2.12.2.1 (paf 437:): /*nonstatic*/const String& extractEmails(const String& string) { // used in classes/mail.C, which supported for backward compatibility
438:): char *emails=string.cstrm();
439:): 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.12.2.1 (paf 444:): if(!result.is_empty())
445:): result<<",";
446:): extractEmail(result, email);
1.39 paf 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.10 paf 454: Request_charsets& charsets;
1.40.2.12.2.1 (paf 455:): String& header;
456:): const String* & from;
457:): bool extract_to; String* & to;
458:): const String* errors_to;
1.40.2.5 paf 459: bool mime_version_specified;
1.40.2.12.2.8 (paf 460:: ArrayValue* parts[P_TYPES_COUNT];
1.40.2.5 paf 461: int parts_count;
462: bool has_content_type;
1.40.2.10 paf 463:
464: Store_message_element_info(
465: Request_charsets& acharsets,
1.40.2.12.2.1 (paf 466:): String& aheader,
467:): const String* & afrom,
468:: bool aextract_to, String* & ato
1.40.2.10 paf 469: ):
1.40.2.12.2.8 (paf 470::
1.40.2.10 paf 471: charsets(acharsets),
472: header(aheader),
473: from(afrom),
474: extract_to(aextract_to), to(ato),
1.40.2.12.2.1 (paf 475:): errors_to(0),
1.40.2.10 paf 476: mime_version_specified(false),
477: parts_count(0),
478: has_content_type(false) {
479: }
1.40.2.5 paf 480: };
481: #endif
482: static void store_message_element(
1.40.2.12.2.1 (paf 483:): HashStringValue::key_type raw_element_name,
484:): HashStringValue::value_type element_value,
485:): Store_message_element_info *info) {
486:): const String& low_element_name=String(raw_element_name, String::L_TAINTED).change_case(
1.40.2.10 paf 487: info->charsets.source(), String::CC_LOWER);
1.1 paf 488:
489: // exclude internals
1.40.2.12.2.1 (paf 490:): if(low_element_name==CHARSET_NAME
491:): || low_element_name==VALUE_NAME
492:): || low_element_name==RAW_NAME
493:): || low_element_name=="date")
1.1 paf 494: return;
495:
496: // grep parts
497: for(int pt=0; pt<P_TYPES_COUNT; pt++) {
1.40.2.12.2.1 (paf 498:): if(low_element_name.starts_with(part_name_begins[pt])) {
1.29 paf 499: // check that $.message# '#' is digit
1.40.2.12.2.7 (paf 500:: size_t start_len=strlen(part_name_begins[pt]);
1.40.2.12.2.1 (paf 501:): if(low_element_name.length()>start_len) {
502:): const char* at_num=low_element_name.mid(start_len, start_len+1).cstr();
1.29 paf 503: if(!isdigit(*at_num))
504: continue;
505: }
1.40.2.5 paf 506: *info->parts[pt]+=element_value;
507: info->parts_count++;
1.1 paf 508: return;
509: }
510: }
511:
1.39 paf 512: bool skip=false;
513:
1.10 paf 514: // fetch some special headers
1.40.2.12.2.1 (paf 515:): if(low_element_name=="from")
516:): info->from=&extractEmails(element_value->as_string());
1.40.2.10 paf 517: if(info->extract_to) { // defined only on WIN32, see mail.C [collecting info for RCPT to-s]
1.40.2.12.2.1 (paf 518:): bool is_to=low_element_name=="to" ;
519:): bool is_cc=low_element_name=="cc" ;
520:): bool is_bcc=low_element_name=="bcc" ;
1.39 paf 521: if(is_to||is_cc||is_bcc) {
1.40.2.5 paf 522: if(!info->to)
1.40.2.12.2.1 (paf 523:): info->to=new String;
1.39 paf 524: else
1.40.2.5 paf 525: *info->to << ",";
1.40.2.12.2.1 (paf 526:): *info->to << extractEmails(element_value->as_string());
1.39 paf 527: }
528:
529: if(is_bcc) // blinding it
530: skip=true;
531: }
1.40.2.12.2.1 (paf 532:): if(low_element_name=="errors-to")
533:): info->errors_to=&extractEmails(element_value->as_string());
534:): if(low_element_name=="mime-version")
1.40.2.5 paf 535: info->mime_version_specified=true;
1.1 paf 536:
1.39 paf 537: if(!skip) {
1.40.2.12.2.1 (paf 538:): const String& source_line=attributed_meaning_to_string(*element_value);
539:): const char* source_line_cstr=source_line.cstr();
540:): String::C mail=Charset::transcode(
541:): info->charsets.source(), source_line_cstr, source_line.length(),
542:): info->charsets.mail());
543:): String& mail_line=*new String;
544:): ///@todo can convert to append_help_length if transcode would return zero-terminated
545:): mail_line.append_know_length(mail.str, mail.size, String::L_MAIL_HEADER);
1.40.2.1 paf 546:
1.40.2.5 paf 547: // append header line
1.40.2.12.2.1 (paf 548:): info->header
1.40.2.5 paf 549: << raw_element_name
1.40.2.12.2.1 (paf 550:): << ":" << mail_line.cstr(String::L_UNSPECIFIED, 0, &info->charsets)
1.40.2.5 paf 551: << "\n";
1.39 paf 552: }
1.1 paf 553:
554: // has content type?
1.40.2.12.2.1 (paf 555:): if(low_element_name==CONTENT_TYPE_NAME)
1.40.2.5 paf 556: info->has_content_type=true;
1.1 paf 557: }
558:
1.40.2.12.2.1 (paf 559:): static const String& file_value_to_string(Request& r, Value* send_value) {
1.40.2.12.2.8 (paf 560:: VFile* vfile;
1.40.2.12.2.1 (paf 561:): const String* file_name=0;
562:): Value* vformat=0;
563:): if(HashStringValue *send_hash=send_value->get_hash()) { // hash
1.1 paf 564: // $.value
1.40.2.12.2.8 (paf 565:: if(Value* value=send_hash->get(value_name))
566:: vfile=value->as_vfile(String::L_AS_IS);
1.1 paf 567: else
568: throw Exception("parser.runtime",
1.40.2.12.2.1 (paf 569:): 0,
1.1 paf 570: "file part has no $value");
571:
572: // $.format
1.40.2.5 paf 573: vformat=send_hash->get(format_name);
1.1 paf 574:
1.6 paf 575: // $.name
1.40.2.12.2.8 (paf 576:: if(Value* vfile_name=send_hash->get(name_name)) // $name specified
1.40.2.12.2.1 (paf 577:): file_name=&vfile_name->as_string();
1.28 paf 578: } else // must be VFile then
1.40.2.12.2.8 (paf 579:: vfile=send_value->as_vfile(String::L_AS_IS);
1.28 paf 580:
581: if(!file_name)
1.40.2.12.2.1 (paf 582:): file_name=&vfile->fields().get(name_name)->as_string();
1.28 paf 583:
1.40.2.3 paf 584: const char* file_name_cstr=file_name->cstr();
1.1 paf 585:
1.40.2.12.2.1 (paf 586:): String& result=*new String;
1.1 paf 587:
588: // content-type: application/octet-stream
1.40.2.12.2.1 (paf 589:): result << "content-type: " << r.mime_type_of(file_name_cstr)
1.1 paf 590: << "; name=\"" << file_name_cstr << "\"\n";
591: // content-disposition: attachment; filename="user_file_name"
1.40.2.12.2.1 (paf 592:): result <<
1.35 paf 593: CONTENT_DISPOSITION_NAME ": "CONTENT_DISPOSITION_VALUE"; "
594: CONTENT_DISPOSITION_FILENAME_NAME"=\"" << file_name_cstr << "\"\n";
1.1 paf 595:
1.40.2.12.2.1 (paf 596:): const String* type=vformat?&vformat->as_string():0;
1.1 paf 597: if(!type/*default = uue*/ || *type=="uue") {
1.40.2.12.2.1 (paf 598:): pa_uuencode(result, *file_name, *vfile);
1.1 paf 599: } else // for now
600: throw Exception("parser.runtime",
601: type,
602: "unknown attachment encode format");
603:
604: return result;
605: }
606:
1.40.2.12.2.1 (paf 607:): static const String& text_value_to_string(Request& r,
608:): PartType pt, Value* send_value,
609:): Store_message_element_info& info) {
610:): String& result=*new String;
1.1 paf 611:
1.40.2.12.2.8 (paf 612:: Value* text_value;
1.40.2.12.2.1 (paf 613:): if(HashStringValue* send_hash=send_value->get_hash()) {
1.1 paf 614: // $.USER-HEADERS
615: info.has_content_type=false; // reset
616: send_hash->for_each(store_message_element, &info);
617: // $.value
1.40.2.5 paf 618: text_value=send_hash->get(value_name);
1.1 paf 619: if(!text_value)
620: throw Exception("parser.runtime",
1.40.2.12.2.1 (paf 621:): 0,
1.40.2.12.2.7 (paf 622:: "%s part has no $" VALUE_NAME, part_name_begins[pt]);
1.1 paf 623: } else
1.40.2.5 paf 624: text_value=send_value;
1.1 paf 625:
626: if(!info.has_content_type) {
1.40.2.12.2.1 (paf 627:): result
1.40.2.5 paf 628: << "content-type: text/" << (pt==P_TEXT?"plain":"html")
1.40.2.10 paf 629: << "; charset=" << info.charsets.mail().name()
1.40.2.5 paf 630: << "\n";
1.1 paf 631: }
632:
633: // header|body separator
1.40.2.12.2.1 (paf 634:): result << "\n";
1.1 paf 635:
636: // body
1.40.2.12.2.1 (paf 637:): const String* body;
1.1 paf 638: switch(pt) {
639: case P_TEXT:
1.40.2.12.2.1 (paf 640:): body=&text_value->as_string();
1.1 paf 641: break;
642: case P_HTML:
643: {
1.40.2.12.2.1 (paf 644:): Temp_lang temp_lang(r, String::Language(String::L_HTML | String::L_OPTIMIZE_BIT));
1.40.2.12.2.8 (paf 645:: if(Junction* junction=text_value->get_junction())
1.40.2.12.2.1 (paf 646:): body=&r.process_to_string(*text_value);
1.26 paf 647: else
1.1 paf 648: throw Exception("parser.runtime",
1.40.2.12.2.1 (paf 649:): 0,
1.1 paf 650: "html part value must be code");
651:
652: break;
653: }
1.40.2.11 paf 654: }
655: if(body) {
1.40.2.12.2.8 (paf 656:: const char* body_ptr=body->cstr(String::L_UNSPECIFIED); // body
1.40.2.11 paf 657: size_t body_size=strlen(body_ptr); // body
658: const void *mail_ptr;
659: size_t mail_size;
1.40.2.12.2.4 (paf 660:: Charset::transcode(
1.40.2.11 paf 661: r.charsets.source(), body_ptr, body_size,
662: r.charsets.mail()/*always set - either mail.charset or $request:charset*/, mail_ptr, mail_size);
1.40.2.12.2.1 (paf 663:): ///@todo
664:): result.append_strdup((const char*)mail_ptr, mail_size, String::L_CLEAN);
1.1 paf 665: }
666:
667: return result;
668: };
669:
670: /// @todo files and messages in order (file, file2, ...)
1.40.2.12.2.1 (paf 671:): const String& VMail::message_hash_to_string(Request& r,
672:): HashStringValue* message_hash, int level,
673:): const String* & from, bool extract_to, String* & to) {
1.34 paf 674:
1.1 paf 675: if(!message_hash)
676: throw Exception("parser.runtime",
1.40.2.12.2.1 (paf 677:): 0,
1.1 paf 678: "message must be hash");
679:
1.40.2.12.2.1 (paf 680:): String& result=*new String;
1.1 paf 681:
1.40.2.12.2.8 (paf 682:: if(Value* vrecodecharset_name=message_hash->get(charset_name))
683:: r.charsets.set_mail(charsets.get(vrecodecharset_name->as_string()));
1.1 paf 684: else
1.40.2.5 paf 685: r.charsets.set_mail(r.charsets.source());
686: // no big deal that we leave it set. they wont miss this point which would reset it
1.1 paf 687:
1.40.2.12.2.4 (paf 688:: Store_message_element_info info(r.charsets,
1.40.2.10 paf 689: result, from, extract_to, to);
1.1 paf 690: {
691: for(int pt=0; pt<P_TYPES_COUNT; pt++)
1.40.2.12.2.1 (paf 692:): info.parts[pt]=new ArrayValue(1);
1.1 paf 693: message_hash->for_each(store_message_element, &info);
1.10 paf 694: if(!info.errors_to)
1.40.2.12.2.1 (paf 695:): result << "errors-to: postmaster\n"; // errors-to: default
1.37 paf 696: if(!info.mime_version_specified)
1.40.2.12.2.1 (paf 697:): result << "MIME-Version: 1.0\n"; // MIME-Version: default
1.1 paf 698: }
699:
1.40.2.5 paf 700: int textCount=info.parts[P_TEXT]->count();
1.1 paf 701: if(textCount>1)
702: throw Exception("parser.runtime",
1.40.2.12.2.1 (paf 703:): 0,
1.1 paf 704: "multiple text parts not supported, use file part");
1.40.2.5 paf 705: int htmlCount=info.parts[P_HTML]->count();
1.1 paf 706: if(htmlCount>1)
707: throw Exception("parser.runtime",
1.40.2.12.2.1 (paf 708:): 0,
1.1 paf 709: "multiple html parts not supported, use file part");
710:
711:
712: bool multipart=info.parts_count>1;
713: bool alternative=textCount && htmlCount;
714: // header
715: char *boundary=0;
716: if(multipart) {
1.40.2.12.2.1 (paf 717:): boundary=new(PointerFreeGC) char[MAX_NUMBER];
1.1 paf 718: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
719: // multi-part
1.40.2.12.2.1 (paf 720:): result
1.40.2.5 paf 721: << "content-type: multipart/mixed; boundary=\""
722: << boundary << "\"\n"
723: "\n"
724: "This is a multi-part message in MIME format.";
1.1 paf 725: }
726:
727: // alternative or not
728: {
729: if(alternative) {
1.40.2.12.2.1 (paf 730:): result << "\n\n--" << boundary << "\n" // intermediate boundary
1.40.2.5 paf 731: "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
1.1 paf 732: }
733: for(int i=0; i<2; i++) {
734: PartType pt=i==0?P_TEXT:P_HTML;
1.40.2.5 paf 735: if(info.parts[pt]->count()) {
1.1 paf 736: if(alternative)
1.40.2.12.2.1 (paf 737:): result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
1.1 paf 738: else if(boundary)
1.40.2.12.2.1 (paf 739:): result << "\n\n--" << boundary << "\n"; // intermediate boundary
740:): result << text_value_to_string(r, pt, info.parts[pt]->get(0), info);
1.1 paf 741: }
742: }
743: if(alternative)
1.40.2.12.2.1 (paf 744:): result << "\n\n--ALT" << boundary << "--\n";
1.1 paf 745: }
746:
747: // files
748: {
1.40.2.5 paf 749: ArrayValue& files=*info.parts[P_FILE];
1.40.2.12.2.1 (paf 750:): for(size_t i=0; i<files.count(); i++) {
1.1 paf 751: if(boundary)
1.40.2.12.2.1 (paf 752:): result << "\n\n--" << boundary << "\n"; // intermediate boundary
753:): result << file_value_to_string(r, files.get(i));
1.1 paf 754: }
755: }
756:
757: // messages
758: {
1.40.2.5 paf 759: ArrayValue& messages=*info.parts[P_MESSAGE];
1.40.2.12.2.1 (paf 760:): for(size_t i=0; i<messages.count(); i++) {
1.1 paf 761: if(boundary)
1.40.2.12.2.1 (paf 762:): result << "\n\n--" << boundary << "\n"; // intermediate boundary
1.1 paf 763:
1.40.2.12.2.1 (paf 764:): const String* dummy_from;
765:): String* dummy_to;
766:): result << message_hash_to_string(r, messages.get(i)->get_hash(), level+1,
1.40.2.10 paf 767: dummy_from, false, dummy_to);
1.1 paf 768: }
769: }
770:
771: // tailer
772: if(boundary)
1.40.2.12.2.1 (paf 773:): result << "\n\n--" << boundary << "--\n"; // finish boundary
1.1 paf 774:
775: // return
776: return result;
777: }
778:
779:
1.40.2.12.2.8 (paf 780:: Value* VMail::get_element(const String& aname, Value& aself, bool looking_up) {
1.1 paf 781: // $fields
1.3 paf 782: #ifdef WITH_MAILRECEIVE
1.40.2.12.2.8 (paf 783:: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
1.40.2.5 paf 784: return vreceived;
1.1 paf 785: #endif
786:
787: // $CLASS,$method
1.40.2.12.2.8 (paf 788:: if(Value* result=VStateless_class::get_element(aname, aself, looking_up))
1.1 paf 789: return result;
790:
1.40.2.12.2.8 (paf 791:: return 0;
1.1 paf 792: }
793:
1.3 paf 794: #if defined(WITH_MAILRECEIVE) && defined(_MSC_VER)
1.1 paf 795: # define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
796: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
797: # ifdef _DEBUG
798: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
799: # else
800: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
801: # endif
802: #endif
E-mail: