File: keyed_mailbox.rb

package info (click to toggle)
rubyfilter 0.12-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 688 kB
  • ctags: 428
  • sloc: ruby: 3,228; makefile: 60
file content (233 lines) | stat: -rw-r--r-- 7,175 bytes parent folder | download | duplicates (3)
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
#--
#   Copyright (C) 2002, 2003 Matt Armstrong.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior
#    written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

require 'rfilter/deliver'
require 'digest/md5'
require 'timeout'

module RFilter

  # A KeyedMailbox object implements a message mailbox indexed by a
  # unique key string for each message.  When a message is saved into
  # the store, the message's key is returned.  Later, the key is used
  # to retrieve file name the message is stored in.
  #
  # The message store has the following characteristics:
  #
  # 1. It is a Maildir, so various mail programs can read the messages
  #    directly and without adversely affecting the mailbox.
  # 2. The key is very hard to guess.
  # 3. The key is short and can be included in a message subject or in
  #    the extension of a return address (suitable for mailing list style
  #    confirmations).
  class KeyedMailbox
    include RFilter::Deliver

    # Creates a confirmation queue object that manages a confirmation
    # queue in directory +path+.
    def initialize(path)
      @path = path.to_str.freeze
    end

    # Saves a message into a confirmation queue and returns a string
    # key that can be used to retrieve it later.  They key is a string
    # of hex digits and dashes, suitable for inclusion in a message
    # subject or "VERP" return address.
    def save(message)
      save_key(message, deliver_maildir(@path, message))
    end

    # Get the file name holding the message associated with +key+, or
    # returns nil if the message is missing.
    def retrieve(key)
      begin
        message_filename(dereference_key(key)).untaint
      rescue Errno::ENOENT
        nil
      end
    end

    # Given a key, delete the message associated with it.
    def delete(key)
      begin
        File.delete(*[key_filename(key),
                      message_filename(dereference_key(key))].compact)
      rescue Errno::ENOENT
      end
    end

    # Expire messages in the confirmation queue older than +age+ days
    # old.
    def expire(age)
      cutoff = Time.now - (60 * 60 * 24) * age
      [ File.join(@path, 'new', '*'),
        File.join(@path, 'cur', '*'),
        File.join(@path, '.index', '*')].each { |glob|
        Dir[glob].each { |file|
          File::delete(file) if File::mtime(file) <= cutoff
        }
      }
    end

    # Yield each key in the mailbox along with its most recent
    # modification time.
    def each_key
      Dir[File.join(@path, '.index', '*')].each { |file_name|
        key = File.basename(file_name)
        if valid_key(key)
          yield key
        end
      }
    end

    # Yield the time a given key was last modified as a Time object or
    # nil if the key doesn't exist.
    def key_mtime(key)
      raise ArgumentError, "expected a valid key" unless valid_key(key)
      File::mtime(File.join(@path, '.index', key))
    rescue Errno::ENOENT
      return nil
    end

    # Return the path of this keyed mailbox (same value passed in
    # #new).
    def path
      return @path
    end

    private

    def valid_key(key)
      (key =~ /^[A-Za-z\d]{10}$/) ? true : false
    end

    def key_filename(key)
      raise ArgumentError, key unless valid_key(key)
      File.join(@path, '.index', key.upcase).untaint
    end

    def dereference_key(key)
      IO.readlines(key_filename(key))[0]
    end

    def message_filename(basename)
      raise ArgumentError, basename unless basename =~ /^[\w\.]+$/
      basename.untaint
      catch(:found) {
        [ File.join(@path, 'new', basename),
          File.join(@path, 'cur', basename) ].each { |curr|
          Dir["#{curr}*"].each { |fullname|
            throw(:found, fullname.untaint)
          }
        }
        nil
      }
    end

    def save_key(message, maildir_filename)

      # First make the required directories
      begin
        Dir.mkdir(File.join(@path, '.index'))
      rescue Errno::EEXIST
        raise unless FileTest::directory?(@path)
      end

      hash = nil
      tmp_dir = File.join(@path, 'tmp')
      index_dir = File.join(@path, '.index')
      try_count = 1
      begin
        hash = hash_str(message)
        tmp_name = File.join(tmp_dir, hash)
        index_name = File.join(index_dir, hash)
        File.open(tmp_name, File::CREAT|File::EXCL|File::WRONLY|File::SYNC,
                  0600) { |f|
          f.print(File.basename(maildir_filename))
        }
        File.link(tmp_name, index_name)
      rescue Errno::EEXIST
        raise if try_count >= 5
        sleep(2)
        try_count = try_count.next
        retry
      ensure
        begin
          File.delete(tmp_name) unless tmp_name.nil?
        rescue Errno::ENOENT
        end
      end
      hash
    end

    def hash_str(message)

      # Hash the message, the current time, the current pid and
      # unambiguously separate them with the 0 byte.
      md5 = Digest::MD5::new
      md5.update(Marshal.dump(message))
      md5.update(0.chr)
      md5.update(Time.now.to_s)
      md5.update(0.chr)
      md5.update(Process.pid.to_s)

      # And if we have the last hash we generated, update with the last
      # two bytes of it.
      last_hash_name = File.join(@path, '.last_hash')
      begin
        timeout(10) {
          File.open(last_hash_name, 'rb') { |f|
            f.flock(File::LOCK_SH)
            f.seek(14)
            if last_two = f.read(2)
              md5.update(0.chr)
              md5.update(last_two)
            end
          }
        }
      rescue Errno::ENOENT
      rescue TimeoutError
      end

      hash = md5.digest

      begin
        timeout(10) {
          File.open(last_hash_name, 'wb', 0600) { |f|
            f.flock(File::LOCK_EX)
            f.print(hash)
          }
        }
      rescue TimeoutError
      end

      hash[0..4].unpack("H*")[0].upcase
    end
  end
end