|
|
1.1 paf 1:
2: /** @file
3: Parser: @b mail class.
4: relies on gmime library, by Jeffrey Stedfast <fejj@helixcode.com>
5:
6: Copyright(c) 2001, 2002 ArtLebedev Group(http://www.artlebedev.com)
7: Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
8:
1.2 ! paf 9: $Id: pa_vmail.C,v 1.1 2002/06/24 14:24:03 paf Exp $
1.1 paf 10: */
11:
12: #include "pa_sapi.h"
13: #include "pa_vmail.h"
14: #include "pa_vstring.h"
15: #include "pa_request.h"
16: #include "pa_common.h"
17: #include "pa_charset.h"
18: #include "pa_charsets.h"
19: #include "pa_vdate.h"
20: #include "pa_vfile.h"
21: #include "pa_uue.h"
22:
23: #ifdef WITH_MAIL_RECEIVE
24: #include "gmime-config.h"
25: #include "gmime.h"
26: #endif
27:
28: // defines
29:
30: #define RAW_NAME "raw"
31:
32: // internals
33:
34: enum PartType {
35: P_TEXT,
36: P_HTML,
37: P_FILE,
38: P_MESSAGE,
39:
40: P_TYPES_COUNT
41: };
42:
43: static const char * const part_name_starts[P_TYPES_COUNT]={"text", "html", "file", "message"};
44:
45: // VMail
46:
47: extern Methoded *mail_base_class;
48:
49: VMail::VMail(Pool& apool) : VStateless_class(apool, 0, mail_base_class),
50: vreceived(apool) {
51: }
52:
53: #ifdef WITH_MAIL_RECEIVE
54:
55: static const String& maybeUpperCase(Pool& pool, const String& src, bool toUpperCase) {
56: return toUpperCase?src.change_case(pool, String::CC_UPPER):src;
57: }
58:
59: static void UTF8toSource(Pool& pool, const char *source_body, size_t source_content_length,
60: const void *& dest_body, size_t& dest_content_length) {
61: if(source_body) {
62: if(!source_content_length)
63: source_content_length=strlen(source_body);
64: Charset::transcode(pool,
65: *utf8_charset, source_body, source_content_length,
66: pool.get_source_charset(), dest_body, dest_content_length);
67: } else {
68: dest_body=0;
69: dest_content_length=0;
70: }
71: }
72:
73: static void putReceived(Hash& received, const char *name, Value *value, bool nameToUpperCase=false) {
74: Pool& pool=received.pool();
75: if(name && value) {
76: received.put(
77: maybeUpperCase(pool, String::OnPool(pool, name, 0, true/*tainted*/), nameToUpperCase),
78: value);
79: }
80: }
81:
82: static void putReceived(Hash& received, const char *name, const char *value, size_t value_size=0, bool nameToUpperCase=false) {
83: if(value) {
84: Pool& pool=received.pool();
85:
86: const void *value_dest_body;
87: size_t value_dest_content_length;
88: UTF8toSource(pool, value, value_size, value_dest_body, value_dest_content_length);
89:
90: putReceived(received, name,
91: new(pool) VString(
92: String::OnPool(pool,(const char *)value_dest_body, value_dest_content_length, true/*tainted*/)));
93: }
94: }
95:
96: static void putReceived(Hash& received, const char *name, time_t value) {
97: Pool& pool=received.pool();
98: if(name)
99: received.put(String::OnPool(pool, name, 0, true/*tainted*/), new(pool) VDate(pool, value));
100: }
101:
102: static void MimeHeaderField2received(const char *name, const char *value, gpointer data) {
103: Hash& received=*static_cast<Hash *>(data);
104:
105: putReceived(received, name, value, 0, true/*nameInUpperCase*/);
106: }
107:
108: static void parse(GMimeStream *stream, Hash& received);
109:
110: #ifndef DOXYGEN
111: struct MimePart2bodyInfo {
112: Hash *body;
113: int partCounts[P_TYPES_COUNT];
114: };
115: #endif
116: static void MimePart2body(GMimePart *part,
117: gpointer data) {
118: MimePart2bodyInfo& i=*static_cast<MimePart2bodyInfo *>(data);
119: Pool& pool=i.body->pool();
120:
121: if(const GMimeContentType *type=g_mime_part_get_content_type(part)) {
122: if(g_mime_content_type_is_type(type, "multipart", "*"))
123: return; // skipping frames
124:
125: PartType partType;
126: if(g_mime_content_type_is_type(type, "text", "plain"))
127: partType=P_TEXT;
128: else if(g_mime_content_type_is_type(type, "text", "html"))
129: partType=P_HTML;
130: else if(g_mime_content_type_is_type(type, "message", "*"))
131: partType=P_MESSAGE;
132: else
133: partType=P_FILE;
134:
135: // partName
136: const char *partName;
137: char partNameBuf[MAX_STRING];
138: const char *partNameStart=part_name_starts[partType];
139: if(int partNo=i.partCounts[partType]++) {
140: snprintf(partNameBuf, MAX_STRING, "%s%d", partNameStart, partNo);
141: partName=partNameBuf;
142: } else
143: partName=partNameStart;
144:
145: // $.partX[
146: VHash& vpartX=*new(pool) VHash(pool); putReceived(*i.body, partName, &vpartX);
147: Hash& partX=vpartX.hash(0);
148: {
149: // $.raw[
150: VHash& vraw=*new(pool) VHash(pool); putReceived(partX, RAW_NAME, &vraw);
151: g_mime_header_foreach(part->headers, MimeHeaderField2received, &vraw.hash(0));
152: }
153: const char *content_filename=0;
154: {
155: // $.content-type[
156: VHash& vcontent_type=*new(pool) VHash(pool); putReceived(partX, "content-type", &vcontent_type);
157: Hash& content_type=vcontent_type.hash(0);
158: {
159: // $.value[text/plain]
160: char value[MAX_STRING];
161: snprintf(value, MAX_STRING, "%s/%s",
162: type->type?type->type:"x-unknown",
163: type->subtype?type->subtype:"x-unknown");
164: putReceived(content_type, VALUE_NAME, value);
165: }
166: GMimeParam *param=type->params;
167: while(param) {
168: // $.charset[windows-1251] && co
169: putReceived(content_type, param->name, param->value, true);
170: if(strcasecmp(param->name, "name")==0)
171: content_filename=param->value;
172: param=param->next;
173: }
174: }
175: // $.description
176: putReceived(partX, "description", part->description);
177: // $.content-id
178: putReceived(partX, "content-id", part->content_id);
179: // $.content-md5
180: putReceived(partX, "content-md5", part->content_md5);
181: // $.content-location
182: putReceived(partX, "content-location", part->content_location);
183:
184: // todo GMimePart:
185: // GMimePartEncodingType encoding;
186: // GMimeDisposition *disposition;
187: if(part->disposition) {
188: GMimeParam *param=part->disposition->params;
189: while(param) {
190: // $.charset[windows-1251] && co
191: if(strcasecmp(param->name, "filename")==0)
192: content_filename=param->value;
193: param=param->next;
194: }
195: }
196:
197: // MESSAGE
198: if(partType==P_MESSAGE) {
199: if(part->content)
200: if(GMimeStream *stream=part->content->stream)
201: parse(stream, partX);
202: } else {
203: // $.value[string|file]
204: size_t buf_len;
205: const void *buf=g_mime_part_get_content(part, &buf_len);
206: if(partType==P_FILE) {
207: VFile& vfile=*new(pool) VFile(pool);
208: vfile.set(true/*tainted*/, buf, buf_len, content_filename);
209: putReceived(partX, VALUE_NAME, &vfile);
210: } else {
211: // P_TEXT, P_HTML
212: putReceived(partX, VALUE_NAME,(const char*)buf, buf_len);
213: }
214: }
215: }
216: }
217:
218: static void parse(GMimeStream *stream, Hash& received) {
219: Pool& pool=received.pool();
220:
221: GMimeMessage *message=g_mime_parser_construct_message(stream);
222: try {
223: const GMimeMessageHeader *messageHeader=message->header;
224: if(!messageHeader)
225: return;
226:
227: // firstly user-defined strings go
228: // user headers
229: {
230: // $.raw[
231: VHash& vraw=*new(pool) VHash(pool); putReceived(received, "raw", &vraw);
232: g_mime_header_foreach(messageHeader->headers, MimeHeaderField2received, &vraw.hash(0));
233: }
234:
235: // maybe-todo-recipients
236: // x(messageHeader->recipients)
237:
238: // secondly standard headers&body go
239: // standard header
240: // .from
241: putReceived(received, "from", messageHeader->from);
242: // .reply-to
243: putReceived(received, "reply-to", messageHeader->reply_to);
244: // .to
245: // todo: messageHeader->recipients
246: // .subject
247: putReceived(received, "subject", messageHeader->subject);
248: // .date(date+gmt_offset)
249: int tt_offset =
250: ((messageHeader->gmt_offset / 100) *(60 * 60))
251: +(messageHeader->gmt_offset % 100) * 60;
252: putReceived(received, "date",
253: messageHeader->date // local sender
254: -tt_offset // move local sender to GMT sender
255: -(timezone+(daylight?60*60*sign(timezone):0)) // move GMT sender to our local time
256: );
257: // .message-id
258: putReceived(received, "message-id", messageHeader->message_id);
259:
260: // .body[part/parts
261: GMimePart *part=message->mime_part;
262: const GMimeContentType *type=g_mime_part_get_content_type(part);
263: MimePart2bodyInfo info={&received};
264: g_mime_part_foreach(part, MimePart2body, &info);
265:
266: // normal unref
267: g_mime_object_unref(GMIME_OBJECT(message));
268: } catch(...) {
269: // abnormal unref
270: g_mime_object_unref(GMIME_OBJECT(message));
271: }
272: }
273: #endif
274:
275:
276:
277: void VMail::fill_received(Request& request) {
278: // store letter to received
279: #ifdef WITH_MAIL_RECEIVE
280: if(request.info.mail_received) {
281: // init
282: g_mime_init(GMIME_INIT_FLAG_UTF8);
283:
284: // create stream with CRLF filter
285: GMimeStream *stream = g_mime_stream_fs_new(fileno(stdin));
286: GMimeStream *istream = g_mime_stream_filter_new_with_stream(stream);
287: GMimeFilter *filter = g_mime_filter_crlf_new(GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
288: g_mime_stream_filter_add(GMIME_STREAM_FILTER(istream), filter);
289: g_mime_stream_unref(stream);
290: stream = istream;
291: try {
292: // parse incoming stream
293: parse(stream, vreceived.hash(0));
294: // normal stream free
295: g_mime_stream_unref(stream);
296: } catch(...) {
297: // abnormal stream free
298: g_mime_stream_unref(stream);
299: }
300: }
301: #endif
302: }
303:
304: #ifndef DOXYGEN
305: struct Store_message_element_info {
306: Charset *charset;
307: String *header;
308: const String **from, **to;
309: Array *parts[P_TYPES_COUNT];
310: int parts_count;
311: bool has_content_type;
312: };
313: #endif
314: static void store_message_element(const Hash::Key& element_name, Hash::Val *aelement_value,
315: void *info) {
316: Value& element_value=*static_cast<Value *>(aelement_value);
317: Store_message_element_info& i=*static_cast<Store_message_element_info *>(info);
318:
319: // exclude internals
320: if(element_name==CHARSET_NAME
321: || element_name==VALUE_NAME
322: || element_name==RAW_NAME)
323: return;
324:
325: // grep parts
326: for(int pt=0; pt<P_TYPES_COUNT; pt++) {
327: if(element_name.starts_with(part_name_starts[pt])) {
328: *i.parts[pt]+=&element_value;
329: i.parts_count++;
330: return;
331: }
332: }
333:
334: // fetch from & to from header for SMTP
335: if(i.from && element_name=="from")
336: *i.from=&element_value.as_string();
337: if(i.to && element_name=="to")
338: *i.to=&element_value.as_string();
339:
340: // append header line
341: *i.header <<
342: element_name << ":" <<
343: attributed_meaning_to_string(element_value, String::UL_MAIL_HEADER).
344: cstr(String::UL_UNSPECIFIED, 0, i.charset, i.charset?i.charset->name().cstr():0) <<
345: "\n";
346:
347: // has content type?
348: if(element_name.change_case(element_name.pool(), String::CC_LOWER)==CONTENT_TYPE_NAME)
349: i.has_content_type=true;
350: }
351:
352: static const String& file_value_to_string(Request& r, const String *source,
353: Value& send_value) {
354: Pool& pool=r.pool();
355: const VFile *vfile;
356: const String *file_name;
357: Value *vformat;
358: if(Hash *send_hash=send_value.get_hash(source)) { // hash
359: // $.value
360: if(Value *value=static_cast<Value *>(send_hash->get(*value_name)))
361: vfile=value->as_vfile(String::UL_AS_IS);
362: else
363: throw Exception("parser.runtime",
364: source,
365: "file part has no $value");
366:
367: // $.format
368: vformat=static_cast<Value *>(send_hash->get(*new(pool) String(pool, "format")));
369:
370: // $.file-name
371: if(Value *vfile_name=static_cast<Value *>(send_hash->get(
372: *new(pool) String(pool, "file-name")))) // specified $file-name
373: file_name=&vfile_name->as_string();
374: } else { // must be VFile
375: vfile=send_value.as_vfile(String::UL_AS_IS);
376: file_name=&static_cast<Value *>(vfile->fields().get(*name_name))->as_string();
377: vformat=0;
378: }
379: const char *file_name_cstr=file_name->cstr();
380:
381: String& result=*new(pool) String(pool);
382:
383: // content-type: application/octet-stream
384: result << "content-type: " << r.mime_type_of(file_name_cstr)
385: << "; name=\"" << file_name_cstr << "\"\n";
386: // content-disposition: attachment; filename="user_file_name"
387: result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
388:
389: const String *type=vformat?&vformat->as_string():0;
390: if(!type/*default = uue*/ || *type=="uue") {
391: pa_uuencode(result, file_name_cstr, *vfile);
392: } else // for now
393: throw Exception("parser.runtime",
394: type,
395: "unknown attachment encode format");
396:
397: return result;
398: }
399:
400: static const String& text_value_to_string(Request& r, const String *source,
401: PartType pt, Value& send_value,
402: Store_message_element_info& info) {
403: Pool& pool=r.pool();
404: String& result=*new(pool) String(pool);
405:
406: Value *text_value;
407: bool has_content_type;
408: if(Hash *send_hash=send_value.get_hash(source)) {
409: // $.USER-HEADERS
410: info.has_content_type=false; // reset
411: send_hash->for_each(store_message_element, &info);
412: // $.value
413: text_value=static_cast<Value *>(send_hash->get(*value_name));
414: if(!text_value)
415: throw Exception("parser.runtime",
416: source,
417: "%s part has no $" VALUE_NAME, part_name_starts[pt]);
418: } else
419: text_value=&send_value;
420:
421: if(!info.has_content_type) {
422: result << "content-type: text/" << (pt==P_TEXT?"plain":"html");
423: if(info.charset)
424: result << "; charset=" << info.charset->name();
425: result << "\n";
426: }
427:
428: // header|body separator
429: result << "\n";
430:
431: // body
432: switch(pt) {
433: case P_TEXT:
434: result.append(text_value->as_string(), String::UL_AS_IS, true /* forced */);
435: break;
436: case P_HTML:
437: {
438: Temp_lang temp_lang(r, String::UL_HTML);
439: if(Junction *junction=text_value->get_junction()) {
440: // execution of found $.html{code} must be in context of ^send[...]
441: // setting code context, would execute in ^.send[>>context<<]
442: //junction->change_context(?.get_junction());
443: junction->root=r.root;
444: junction->rcontext=r.rcontext;
445: junction->wcontext=r.wcontext;
446:
447: result << r.process_to_string(*text_value);
448: } else
449: throw Exception("parser.runtime",
450: source,
451: "html part value must be code");
452:
453: break;
454: }
455: }
456:
457: return result;
458: };
459:
460: /// @todo files and messages in order (file, file2, ...)
461: const String& VMail::message_hash_to_string(Request& r, const String *source,
462: Hash *message_hash, int level,
463: const String **from, const String **to) {
464: if(!message_hash)
465: throw Exception("parser.runtime",
466: source,
467: "message must be hash");
468:
469: String& result=*NEW String(pool());
470:
471: Charset *charset;
472: if(Value *vrecodecharset_name=static_cast<Value *>(message_hash->get(*charset_name)))
473: charset=&charsets->get_charset(vrecodecharset_name->as_string());
474: else
475: charset=&pool().get_source_charset();
476:
477: Store_message_element_info info={
478: charset,
479: &result,
480: from, to
481: };
482: {
483: if(from)
484: *from=0;
485: if(to)
486: *to=0;
487: for(int pt=0; pt<P_TYPES_COUNT; pt++)
488: info.parts[pt]=NEW Array(pool());
489: message_hash->for_each(store_message_element, &info);
490: }
491:
492: int textCount=info.parts[P_TEXT]->size();
493: if(textCount>1)
494: throw Exception("parser.runtime",
495: source,
496: "multiple text parts not supported, use file part");
497: int htmlCount=info.parts[P_HTML]->size();
498: if(htmlCount>1)
499: throw Exception("parser.runtime",
500: source,
501: "multiple html parts not supported, use file part");
502:
503:
504: bool multipart=info.parts_count>1;
505: bool alternative=textCount && htmlCount;
506: // header
507: char *boundary=0;
508: if(multipart) {
509: boundary=(char *)malloc(MAX_NUMBER);
510: snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
511: // multi-part
512: result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
513: result << "\n"
514: "This is a multi-part message in MIME format.";
515: }
516:
517: // alternative or not
518: {
519: if(alternative) {
520: result << "\n\n--" << boundary << "\n"; // intermediate boundary
521: result << "content-type: multipart/alternative; boundary=\"ALT" << boundary << "\"\n";
522: }
523: for(int i=0; i<2; i++) {
524: PartType pt=i==0?P_TEXT:P_HTML;
525: if(info.parts[pt]->size()) {
526: if(alternative)
527: result << "\n\n--ALT" << boundary << "\n"; // intermediate boundary
528: else if(boundary)
529: result << "\n\n--" << boundary << "\n"; // intermediate boundary
530: result << text_value_to_string(r, source, pt,
531: *static_cast<Value *>(info.parts[pt]->get(0)), info);
532: }
533: }
534: if(alternative)
535: result << "\n\n--ALT" << boundary << "--\n";
536: }
537:
538: // files
539: {
540: Array& files=*info.parts[P_FILE];
541: for(int i=0; i<files.size(); i++) {
542: if(boundary)
543: result << "\n\n--" << boundary << "\n"; // intermediate boundary
544: result << file_value_to_string(r, source, *static_cast<Value *>(files.get(i)));
545: }
546: }
547:
548: // messages
549: {
550: Array& messages=*info.parts[P_MESSAGE];
551: for(int i=0; i<messages.size(); i++) {
552: if(boundary)
553: result << "\n\n--" << boundary << "\n"; // intermediate boundary
554:
555: result << message_hash_to_string(r, source,
556: static_cast<Value *>(messages.get(i))->get_hash(source),
557: level+1);
558: }
559: }
560:
561: // tailer
562: if(boundary)
563: result << "\n\n--" << boundary << "--\n"; // finish boundary
564:
565: // return
566: return result;
567: }
568:
569:
570: Value *VMail::get_element(const String& aname) {
571: // $fields
572: #ifdef WITH_MAIL_RECEIVE
573: if(aname==MAIL_RECEIVED_ELEMENT_NAME)
574: return &vreceived;
575: #endif
576:
577: // $CLASS,$method
578: if(Value *result=VStateless_class::get_element(aname))
579: return result;
580:
581: return 0;
582: }
583:
584: #if defined(WITH_MAIL_RECEIVE) && defined(_MSC_VER)
585: # define GNOME_LIBS "/parser3project/win32mailreceive/win32/gnome"
586: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
587: # ifdef _DEBUG
588: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Debug/libgmime.lib")
589: # else
590: # pragma comment(lib, GNOME_LIBS "/gmime-x.x.x/Release/libgmime.lib")
591: # endif
592: #endif