Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
* [sup-talk] missing run-mailcap
@ 2008-01-22 16:40 John Bent
  2008-01-22 22:33 ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-22 16:40 UTC (permalink / raw)


I saw some earlier discussion in the list archives about a missing run-mailcap
script.  I had this same problem too on my mac (10.4.11 Power PC).  It's sort
of a kluge but I dealt with this by writing my own /usr/bin/run-mailcap which
just does a simple hash lookup of mime type to rename the file with an
appropriate extension and then uses the mac 'open' command.  I haven't fully
populated the hash table yet; I'll do that lazily.  If the 'file' command
could return an extension, I'd be tempted to use it and avoid the hash table
but the best it can do is return the mime-type which we already know.  Or if
there was some way to pass the mime-type to the 'open' command, that'd be
better too.  But at least this works...

===========================================================================

 
#! /usr/bin/perl

my %mimes = (
    'image/jpeg'    => 'jpg',
    'text/html'     => 'html',
);

my ( $type, $file ) = split( /:/, $ARGV[1] );
Log( "$type - $file\n" );
my $extension = $mimes{$type};
if ( ! defined $extension ) {
    Log( "Unknown type $type\n" );
} else {
    rename( $file, "$file.$extension" );
    Log( "open $file.$extension\n" );
    system( "open $file.$extension" );
}

#######################################################################

my $log_fd;
sub
Log {
    if ( ! defined $log_fd ) {
        open LOG, ">>/tmp/mailcap.log" or warn "open log: $!\n";
        $log_fd = \*LOG;
    }
    #print "@_";
    print LOG "@_";
}


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-22 16:40 [sup-talk] missing run-mailcap John Bent
@ 2008-01-22 22:33 ` John Bent
  2008-01-24 21:40   ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-22 22:33 UTC (permalink / raw)


Excerpts from John Bent's message of Tue Jan 22 09:40:05 -0700 2008:
> I saw some earlier discussion in the list archives about a missing run-mailcap
> script.  I had this same problem too on my mac (10.4.11 Power PC).  It's sort
> of a kluge but I dealt with this by writing my own /usr/bin/run-mailcap which
> just does a simple hash lookup of mime type to rename the file with an
> appropriate extension and then uses the mac 'open' command.  I haven't fully
> populated the hash table yet; I'll do that lazily.  If the 'file' command
> could return an extension, I'd be tempted to use it and avoid the hash table
> but the best it can do is return the mime-type which we already know.  Or if
> there was some way to pass the mime-type to the 'open' command, that'd be
> better too.  But at least this works...
> 

Here's a cleaner version that doesn't have anything hard-coded but relies
on the 'find' command and the MIME::Types perl module:

============================================================================


#! /opt/local/bin/perl

use MIME::Types;

my ( $type, $file ) = split( /:/, $ARGV[1] );
if ( $type eq 'application/octet-stream' ) {
    $type = findType( $file );
}
my $extension = findExtension( $type );

my $newfile    = "$file.$extension";
my $command    = "open $newfile";
rename( $file, $newfile );
Log( "Extension for $type is $extension -> $command\n" );
system( $command ); 

#######################################################################

sub
findExtension {
    my $mimetypes           = MIME::Types->new;
    my MIME::Type $mimetype = $mimetypes->type($type);
    my @extensions          = $mimetype->extensions;
    return $extensions[0];
}

sub
findType {
    my $file = shift;
    my $type = `file -i -b $file`;
    chomp( $type );
    return $type;
}

my $log_fd;
sub
Log {
    if ( ! defined $log_fd ) {
        open LOG, ">>/tmp/mailcap.log" or warn "open log: $!\n";
        $log_fd = \*LOG;
    }
    #print "@_";
    print LOG "@_";
}


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-22 22:33 ` John Bent
@ 2008-01-24 21:40   ` John Bent
  2008-01-24 23:26     ` William Morgan
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-24 21:40 UTC (permalink / raw)


