mailman 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ support), that works with Rails "out of the box".
10
10
  end
11
11
  end
12
12
 
13
- See the {file:USER_GUIDE.md} for more information.
13
+ See the [USER_GUIDE.md](http://rubydoc.info/github/titanous/mailman/master/file/USER_GUIDE.md) for more information.
14
14
 
15
15
  ## Installation
16
16
 
@@ -21,6 +21,11 @@ See the {file:USER_GUIDE.md} for more information.
21
21
  This project is sponsored by the [Ruby Summer of Code](http://rubysoc.org),
22
22
  and my mentor is [Steven Soroka](http://github.com/ssoroka).
23
23
 
24
+ ### Contributors
25
+
26
+ Tim Carey-Smith
27
+ Nicolas Aguttes
28
+
24
29
  ## Copyright
25
30
 
26
31
  Copyright (c) 2010 Jonathan Rudenberg. See LICENSE for details.
data/USER_GUIDE.md CHANGED
@@ -96,6 +96,23 @@ All captures from matchers are available as block arguments:
96
96
  puts "Got message from #{username} about Ticket #{ticket_id}"
97
97
  end
98
98
 
99
+ #### Class Routing
100
+
101
+ Messages can also be routed to methods. For instance, to route to an
102
+ ActionMailer mailer with a `receive` method defined, this will work:
103
+
104
+ from '%user%@example.org', SampleMailer
105
+
106
+ Messages can also be routed to arbitrary instance methods:
107
+
108
+ from '%user%@example.org', 'ExampleClass#new_message'
109
+
110
+ The method should accept two arguments, the message object, and the params:
111
+
112
+ def receive(message, params)
113
+ # process message here
114
+ end
115
+
99
116
  #### Route Helpers
100
117
 
101
118
  There are two helpers available inside of route blocks:
@@ -116,7 +133,7 @@ the properties available.
116
133
 
117
134
  ### Conditions
118
135
 
119
- Currently there are four conditions available: `to`, `from`, `subject`, `body`
136
+ Currently there are five conditions available: `to`, `from`, `cc`, `subject`, `body`
120
137
 
121
138
  More can be added easily (see `lib/mailman/route/conditions.rb`).
122
139
 
@@ -145,7 +162,8 @@ will poll every minute by default (this can be changed with
145
162
  `Mailman.config.poll_interval`). After new messages are processed, they will
146
163
  be deleted from the server. *No copy of messages will be saved anywhere
147
164
  after processing*. If you want to keep a copy of messages, it is recommended
148
- that you use a mail retriever with the Maildir receiver.
165
+ that you use a mail retriever with the Maildir receiver. You could also use
166
+ Gmail and set it to keep messages after they have been retrieved with POP3.
149
167
 
150
168
 
151
169
  ### Maildir
@@ -183,10 +201,11 @@ set, Mailman will use POP3 polling as the receiver.
183
201
  **Example**:
184
202
 
185
203
  Mailman.config.pop3 = {
186
- :username => 'chunky',
187
- :password => 'bacon',
188
- :server => 'example.org',
189
- :port => 110 # defaults to 110
204
+ :username => 'chunkybacon@gmail.com',
205
+ :password => 'foobar',
206
+ :server => 'pop.gmail.com',
207
+ :port => 995, # defaults to 110
208
+ :ssl => true # defaults to false
190
209
  }
191
210
 
192
211
 
@@ -43,23 +43,24 @@ module Mailman
43
43
  elsif Mailman.config.pop3
44
44
  options = {:processor => @processor}.merge(Mailman.config.pop3)
45
45
  Mailman.logger.info "POP3 receiver enabled (#{options[:username]}@#{options[:server]})."
46
+ if Mailman.config.poll_interval > 0 # we should poll
47
+ polling = true
48
+ Mailman.logger.info "Polling enabled. Checking every #{Mailman.config.poll_interval} seconds."
49
+ else
50
+ polling = false
51
+ Mailman.logger.info 'Polling disabled. Checking for messages once.'
52
+ end
53
+
46
54
  connection = Receiver::POP3.new(options)
47
- begin
55
+ loop do
56
+ Mailman.logger.debug "Checking POP3 server for messages..."
48
57
  connection.connect
49
- if Mailman.config.poll_interval > 0 # we should poll
50
- Mailman.logger.info "Polling enabled. Checking every #{Mailman.config.poll_interval} seconds."
51
- loop do
52
- Mailman.logger.debug "Polling POP3 server for messages..."
53
- connection.get_messages
54
- sleep Mailman.config.poll_interval
55
- end
56
- else # one-time retrieval
57
- Mailman.logger.info "Polling disabled. Checking for messages..."
58
- connection.get_messages
59
- end
60
- ensure
58
+ connection.get_messages
61
59
  connection.disconnect
60
+ break if !polling
61
+ sleep Mailman.config.poll_interval
62
62
  end
63
+
63
64
  elsif Mailman.config.maildir
64
65
  Mailman.logger.info "Maildir receiver enabled (#{Mailman.config.maildir})."
65
66
  maildir = Maildir.new(Mailman.config.maildir)
@@ -15,11 +15,13 @@ module Mailman
15
15
  # @option options [Integer] :port the port to connect to
16
16
  # @option options [String] :username the username to authenticate with
17
17
  # @option options [String] :password the password to authenticate with
18
+ # @option options [true,false] :ssl enable SSL
18
19
  def initialize(options)
19
20
  @processor = options[:processor]
20
21
  @username = options[:username]
21
22
  @password = options[:password]
22
23
  @connection = Net::POP3.new(options[:server], options[:port])
24
+ @connection.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if options[:ssl]
23
25
  end
24
26
 
25
27
  # Connects to the POP3 server.
@@ -30,8 +30,9 @@ module Mailman
30
30
  def self.inherited(condition)
31
31
  condition_name = condition.to_s.split('::')[-1][0...-9].downcase
32
32
  Route.class_eval <<-EOM
33
- def #{condition_name}(pattern, &block)
33
+ def #{condition_name}(pattern, klass = nil, &block)
34
34
  @conditions << #{condition}.new(pattern)
35
+ @klass = klass
35
36
  if block_given?
36
37
  @block = block
37
38
  end
@@ -40,8 +41,8 @@ module Mailman
40
41
  EOM
41
42
 
42
43
  Application.class_eval <<-EOM
43
- def #{condition_name}(pattern, &block)
44
- @router.add_route Route.new.#{condition_name}(pattern, &block)
44
+ def #{condition_name}(pattern, klass = nil, &block)
45
+ @router.add_route Route.new.#{condition_name}(pattern, klass, &block)
45
46
  end
46
47
  EOM
47
48
  end
@@ -4,9 +4,11 @@ module Mailman
4
4
  # Matches against the To addresses of a message.
5
5
  class ToCondition < Condition
6
6
  def match(message)
7
- message.to.each do |address|
8
- if result = @matcher.match(address)
9
- return result
7
+ if !message.to.nil?
8
+ message.to.each do |address|
9
+ if result = @matcher.match(address)
10
+ return result
11
+ end
10
12
  end
11
13
  end
12
14
  nil
@@ -39,5 +41,20 @@ module Mailman
39
41
  end
40
42
  end
41
43
 
44
+ # Matches against the CC header of a message.
45
+ class CcCondition < Condition
46
+ def match(message)
47
+ if !message.cc.nil?
48
+ message.cc.each do |address|
49
+ if result = @matcher.match(address)
50
+ return result
51
+ end
52
+ end
53
+ end
54
+ nil
55
+ end
56
+ end
57
+
58
+
42
59
  end
43
60
  end
data/lib/mailman/route.rb CHANGED
@@ -7,6 +7,10 @@ module Mailman
7
7
  # @return [Proc] the block that should be run if the conditions match
8
8
  attr_reader :block
9
9
 
10
+ # @return [Class,String] the class (and optional instance method) to run
11
+ # instead of a block
12
+ attr_reader :klass
13
+
10
14
  # @return [Array] the list of condition instances associated with the route
11
15
  attr_reader :conditions
12
16
 
@@ -16,7 +20,7 @@ module Mailman
16
20
 
17
21
  # Checks whether a message matches the route.
18
22
  # @param [Mail::Message] message the message to match against
19
- # @return [Hash] the +:block+ associated with the route, the
23
+ # @return [Hash] the +:block+ and +:klass+ associated with the route, the
20
24
  # +:params+ hash, and the block +:args+ array.
21
25
  def match!(message)
22
26
  params = {}
@@ -29,7 +33,7 @@ module Mailman
29
33
  return nil
30
34
  end
31
35
  end
32
- { :block => @block, :params => params, :args => args }
36
+ { :block => @block, :klass => @klass, :params => params, :args => args }
33
37
  end
34
38
 
35
39
  end
@@ -49,7 +49,14 @@ module Mailman
49
49
 
50
50
  if result
51
51
  @params.merge!(result[:params])
52
- if result[:block].arity > 0
52
+ if !result[:klass].nil?
53
+ if result[:klass].is_a?(Class) # no instance method specified
54
+ result[:klass].new.send(:receive, @message, @params)
55
+ elsif result[:klass].kind_of?(String) # instance method specified
56
+ klass, method = result[:klass].split('#')
57
+ klass.camelize.constantize.new.send(method.to_sym, @message, @params)
58
+ end
59
+ elsif result[:block].arity > 0
53
60
  instance_exec(*result[:args], &result[:block])
54
61
  else
55
62
  instance_exec(&result[:block])
@@ -1,3 +1,3 @@
1
1
  module Mailman
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jonathan Rudenberg
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-28 00:00:00 -04:00
17
+ date: 2010-09-02 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency