Qore Mime Module Reference  1.3
 All Classes Namespaces Functions Variables Groups Pages
Mime.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file Mime.qm Mime module definition
3 
4 /* Mime.qm Copyright 2012 David Nichols
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 
26 
27 /* Version History
28  * 2012-11-02 v1.3: David Nichols <david@qore.org>
29  + made get MultiPartMessage::getMsgAndHeaders() abstract as originally intended
30  + added MultiPartMessage::parseBody() static method
31 
32  * 2012-09-17 v1.2.1: David Nichols <david@qore.org>
33  + fixed q-encoding mail headers (always encode '_' chars otherwise they will be decoded as spaces)
34 
35  * 2012-06-14 v1.2: David Nichols <david@qore.org>
36  + added MIME decoding functions
37 
38  * 2012-05-31 v1.1: David Nichols <david@qore.org>
39  + added initial MultiPartMessage classes
40 
41  * 2012-05-24 v1.0: David Nichols <david@qore.org>
42  + pulled MIME implementation out of other code and made a user module to aggregate MIME definitions and functions
43 
44  based on:
45  - http://tools.ietf.org/html/rfc1521: quoted printable & base 64 transfer encodings
46  - http://tools.ietf.org/html/rfc2045: mime headers, content types, etc
47  - http://tools.ietf.org/html/rfc2047: "Q" and "B" encoded words (implemented by the Mime module)
48 */
49 
89 
90 namespace Mime {
95 
96  const MimeTypeUnknown = "application/octet-stream";
97 
99  const MimeTypeText = "text/plain";
100 
102  const MimeTypeHtml = "text/html";
103 
105  const MimeTypeCsv = "text/csv";
106 
108  const MimeTypeJpeg = "image/jpeg";
109 
111  const MimeTypePng = "image/png";
112 
114  const MimeTypeSoapXml = "application/soap+xml";
115 
117  const MimeTypeYaml = "text/x-yaml";
118 
120  const MimeTypeXml = "text/xml";
121 
123  const MimeTypeXmlApp = "application/xml";
124 
126  const MimeTypeJsonRpc = "application/json";
127 
130 
132  const MimeTypeYamlRpc = "application/x-yaml";
133 
135 
137  const MimeTypes = (
138  "7z": "application/x-7z-compressed",
139  "ai": "application/postscript",
140  "aif": "audio/x-aiff",
141  "aifc": "audio/x-aiff",
142  "aiff": "audio/x-aiff",
143  "asc": MimeTypeText,
144  "atom": "application/atom+xml",
145  "au": "audio/basic",
146  "avi": "video/x-msvideo",
147  "bcpio": "application/x-bcpio",
148  "bin": MimeTypeUnknown,
149  "bmp": "image/bmp",
150  "bz2": "application/x-bzip",
151  "cc": MimeTypeText,
152  "cdf": "application/x-netcdf",
153  "cgm": "image/cgm",
154  "class": MimeTypeUnknown,
155  "cpio": "application/x-cpio",
156  "cpp": MimeTypeText,
157  "cpt": "application/mac-compactpro",
158  "csh": "application/x-csh",
159  "css": "text/css",
160  "csv": MimeTypeCsv,
161  "dcr": "application/x-director",
162  "dif": "video/x-dv",
163  "dir": "application/x-director",
164  "djv": "image/vnd.djvu",
165  "djvu": "image/vnd.djvu",
166  "dll": MimeTypeUnknown,
167  "dmg": MimeTypeUnknown,
168  "dms": MimeTypeUnknown,
169  "doc": "application/msword",
170  "dtd": "application/xml-dtd",
171  "dv": "video/x-dv",
172  "dvi": "application/x-dvi",
173  "dxr": "application/x-director",
174  "eps": "application/postscript",
175  "etx": "text/x-setext",
176  "exe": MimeTypeUnknown,
177  "ez": "application/andrew-inset",
178  "gif": "image/gif",
179  "gram": "application/srgs",
180  "grxml": "application/srgs+xml",
181  "gtar": "application/x-gtar",
182  "gz": "application/x-gzip",
183  "h": MimeTypeText,
184  "hdf": "application/x-hdf",
185  "hh": MimeTypeText,
186  "hpp": MimeTypeText,
187  "hqx": "application/mac-binhex40",
188  "htm": MimeTypeHtml,
189  "html": MimeTypeHtml,
190  "ice": "x-conference/x-cooltalk",
191  "ico": "image/x-icon",
192  "ics": "text/calendar",
193  "ief": "image/ief",
194  "ifb": "text/calendar",
195  "iges": "model/iges",
196  "igs": "model/iges",
197  "jar": "application/java-archive",
198  "java": MimeTypeText,
199  "jnlp": "application/x-java-jnlp-file",
200  "jp2": "image/jp2",
201  "jpe": MimeTypeJpeg,
202  "jpeg": MimeTypeJpeg,
203  "jpg": MimeTypeJpeg,
204  "js": "application/x-javascript",
205  "kar": "audio/midi",
206  "latex": "application/x-latex",
207  "lha": MimeTypeUnknown,
208  "lzh": MimeTypeUnknown,
209  "m3u": "audio/x-mpegurl",
210  "m4a": "audio/mp4a-latm",
211  "m4b": "audio/mp4a-latm",
212  "m4p": "audio/mp4a-latm",
213  "m4u": "video/vnd.mpegurl",
214  "m4v": "video/x-m4v",
215  "mac": "image/x-macpaint",
216  "man": "application/x-troff-man",
217  "mathml": "application/mathml+xml",
218  "me": "application/x-troff-me",
219  "mesh": "model/mesh",
220  "mid": "audio/midi",
221  "midi": "audio/midi",
222  "mif": "application/vnd.mif",
223  "mov": "video/quicktime",
224  "movie": "video/x-sgi-movie",
225  "mp2": "audio/mpeg",
226  "mp3": "audio/mpeg",
227  "mp4": "video/mp4",
228  "mpe": "video/mpeg",
229  "mpeg": "video/mpeg",
230  "mpg": "video/mpeg",
231  "mpga": "audio/mpeg",
232  "ms": "application/x-troff-ms",
233  "msh": "model/mesh",
234  "mxu": "video/vnd.mpegurl",
235  "nc": "application/x-netcdf",
236  "oda": "application/oda",
237  "ogg": "application/ogg",
238  "pbm": "image/x-portable-bitmap",
239  "pct": "image/pict",
240  "pdb": "chemical/x-pdb",
241  "pdf": "application/pdf",
242  "pgm": "image/x-portable-graymap",
243  "pgn": "application/x-chess-pgn",
244  "pic": "image/pict",
245  "pict": "image/pict",
246  "png": MimeTypePng,
247  "pnm": "image/x-portable-anymap",
248  "pnt": "image/x-macpaint",
249  "pntg": "image/x-macpaint",
250  "ppm": "image/x-portable-pixmap",
251  "ppt": "application/vnd.ms-powerpoint",
252  "ps": "application/postscript",
253 
254  // Qore script
255  "q": MimeTypeText,
256 
257  // Qore class include file
258  "qc": MimeTypeText,
259 
260  // Qore include file
261  "ql": MimeTypeText,
262 
263  // Qore user module source
264  "qm": MimeTypeText,
265 
266  "qt": "video/quicktime",
267  "qti": "image/x-quicktime",
268  "qtif": "image/x-quicktime",
269  "ra": "audio/x-pn-realaudio",
270  "ram": "audio/x-pn-realaudio",
271  "ras": "image/x-cmu-raster",
272  "rdf": "application/rdf+xml",
273  "rgb": "image/x-rgb",
274  "rm": "application/vnd.rn-realmedia",
275  "roff": "application/x-troff",
276  "rtf": "text/rtf",
277  "rtx": "text/richtext",
278  "sgm": "text/sgml",
279  "sgml": "text/sgml",
280  "sh": "application/x-sh",
281  "shar": "application/x-shar",
282  "silo": "model/mesh",
283  "sit": "application/x-stuffit",
284  "skd": "application/x-koan",
285  "skm": "application/x-koan",
286  "skp": "application/x-koan",
287  "skt": "application/x-koan",
288  "smi": "application/smil",
289  "smil": "application/smil",
290  "snd": "audio/basic",
291  "so": MimeTypeUnknown,
292  "spl": "application/x-futuresplash",
293  "src": "application/x-wais-source",
294  "sv4cpio": "application/x-sv4cpio",
295  "sv4crc": "application/x-sv4crc",
296  "svg": "image/svg+xml",
297  "swf": "application/x-shockwave-flash",
298  "t": "application/x-troff",
299  "tar": "application/x-tar",
300  "tar.bz2": "application/x-bzip-compressed-tar",
301  "tar.gz": "application/x-tar-gz",
302  "tbz": "application/x-bzip-compressed-tar",
303  "tcl": "application/x-tcl",
304  "tex": "application/x-tex",
305  "texi": "application/x-texinfo",
306  "texinfo": "application/x-texinfo",
307  "text": MimeTypeText,
308  "tgz": "application/x-tar-gz",
309  "tif": "image/tiff",
310  "tiff": "image/tiff",
311  "tr": "application/x-troff",
312  "tsv": "text/tab-separated-values",
313  "txt": MimeTypeText,
314  "ustar": "application/x-ustar",
315  "vcd": "application/x-cdlink",
316  "vrml": "model/vrml",
317  "vxml": "application/voicexml+xml",
318  "wav": "audio/x-wav",
319  "wbmp": "image/vnd.wap.wbmp",
320  "wbmxl": "application/vnd.wap.wbxml",
321  "wml": "text/vnd.wap.wml",
322  "wmlc": "application/vnd.wap.wmlc",
323  "wmls": "text/vnd.wap.wmlscript",
324  "wmlsc": "application/vnd.wap.wmlscriptc",
325  "wrl": "model/vrml",
326  "xbm": "image/x-xbitmap",
327  "xht": "application/xhtml+xml",
328  "xhtml": "application/xhtml+xml",
329  "xls": "application/vnd.ms-excel",
330  "xml": MimeTypeXml,
331  "xpm": "image/x-xpixmap",
332  "xsl": "application/xml",
333  "xslt": "application/xslt+xml",
334  "xul": "application/vnd.mozilla.xul+xml",
335  "xwd": "image/x-xwindowdump",
336  "xyz": "chemical/x-xyz",
337  "yaml": MimeTypeYaml,
338  "zip": "application/zip",
339  );
341 
345 
346  const ContentTransEnc7Bit = "7bit";
347 
349  const ContentTransEnc8Bit = "8bit";
350 
352  const ContentTransEncBinary = "binary";
353 
355  const ContentTransEncBase64 = "base64";
356 
358  const ContentTransEncQuotedPrintable = "quoted-printable";
359 
361 
363  const MimeBase64LineLen = 76;
364 
366 
370 
374 
375 
380  string mime_encode_base64(data str, int len = MimeBase64LineLen);
381 
382 
384 
387  string mime_encode_quoted_printable(string str, bool hdr = False);
388 
389 
391 
396  data mime_encode_transfer_data(data data, string enc);
397 
398 
400  string mime_encode_header_word_b(string str);
401 
402 
404  string mime_encode_header_word_q(string str);
405 
407 
411 
412 
416  binary mime_decode_base64(data str);
417 
418 
420 
425  string mime_decode_base64_to_string(data str, *string encoding);
426 
427 
429 
434  string mime_decode_quoted_printable(string str, *string encoding);
435 
436 
438 
444  data mime_decode_transfer_data(data data, string enc, *string ct);
445 
446 
448 
451  string mime_decode_header(string hdr);
452 
454 
458 
459  const MPT_FORM_DATA = "form-data";
460 
462  const MPT_MIXED = "mixed";
463 
465  const MPT_MESSAGE = "message";
466 
468  const MPT_DIGEST = "digest";
469 
471  const MPT_ALTERNATIVE = "alternative";
472 
474  const MPT_RELATED = "related";
475 
477  const MPT_SIGNED = "signed";
478 
480  const MPT_ENCRYPTED = "encrypted";
481 
483  const MPT_BYTERANGES = "byteranges";
485 
488 
489 public:
490  const MP_DEFAULT_MSG = "This is a MIME multipart message";
491 
493  private :
495  string mptype;
496 
498  string boundary;
499 
501  string ct;
502 
504  list l = ();
505 
507  hash hdr;
508 
509 public:
511 
513 
516  constructor(string mptype, string boundary = MultiPartMessage::getBoundary());
517 
518 
520  static string getBoundary();
521 
523 
527  static string getRandomString(int len);
528 
530 
588  static hash parseBody(string boundary, string body, bool decode = True);
589 
591  binary serialize();
592 
593 
595 
599  static binary serializeHeaders(hash hdr);
600 
602  abstract hash getMsgAndHeaders();
603 
604  // don't reimplement this method; fix/enhance it in the module
605 
606 private:
607  final hash getMsgAndHeadersIntern(*string content_type, bool conlen = True);
608 public:
609 
610 
611  // don't reimplement this method; fix/enhance it in the module
612 
613 private:
614  splicePartIntern(data data, hash hdr);
615 public:
616 
617 
618  // don't reimplement this method; fix/enhance it in the module
619 
620 private:
621  addPartIntern(data data, hash hdr);
622 public:
623 
624 
625 
626 private:
627  static hash getPart(data data, hash hdr);
628 public:
629 
631  };
632 
635 
636 public:
638  private :
639  // the starting part ID
640  string startid;
641 
642 public:
644 
646 
648  constructor(string boundary = MultiPartMessage::getBoundary());
649 
650 
652 
659  splicePart(data data, string id, string content_type, *hash hdr);
660 
661 
663 
670  addPart(data data, string id, string content_type, *hash hdr);
671 
672 
674 
679 
680  };
681 
684 
685 public:
687 
689  constructor(string boundary = MultiPartMessage::getBoundary());
690 
691 
693 
705  splicePart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
706 
707 
709 
721  addPart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
722 
723 
725 
739  spliceEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
740 
741 
743 
757  addEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
758 
759 
761 
766 
767  };
768 };