summaryrefslogtreecommitdiff
path: root/Mailman/Pending.py
blob: 44b7c94e723c38dc3e91a9d9639412070a122208 (plain) (blame)
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
# Copyright (C) 1998,1999,2000,2001 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

""" Track pending confirmation of subscriptions.

new(stuff...) places an item's data in the db, returning its cookie.

confirmed(cookie) returns a tuple for the data, removing the item
from the db.  It returns None if the cookie is not registered.
"""

import os
import time
import sha
import marshal
import random
import errno

from Mailman import mm_cfg
from Mailman import LockFile

DBFILE = os.path.join(mm_cfg.DATA_DIR, 'pending.db')
LOCKFILE = os.path.join(mm_cfg.LOCK_DIR, 'pending.lock')

# Types of pending records
SUBSCRIPTION = 'S'
UNSUBSCRIPTION = 'U'



def new(*content):
    """Create a new entry in the pending database, returning cookie for it."""
    # It's a programming error if this assertion fails!
    assert content[:1] in ((SUBSCRIPTION,), (UNSUBSCRIPTION,))
    # Acquire the pending database lock, letting TimeOutError percolate up.
    lock = LockFile.LockFile(LOCKFILE)
    lock.lock(timeout=30)
    try:
        # Load the current database
        db = _load()
        # Calculate a unique cookie
        while 1:
            n = random.random()
            now = time.time()
            hashfood = str(now) + str(n) + str(content)
            cookie = sha.new(hashfood).hexdigest()
            if not db.has_key(cookie):
                break
        # Store the content, plus the time in the future when this entry will
        # be evicted from the database, due to staleness.
        db[cookie] = content
        evictions = db.setdefault('evictions', {})
        evictions[cookie] = now + mm_cfg.PENDING_REQUEST_LIFE
        _save(db)
        return cookie
    finally:
        lock.unlock()



def confirm(cookie):
    """Return data for cookie, removing it from db, or None if not found."""
    # Acquire the pending database lock, letting TimeOutError percolate up.
    lock = LockFile.LockFile(LOCKFILE)
    lock.lock(timeout=30)
    try:
        # Load the database
        db = _load()
        missing = []
        content = db.get(cookie, missing)
        if content is missing:
            return None
        # Remove the entry from the database
        del db[cookie]
        _save(db)
        return content
    finally:
        lock.unlock()



def _load():
    # Lock must be acquired.
    try:
        fp = open(DBFILE)
        return marshal.load(fp)
    except IOError, e:
        if e.errno <> errno.ENOENT: raise
        # No database yet, so initialize a fresh one
        return {'evictions': {}}


def _save(db):
    # Lock must be acquired.
    evictions = db['evictions']
    now = time.time()
    for cookie, data in db.items():
        if cookie in ('evictions', 'version'):
            continue
        timestamp = evictions[cookie]
        if now > timestamp:
            # The entry is stale, so remove it.
            del db[cookie]
            del evictions[cookie]
    db['version'] = mm_cfg.PENDING_FILE_SCHEMA_VERSION
    omask = os.umask(007)
    try:
        fp = open(DBFILE, 'w')
        marshal.dump(db, fp)
        fp.close()
    finally:
        os.umask(omask)



def _update(olddb):
    # Update an old pending_subscriptions.db database to the new format
    lock = LockFile.LockFile(LOCKFILE)
    lock.lock(timeout=30)
    try:
        # We don't need this entry anymore
        if olddb.has_key('lastculltime'):
            del olddb['lastculltime']
        db = _load()
        evictions = db.setdefault('evictions', {})
        for cookie, data in olddb.items():
            # The cookies used to be kept as a 6 digit integer.  We now keep
            # the cookies as a string (sha in our case, but it doesn't matter
            # for cookie matching).
            cookie = str(cookie)
            # The old format kept the content as a tuple and tacked the
            # timestamp on as the last element of the tuple.  We keep the
            # timestamps separate, but require the prepending of a record type
            # indicator.  We know that the only things that were kept in the
            # old format were subscription requests.  Also, the old request
            # format didn't have the subscription language.  Best we can do
            # here is use the server default.
            db[cookie] = (SUBSCRIPTION,) + data[:-1] + \
                         (mm_cfg.DEFAULT_SERVER_LANGUAGE,)
            # The old database format kept the timestamp as the time the
            # request was made.  The new format keeps it as the time the
            # request should be evicted.
            evictions[cookie] = data[-1] + mm_cfg.PENDING_REQUEST_LIFE
        _save(db)
    finally:
        lock.unlock()