forked from rubytaiwan/ruby-taiwan
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
152 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'nokogiri' | ||
|
||
module MailHelper | ||
|
||
# find the email body in a message | ||
def plaintext_body(message) | ||
if message.multipart? | ||
message.parts.each do |p| | ||
if p.content_type =~ /text\/plain/ | ||
encoding = p.content_type.to_s.split("=").last.to_s | ||
return extract_content(p.body,encoding) | ||
end | ||
end | ||
raise "mail body multipart, but not text/plain part" | ||
elsif message.content_type == 'text/plain' | ||
return extract_content(message.body, message.encoding) | ||
else | ||
raise "mail body is not multipart nor text/plain" | ||
end | ||
end | ||
|
||
# extract email content from a body | ||
# use the sender email line as separation | ||
def extract_reply(body, sender_email) | ||
body.strip | ||
.gsub(/\n^[^\r\n]*#{sender_email}.*:.*\z/m, '') | ||
.strip | ||
end | ||
|
||
private | ||
def extract_content(html, coding) | ||
doc = Nokogiri::HTML(html.to_s, nil, coding) | ||
return doc.css("body").text | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defaults: &defaults | ||
server: 'pop.gmail.com' | ||
port: 995 | ||
ssl: true | ||
|
||
development: | ||
<<: *defaults | ||
username: '' | ||
password: '' | ||
|
||
test: | ||
<<: *defaults | ||
username: '' | ||
password: '' | ||
|
||
production: | ||
<<: *defaults | ||
username: '' | ||
password: '' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env ruby | ||
require 'rubygems' | ||
require 'bundler' | ||
Bundler.require :mailman | ||
|
||
require File.expand_path('../../app/helpers/mail_helper', __FILE__) | ||
require File.expand_path('../../app/models/reply_listener', __FILE__) | ||
include MailHelper | ||
|
||
require 'daemon_spawn' | ||
|
||
env = ENV['RAILS_ENV'] || 'development' | ||
config = YAML::load(open(File.expand_path('../../config/mailman.yml', __FILE__)))[env] | ||
Mailman.config.pop3 = { | ||
:username => config["username"], | ||
:password => config["password"], | ||
:server => config["server"], | ||
:port => config["port"], | ||
:ssl => config["ssl"] | ||
} | ||
|
||
redis_config = YAML.load_file(File.expand_path('../../config/redis.yml', __FILE__))[env] | ||
Resque.redis = Redis.new(:host => redis_config['host'], :port => redis_config['port']) | ||
Resque.redis.namespace = redis_config['redis_namespace'] | ||
|
||
working_dir = File.expand_path('../../', __FILE__) | ||
logs = "#{working_dir}/log/mailman.log" | ||
pids = "#{working_dir}/tmp/pids/mailman.pid" | ||
|
||
class MailmanDaemon < DaemonSpawn::Base | ||
def start(args) | ||
Mailman::Application.run do | ||
to('notification+%reply_id%-%key%@ruby-hk.org') do |reply_id, key| | ||
begin | ||
from_email = message.from.first | ||
message_id = message.message_id | ||
reply_text = extract_reply(plaintext_body(message), Setting.email_sender) | ||
puts "receive email from #{from_email}, reply_id: #{reply_id}, key: #{key}" | ||
|
||
Resque.enqueue(ReplyListener, reply_id, key, message_id, from_email, reply_text) | ||
rescue StandardError => e | ||
puts "ERROR: #{e.inspect}" | ||
|
||
# write mail to a file for debug | ||
emailfile = "#{working_dir}/log/mailman-#{rand(100000)}.eml" | ||
puts "Write file to debug: #{emailfile}" | ||
File.open(emailfile, 'w') {|f| f.write(message) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
def stop | ||
puts "Try to shutdown mailman" | ||
end | ||
end | ||
|
||
|
||
MailmanDaemon.spawn!({ | ||
:processes => 1, | ||
:log_file => logs, | ||
:pid_file => pids, | ||
:sync_log => true, | ||
:working_dir => working_dir, | ||
:singleton => true | ||
}) | ||
|
||
|
||
|
This file was deleted.
Oops, something went wrong.