Reading messages that might be MIME-encoded

Here's an example program:

      # A more complete coding might pass in a callback, to
      #    generalize puts, or another for exceptions, or 
      #    "depth-gauge" variables to operationalize dependences
      #    on where one is in the recursion, or ...
      package require mime


      # The mime package doesn't know about uuencodings.  
      #    Take care of this later.
      proc is_uuencoded body {
          return 0
      }

      proc print_text_part message {
          if [catch {mime::initialize -string $message} token] {
      puts stderr $token
              # Do some error-handling here.
              exit 1
          }
          print_text_part_t $token  
      }


      proc print_text_part_t token {
          set content [mime::getproperty $token content]
      puts "Looking at $content."
          switch $content {
              application/x-wordperfect6.1  -
              application/ppt  -
              application/pdf  -
              image/jpeg  -
              application/x-gzip  -
              application/msword  -
              application/octet-stream  -
              application/x-tar  -
              application/msword -
              application/x-wordperfect6.1 -
              application/x-msdownload -
              application/x-tar -
              application/zip {
                  # Is there anything printable here?
              }                 
              text/html {
                  # How do you want to handle this?
              }
              message/rfc822 -
              TEXT/PLAIN -
              text/rfc822-headers -
              text/enriched -
              text/plain {
                set body [mime::getbody $token]
                if [is_uuencoded $body] {
                }
                puts --------------------------------------\n$body
              }
              message/delivery-status -
              multipart/digest -
              multipart/related -
              multipart/report -
              multipart/alternative -
              multipart/mixed {
                  foreach part [mime::getproperty $token parts] {
                      print_text_part_t $part  
                  }
              }
              default {
                  puts stderr "Whoops!  What is a '$content'?"
              }
          }
      }

      print_text_part $message

$message might have a value such as
      From [email protected] Thu Jan 31 18:30:59 2002
      Date: Thu, 31 Jan 2002 17:53:32 -0600 (CST)
      From: Cameron Laird <[email protected]>
      To: [email protected]
      Subject: This is the subject line.
      Message-ID: <[email protected]>
      MIME-Version: 1.0
      Content-Type: MULTIPART/MIXED; BOUNDARY="0-830506451-1012521212=:60621"
      Status: R

        This message is in MIME format.  The first part should be readable text,
        while the remaining parts are likely unreadable without MIME-aware tools.
        Send mail to [email protected] for more info.

      --0-830506451-1012521212=:60621
      Content-Type: TEXT/PLAIN; charset=US-ASCII  

      Here's content.


      --0-830506451-1012521212=:60621
      Content-Type: TEXT/HTML; charset=US-ASCII; name="something.html"
      Content-Transfer-Encoding: BASE64
      Content-ID: <[email protected]>
      Content-Description:
      Content-Disposition: attachment; filename="something-2TMP.html"

      PGh0bWw+DQo8aGVhZD4NCjxNRVRBIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlw
      ...
      CkxvY2FsIGFkZGl0aW9uczoNCjwvcHJlPg0KPC9ib2R5Pg0KPC9odG1sPg==
      --0-830506451-1012521212=:60621
      Content-Type: APPLICATION/ZIP; name="rewrite.zip"
      Content-Transfer-Encoding: BASE64
      Content-ID: <[email protected]>
      Content-Description: What is this?
                                          
      Content-Disposition: attachment; filename="rewrite.zip"

      UEsDBBQAAAAIAPSOPyzW4uC1uyIAAAdXAAAUABUAQ0wwMjAyLWVkaXRlZEJE
      ...
      AAAAAAEAAQBPAAAAAiMAAAAA
      --0-830506451-1012521212=:60621--

I am trying to write a tclapp that will accept a list of files as arguments and save all the attachments. I have just it just about complete, but I ran into a snag when it came to reading and saving the attachment data. My question is, how do you save the attachments using the mime package from tcllib? I've tried mime::getbody, but I get an error that the attachment is not a leaf.

Figured it out. Here is my solution. Mime File Attachment Extractor