summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Gemfile2
-rw-r--r--Gemfile.lock4
-rw-r--r--bin/news-schleuder3
-rw-r--r--lib/news_schleuder.rb68
-rw-r--r--lib/news_schleuder/yaml_loader.rb2
-rw-r--r--spec/data/testfile.yaml2
-rw-r--r--spec/news_schleuder/yaml_loader_spec.rb28
8 files changed, 94 insertions, 17 deletions
diff --git a/.gitignore b/.gitignore
index eb8e985..674ed05 100644
--- a/.gitignore
+++ b/.gitignore
@@ -46,3 +46,5 @@ pkg
# For rubinius:
#*.rbc
+
+spec/data/testfile.yaml
diff --git a/Gemfile b/Gemfile
index 9318bad..83ad9fe 100644
--- a/Gemfile
+++ b/Gemfile
@@ -3,7 +3,7 @@ source "http://rubygems.org"
# Example:
# gem "activesupport", ">= 2.3.5"
-gem 'mail', ">= 2.3.0"
+gem 'mail-gnupg', ">= 0.0.1"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
diff --git a/Gemfile.lock b/Gemfile.lock
index dd17ea1..eeb83ad 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -35,7 +35,7 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.0.0)
jeweler (~> 1.6.4)
- mail
- mocha
+ mail (>= 2.3.0)
+ mocha (>= 0.9.12)
rcov
rspec (~> 2.3.0)
diff --git a/bin/news-schleuder b/bin/news-schleuder
index 2f3d2f7..e515541 100644
--- a/bin/news-schleuder
+++ b/bin/news-schleuder
@@ -12,9 +12,6 @@ OptionParser.new do |opts|
opts.on("-m", "--members FILE", "Set the members list") do |v|
options[:members] = v
end
- opts.on("-g", "--gpg-home DIRECTORY", "Set the gpg homedirectory") do |v|
- options[:gpg_home] = v
- end
opts.on("-s", "--schleuder-home DIRECTORY", "Set the home directory of the schleuder list to use as a sender") do |v|
options[:schleuder_home] = v
end
diff --git a/lib/news_schleuder.rb b/lib/news_schleuder.rb
index b6cb2c7..de0cc51 100644
--- a/lib/news_schleuder.rb
+++ b/lib/news_schleuder.rb
@@ -1,6 +1,8 @@
-require 'mail'
+require 'mail_gnupg'
require 'yaml'
+require 'active_support/inflector'
+
require 'news_schleuder/yaml_loader'
class NewsSchleuder
@@ -12,16 +14,32 @@ class NewsSchleuder
def deliver
verify_requirements
+ init
+ members.each do |member|
+ build_mail(member).deliver
+ post_processor(member)
+ end
end
private
def verify_requirements
+ raise "You should define the newsletter options" unless options[:newsletter]
raise "Newsletter should contain subject and body" unless mail_template['subject'] && mail_template['body']
+ raise "You should define the schleuder option" unless options[:schleuder_home]
raise "Schleuder config should contain myaddr and pgp_password" unless schleuder_config['myaddr'] && schleuder_config['gpg_password']
raise "Members list should contain an array of members" unless members.is_a?(Array)
end
+ def init
+ ENV.delete('GPG_AGENT_INFO')
+ Mail.register_interceptor(MailGnupg::Interceptor.new(
+ :keyring_location => options[:schleuder_home],
+ :password => schleuder_config['gpg_password'],
+ :strategy => {:encrypt => :opportunustic})
+ )
+ end
+
def mail_template
@mail_template ||= NewsSchleuder::YAMLLoader.load_file(options[:newsletter])
end
@@ -34,4 +52,52 @@ class NewsSchleuder
@schleuder_config ||= NewsSchleuder::YAMLLoader.load_file(schleuder_config_file)
end
+ def members
+ @members ||= NewsSchleuder::YAMLLoader.load_file(options[:members]||File.join(options[:schleuder_home],'news_members.yaml'))
+ end
+
+
+ def post_processor(member)
+ File.open(sent_members_filename,'a'){|f| f << "- #{member}\n" }
+ end
+
+ def sent_members_filename
+ @sent_members_filename ||= File.join(options[:schleuder_home],"news_sent.#{Time.now.strftime('%Y%m%d')}-#{Process.pid}.yaml")
+ end
+
+ def build_mail(member)
+ Mail.new(
+ :to => member,
+ :from => schleuder_config['myaddr'],
+ :return_path => bounce_addr,
+ :message_id => get_message_id,
+ :openpgp => "id=#{sender_key_id} (Send an email to #{send_key_addr} to receive the public-key); preference=signencrypt (All kind of emails are accepted)",
+
+ :subject => mail_template['subject'],
+
+ :body => mail_template['body']
+
+
+ ) do |m|
+ m.delivery_method :smtp, {
+ :openssl_verify_mode => 'none'
+ }
+ end
+ end
+
+ def send_key_addr
+ @send_key_addr ||= schleuder_config['myaddr'].split('@').join('-sendkey@')
+ end
+
+ def bounce_addr
+ @bounc_addr ||= schleuder_config['myaddr'].split('@').join('-bounce@')
+ end
+
+ def sender_key_id
+ @sender_key_id ||= GPGME::Key.find_exact(:public, schleuder_config['myaddr']).first.subkeys.first.keyid
+ end
+
+ def get_message_id
+ @message_id ||= Mail::MessageIdField.new.to_s
+ end
end
diff --git a/lib/news_schleuder/yaml_loader.rb b/lib/news_schleuder/yaml_loader.rb
index 6344ede..29c52ea 100644
--- a/lib/news_schleuder/yaml_loader.rb
+++ b/lib/news_schleuder/yaml_loader.rb
@@ -3,7 +3,7 @@ class NewsSchleuder
class << self
def load_file(filename)
result = YAML.load_file(filename)
- raise "File #{filename} does not contain valid yaml" unless result.is_a?(Hash)
+ raise "File #{filename} does not contain valid yaml" unless result.is_a?(Hash) || result.is_a?(Array)
result
end
end
diff --git a/spec/data/testfile.yaml b/spec/data/testfile.yaml
deleted file mode 100644
index 80e3303..0000000
--- a/spec/data/testfile.yaml
+++ /dev/null
@@ -1,2 +0,0 @@
-subject: 'Some news'
-body: 'News on the roof' \ No newline at end of file
diff --git a/spec/news_schleuder/yaml_loader_spec.rb b/spec/news_schleuder/yaml_loader_spec.rb
index 12087a8..31c4a0a 100644
--- a/spec/news_schleuder/yaml_loader_spec.rb
+++ b/spec/news_schleuder/yaml_loader_spec.rb
@@ -17,15 +17,29 @@ describe "NewsSchleuder::YAMLLoader" do
end
context "with a valid yaml file" do
- before(:each) do
- File.open(@file, 'w'){|f| f << "subject: 'Some news'\nbody: 'News on the roof'" }
- end
- it "should be happy" do
- lambda { NewsSchleuder::YAMLLoader.load_file(@file) }.should_not raise_error
+ context "containing a hash" do
+ before(:each) do
+ File.open(@file, 'w'){|f| f << "subject: 'Some news'\nbody: 'News on the roof'" }
+ end
+ it "should be happy" do
+ lambda { NewsSchleuder::YAMLLoader.load_file(@file) }.should_not raise_error
+ end
+
+ it "should return a hash" do
+ NewsSchleuder::YAMLLoader.load_file(@file).should be_kind_of(Hash)
+ end
end
+ context "containing an array" do
+ before(:each) do
+ File.open(@file, 'w'){|f| f << "- b\n- a" }
+ end
+ it "should be happy" do
+ lambda { NewsSchleuder::YAMLLoader.load_file(@file) }.should_not raise_error
+ end
- it "should return a hash" do
- NewsSchleuder::YAMLLoader.load_file(@file).should be_kind_of(Hash)
+ it "should return a hash" do
+ NewsSchleuder::YAMLLoader.load_file(@file).should be_kind_of(Array)
+ end
end
end
end