ftp -o - https://jcs.org/move_in | sh -
at master 72 lines 1.4 kB view raw
1#!/usr/bin/env ruby 2# vim:ft=ruby 3# 4# forks and executes the requested program with the temporary file given, 5# allowing mutt to return to its message view while you view the attachment in 6# the program 7# 8# usage in ~/.mailcap: 9# application/*; ~/bin/mutt_bgrun %t %s 10# 11 12require "fileutils" 13require "open3" 14 15mime = ARGV[0] 16orig_file = ARGV[1] 17 18if !orig_file 19 STDERR.puts "usage: #{$0} <mime type> <filename>" 20 exit 1 21end 22 23if mime == "application/octet-stream" 24 if orig_file.to_s.match(/\.pdf$/i) 25 mime = "application/pdf" 26 end 27end 28 29viewer = nil 30if `uname`.strip == "Darwin" 31 viewer = "open" 32else 33 case mime.to_s.downcase 34 when 35 "application/pdf", 36 /^image\// 37 viewer = "firefox" 38 end 39end 40 41if !viewer 42 STDERR.puts "no viewer for type #{mime.inspect}" 43 exit 1 44end 45 46tmp_dir = `mktemp -d`.chomp 47tmp_file = tmp_dir + "/" + File.basename(orig_file).gsub(/[^A-Za-z_\.-]/, "") 48launch_file = tmp_file.dup 49 50if viewer == "firefox" 51 launch_file = "file:///#{launch_file}" 52end 53 54system("cp", orig_file, tmp_file) 55 56Kernel.fork do 57 i, _, _, w = Open3.popen3(viewer, launch_file) 58 i.close 59 60 begin 61 Process.waitpid(w[:pid]) 62 rescue Errno::ECHILD 63 # process ended really quickly, most likely just handed it off to another 64 # process, so wait a bit for it to process the tmp file before removing it 65 sleep 5 66 end 67 68 sleep 5 69 70 File.unlink tmp_file 71 Dir.unlink tmp_dir 72end