summaryrefslogtreecommitdiff
path: root/fake-service/lib/pixelated_service/mail.rb
blob: 3e67636e91ca0bd23009486fb353e92ed7dc9a34 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
module PixelatedService
  class Mail
    attr_reader :to, :cc, :bcc, :from, :subject, :body, :headers, :status, :draft_reply_for
    attr_accessor :ident, :security_casing

    def initialize(data = {})
      @ident = data[:ident]
      @to = data[:to]
      @cc = data[:cc]
      @bcc = data[:bcc]
      @from = data[:from]
      @subject = data[:subject]
      @body = data[:body]
      @headers = data[:headers] || {}
      @status = data[:status] || []
      @draft_reply_for = data[:draft_reply_for] || []
      self.tags = data[:tags] || Tags.new
    end

    def hash
      @ident.hash
    end

    def eql?(object)
      self == object
    end

    def tags=(t)
      @tags.mail = nil if @tags
      @tags = t
      @tags.mail = self if @tags
      t
    end

    def ==(object)
      @ident == object.ident
    end

    def tag_names
      @tags.names
    end

    def is_tagged?(t)
      @tags.is_tagged?(t)
    end

    def tag_names=(vs)
      to_remove = self.tag_names - vs
      to_add = vs - self.tag_names

      to_remove.each do |tn|
        self.remove_tag(tn)
      end

      to_add.each do |v|
        self.add_tag(v)
      end
    end

    def add_tag(nm)
      @tags.add_tag(nm)
    end

    def remove_tag(nm)
      @tags.remove(nm)
    end

    def has_trash_tag?
      tag_names.include? "trash"
    end

    def starred=(v); v ? add_status(:starred) : remove_status(:starred); end
    def starred?; status?(:starred); end
    def read=(v); v ? add_status(:read) : remove_status(:read); end
    def read?; status?(:read); end
    def replied=(v); v ? add_status(:replied) : remove_status(:replied); end
    def replied?; status?(:replied); end

    def add_status(n)
      unless self.status?(n)
        @status = @status + [n]
        @tags.added_status(n)
        PixelatedService.mail_service.stats_status_added(n, self)
      end
    end

    def remove_status(n)
      if self.status?(n)
        @status = @status - [n]
        @tags.removed_status(n)
        PixelatedService.mail_service.stats_status_removed(n, self)
      end
    end

    def status?(n)
      @status.include?(n)
    end

    def to_json(*args)
      {
        header: {
          to: Array(@to),
          from: @from,
          subject: @subject,
        }.merge(@headers).merge({date: @headers[:date].iso8601}),
        ident: ident,
        tags: @tags.names,
        status: @status,
        security_casing: @security_casing,
        draft_reply_for: @draft_reply_for,
        body: @body
      }.to_json(*args)
    end


    def self.from_json(obj, new_ident = nil)
      ident = obj['ident']
      draft_reply_for = obj['draft_reply_for']
      hdrs = obj['header']
      to = hdrs['to']
      cc = hdrs['cc']
      bcc = hdrs['bcc']
      from = hdrs['from']
      subject = hdrs['subject']
      new_hdrs = {}
      hdrs.each do |k,v|
        new_hdrs[k.to_sym] = v unless %w(from to subject).include?(k)
      end
      tag_names = obj['tags']
      st = obj['status']
      bd = obj['body']

      mail = new(:subject => subject,
          :from => from,
          :to => Array(to),
          :cc => Array(cc),
          :bcc => Array(bcc),
          :headers => new_hdrs,
          :status => st,
          :draft_reply_for => draft_reply_for,
          :ident => (ident.to_s.empty? ? new_ident : ident),
          :body => bd)

      tag_names.each do |tag_name|
        mail.add_tag tag_name
      end

      mail

    end

    def to_s
      ([
        ("#{INTERNAL_TO_EXTERNAL_HEADER[:to]}: #{format_header_value_out(:to, @to)}" if @to),
        ("#{INTERNAL_TO_EXTERNAL_HEADER[:from]}: #{format_header_value_out(:from, @from)}" if @from),
        ("#{INTERNAL_TO_EXTERNAL_HEADER[:subject]}: #{format_header_value_out(:subject, @subject)}" if @subject),
        ("#{INTERNAL_TO_EXTERNAL_HEADER[:x_tw_pixelated_tags]}: #{format_header_value_out(:x_tw_smail_tags, @tags.names)}" if !@tags.names.empty?),
        ("#{INTERNAL_TO_EXTERNAL_HEADER[:x_tw_pixelated_status]}: #{format_header_value_out(:x_tw_smail_status, @status)}" if !@status.empty?),
        ("#{INTERNAL_TO_EXTERNAL_HEADER[:x_tw_pixelated_ident]}: #{format_header_value_out(:x_tw_smail_ident, @ident)}"),
       ].compact + @headers.map { |k,v| "#{INTERNAL_TO_EXTERNAL_HEADER[k]}: #{format_header_value_out(k, v)}"}).sort.join("\n") + "\n\n#{@body}"
    end

    SPECIAL_HEADERS = [:subject, :from, :to, :x_tw_pixelated_tags, :x_tw_smail_status, :x_tw_smail_ident]
    INTERNAL_TO_EXTERNAL_HEADER = {
      :subject => "Subject",
      :date => "Date",
      :from => "From",
      :to => "To",
      :cc => "CC",
      :bcc => "BCC",
      :message_id => "Message-ID",
      :mime_version => "Mime-Version",
      :content_type => "Content-Type",
      :content_transfer_encoding => "Content-Transfer-Encoding",
      :x_tw_pixelated_tags => "X-TW-Pixelated-Tags",
      :x_tw_pixelated_status => "X-TW-Pixelated-Status",
      :x_tw_pixelated_ident => "X-TW-Pixelated-Ident",
    }

    def format_header_value_out(k,v)
      case k
      when :date
        v.strftime("%a, %d %b %Y %H:%M:%S %z")
      when :to, :cc, :bcc
        Array(v).join(", ")
      when :x_tw_pixelated_tags, :x_tw_smail_status
        v.join(", ")
      else
        v
      end
    end

    class << self
      def formatted_header(k, ls)
        format_header_value(k, ls[k] && ls[k][1])
      end

      def has_header(hdr_name, ls, val, otherwise)
        if ls[hdr_name]
          val
        else
          otherwise
        end
      end

      def time_rand from = (Time.now - 300000000), to = Time.now
        Time.at(from + rand * (to.to_f - from.to_f))
      end

      # io is String or IO
      def read(io, ident = nil)
        io = StringIO.new(io) if io.is_a? String
        headers = {}
        body = ""
        reading_headers = true
        previous_header = nil
        first = true
        io.each do |ln|
          if first && ln =~ /^From /
            # Ignore line delimiter things
          else
            if reading_headers
              if ln.chomp == ""
                reading_headers = false
              else
                if previous_header && ln =~ /^\s+/
                  previous_header[1] << " #{ln.strip}"
                else
                  key, value = ln.chomp.split(/: /, 2)
                  previous_header = [key, value]
                  headers[internal_header_name(key)] = previous_header
                end
              end
            else
              body << ln
            end
          end
          if first
            first = false
          end
        end

        header_data = {}
        headers.each do |k, (_, v)|
          unless special_header?(k)
            header_data[k] = format_header_value(k, v)
          end
        end

        unless header_data[:date]
          header_data[:date] = time_rand
        end


        new(:subject => formatted_header(:subject, headers),
            :from => formatted_header(:from, headers),
            :to => formatted_header(:to, headers),
            :headers => header_data,
            :tags => formatted_header(:x_tw_pixelated_tags, headers),
            :status => formatted_header(:x_tw_pixelated_status, headers),
            :ident => has_header(:x_tw_pixelated_ident, headers, formatted_header(:x_tw_smail_ident, headers), ident),
            :body => body
            )
      end


      private
      def internal_header_name(k)
        k.downcase.gsub(/-/, '_').to_sym
      end

      def special_header?(k)
        SPECIAL_HEADERS.include?(k)
      end

      def format_header_value(k, v)
        case k
        when :date
          DateTime.parse(v)
        when :to, :cc, :bcc
          vs = (v || "").split(/, /)
          if vs.length == 1
            vs[0]
          else
            vs
          end
        when :x_tw_pixelated_tags
          Tags.new *(v || "").split(/, /)
        when :x_tw_pixelated_status
          (v || "").split(/, /).map { |ss| ss.to_sym }
        when :x_tw_pixelated_ident
          v.to_i
        else
          v
        end
      end
    end
  end
end