start on mailing list interface
This commit is contained in:
parent
ee6a27beb3
commit
fd41bfa566
8 changed files with 501 additions and 9 deletions
165
script/mail_new_activity
Executable file
165
script/mail_new_activity
Executable file
|
|
@ -0,0 +1,165 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
ENV["RAILS_ENV"] ||= "production"
|
||||
|
||||
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
||||
require File.expand_path('../../config/boot', __FILE__)
|
||||
require APP_PATH
|
||||
Rails.application.require_environment!
|
||||
|
||||
class String
|
||||
def force_to_ascii
|
||||
encode("us-ascii", :invalid => :replace, :undef => :replace,
|
||||
:replace => "?")
|
||||
end
|
||||
|
||||
# like ActionView::Helpers::TextHelper but preserve > and indentation when
|
||||
# wrapping lines
|
||||
def word_wrap(len)
|
||||
split("\n").collect do |line|
|
||||
if line.length <= len
|
||||
line
|
||||
elsif m = line.match(/^(> ?| +)(.*)/)
|
||||
ind = m[1]
|
||||
if len - ind.length < 0
|
||||
ind = " "
|
||||
end
|
||||
m[2].gsub(/(.{1,#{len - ind.length}})(\s+|$)/, "#{ind}\\1\n").strip
|
||||
else
|
||||
line.gsub(/(.{1,#{len}})(\s+|$)/, "\\1\n").strip
|
||||
end
|
||||
end * "\n"
|
||||
end
|
||||
end
|
||||
|
||||
EMAIL_WIDTH = 72
|
||||
LAST_STORY_KEY = "mailing:last_story_id"
|
||||
LAST_COMMENT_KEY = "mailing:last_comment_id"
|
||||
|
||||
mailing_list_users = User.where(:mailing_list_enabled => true)
|
||||
|
||||
last_story_id = (Keystore.value_for(LAST_STORY_KEY) || Story.last.id).to_i
|
||||
|
||||
Story.where("id > ?", last_story_id).order(:id).each do |s|
|
||||
s.fetch_story_cache!
|
||||
s.save
|
||||
|
||||
mailing_list_users.each do |u|
|
||||
if (s.tags.map{|t| t.id } & u.tag_filters.map{|t| t.tag_id }).any?
|
||||
next
|
||||
end
|
||||
|
||||
IO.popen([ {}, "/usr/sbin/sendmail", "-i", "-f", "nobody@lobste.rs",
|
||||
u.email ], "w") do |mail|
|
||||
mail.puts "From: #{s.user.username} <#{s.user.username}@lobste.rs>"
|
||||
mail.puts "Reply-To: lobsters-#{u.mailing_list_token}@lobste.rs"
|
||||
mail.puts "To: lobsters-#{u.mailing_list_token}@lobste.rs"
|
||||
mail.puts "X-BeenThere: lobsters-#{u.mailing_list_token}.lobste.rs"
|
||||
mail.puts "List-Id: Lobsters <lobsters-#{u.mailing_list_token}.lobste.rs>"
|
||||
mail.puts "List-Unsubscribe: <https://lobste.rs/settings>"
|
||||
mail.puts "Precedence: list"
|
||||
mail.puts "Content-Type: text/plain; charset=\"us-ascii\""
|
||||
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 ""
|
||||
|
||||
if s.description.present?
|
||||
mail.puts s.description.to_s.force_to_ascii.word_wrap(EMAIL_WIDTH)
|
||||
end
|
||||
|
||||
if s.url.present?
|
||||
if s.description.present?
|
||||
mail.puts ""
|
||||
end
|
||||
|
||||
mail.puts "Via: #{s.url}"
|
||||
|
||||
if s.story_cache.present?
|
||||
mail.puts ""
|
||||
mail.puts s.story_cache.to_s.force_to_ascii.word_wrap(EMAIL_WIDTH)
|
||||
end
|
||||
end
|
||||
|
||||
mail.puts ""
|
||||
mail.puts "-- "
|
||||
mail.puts "Vote: #{s.short_id_url}"
|
||||
end
|
||||
end
|
||||
|
||||
last_story_id = s.id
|
||||
end
|
||||
|
||||
Keystore.put(LAST_STORY_KEY, last_story_id)
|
||||
|
||||
# repeat for comments
|
||||
|
||||
last_comment_id = (Keystore.value_for(LAST_COMMENT_KEY) ||
|
||||
Comment.last.id).to_i
|
||||
|
||||
Comment.where("id > ?", last_comment_id).order(:id).each do |c|
|
||||
mailing_list_users.each do |u|
|
||||
if (c.story.tags.map{|t| t.id } & u.tag_filters.map{|t| t.tag_id }).any?
|
||||
next
|
||||
end
|
||||
|
||||
IO.popen([ {}, "/usr/sbin/sendmail", "-i", "-f", "nobody@lobste.rs",
|
||||
u.email ], "w") do |mail|
|
||||
mail.puts "From: #{c.user.username} <#{c.user.username}@lobste.rs>"
|
||||
mail.puts "Reply-To: lobsters-#{u.mailing_list_token}@lobste.rs"
|
||||
mail.puts "To: lobsters-#{u.mailing_list_token}@lobste.rs"
|
||||
mail.puts "List-Id: Lobsters <lobsters-#{u.mailing_list_token}.lobste.rs>"
|
||||
mail.puts "List-Unsubscribe: <https://lobste.rs/settings>"
|
||||
mail.puts "Precedence: list"
|
||||
mail.puts "Content-Type: text/plain; charset=\"us-ascii\""
|
||||
mail.puts "Message-ID: <#{c.mailing_list_message_id}>"
|
||||
|
||||
refs = [ "<#{c.story.mailing_list_message_id}>" ]
|
||||
|
||||
if c.parent_comment_id
|
||||
mail.puts "In-Reply-To: <#{c.parent_comment.mailing_list_message_id}>"
|
||||
|
||||
thread = []
|
||||
indent_level = 0
|
||||
Comment.ordered_for_story_or_thread_for_user(nil, c.thread_id,
|
||||
nil).reverse.each do |cc|
|
||||
if indent_level > 0 && cc.indent_level < indent_level
|
||||
thread.unshift cc
|
||||
indent_level = cc.indent_level
|
||||
elsif cc.id == c.id
|
||||
indent_level = cc.indent_level
|
||||
end
|
||||
end
|
||||
|
||||
thread.each do |cc|
|
||||
refs.push "<#{cc.mailing_list_message_id}>"
|
||||
end
|
||||
else
|
||||
mail.puts "In-Reply-To: <#{c.story.mailing_list_message_id}>"
|
||||
end
|
||||
|
||||
mail.print "References:"
|
||||
refs.each do |ref|
|
||||
mail.puts " #{ref}"
|
||||
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 ""
|
||||
|
||||
mail.puts c.comment.to_s.force_to_ascii.word_wrap(EMAIL_WIDTH)
|
||||
|
||||
mail.puts ""
|
||||
mail.puts "-- "
|
||||
mail.puts "Vote: #{c.short_id_url}"
|
||||
end
|
||||
end
|
||||
|
||||
last_comment_id = c.id
|
||||
end
|
||||
|
||||
Keystore.put(LAST_COMMENT_KEY, last_comment_id)
|
||||
149
script/parse_inbound_mail
Executable file
149
script/parse_inbound_mail
Executable file
|
|
@ -0,0 +1,149 @@
|
|||
#!/usr/bin/env ruby
|
||||
#
|
||||
# postfix main.cf:
|
||||
# relay_domains = lobste.rs
|
||||
# transport_maps = hash:/etc/postfix/transport
|
||||
# defer_transports =
|
||||
#
|
||||
# postfix transports:
|
||||
# lobste.rs lobsters:
|
||||
#
|
||||
# postfix master.cf:
|
||||
# lobsters unix - n n - 2 pipe
|
||||
# flags=Fqhu user=lobsters size=1024000 argv=/d/rails/lobsters/script/parse_inbound_mail ${recipient} ${sender}
|
||||
|
||||
ENV["RAILS_ENV"] ||= "production"
|
||||
|
||||
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
||||
require File.expand_path('../../config/boot', __FILE__)
|
||||
require APP_PATH
|
||||
Rails.application.require_environment!
|
||||
|
||||
# postfix exit codes
|
||||
EX_NOUSER = 67
|
||||
EX_TEMPFAIL = 75
|
||||
EX_UNAVAILABLE = 69
|
||||
|
||||
recipient = ARGV[0]
|
||||
user_token = recipient.gsub(/^lobsters-/, "").gsub(/@.*/, "")
|
||||
sender = ARGV[1]
|
||||
message = ""
|
||||
email = nil
|
||||
|
||||
while !STDIN.eof?
|
||||
message += STDIN.gets.to_s
|
||||
end
|
||||
|
||||
if message.match(/^X-BeenThere: lobsters-/i)
|
||||
# avoid looping
|
||||
exit
|
||||
end
|
||||
|
||||
sending_user = User.where(:mailing_list_enabled => true,
|
||||
:mailing_list_token => user_token).first
|
||||
|
||||
if !sending_user
|
||||
STDERR.puts "no user with mailing list token #{user_token}"
|
||||
|
||||
# if this looks like a user token but invalid, generate a bounce to be
|
||||
# helpful. otherwise supress it to avoid talking back to spammers
|
||||
exit(recipient.match(/^lobsters-/) ? EX_NOUSER : 0)
|
||||
end
|
||||
|
||||
# the mail gem stupidly spams STDERR while parsing e-mail, so silence that
|
||||
# stream to avoid anything getting back to postfix
|
||||
begin
|
||||
Utils.silence_stream(STDERR) do
|
||||
email = Mail.read_from_string(message)
|
||||
end
|
||||
|
||||
if !email
|
||||
raise
|
||||
end
|
||||
rescue
|
||||
STDERR.puts "error parsing e-mail"
|
||||
exit EX_UNAVAILABLE
|
||||
end
|
||||
|
||||
# figure out what this reply is to
|
||||
irt = email[:in_reply_to].to_s.gsub(/[^A-Za-z0-9@\.]/, "")
|
||||
|
||||
if m = irt.match(/^comment\.([^\.]+)\.\d+@/)
|
||||
parent = Comment.find_by_short_id(m[1])
|
||||
elsif m = irt.match(/^story\.([^\.]+)\.\d+@/)
|
||||
parent = Story.find_by_short_id(m[1])
|
||||
end
|
||||
|
||||
if !parent
|
||||
STDERR.puts "no valid comment or story being replied to"
|
||||
exit EX_NOUSER
|
||||
end
|
||||
|
||||
body = nil
|
||||
possible_charset = nil
|
||||
|
||||
if email.multipart?
|
||||
# parts[0] - multipart/alternative
|
||||
# parts[0].parts[0] - text/plain
|
||||
# parts[0].parts[1] - text/html
|
||||
if (p = email.parts.first.parts.select{|p|
|
||||
p.content_type.match(/text\/plain/) }).any?
|
||||
begin
|
||||
possible_charset = p.first.content_type_parameters["charset"]
|
||||
rescue
|
||||
end
|
||||
|
||||
# parts[0] - text/plain
|
||||
elsif (p = email.parts.select{|p|
|
||||
p.content_type.match(/text\/plain/) }).any?
|
||||
body = p.first.body.to_s
|
||||
|
||||
begin
|
||||
possible_charset = p.first.content_type_parameters["charset"]
|
||||
rescue
|
||||
end
|
||||
end
|
||||
elsif email.content_type.to_s.match(/text\/plain/)
|
||||
body = email.body.to_s
|
||||
|
||||
begin
|
||||
possible_charset = email.content_type_parameters["charset"]
|
||||
rescue
|
||||
end
|
||||
|
||||
elsif !email.content_type.to_s.present?
|
||||
# no content-type header, assume it's text/plain
|
||||
body = email.body.to_s
|
||||
end
|
||||
|
||||
if !body.present?
|
||||
# oh well
|
||||
STDERR.puts "no valid text/plain body found"
|
||||
exit EX_UNAVAILABLE
|
||||
end
|
||||
|
||||
# try to remove sig lines
|
||||
body.gsub!(/^-- \n.+\z/, "")
|
||||
|
||||
# TODO: try to strip out attribution line, followed by an optional blank line,
|
||||
# and then lines prefixed with >
|
||||
|
||||
body.strip!
|
||||
|
||||
c = Comment.new
|
||||
c.user_id = sending_user.id
|
||||
c.comment = body
|
||||
|
||||
if parent.is_a?(Comment)
|
||||
c.story_id = parent.story_id
|
||||
c.parent_comment_id = parent.id
|
||||
else
|
||||
c.story_id = parent.id
|
||||
end
|
||||
|
||||
if c.save
|
||||
exit
|
||||
else
|
||||
STDERR.puts c.errors.inspect
|
||||
exit EX_UNAVAILABLE
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue