mail_new_activity: properly implement QP for email subjects

This commit is contained in:
joshua stein 2016-01-11 12:46:41 -06:00
parent ffa1b879da
commit 28a6faab9d

View file

@ -8,32 +8,14 @@ require APP_PATH
Rails.application.require_environment!
class String
def force_to_ascii
# fixup some "smart" quotes and things instead of forcing them to "?" chars
gsub("\u201C", '"').
gsub("\u201D", '"').
gsub("\u2018", "'").
gsub("\u2019", "'").
gsub("\u2013", "-").
gsub("\u2014", "--").
gsub("\u2026", "...").
encode("us-ascii", :invalid => :replace, :undef => :replace,
:replace => "?")
end
def quoted_printable(encoded_word = false)
s = ""
if encoded_word
s << "=?UTF-8?Q?"
end
s << [ self ].pack("M")
s = [ self ].pack("M")
if encoded_word
s << "?="
s.split(/\r?\n/).map{|l| "=?UTF-8?Q?#{l.gsub(/=*$/, "")}?=" }.join("\n\t")
else
s
end
s
end
# like ActionView::Helpers::TextHelper but preserve > and indentation when
@ -55,6 +37,16 @@ class String
end
end
def story_subject(story, prefix = "")
ss = "#{prefix}#{story.title}"
story.tags.sort_by{|t| t.tag }.each do |t|
ss << " [#{t.tag}]"
end
ss.quoted_printable(true)
end
EMAIL_WIDTH = 72
LAST_STORY_KEY = "mailing:last_story_id"
LAST_COMMENT_KEY = "mailing:last_comment_id"
@ -103,9 +95,7 @@ Story.where("id > ? AND is_expired = ?", last_story_id, false).order(:id).each d
mail.puts "Content-Transfer-Encoding: quoted-printable"
mail.puts "Message-ID: <#{s.mailing_list_message_id}>"
mail.puts "Date: " << s.created_at.strftime("%a, %d %b %Y %H:%M:%S %z")
mail.puts "Subject: " << s.title.force_to_ascii <<
s.tags.sort_by{|t| t.tag }.map{|t| " [#{t.tag}]" }.join
mail.puts "Subject: " << story_subject(s)
mail.puts ""
body = []
@ -221,9 +211,7 @@ last_comment_id, false, false).order(:id).each do |c|
end
mail.puts "Date: " << c.created_at.strftime("%a, %d %b %Y %H:%M:%S %z")
mail.puts "Subject: Re: " << c.story.title.force_to_ascii <<
c.story.tags.sort_by{|t| t.tag }.map{|t| " [#{t.tag}]" }.join
mail.puts "Subject: " << story_subject(c.story, "Re: ")
mail.puts ""
body = []