mail_processor 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Rune Myrland
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,61 @@
1
+ = mail_processor
2
+
3
+ Is a convenience processor for incoming mails. Pop3 and MailDir
4
+ is supported at the moment. The mail is deleted after it is processed.
5
+
6
+
7
+ == Usage
8
+
9
+ === MailDir
10
+
11
+ processor = Processor.new do
12
+ retriever :mail_dir do
13
+ glob "#{ENV['HOME']}/MailDir/new/*"
14
+ end
15
+ end
16
+
17
+ # Process all mails in the directory
18
+ processor.process do |popped|
19
+ # Popped is raw mail data, which may be fed
20
+ # to the 'mail' gem for further processing
21
+ puts popped
22
+ end
23
+
24
+
25
+ === Pop3
26
+
27
+ processor = Processor.new do
28
+ retriever :pop3 do
29
+ address "pop.example.com"
30
+ username "test@example.com"
31
+ password "**SECRET**"
32
+ end
33
+ end
34
+
35
+ # Process all mails in the queue
36
+ processor.process do |popped|
37
+ # Popped is raw mail data, which may be fed
38
+ # to the 'mail' gem for further processing
39
+ puts popped
40
+ end
41
+
42
+
43
+ == Feature / problems
44
+
45
+ Mails are automatically deleted from the server/directory after
46
+ they are processed.
47
+
48
+
49
+ == Note on Patches/Pull Requests
50
+
51
+ * Fork the project.
52
+ * Make your feature addition or bug fix.
53
+ * Add tests for it. This is important so I don't break it in a
54
+ future version unintentionally.
55
+ * Commit, do not mess with rakefile, version, or history.
56
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
57
+ * Send me a pull request. Bonus points for topic branches.
58
+
59
+ == Copyright
60
+
61
+ Copyright (c) 2010 Rune Myrland. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "mail_processor"
8
+ gem.summary = %Q{Is a convenience processor for incoming mails.}
9
+ gem.description = %Q{Pop3 and MailDir is supported at the moment.}
10
+ gem.email = "rune@epubify.com"
11
+ gem.homepage = "http://github.com/wrimle/mail_processor"
12
+ gem.authors = ["Rune Myrland"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "mail_processor #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'log4r'
5
+
6
+ include Log4r
7
+ include MailProcessor
8
+
9
+ class File
10
+ def self.read_binary filename
11
+ f = File.new(filename, "rb")
12
+ content = f.read()
13
+ f.close()
14
+ content
15
+ end
16
+ end
17
+
18
+ module MailProcessor
19
+
20
+ class MailDir
21
+ def initialize options = {}, &block
22
+ @log = Logger.new self.class.to_s
23
+ @log.outputters = Outputter.stdout
24
+
25
+ @attributes = {
26
+ :glob => "#{ENV['HOME']}/MailDir/new/*",
27
+ }.merge(options)
28
+ instance_eval &block if block_given?
29
+ end
30
+
31
+
32
+ def glob v = nil
33
+ if v
34
+ @attributes[:glob] = v
35
+ else
36
+ @attributes[:glob]
37
+ end
38
+ end
39
+
40
+
41
+ def process(options = {}, &block)
42
+ a = @attributes.merge(options)
43
+
44
+ count = 0
45
+ Dir.glob(a[ :glob ]) do |filename|
46
+ yield(File.read_binary(filename))
47
+ FileUtils::rm(filename)
48
+ count += 1
49
+ end
50
+ @log.info "Processed #{count} mails"
51
+
52
+ count == 0
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,101 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'net/pop'
4
+ require 'rubygems'
5
+ require 'log4r'
6
+
7
+ include MailProcessor
8
+
9
+ module MailProcessor
10
+
11
+ class Pop3
12
+ def initialize options = {}, &block
13
+ @log = Logger.new self.class.to_s
14
+ @log.outputters = Outputter.stdout
15
+
16
+ @attributes = {
17
+ :address => "pop.gmail.com",
18
+ :port => 110, # 995,
19
+ :username => nil,
20
+ :password => nil,
21
+ # Must have ruby 1.9 to use this
22
+ :use_ssl => false, # true
23
+ }.merge(options)
24
+ instance_eval &block if block_given?
25
+ self
26
+ end
27
+
28
+
29
+ def address v = nil
30
+ if v
31
+ @attributes[:address] = v
32
+ else
33
+ @attributes[:address]
34
+ end
35
+ end
36
+
37
+
38
+ def port v = nil
39
+ if v
40
+ @attributes[:port] = v
41
+ else
42
+ @attributes[:port]
43
+ end
44
+ end
45
+
46
+
47
+ def username v = nil
48
+ if v
49
+ @attributes[:username] = v
50
+ else
51
+ @attributes[:username]
52
+ end
53
+ end
54
+
55
+
56
+ def password v = nil
57
+ if v
58
+ @attributes[:password] = v
59
+ else
60
+ @attributes[:password]
61
+ end
62
+ end
63
+
64
+
65
+ def use_ssl?
66
+ @attributes[:use_ssl]
67
+ end
68
+
69
+
70
+ def use_ssl v = nil
71
+ if v
72
+ @attributes[:use_ssl] = v
73
+ else
74
+ @attributes[:use_ssl]
75
+ end
76
+ end
77
+
78
+
79
+ def process(options = {}, &block)
80
+ a = @attributes.merge(options)
81
+
82
+ didWork = false
83
+ Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if a[:use_ssl]
84
+ pop = Net::POP3.new(a[:address], a[:port])
85
+ pop.start(a[:username], a[:password])
86
+ if pop.mails.empty?
87
+ @log.info 'No mail.'
88
+ else
89
+ pop.each_mail do |m|
90
+ yield m.pop
91
+ m.delete
92
+ end
93
+ @log.info "Processed #{pop.mails.size} mails."
94
+ pop.finish
95
+ didWork = true
96
+ end
97
+
98
+ didWork
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module MailProcessor
4
+ class UnkownRetrieverError < StandardError; end
5
+
6
+
7
+ class Processor
8
+ def initialize options = {}, &block
9
+ @attributes = {
10
+ :retriever => nil
11
+ }
12
+ instance_eval &block
13
+ self
14
+ end
15
+
16
+
17
+ def retriever method, options = {}, &block
18
+ @retriever = case method
19
+ when :pop3 then
20
+ Pop3.new options, &block
21
+ when :mail_dir then
22
+ @retriever = MailDir.new options, &block
23
+ else
24
+ raise UnkownRetrieverError, "Retriever type #{method.to_s} not known"
25
+ end
26
+ end
27
+
28
+
29
+ def process options = {}, &block
30
+ @retriever.process &block
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module MailProcessor; end
4
+
5
+ require 'mail_processor/pop3'
6
+ require 'mail_processor/mail_dir'
7
+ require 'mail_processor/processor'
@@ -0,0 +1 @@
1
+ First mail
@@ -0,0 +1 @@
1
+ Second mail
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "MailProcessor" do
4
+
5
+ context "Pop3" do
6
+ it "works" do
7
+ pending("needs a real mail account")
8
+
9
+ processor = Processor.new do
10
+ retriever :pop3 do
11
+ address "pop.example.com"
12
+ username "test@example.com"
13
+ password "**SECRET**"
14
+ end
15
+ end
16
+
17
+ processor.process do |popped|
18
+ puts popped
19
+ end
20
+ end
21
+ end
22
+
23
+
24
+ context "MailDir" do
25
+ it "works" do
26
+ FileUtils::mkdir_p("spec/MailDir/new")
27
+ Dir.glob("spec/data/*.eml") do |filename|
28
+ FileUtils::cp(filename, "spec/MailDir/new/")
29
+ end
30
+ processor = Processor.new do
31
+ retriever :mail_dir do
32
+ glob "./spec/MailDir/new/mail*.eml"
33
+ end
34
+ end
35
+
36
+ processor.process do |popped|
37
+ puts popped
38
+ end
39
+ end
40
+ end
41
+
42
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'mail_processor'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'mail_processor'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMailProcessor < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_processor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Rune Myrland
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-19 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Pop3 and MailDir is supported at the moment.
38
+ email: rune@epubify.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/mail_processor.rb
54
+ - lib/mail_processor/mail_dir.rb
55
+ - lib/mail_processor/pop3.rb
56
+ - lib/mail_processor/processor.rb
57
+ - spec/data/mail_0001.eml
58
+ - spec/data/mail_0002.eml
59
+ - spec/mail_processor_spec.rb
60
+ - spec/spec.opts
61
+ - spec/spec_helper.rb
62
+ - test/helper.rb
63
+ - test/test_mail_processor.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/wrimle/mail_processor
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.7
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Is a convenience processor for incoming mails.
98
+ test_files:
99
+ - spec/mail_processor_spec.rb
100
+ - spec/spec_helper.rb
101
+ - test/helper.rb
102
+ - test/test_mail_processor.rb