To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / imap.rb @ 1568:bc47b68a9487

History | View | Annotate | Download (2.26 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1494:e248c7af89ec Chris
# Copyright (C) 2006-2014  Jean-Philippe Lang
3 0:513646585e45 Chris
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18
require 'net/imap'
19
20
module Redmine
21
  module IMAP
22
    class << self
23
      def check(imap_options={}, options={})
24
        host = imap_options[:host] || '127.0.0.1'
25
        port = imap_options[:port] || '143'
26
        ssl = !imap_options[:ssl].nil?
27
        folder = imap_options[:folder] || 'INBOX'
28 909:cbb26bc654de Chris
29
        imap = Net::IMAP.new(host, port, ssl)
30 0:513646585e45 Chris
        imap.login(imap_options[:username], imap_options[:password]) unless imap_options[:username].nil?
31
        imap.select(folder)
32 1464:261b3d9a4903 Chris
        imap.uid_search(['NOT', 'SEEN']).each do |uid|
33
          msg = imap.uid_fetch(uid,'RFC822')[0].attr['RFC822']
34
          logger.debug "Receiving message #{uid}" if logger && logger.debug?
35 0:513646585e45 Chris
          if MailHandler.receive(msg, options)
36 1464:261b3d9a4903 Chris
            logger.debug "Message #{uid} successfully received" if logger && logger.debug?
37 0:513646585e45 Chris
            if imap_options[:move_on_success]
38 1464:261b3d9a4903 Chris
              imap.uid_copy(uid, imap_options[:move_on_success])
39 0:513646585e45 Chris
            end
40 1464:261b3d9a4903 Chris
            imap.uid_store(uid, "+FLAGS", [:Seen, :Deleted])
41 0:513646585e45 Chris
          else
42 1464:261b3d9a4903 Chris
            logger.debug "Message #{uid} can not be processed" if logger && logger.debug?
43
            imap.uid_store(uid, "+FLAGS", [:Seen])
44 0:513646585e45 Chris
            if imap_options[:move_on_failure]
45 1464:261b3d9a4903 Chris
              imap.uid_copy(uid, imap_options[:move_on_failure])
46
              imap.uid_store(uid, "+FLAGS", [:Deleted])
47 0:513646585e45 Chris
            end
48
          end
49
        end
50
        imap.expunge
51 1464:261b3d9a4903 Chris
        imap.logout
52
        imap.disconnect
53 0:513646585e45 Chris
      end
54 909:cbb26bc654de Chris
55 0:513646585e45 Chris
      private
56 909:cbb26bc654de Chris
57 0:513646585e45 Chris
      def logger
58 1115:433d4f72a19b Chris
        ::Rails.logger
59 0:513646585e45 Chris
      end
60
    end
61
  end
62
end