summaryrefslogtreecommitdiff
path: root/fake-service/lib/pixelated_service/search/scope_match.rb
blob: 170c54cf3a71faa3960d928e6f0afe66e083190c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

module PixelatedService
  class Search
    class ScopeMatch
      def initialize(scope, data)
        @scope = scope.downcase.gsub(/-/, '_').to_sym
        @data = data
      end

      def to_s
        "Scope(#@scope, #@data)"
      end

      def is_search_scope?
        [:in, :tag, :is].include?(@scope) &&
          %w(_default_ trash all sent drafts).include?(@data.match_string.downcase)
      end

      def search_scope
        case @data.match_string.downcase
        when '_default_'
          PixelatedService::MailScopeFilter::Default
        when 'all'
          PixelatedService::MailScopeFilter::All
        when 'trash'
          PixelatedService::MailScopeFilter::Trash
        when 'sent'
          PixelatedService::MailScopeFilter::Sent
        when 'drafts'
          PixelatedService::MailScopeFilter::Drafts
        end
      end

      def match?(mail)
        strs =
          case @scope
          when :to
            mail.to
          when :from, :sender
            mail.from
          when :cc
            mail.headers[:cc]
          when :bcc
            mail.headers[:bcc]
          when :subject
            mail.subject
          when :rcpt, :rcpts, :recipient, :recipients
            [mail.to, mail.headers[:cc], mail.headers[:bcc]].flatten.compact
          when :body
            mail.body
          when :tag, :tags, :in
            return @data.match_exact_string?(mail.tag_names)
            # has:seal, has:imprint, has:lock
          when :is
            case @data.str
            when "starred"
              return mail.starred?
            when "read"
              return mail.read?
            when "replied"
              return mail.replied?
              # sealed, imprinted, signed, locked, encrypted,
            else
              raise "NOT IMPLEMENTED: is:#{@data}"
            end
          when :before
            raise "NOT IMPLEMENTED"
          when :after
            raise "NOT IMPLEMENTED"
          when :att, :attachment
            raise "NOT IMPLEMENTED"
          else
            mail.headers[@scope] || (return false)
          end
        @data.match_string? strs
      end
    end
  end
end