PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/sup/message-chunks.rb

https://github.com/shenson/sup
Ruby | 271 lines | 198 code | 35 blank | 38 comment | 22 complexity | e31073094c388428e297ab5922d06da7 MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'tempfile'
  2. ## Here we define all the "chunks" that a message is parsed
  3. ## into. Chunks are used by ThreadViewMode to render a message. Chunks
  4. ## are used for both MIME stuff like attachments, for Sup's parsing of
  5. ## the message body into text, quote, and signature regions, and for
  6. ## notices like "this message was decrypted" or "this message contains
  7. ## a valid signature"---basically, anything we want to differentiate
  8. ## at display time.
  9. ##
  10. ## A chunk can be inlineable, expandable, or viewable. If it's
  11. ## inlineable, #color and #lines are called and the output is treated
  12. ## as part of the message text. This is how Text and one-line Quotes
  13. ## and Signatures work.
  14. ##
  15. ## If it's not inlineable but is expandable, #patina_color and
  16. ## #patina_text are called to generate a "patina" (a one-line widget,
  17. ## basically), and the user can press enter to toggle the display of
  18. ## the chunk content, which is generated from #color and #lines as
  19. ## above. This is how Quote, Signature, and most widgets
  20. ## work. Exandable chunks can additionally define #initial_state to be
  21. ## :open if they want to start expanded (default is to start collapsed).
  22. ##
  23. ## If it's not expandable but is viewable, a patina is displayed using
  24. ## #patina_color and #patina_text, but no toggling is allowed. Instead,
  25. ## if #view! is defined, pressing enter on the widget calls view! and
  26. ## (if that returns false) #to_s. Otherwise, enter does nothing. This
  27. ## is how non-inlineable attachments work.
  28. ##
  29. ## Independent of all that, a chunk can be quotable, in which case it's
  30. ## included as quoted text during a reply. Text, Quotes, and mime-parsed
  31. ## attachments are quotable; Signatures are not.
  32. ## monkey-patch time: make temp files have the right extension
  33. class Tempfile
  34. def make_tmpname basename, n
  35. sprintf '%d-%d-%s', $$, n, basename
  36. end
  37. end
  38. module Redwood
  39. module Chunk
  40. class Attachment
  41. HookManager.register "mime-decode", <<EOS
  42. Decodes a MIME attachment into text form. The text will be displayed
  43. directly in Sup. For attachments that you wish to use a separate program
  44. to view (e.g. images), you should use the mime-view hook instead.
  45. Variables:
  46. content_type: the content-type of the attachment
  47. charset: the charset of the attachment, if applicable
  48. filename: the filename of the attachment as saved to disk
  49. sibling_types: if this attachment is part of a multipart MIME attachment,
  50. an array of content-types for all attachments. Otherwise,
  51. the empty array.
  52. Return value:
  53. The decoded text of the attachment, or nil if not decoded.
  54. EOS
  55. HookManager.register "mime-view", <<EOS
  56. Views a non-text MIME attachment. This hook allows you to run
  57. third-party programs for attachments that require such a thing (e.g.
  58. images). To instead display a text version of the attachment directly in
  59. Sup, use the mime-decode hook instead.
  60. Note that by default (at least on systems that have a run-mailcap command),
  61. Sup uses the default mailcap handler for the attachment's MIME type. If
  62. you want a particular behavior to be global, you may wish to change your
  63. mailcap instead.
  64. Variables:
  65. content_type: the content-type of the attachment
  66. filename: the filename of the attachment as saved to disk
  67. Return value:
  68. True if the viewing was successful, false otherwise. If false, calling
  69. /usr/bin/run-mailcap will be tried.
  70. EOS
  71. #' stupid ruby-mode
  72. ## raw_content is the post-MIME-decode content. this is used for
  73. ## saving the attachment to disk.
  74. attr_reader :content_type, :filename, :lines, :raw_content
  75. bool_reader :quotable
  76. def initialize content_type, filename, encoded_content, sibling_types
  77. @content_type = content_type.downcase
  78. @filename = filename
  79. @quotable = false # changed to true if we can parse it through the
  80. # mime-decode hook, or if it's plain text
  81. @raw_content =
  82. if encoded_content.body
  83. encoded_content.decode
  84. else
  85. "For some bizarre reason, RubyMail was unable to parse this attachment.\n"
  86. end
  87. text = case @content_type
  88. when /^text\/plain\b/
  89. @raw_content
  90. else
  91. HookManager.run "mime-decode", :content_type => content_type,
  92. :filename => lambda { write_to_disk },
  93. :charset => encoded_content.charset,
  94. :sibling_types => sibling_types
  95. end
  96. @lines = nil
  97. if text
  98. text = text.transcode(encoded_content.charset || $encoding)
  99. @lines = text.gsub("\r\n", "\n").gsub(/\t/, " ").gsub(/\r/, "").split("\n")
  100. @quotable = true
  101. end
  102. end
  103. def color; :none end
  104. def patina_color; :attachment_color end
  105. def patina_text
  106. if expandable?
  107. "Attachment: #{filename} (#{lines.length} lines)"
  108. else
  109. "Attachment: #{filename} (#{content_type}; #{@raw_content.size.to_human_size})"
  110. end
  111. end
  112. ## an attachment is exapndable if we've managed to decode it into
  113. ## something we can display inline. otherwise, it's viewable.
  114. def inlineable?; false end
  115. def expandable?; !viewable? end
  116. def initial_state; :open end
  117. def viewable?; @lines.nil? end
  118. def view_default! path
  119. case Config::CONFIG['arch']
  120. when /darwin/
  121. cmd = "open '#{path}'"
  122. else
  123. cmd = "/usr/bin/run-mailcap --action=view '#{@content_type}:#{path}'"
  124. end
  125. debug "running: #{cmd.inspect}"
  126. BufferManager.shell_out(cmd)
  127. $? == 0
  128. end
  129. def view!
  130. path = write_to_disk
  131. ret = HookManager.run "mime-view", :content_type => @content_type,
  132. :filename => path
  133. ret || view_default!(path)
  134. end
  135. def write_to_disk
  136. file = Tempfile.new(@filename || "sup-attachment")
  137. file.print @raw_content
  138. file.close
  139. file.path
  140. end
  141. ## used when viewing the attachment as text
  142. def to_s
  143. @lines || @raw_content
  144. end
  145. end
  146. class Text
  147. attr_reader :lines
  148. def initialize lines
  149. @lines = lines
  150. ## trim off all empty lines except one
  151. @lines.pop while @lines.length > 1 && @lines[-1] =~ /^\s*$/ && @lines[-2] =~ /^\s*$/
  152. end
  153. def inlineable?; true end
  154. def quotable?; true end
  155. def expandable?; false end
  156. def viewable?; false end
  157. def color; :none end
  158. end
  159. class Quote
  160. attr_reader :lines
  161. def initialize lines
  162. @lines = lines
  163. end
  164. def inlineable?; @lines.length == 1 end
  165. def quotable?; true end
  166. def expandable?; !inlineable? end
  167. def viewable?; false end
  168. def patina_color; :quote_patina_color end
  169. def patina_text; "(#{lines.length} quoted lines)" end
  170. def color; :quote_color end
  171. end
  172. class Signature
  173. attr_reader :lines
  174. def initialize lines
  175. @lines = lines
  176. end
  177. def inlineable?; @lines.length == 1 end
  178. def quotable?; false end
  179. def expandable?; !inlineable? end
  180. def viewable?; false end
  181. def patina_color; :sig_patina_color end
  182. def patina_text; "(#{lines.length}-line signature)" end
  183. def color; :sig_color end
  184. end
  185. class EnclosedMessage
  186. attr_reader :lines
  187. def initialize from, to, cc, date, subj
  188. @from = from ? "unknown sender" : from.full_adress
  189. @to = to ? "" : to.map { |p| p.full_address }.join(", ")
  190. @cc = cc ? "" : cc.map { |p| p.full_address }.join(", ")
  191. if date
  192. @date = date.rfc822
  193. else
  194. @date = ""
  195. end
  196. @subj = subj
  197. @lines = "\nFrom: #{from}\n"
  198. @lines += "To: #{to}\n"
  199. if !cc.empty?
  200. @lines += "Cc: #{cc}\n"
  201. end
  202. @lines += "Date: #{date}\n"
  203. @lines += "Subject: #{subj}\n\n"
  204. end
  205. def inlineable?; false end
  206. def quotable?; false end
  207. def expandable?; true end
  208. def initial_state; :closed end
  209. def viewable?; false end
  210. def patina_color; :generic_notice_patina_color end
  211. def patina_text; "Begin enclosed message sent on #{@date}" end
  212. def color; :quote_color end
  213. end
  214. class CryptoNotice
  215. attr_reader :lines, :status, :patina_text
  216. def initialize status, description, lines=[]
  217. @status = status
  218. @patina_text = description
  219. @lines = lines
  220. end
  221. def patina_color
  222. case status
  223. when :valid then :cryptosig_valid_color
  224. when :invalid then :cryptosig_invalid_color
  225. else :cryptosig_unknown_color
  226. end
  227. end
  228. def color; patina_color end
  229. def inlineable?; false end
  230. def quotable?; false end
  231. def expandable?; !@lines.empty? end
  232. def viewable?; false end
  233. end
  234. end
  235. end