76 lines
1.7 KiB
Ruby
Executable file
76 lines
1.7 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
#
|
|
# postfix main.cf:
|
|
# relay_domains = example.com
|
|
# transport_maps = hash:/etc/postfix/transport
|
|
# defer_transports =
|
|
#
|
|
# postfix transports:
|
|
# example.com 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
|
|
|
|
message = ""
|
|
while !STDIN.eof?
|
|
message += STDIN.gets.to_s
|
|
end
|
|
|
|
parser = EmailParser.new(sender = ARGV[1], recipient = ARGV[0], message)
|
|
|
|
if parser.been_here?
|
|
# avoid looping, quietly
|
|
exit
|
|
|
|
elsif !parser.sending_user
|
|
STDERR.puts "no active user with mailing list token #{parser.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(parser.user_token ? EX_NOUSER : 0)
|
|
|
|
elsif !parser.email
|
|
STDERR.puts "error parsing e-mail"
|
|
exit EX_UNAVAILABLE
|
|
|
|
elsif !parser.parent
|
|
STDERR.puts "no valid comment or story being replied to"
|
|
exit EX_NOUSER
|
|
|
|
elsif !parser.body.present?
|
|
STDERR.puts "no valid text/plain body found"
|
|
exit EX_UNAVAILABLE
|
|
end
|
|
|
|
c = Comment.new
|
|
c.user_id = parser.sending_user.id
|
|
c.comment = parser.body
|
|
c.is_from_email = true
|
|
|
|
if parser.parent.is_a?(Comment)
|
|
c.story_id = parser.parent.story_id
|
|
c.parent_comment_id = parser.parent.id
|
|
else
|
|
c.story_id = parser.parent.id
|
|
end
|
|
|
|
if c.save
|
|
exit
|
|
else
|
|
STDERR.puts c.errors.inspect
|
|
exit EX_UNAVAILABLE
|
|
end
|