]> git.sven.stormbind.net Git - sven/vym.git/blob - scripts/vym-addmail.rb
New upstream version 2.9.22
[sven/vym.git] / scripts / vym-addmail.rb
1 #!/usr/bin/ruby
2
3 # This ruby script can be called from mutt to create a branch in vym
4 # with the mail currently visible in mutts pager.
5 # The mail itself will be added as note to a branch, which has the
6 # heading "YYYY-MM-DD Email: $SUBJECT"
7 #
8 # vym is notified using DBUS, which usually only is available on Linux
9 # systems
10 #
11 # Example entry for .muttrc:
12 # macro pager Y "|/home/uwe/vym/branches/develop/scripts/vym-addmail.rb\n" "Add note to production inst ance of vym"
13 #
14 # vym itself needs to be starting using the name option, using  "vym -n production"
15 #
16
17 require File.expand_path("../vym-ruby", __FILE__)
18 require 'tempfile'
19 require 'mail'
20
21 mail_in = ""
22 ARGF.each_line do |line|
23   mail_in << line
24 end
25
26 begin
27   out = Tempfile.new("temp")
28   begin
29     mail = Mail.read_from_string(Mail::Utilities.binary_unsafe_to_crlf(mail_in))
30     puts "Mail is multipart: #{mail.multipart?}"
31
32     # Write header
33     out << "<html><body>"
34     out << "<pre>"
35     out << "Subject: #{mail.subject}\n"
36     out << "From: #{mail.header[:From]}\n"
37     out << "To: #{mail.header[:To]}\n"
38     out << "Cc: #{mail.header[:Cc]}\n"
39     out << "Date: #{mail.date.to_s}\n"
40     out << "</pre>"
41
42     text = ""
43     if mail.multipart? then
44       puts "Warning: multipart mail detected. Only using first part."
45       text = mail.parts.first.decoded
46     else
47       #out <<  mail.body.raw_source.gsub("\n", "<br/>")
48       text = mail.decoded
49     end
50
51     if text.include?("<html")
52       out << text
53     else
54       out << "<p>"
55       text.gsub!("\r\n\r\n", "</p><p>")
56       text.gsub!("\r\n", "")
57       out << text
58       out << "</p>"
59     end
60
61     out << "</body></html>"
62
63     out.rewind
64
65     name = "production"
66     #name = "test"
67
68     vym_mgr = VymManager.new
69     vym_mgr.show_running
70     vym = vym_mgr.find(name)
71
72     if !vym
73       puts "Couldn't find instance named \"#{name}\", please start one:"
74       puts "vym -l -n #{name} -t test/default.vym"
75
76       puts "Currently running vym instances: #{vym_mgr.show_running}"
77       exit
78     end
79
80     # Before doing anything, make sure there is a return value available
81     # Otherwise the script might block     // FIXME
82     #version = vym.version
83
84     puts "Found #{vym.mapCount} maps"
85
86     #puts vym.show_methods
87
88     n = vym.currentMapID()
89     puts "ID of current map: #{n}"
90
91     map = vym.map (n)
92     puts "Map title: #{map.getMapTitle}"
93     puts "Map path:  #{map.getDestPath}"
94     puts "Map name:  #{map.getFileName}"
95
96     map.addBranch()
97     map.selectLatestAdded
98
99     date = mail.date.to_s.gsub!(/T.*$/,"")
100     map.setHeadingPlainText("#{date} Email: #{mail.subject}")
101     map.loadNote(out.path)
102     map.colorBranch("#0000ff")
103   ensure
104     out.close
105     out.unlink
106   end
107
108 end