diff --git a/Gemfile.lock b/Gemfile.lock
index ff5e238b0ff..562f3a91979 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -486,4 +486,4 @@ DEPENDENCIES
unicorn
BUNDLED WITH
- 1.10.2
+ 1.10.3
diff --git a/app/models/post.rb b/app/models/post.rb
index 281b6a3d4e9..0dbe2c6c3b4 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -6,6 +6,7 @@ require_dependency 'enum'
require_dependency 'post_analyzer'
require_dependency 'validators/post_validator'
require_dependency 'plugin/filter'
+require_dependency 'email_cook'
require 'archetype'
require 'digest/sha1'
@@ -76,7 +77,7 @@ class Post < ActiveRecord::Base
end
def self.cook_methods
- @cook_methods ||= Enum.new(:regular, :raw_html)
+ @cook_methods ||= Enum.new(:regular, :raw_html, :email)
end
def self.find_by_detail(key, value)
@@ -161,16 +162,20 @@ class Post < ActiveRecord::Base
# case we can skip the rendering pipeline.
return raw if cook_method == Post.cook_methods[:raw_html]
- # Default is to cook posts
- cooked = if !self.user || SiteSetting.tl3_links_no_follow || !self.user.has_trust_level?(TrustLevel[3])
- post_analyzer.cook(*args)
- else
- # At trust level 3, we don't apply nofollow to links
- cloned = args.dup
- cloned[1] ||= {}
- cloned[1][:omit_nofollow] = true
- post_analyzer.cook(*cloned)
- end
+ cooked = nil
+ if cook_method == Post.cook_methods[:email]
+ cooked = EmailCook.new(raw).cook
+ else
+ cooked = if !self.user || SiteSetting.tl3_links_no_follow || !self.user.has_trust_level?(TrustLevel[3])
+ post_analyzer.cook(*args)
+ else
+ # At trust level 3, we don't apply nofollow to links
+ cloned = args.dup
+ cloned[1] ||= {}
+ cloned[1][:omit_nofollow] = true
+ post_analyzer.cook(*cloned)
+ end
+ end
new_cooked = Plugin::Filter.apply(:after_post_cook, self, cooked)
diff --git a/lib/email_cook.rb b/lib/email_cook.rb
new file mode 100644
index 00000000000..33693190cbb
--- /dev/null
+++ b/lib/email_cook.rb
@@ -0,0 +1,36 @@
+# A very simple formatter for imported emails
+class EmailCook
+
+ def initialize(raw)
+ @raw = raw
+ end
+
+ def cook
+ result = ""
+
+ in_quote = false
+ quote_buffer = ""
+ @raw.each_line do |l|
+
+ if l =~ /^\s*>/
+ in_quote = true
+ quote_buffer << l.sub(/^[\s>]*/, '') << "
"
+ elsif in_quote
+ result << "
#{quote_buffer}" + quote_buffer = "" + in_quote = false + else + result << l << "
#{quote_buffer}" + end + + result.gsub!(/(