Excerpts from John Bent's message of Tue Jan 22 15:33:22 -0700 2008:
> Excerpts from John Bent's message of Tue Jan 22 09:40:05 -0700 2008:
> > I saw some earlier discussion in the list archives about a missing run-mailcap
> > script.  I had this same problem too on my mac (10.4.11 Power PC).  It's sort
> > of a kluge but I dealt with this by writing my own /usr/bin/run-mailcap which
> > just does a simple hash lookup of mime type to rename the file with an
> > appropriate extension and then uses the mac 'open' command.  

When sup opens an attachment, it copies the attachment to 
/tmp/sup-attachment-ID and then calls run-mailcap.  But sup knows what name
the file was attached as.  Could sup save the attachment to /tmp/filename?
Or at least append the extension?  Although my preference would be to use
the fullname which is presumably what the file is named on the originating
machine.  Often I'll open an attachment first and if I like it, then I'll save
it using whichever viewer it's in (e.g. someone sends me an MS Word, I open it
in word, and then save it through word).  I then get a dialog about how to
save it which has as a starting point the current path which is 
/tmp/sup-attachment-ID which I'll always have to change.  But if it was the
original attached name, I'd probably often be happy and would prefer 
to keep that name.  

By the way, sup works so well that I've now imported my old school emails
which I'd previously given up on.  I now have blazing fast search over my
last ten years of email!


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-24 21:40   ` John Bent
@ 2008-01-24 23:26     ` William Morgan
  2008-01-24 23:52       ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: William Morgan @ 2008-01-24 23:26 UTC (permalink / raw)


Reformatted excerpts from John Bent's message of 2008-01-24:
> When sup opens an attachment, it copies the attachment to
> /tmp/sup-attachment-ID and then calls run-mailcap.  But sup knows what
> name the file was attached as.  Could sup save the attachment to
> /tmp/filename?  Or at least append the extension?

In 0.4 you should see the extension being preserved. Let me know if
that's not the case.

So you should be able to simply call "open" on that file, rather than
having to use your Perl script.

> Although my preference would be to use the fullname which is
> presumably what the file is named on the originating machine. 

This might be doable. I've been using the tempfile Ruby library to
handle these files, which does two things: it obfuscates the name to
avoid collisions, and it ensures the file is deleted once all references
to the variable are garbage-collected. It might not be hard to replace
this with something that only does the latter behavior. I might be able
to programmatically delete the files too, instead of waiting for garbage
collection.

> By the way, sup works so well that I've now imported my old school
> emails which I'd previously given up on.  I now have blazing fast
> search over my last ten years of email!

Excellent!

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-24 23:26     ` William Morgan
@ 2008-01-24 23:52       ` John Bent
  2008-01-25  0:17         ` William Morgan
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-24 23:52 UTC (permalink / raw)


Excerpts from William Morgan's message of Thu Jan 24 16:26:20 -0700 2008:
> Reformatted excerpts from John Bent's message of 2008-01-24:
> > When sup opens an attachment, it copies the attachment to
> > /tmp/sup-attachment-ID and then calls run-mailcap.  But sup knows what
> > name the file was attached as.  Could sup save the attachment to
> > /tmp/filename?  Or at least append the extension?
> 
> In 0.4 you should see the extension being preserved. Let me know if
> that's not the case.
> 
> So you should be able to simply call "open" on that file, rather than
> having to use your Perl script.
> 
Yep, just like you say.  Although, I think I do still need to have something
in /usr/bin/run-mailcap that parses 
"--action=view application/vnd.ms-powerpoint:/tmp/foo.ppt," and then execs
"open /tmp/foo.ppt."  But that's much better than having to guess an extension
from the mime-type.

John


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-24 23:52       ` John Bent
@ 2008-01-25  0:17         ` William Morgan
  2008-01-25  0:57           ` Grant Hollingworth
  0 siblings, 1 reply; 18+ messages in thread
From: William Morgan @ 2008-01-25  0:17 UTC (permalink / raw)


