lib/sup/modes/edit-message-mode.rb in sup-0.10.2 vs lib/sup/modes/edit-message-mode.rb in sup-0.11

- old
+ new

@@ -1,14 +1,10 @@ require 'tempfile' require 'socket' # just for gethostname! require 'pathname' require 'rmail' -# from jcode.rb, not included in ruby 1.9 -PATTERN_UTF8 = '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]' -RE_UTF8 = Regexp.new(PATTERN_UTF8, 0, 'n') - module Redwood class SendmailCommandFailed < StandardError; end class EditMessageMode < LineCursorMode @@ -38,10 +34,32 @@ body: an array of lines of body text. Return value: none EOS + HookManager.register "mentions-attachments", <<EOS +Detects if given message mentions attachments the way it is probable +that there should be files attached to the message. +Variables: + header: a hash of headers. See 'signature' hook for documentation. + body: an array of lines of body text. +Return value: + True if attachments are mentioned. +EOS + + HookManager.register "crypto-mode", <<EOS +Modifies cryptography settings based on header and message content, before +editing a new message. This can be used to set, for example, default cryptography +settings. +Variables: + header: a hash of headers. See 'signature' hook for documentation. + body: an array of lines of body text. + crypto_selector: the UI element that controls the current cryptography setting. +Return value: + none +EOS + attr_reader :status attr_accessor :body, :header bool_reader :edited register_keymap do |k| @@ -90,10 +108,13 @@ HorizontalSelector.new "Crypto:", [:none] + CryptoManager::OUTGOING_MESSAGE_OPERATIONS.keys, ["None"] + CryptoManager::OUTGOING_MESSAGE_OPERATIONS.values end add_selector @crypto_selector if @crypto_selector HookManager.run "before-edit", :header => @header, :body => @body + if @crypto_selector + HookManager.run "crypto-mode", :header => @header, :body => @body, :crypto_selector => @crypto_selector + end super opts regen_text end @@ -191,20 +212,20 @@ string.gsub!(/ /,'_') # .. translate space to underscores "=?utf-8?q?#{string}?=" end def mime_encode_subject string - return string unless string.match(RE_UTF8) + return string if string.ascii_only? mime_encode string end RE_ADDRESS = /(.+)( <.*@.*>)/ # Encode "bælammet mitt <user@example.com>" into # "=?utf-8?q?b=C3=A6lammet_mitt?= <user@example.com> def mime_encode_address string - return string unless string.match(RE_UTF8) + return string if string.ascii_only? string.sub(RE_ADDRESS) { |match| mime_encode($1) + $2 } end def move_cursor_left if curpos < @selectors.length @@ -442,10 +463,14 @@ def sanitize_body body body.gsub(/^From /, ">From ") end def mentions_attachments? - @body.any? { |l| l =~ /^[^>]/ && l =~ /\battach(ment|ed|ing|)\b/i } + if HookManager.enabled? "mentions-attachments" + HookManager.run "mentions-attachments", :header => @header, :body => @body + else + @body.any? { |l| l =~ /^[^>]/ && l =~ /\battach(ment|ed|ing|)\b/i } + end end def top_posting? @body.join("\n") =~ /(\S+)\s*Excerpts from.*\n(>.*\n)+\s*\Z/ end