Subject: [sup-talk] mailcap for os x

From: [email-masked] (Grant Hollingworth)
Date: Thu, 29 Nov 2007 11:22:10 -0500

run-mailcap(1), which sup uses to view attachments, doesn't exist on OS X.

I used the mime-decode hook to monkey patch Attachment.view! to also use the mime-decode hook. A case statement works as a pretty good replacement for my simple .mailcap.

view_attachment[1] uses file(1) to figure out what an attachment's extension should be, before passing it to open(1). For quicklook I went another way, converting from the MIME type to the OS X UTI[2] using mime2uti, an ugly little program I made.

It's messy, but seems to work for my attachements.

[1] http://filibusta.crema.unimi.it/~gufo/files/view_attachment.sh
[2] http://en.wikipedia.org/wiki/Uniform_Type_Identifier
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mime-decode.rb
Type: application/octet-stream
Size: 957 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/sup-talk/attachments/20071129/ab231270/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mime2uti.m
Type: application/octet-stream
Size: 763 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/sup-talk/attachments/20071129/ab231270/attachment-0001.obj

---

From: [email-masked] (William Morgan)
Date: Thu, 27 Dec 2007 10:50:26 -0800

Excerpts from Grant Hollingworth's message of Thu Nov 29 08:22:10 -0800 2007:
> I used the mime-decode hook to monkey patch Attachment.view! to also
> use the mime-decode hook. A case statement works as a pretty good
> replacement for my simple .mailcap.

Hi Grant,

I'd willingly accept a patch to hook into Attachment#view! and allow
overriding of the default run-mailcap behavior.

-- 
William <wmorgan-sup at masanjin.net>

---

From: [email-masked] (Grant Hollingworth)
Date: Tue, 15 Jan 2008 11:34:48 -0500

> overriding of the default run-mailcap behavior.

Sure, but I don't know of the best way to do that. It wouldn't be like other
hooks, because it has to replace behaviour and not just supplement it.

Lots of systems don't have run-mailcap(1). Probably we should port it and push
it out as a gem that Sup depends on.

---

From: [email-masked] (William Morgan)
Date: Tue, 15 Jan 2008 19:20:45 -0800

Reformatted excerpts from Grant Hollingworth's message of 2008-01-15:
> Sure, but I don't know of the best way to do that. It wouldn't be like
> other hooks, because it has to replace behaviour and not just
> supplement it.

That's not a problem. There are plenty of hooks that override default
behavior if they're present, but do nothing if not. The quoteline one
Marcus just submitted is a good example.

> Lots of systems don't have run-mailcap(1). Probably we should port it
> and push it out as a gem that Sup depends on.

If we want to make some kind of a mailcap-parsing gem, there's a Python
version that is probably more directly portable:
http://docs.python.org/lib/module-mailcap.html
Ruby doesn't seem to have any mailcap library, so this would be a good
contribution to the community, especially if it played nicely with
MIME::Types.

Of course, mailcap only gets you Unix systems. Or does OS X use
mailcaps? I imagine for Windows you would need some hideous registry
parsing thing that I'm not even close to writing...

---

From: [email-masked] (William Morgan)
Date: Tue, 15 Jan 2008 19:39:55 -0800

Reformatted excerpts from William Morgan's message of 2008-01-15:
> Or does OS X use mailcaps?

Hm, I think you answered this in your original email.

---

From: [email-masked] (William Morgan)
Date: Tue, 15 Jan 2008 20:06:01 -0800

> Marcus just submitted is a good example.

Something like this. Then you can move all the quick_look and
explicit_view stuff in your original email into a mime-view.rb hook.

diff --git a/lib/sup/message-chunks.rb b/lib/sup/message-chunks.rb
index 08dcf27..8e5def9 100644
--- a/lib/sup/message-chunks.rb
+++ b/lib/sup/message-chunks.rb
@@ -53,6 +53,16 @@ Variables:
 Return value:
   The decoded text of the attachment, or nil if not decoded.
 EOS
+
+    HookManager.register "mime-view", <<EOS
+Executes when viewing a MIME attachment, i.e., launching a separate
+viewer program.
+Variables:
+   content_type: the content-type of the attachment
+       filename: the filename of the attachment as saved to disk
+Return value:
+  True if the viewing was successful, false otherwise.
+EOS
 #' stupid ruby-mode
 
     ## raw_content is the post-MIME-decode content. this is used for
@@ -105,12 +115,18 @@ EOS
     def expandable?; !viewable? end
     def initial_state; :open end
     def viewable?; @lines.nil? end
-    def view!
-      path = write_to_disk
+    def view_default! path
       system "/usr/bin/run-mailcap --action=view #{@content_type}:#{path} > /dev/null 2> /dev/null"
       $? == 0
     end
 
+    def view!
+      path = write_to_disk
+      ret = HookManager.run "mime-view", :content_type => @content_type,
+                                         :filename => path
+      view_default! path unless ret
+    end
+
     def write_to_disk
       file = Tempfile.new(@filename || "sup-attachment")
       file.print @raw_content