+;; Against compiler warnings.
+(defvar nndoc-mime-split-ordinal)
+
+(defun nndoc-dissect-mime-parts ()
+ "Go through a MIME composite article and partition it into sub-articles.
+When a MIME entity contains sub-entities, dissection produces one article for
+the header of this entity, and one article per sub-entity."
+ (setq nndoc-dissection-alist nil
+ nndoc-mime-split-ordinal 0)
+ (save-excursion
+ (set-buffer nndoc-current-buffer)
+ (message-narrow-to-head)
+ (let ((case-fold-search t)
+ (message-id (message-fetch-field "Message-ID"))
+ (references (message-fetch-field "References")))
+ (setq nndoc-mime-header (buffer-substring (point-min) (point-max))
+ nndoc-mime-subject (message-fetch-field "Subject"))
+ (while (string-match "\
+^\\(Subject\\|Message-ID\\|References\\|Lines\\|\
+MIME-Version\\|Content-Type\\|Content-Transfer-Encoding\\|\
+\\):.*\n\\([ \t].*\n\\)*"
+ nndoc-mime-header)
+ (setq nndoc-mime-header (replace-match "" t t nndoc-mime-header)))
+ (widen)
+ (nndoc-dissect-mime-parts-sub (point-min) (point-max)
+ nil message-id references))))
+
+(defun nndoc-dissect-mime-parts-sub (begin end position message-id references)
+ "Dissect an entity within a composite MIME message.
+The article, which corresponds to a MIME entity, extends from BEGIN to END.
+The string POSITION holds a dotted decimal representation of the article
+position in the hierarchical structure, it is nil for the outer entity.
+The generated article should use MESSAGE-ID and REFERENCES field values."
+ ;; Note: `case-fold-search' is already `t' from the calling function.
+ (let ((head-begin begin)
+ (body-end end)
+ head-end body-begin type subtype composite comment)
+ (save-excursion
+ ;; Gracefully handle a missing body.
+ (goto-char head-begin)
+ (if (search-forward "\n\n" body-end t)
+ (setq head-end (1- (point))
+ body-begin (point))
+ (setq head-end end
+ body-begin end))
+ ;; Save MIME attributes.
+ (goto-char head-begin)
+ (if (re-search-forward "\
+^Content-Type: *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)"
+ head-end t)
+ (setq type (downcase (match-string 1))
+ subtype (downcase (match-string 2)))
+ (setq type "text"
+ subtype "plain"))
+ (setq composite (string= type "multipart")
+ comment (concat position
+ (when (and position composite) ".")
+ (when composite "*")
+ (when (or position composite) " ")
+ (cond ((string= subtype "plain") type)
+ ((string= subtype "basic") type)
+ (t subtype))))
+ ;; Generate dissection information for this entity.
+ (push (list (incf nndoc-mime-split-ordinal)
+ head-begin head-end body-begin body-end
+ (count-lines body-begin body-end)
+ comment message-id references)
+ nndoc-dissection-alist)
+ ;; Recurse for all sub-entities, if any.
+ (goto-char head-begin)
+ (when (re-search-forward
+ (concat "\
+^Content-Type: *multipart/\\([a-z]+\\);\\(.*;\\)*"
+ "[ \t\n]*[ \t]boundary=\"?\\([^\"\n]*[^\" \t\n]\\)")
+ head-end t)
+ (let ((boundary (concat "\n--" (match-string 3) "\\(--\\)?[ \t]*\n"))
+ (part-counter 0)
+ begin end eof-flag)
+ (goto-char head-end)
+ (setq eof-flag (not (re-search-forward boundary body-end t)))
+ (while (not eof-flag)
+ (setq begin (point))
+ (cond ((re-search-forward boundary body-end t)
+ (or (not (match-string 1))
+ (string= (match-string 1) "")
+ (setq eof-flag t))
+ (forward-line -1)
+ (setq end (point))
+ (forward-line 1))
+ (t (setq end body-end
+ eof-flag t)))
+ (nndoc-dissect-mime-parts-sub begin end
+ (concat position (when position ".")
+ (format "%d"
+ (incf part-counter)))
+ (nnmail-message-id)
+ message-id)))))))
+