Reformatted excerpts from John Bent's message of 2008-01-24:
> Yep, just like you say.  Although, I think I do still need to have
> something in /usr/bin/run-mailcap that parses "--action=view
> application/vnd.ms-powerpoint:/tmp/foo.ppt," and then execs "open
> /tmp/foo.ppt."  But that's much better than having to guess an
> extension from the mime-type.

You don't even need to fake run-mailcap. You should just be able to have
a one-line ~/.sup/hooks/mime-view.rb that looks like:

  system "open #{filename}"

If open is a standard OS X command, then I will add some code to
auto-detect it at some point.

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-25  0:17         ` William Morgan
@ 2008-01-25  0:57           ` Grant Hollingworth
  2008-01-25  4:40             ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: Grant Hollingworth @ 2008-01-25  0:57 UTC (permalink / raw)


* William Morgan [Thu Jan 24 19:17:17 -0500 2008]:
> If open is a standard OS X command, then I will add some code to
> auto-detect it at some point.

It is. Unfortunately, you can tell it which application to use, but
not which MIME type.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-25  0:57           ` Grant Hollingworth
@ 2008-01-25  4:40             ` John Bent
  2008-01-26  0:35               ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-25  4:40 UTC (permalink / raw)


Excerpts from Grant Hollingworth's message of Thu Jan 24 17:57:50 -0700 2008:
> * William Morgan [Thu Jan 24 19:17:17 -0500 2008]:
> > If open is a standard OS X command, then I will add some code to
> > auto-detect it at some point.
> 
> It is. Unfortunately, you can tell it which application to use, but
> not which MIME type.

True, but if it has the correct extension (which it does in 0.4), you 
don't need to tell it anything.  

That's great that I don't need to fake run-mailcap since there's a hook.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-25  4:40             ` John Bent
@ 2008-01-26  0:35               ` John Bent
  2008-01-30 17:29                 ` William Morgan
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-26  0:35 UTC (permalink / raw)


Excerpts from John Bent's message of Thu Jan 24 21:40:08 -0700 2008:
> Excerpts from Grant Hollingworth's message of Thu Jan 24 17:57:50 -0700 2008:
> > * William Morgan [Thu Jan 24 19:17:17 -0500 2008]:
> > > If open is a standard OS X command, then I will add some code to
> > > auto-detect it at some point.
> > 
> > It is. Unfortunately, you can tell it which application to use, but
> > not which MIME type.
> 
> True, but if it has the correct extension (which it does in 0.4), you 
> don't need to tell it anything.  
> 
> That's great that I don't need to fake run-mailcap since there's a hook.
>
The hook works (system "open \'#{filename}\'"), but sup thinks it is failing.
I get a message saying view failed, displaying as text.  Maybe it should
be (! system "open \'#{filename}\'") ?  Also, I added the \' because I noticed
before that sometimes filename has spaces in it and then open won't work.

John


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-26  0:35               ` John Bent
@ 2008-01-30 17:29                 ` William Morgan
  2008-01-30 17:54                   ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: William Morgan @ 2008-01-30 17:29 UTC (permalink / raw)


Reformatted excerpts from John Bent's message of 2008-01-25:
> The hook works (system "open \'#{filename}\'"), but sup thinks it is
> failing.  I get a message saying view failed, displaying as text.
> Maybe it should be (! system "open \'#{filename}\'") ?

Weird. Kernel#system returns true if the command succeeded and false
otherwise, so it should work as is.

You could try:
  system "open '#{filename}'"
  $?.success?

But there's no reason that should work and the original shouldn't. You
don't have a debugging print statement or anything like that as the
final line of the hook, do you?

> Also, I added the \' because I noticed before that sometimes filename
> has spaces in it and then open won't work.

