| 1 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 2 |
;;;; print-mime.lisp: Tools for printing a mime object |
|---|
| 3 |
;;;; Copyright (C) 2004 Robert Marlow <bobstopper@bobturf.org> |
|---|
| 4 |
;;;; |
|---|
| 5 |
;;;; This library is free software; you can redistribute it and/or |
|---|
| 6 |
;;;; modify it under the terms of the GNU Library General Public |
|---|
| 7 |
;;;; License as published by the Free Software Foundation; either |
|---|
| 8 |
;;;; version 2 of the License, or (at your option) any later version. |
|---|
| 9 |
;;;; |
|---|
| 10 |
;;;; This library is distributed in the hope that it will be useful, |
|---|
| 11 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 |
;;;; Library General Public License for more details. |
|---|
| 14 |
;;;; |
|---|
| 15 |
;;;; You should have received a copy of the GNU Library General Public |
|---|
| 16 |
;;;; License along with this library; if not, write to the |
|---|
| 17 |
;;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|---|
| 18 |
;;;; Boston, MA 02111-1307, USA. |
|---|
| 19 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
(in-package :mime) |
|---|
| 24 |
|
|---|
| 25 |
(defun add-to-print-headers (key alist headers-out) |
|---|
| 26 |
"Adds a requested header from the provided alist to a printable |
|---|
| 27 |
headers string" |
|---|
| 28 |
(aif (assoc key alist) |
|---|
| 29 |
(concatenate 'string headers-out |
|---|
| 30 |
(format nil "~A: ~A~%" |
|---|
| 31 |
(if (eql :mime-version (car it)) |
|---|
| 32 |
"MIME-Version" |
|---|
| 33 |
(string-capitalize (symbol-name (car it)))) |
|---|
| 34 |
(cond |
|---|
| 35 |
((eql :content-id (car it)) |
|---|
| 36 |
(format nil "<~A>" (cdr it))) |
|---|
| 37 |
((eql :content-transfer-encoding (car it)) |
|---|
| 38 |
(string-downcase (symbol-name (cdr it)))) |
|---|
| 39 |
(t |
|---|
| 40 |
(cdr it))))) |
|---|
| 41 |
headers-out)) |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
;;; Note that the way we print the headers, other than type-compulsory |
|---|
| 45 |
;;; content-type parameters, all parameters are folded as per RFC822, once |
|---|
| 46 |
;;; for each parameter such that each parameter begins a new line. This is |
|---|
| 47 |
;;; so header lines don't get too long and so we don't have to fuss about |
|---|
| 48 |
;;; too much counting line length and figuring out where to break up long |
|---|
| 49 |
;;; lines. I couldn't find anything in the RFC which prohibited this so |
|---|
| 50 |
;;; I'm taking the easy route. |
|---|
| 51 |
|
|---|
| 52 |
(defun print-headers (stream headers version-p) |
|---|
| 53 |
"Prints headers in the provided assoc-list" |
|---|
| 54 |
(let ((headers-to-print '(:content-type |
|---|
| 55 |
:content-transfer-encoding |
|---|
| 56 |
:content-description |
|---|
| 57 |
:content-id |
|---|
| 58 |
:content-disposition)) |
|---|
| 59 |
(headers-out "")) |
|---|
| 60 |
(if version-p |
|---|
| 61 |
(push :mime-version headers-to-print)) |
|---|
| 62 |
(mapc (lambda (header-key) |
|---|
| 63 |
(setq headers-out |
|---|
| 64 |
(add-to-print-headers header-key |
|---|
| 65 |
headers |
|---|
| 66 |
headers-out))) |
|---|
| 67 |
headers-to-print) |
|---|
| 68 |
(format stream "~A" headers-out))) |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
(defgeneric print-mime (stream mime-obj headers-p version-p) |
|---|
| 72 |
(:documentation |
|---|
| 73 |
"Prints a mime object's contents, optionally with headers")) |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
(defmethod print-mime (stream (mime-obj mime) headers-p version-p) |
|---|
| 77 |
(format stream "~A~A" |
|---|
| 78 |
(if headers-p |
|---|
| 79 |
(concatenate 'string |
|---|
| 80 |
(print-headers nil (get-mime-headers mime-obj) |
|---|
| 81 |
version-p) |
|---|
| 82 |
(string #\newline)) |
|---|
| 83 |
"") |
|---|
| 84 |
(encode-content mime-obj))) |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
(defmethod print-mime (stream (mime-obj multipart-mime) headers-p version-p) |
|---|
| 88 |
(format stream "~A~%~A~{~{--~A~%~:/mime:print-mime/~%~}~}~%--~A--~%~A" |
|---|
| 89 |
(if headers-p |
|---|
| 90 |
(concatenate 'string |
|---|
| 91 |
(print-headers nil (get-mime-headers mime-obj) |
|---|
| 92 |
version-p) |
|---|
| 93 |
(string #\newline)) |
|---|
| 94 |
"") |
|---|
| 95 |
(aif (prologue mime-obj) |
|---|
| 96 |
(concatenate 'string it (string #\newline)) |
|---|
| 97 |
"") |
|---|
| 98 |
(mapcar (lambda (mime-child) |
|---|
| 99 |
(list |
|---|
| 100 |
(boundary mime-obj) |
|---|
| 101 |
mime-child)) |
|---|
| 102 |
(content mime-obj)) |
|---|
| 103 |
(boundary mime-obj) |
|---|
| 104 |
(aif (epilogue mime-obj) |
|---|
| 105 |
(concatenate 'string it (string #\newline)) |
|---|
| 106 |
""))) |
|---|
| 107 |
|
|---|
| 108 |
|
|---|