##// END OF EJS Templates
IMAP: add options to move received emails....
Jean-Philippe Lang -
r2218:16eb0421e5ac
parent child
Show More
@@ -1,51 +1,60
1 # redMine - project management software
1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'net/imap'
19 19
20 20 module Redmine
21 21 module IMAP
22 22 class << self
23 23 def check(imap_options={}, options={})
24 24 host = imap_options[:host] || '127.0.0.1'
25 25 port = imap_options[:port] || '143'
26 26 ssl = !imap_options[:ssl].nil?
27 27 folder = imap_options[:folder] || 'INBOX'
28 28
29 29 imap = Net::IMAP.new(host, port, ssl)
30 30 imap.login(imap_options[:username], imap_options[:password]) unless imap_options[:username].nil?
31 31 imap.select(folder)
32 32 imap.search(['NOT', 'SEEN']).each do |message_id|
33 33 msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
34 34 logger.debug "Receiving message #{message_id}" if logger && logger.debug?
35 35 if MailHandler.receive(msg, options)
36 logger.debug "Message #{message_id} successfully received" if logger && logger.debug?
37 if imap_options[:move_on_success]
38 imap.copy(message_id, imap_options[:move_on_success])
39 end
36 40 imap.store(message_id, "+FLAGS", [:Seen, :Deleted])
37 41 else
42 logger.debug "Message #{message_id} can not be processed" if logger && logger.debug?
38 43 imap.store(message_id, "+FLAGS", [:Seen])
44 if imap_options[:move_on_failure]
45 imap.copy(message_id, imap_options[:move_on_failure])
46 imap.store(message_id, "+FLAGS", [:Deleted])
47 end
39 48 end
40 49 end
41 50 imap.expunge
42 51 end
43 52
44 53 private
45 54
46 55 def logger
47 56 RAILS_DEFAULT_LOGGER
48 57 end
49 58 end
50 59 end
51 60 end
@@ -1,107 +1,114
1 # redMine - project management software
1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 namespace :redmine do
19 19 namespace :email do
20 20
21 21 desc <<-END_DESC
22 22 Read an email from standard input.
23 23
24 24 Issue attributes control options:
25 25 project=PROJECT identifier of the target project
26 26 status=STATUS name of the target status
27 27 tracker=TRACKER name of the target tracker
28 28 category=CATEGORY name of the target category
29 29 priority=PRIORITY name of the target priority
30 30 allow_override=ATTRS allow email content to override attributes
31 31 specified by previous options
32 32 ATTRS is a comma separated list of attributes
33 33
34 34 Examples:
35 35 # No project specified. Emails MUST contain the 'Project' keyword:
36 36 rake redmine:email:read RAILS_ENV="production" < raw_email
37 37
38 38 # Fixed project and default tracker specified, but emails can override
39 39 # both tracker and priority attributes:
40 40 rake redmine:email:read RAILS_ENV="production" \\
41 41 project=foo \\
42 42 tracker=bug \\
43 43 allow_override=tracker,priority < raw_email
44 44 END_DESC
45 45
46 46 task :read => :environment do
47 47 options = { :issue => {} }
48 48 %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
49 49 options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
50 50
51 51 MailHandler.receive(STDIN.read, options)
52 52 end
53 53
54 54 desc <<-END_DESC
55 55 Read emails from an IMAP server.
56 56
57 57 Available IMAP options:
58 58 host=HOST IMAP server host (default: 127.0.0.1)
59 59 port=PORT IMAP server port (default: 143)
60 60 ssl=SSL Use SSL? (default: false)
61 61 username=USERNAME IMAP account
62 62 password=PASSWORD IMAP password
63 63 folder=FOLDER IMAP folder to read (default: INBOX)
64 64
65 65 Issue attributes control options:
66 66 project=PROJECT identifier of the target project
67 67 status=STATUS name of the target status
68 68 tracker=TRACKER name of the target tracker
69 69 category=CATEGORY name of the target category
70 70 priority=PRIORITY name of the target priority
71 71 allow_override=ATTRS allow email content to override attributes
72 72 specified by previous options
73 73 ATTRS is a comma separated list of attributes
74 74
75 Processed emails control options:
76 move_on_success=MAILBOX move emails that were successfully received
77 to MAILBOX instead of deleting them
78 move_on_failure=MAILBOX move emails that were ignored to MAILBOX
79
75 80 Examples:
76 81 # No project specified. Emails MUST contain the 'Project' keyword:
77 82
78 83 rake redmine:email:receive_iamp RAILS_ENV="production" \\
79 84 host=imap.foo.bar username=redmine@example.net password=xxx
80 85
81 86
82 87 # Fixed project and default tracker specified, but emails can override
83 88 # both tracker and priority attributes:
84 89
85 90 rake redmine:email:receive_iamp RAILS_ENV="production" \\
86 91 host=imap.foo.bar username=redmine@example.net password=xxx ssl=1 \\
87 92 project=foo \\
88 93 tracker=bug \\
89 94 allow_override=tracker,priority
90 95 END_DESC
91 96
92 97 task :receive_imap => :environment do
93 98 imap_options = {:host => ENV['host'],
94 99 :port => ENV['port'],
95 100 :ssl => ENV['ssl'],
96 101 :username => ENV['username'],
97 102 :password => ENV['password'],
98 :folder => ENV['folder']}
103 :folder => ENV['folder'],
104 :move_on_success => ENV['move_on_success'],
105 :move_on_failure => ENV['move_on_failure']}
99 106
100 107 options = { :issue => {} }
101 108 %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
102 109 options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
103 110
104 111 Redmine::IMAP.check(imap_options, options)
105 112 end
106 113 end
107 114 end
General Comments 0
You need to be logged in to leave comments. Login now