Good point. Backslashes optional though. :)

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-30 17:29                 ` William Morgan
@ 2008-01-30 17:54                   ` John Bent
  2008-01-30 18:26                     ` William Morgan
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-30 17:54 UTC (permalink / raw)


Excerpts from William Morgan's message of Wed Jan 30 10:29:30 -0700 2008:
> Reformatted excerpts from John Bent's message of 2008-01-25:
> > The hook works (system "open \'#{filename}\'"), but sup thinks it is
> > failing.  I get a message saying view failed, displaying as text.
> > Maybe it should be (! system "open \'#{filename}\'") ?
> 
> Weird. Kernel#system returns true if the command succeeded and false
> otherwise, so it should work as is.
> 
> You could try:
>   system "open '#{filename}'"
>   $?.success?
> 
same problem.  the attachment is successfully opened but sup thinks it
failed and sup displays it as text.

> But there's no reason that should work and the original shouldn't. You
> don't have a debugging print statement or anything like that as the
> final line of the hook, do you?
> 
nope, just the exact two lines you supplied above.

I wonder if the problem is that system isn't correctly understanding
the return value from open?  

tangerine:~>open K.png
tangerine:~>echo $?
0

hmmm, I would think 0 is the expected success value?  How could I reverse
true and false?  I'm curious to try that but 
! $?.success? 
$?.success?true:false 
don't work, they error out and sup defaults to run-mailcap.

John


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-30 17:54                   ` John Bent
@ 2008-01-30 18:26                     ` William Morgan
  2008-01-30 18:35                       ` John Bent
  0 siblings, 1 reply; 18+ messages in thread
From: William Morgan @ 2008-01-30 18:26 UTC (permalink / raw)


Reformatted excerpts from John Bent's message of 2008-01-30:
> I wonder if the problem is that system isn't correctly understanding
> the return value from open?  

Try with irb:

  $ irb
  irb(main):001:0> system "ls"
  [...]
  => true
  irb(main):002:0> system "asdfasdfa"
  => false

What happens with open?

> hmmm, I would think 0 is the expected success value?  How could I reverse
> true and false?  I'm curious to try that but 
> ! $?.success? 

This one should work.

As a sanity check:
  system "open '#{filename}'"
  true

Should always count as a success. Is that true?

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-30 18:26                     ` William Morgan
@ 2008-01-30 18:35                       ` John Bent
  2008-02-03  3:01                         ` William Morgan
  0 siblings, 1 reply; 18+ messages in thread
From: John Bent @ 2008-01-30 18:35 UTC (permalink / raw)


Excerpts from William Morgan's message of Wed Jan 30 11:26:03 -0700 2008:
> Reformatted excerpts from John Bent's message of 2008-01-30:
> > I wonder if the problem is that system isn't correctly understanding
> > the return value from open?  
> 
> Try with irb:
> 
>   $ irb
>   irb(main):001:0> system "ls"
>   [...]
>   => true
>   irb(main):002:0> system "asdfasdfa"
>   => false
> 
> What happens with open?
> 
=> true

> > hmmm, I would think 0 is the expected success value?  How could I reverse
> > true and false?  I'm curious to try that but 
> > ! $?.success? 
> 
> This one should work.
> 
> As a sanity check:
>   system "open '#{filename}'"
>   true
> 
> Should always count as a success. Is that true?
> 
same problem.  It opens successfully, but sup thinks it doesn't and
displays it as text.  All I see in the log buffer is:
 hook: read 'mime-view' from /Users/johnbent/.sup/hooks/mime-view.rb

Ah.  But
system "open '#{filename}'"
false

works perfectly!  (not sure that's expected but I'm happy!)

Thanks,

John


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-30 18:35                       ` John Bent
@ 2008-02-03  3:01                         ` William Morgan
  0 siblings, 0 replies; 18+ messages in thread
From: William Morgan @ 2008-02-03  3:01 UTC (permalink / raw)


Reformatted excerpts from John Bent's message of 2008-01-30:
> Ah.  But
> system "open '#{filename}'"
> false
> 
> works perfectly!  (not sure that's expected but I'm happy!)

The only explanation is that you are living in a bizarro world where
true is false and false is true.

Seriously. That makes no sense.

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-05  0:13 ` William Morgan
@ 2008-01-05  1:01   ` Giorgio Lando
  0 siblings, 0 replies; 18+ messages in thread
From: Giorgio Lando @ 2008-01-05  1:01 UTC (permalink / raw)


> I don't have an actual answer for this, but if there's another option
> for handling MIME attachments on your system, I'm happy to include it.
> Run-mailcap was just what I had on hand at the time I was developing.
> It doesn't exist on Macs either.
> 
> I'm just trying to avoid writing my own code to do this, since about a
> million other people have written that code already, and since it's
> pretty OS-dependent.

Do not mind, I am going to package a standalone run-mailcap for my
distro (it will be a sup depo when I find the time to package sup &
friends)
Giorgio 


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-04 23:45 Giorgio Lando
  2008-01-04 23:59 ` Giorgio Lando
