BigW Consortium Gitlab

mail_room.rb 1.39 KB
Newer Older
1 2
require 'yaml'
require 'json'
3
require_relative 'redis' unless defined?(Gitlab::Redis)
4 5 6 7 8 9 10 11 12 13 14 15

module Gitlab
  module MailRoom
    class << self
      def enabled?
        config[:enabled] && config[:address]
      end

      def config
        @config ||= fetch_config
      end

16 17
      def reset_config!
        @config = nil
18 19
      end

20 21 22
      private

      def fetch_config
23
        return {} unless File.exist?(config_file)
24 25 26 27 28 29 30 31 32 33

        rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
        all_config = YAML.load_file(config_file)[rails_env].deep_symbolize_keys

        config = all_config[:incoming_email] || {}
        config[:enabled] = false if config[:enabled].nil?
        config[:port] = 143 if config[:port].nil?
        config[:ssl] = false if config[:ssl].nil?
        config[:start_tls] = false if config[:start_tls].nil?
        config[:mailbox] = 'inbox' if config[:mailbox].nil?
34
        config[:idle_timeout] = 60 if config[:idle_timeout].nil?
35 36

        if config[:enabled] && config[:address]
37 38 39 40 41 42
          gitlab_redis = Gitlab::Redis.new(rails_env)
          config[:redis_url] = gitlab_redis.url

          if gitlab_redis.sentinels?
            config[:sentinels] = gitlab_redis.sentinels
          end
43
        end
44 45

        config
46 47 48
      end

      def config_file
49
        ENV['MAIL_ROOM_GITLAB_CONFIG_FILE'] || File.expand_path('../../../config/gitlab.yml', __FILE__)
50 51 52 53
      end
    end
  end
end