@ 2008-01-05  0:13 ` William Morgan
  2008-01-05  1:01   ` Giorgio Lando
  1 sibling, 1 reply; 18+ messages in thread
From: William Morgan @ 2008-01-05  0:13 UTC (permalink / raw)


Excerpts from Giorgio Lando's message of Fri Jan 04 15:45:05 -0800 2008:
> Grepping in the sup directory, I see that it uses a
> /usr/bin/run-mailcap. I have not this in my linux system (archlinux),
> and I guess that it is something debian specific (I have not in my
> freebsd, and I do not remember to have seen it in my slackware system
> - not sure of this).  I do not know how mailcap is used here (mutt
> resorts to it fine).
> 
> Do you know where I could find run-mailcap?

I don't have an actual answer for this, but if there's another option
for handling MIME attachments on your system, I'm happy to include it.
Run-mailcap was just what I had on hand at the time I was developing.
It doesn't exist on Macs either.

I'm just trying to avoid writing my own code to do this, since about a
million other people have written that code already, and since it's
pretty OS-dependent.

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
  2008-01-04 23:45 Giorgio Lando
@ 2008-01-04 23:59 ` Giorgio Lando
  2008-01-05  0:13 ` William Morgan
  1 sibling, 0 replies; 18+ messages in thread
From: Giorgio Lando @ 2008-01-04 23:59 UTC (permalink / raw)


> Do you know where I could find run-mailcap?
> Giorgio Lando

Do not mind about this, I have been able to extract it from the debian package
and now it works
Giorgio Lando


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [sup-talk] missing run-mailcap
@ 2008-01-04 23:45 Giorgio Lando
  2008-01-04 23:59 ` Giorgio Lando
  2008-01-05  0:13 ` William Morgan
  0 siblings, 2 replies; 18+ messages in thread
From: Giorgio Lando @ 2008-01-04 23:45 UTC (permalink / raw)


A further problem: I am currently unable to convince sup to run my ~/.mailcap entries for attachment 
(the mimetypes is properly identified).

Grepping in the sup directory, I see that it uses a /usr/bin/run-mailcap. I have not this in my linux 
system (archlinux), and I guess that it is something debian specific (I have not in my freebsd, 
and I do not remember to have seen it in my slackware system - not sure of this).
I do not know how mailcap is used here (mutt resorts to it fine).

Do you know where I could find run-mailcap?
Giorgio Lando


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2008-02-03  3:01 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-22 16:40 [sup-talk] missing run-mailcap John Bent
2008-01-22 22:33 ` John Bent
2008-01-24 21:40   ` John Bent
2008-01-24 23:26     ` William Morgan
2008-01-24 23:52       ` John Bent
2008-01-25  0:17         ` William Morgan
2008-01-25  0:57           ` Grant Hollingworth
2008-01-25  4:40             ` John Bent
2008-01-26  0:35               ` John Bent
2008-01-30 17:29                 ` William Morgan
2008-01-30 17:54                   ` John Bent
2008-01-30 18:26                     ` William Morgan
2008-01-30 18:35                       ` John Bent
2008-02-03  3:01                         ` William Morgan
  -- strict thread matches above, loose matches on Subject: below --
2008-01-04 23:45 Giorgio Lando
2008-01-04 23:59 ` Giorgio Lando
2008-01-05  0:13 ` William Morgan
2008-01-05  1:01   ` Giorgio Lando

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox