imip-agent

Annotated imiptools/data.py

333:aa2e2008a863
2015-02-12 Paul Boddie Always convert mail addresses to lower case.
paul@213 1
#!/usr/bin/env python
paul@213 2
paul@213 3
"""
paul@213 4
Interpretation of vCalendar content.
paul@213 5
paul@213 6
Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
paul@213 7
paul@213 8
This program is free software; you can redistribute it and/or modify it under
paul@213 9
the terms of the GNU General Public License as published by the Free Software
paul@213 10
Foundation; either version 3 of the License, or (at your option) any later
paul@213 11
version.
paul@213 12
paul@213 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@213 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@213 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@213 16
details.
paul@213 17
paul@213 18
You should have received a copy of the GNU General Public License along with
paul@213 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@213 20
"""
paul@213 21
paul@256 22
from datetime import datetime, timedelta
paul@213 23
from email.mime.text import MIMEText
paul@291 24
from imiptools.dates import format_datetime, get_datetime, get_freebusy_period, \
paul@318 25
                            to_timezone, to_utc_datetime
paul@327 26
from imiptools.period import period_overlaps
paul@316 27
from pytz import timezone
paul@213 28
from vCalendar import iterwrite, parse, ParseError, to_dict, to_node
paul@256 29
from vRecurrence import get_parameters, get_rule
paul@213 30
import email.utils
paul@213 31
paul@213 32
try:
paul@213 33
    from cStringIO import StringIO
paul@213 34
except ImportError:
paul@213 35
    from StringIO import StringIO
paul@213 36
paul@213 37
class Object:
paul@213 38
paul@213 39
    "Access to calendar structures."
paul@213 40
paul@213 41
    def __init__(self, fragment):
paul@213 42
        self.objtype, (self.details, self.attr) = fragment.items()[0]
paul@213 43
paul@213 44
    def get_items(self, name, all=True):
paul@213 45
        return get_items(self.details, name, all)
paul@213 46
paul@213 47
    def get_item(self, name):
paul@213 48
        return get_item(self.details, name)
paul@213 49
paul@213 50
    def get_value_map(self, name):
paul@213 51
        return get_value_map(self.details, name)
paul@213 52
paul@213 53
    def get_values(self, name, all=True):
paul@213 54
        return get_values(self.details, name, all)
paul@213 55
paul@213 56
    def get_value(self, name):
paul@213 57
        return get_value(self.details, name)
paul@213 58
paul@213 59
    def get_utc_datetime(self, name):
paul@213 60
        return get_utc_datetime(self.details, name)
paul@213 61
paul@318 62
    def get_datetime(self, name):
paul@318 63
        dt, attr = get_datetime_item(self.details, name)
paul@318 64
        return dt
paul@318 65
paul@289 66
    def get_datetime_item(self, name):
paul@289 67
        return get_datetime_item(self.details, name)
paul@289 68
paul@213 69
    def to_node(self):
paul@213 70
        return to_node({self.objtype : [(self.details, self.attr)]})
paul@213 71
paul@213 72
    def to_part(self, method):
paul@213 73
        return to_part(method, [self.to_node()])
paul@213 74
paul@213 75
    # Direct access to the structure.
paul@213 76
paul@213 77
    def __getitem__(self, name):
paul@213 78
        return self.details[name]
paul@213 79
paul@213 80
    def __setitem__(self, name, value):
paul@213 81
        self.details[name] = value
paul@213 82
paul@213 83
    def __delitem__(self, name):
paul@213 84
        del self.details[name]
paul@213 85
paul@256 86
    # Computed results.
paul@256 87
paul@318 88
    def get_periods(self, tzid, window_size=100):
paul@318 89
        return get_periods(self, tzid, window_size)
paul@256 90
paul@291 91
    def get_periods_for_freebusy(self, tzid, window_size=100):
paul@318 92
        periods = self.get_periods(tzid, window_size)
paul@291 93
        return get_periods_for_freebusy(self, periods, tzid)
paul@291 94
paul@213 95
# Construction and serialisation.
paul@213 96
paul@213 97
def make_calendar(nodes, method=None):
paul@213 98
paul@213 99
    """
paul@213 100
    Return a complete calendar node wrapping the given 'nodes' and employing the
paul@213 101
    given 'method', if indicated.
paul@213 102
    """
paul@213 103
paul@213 104
    return ("VCALENDAR", {},
paul@213 105
            (method and [("METHOD", {}, method)] or []) +
paul@213 106
            [("VERSION", {}, "2.0")] +
paul@213 107
            nodes
paul@213 108
           )
paul@213 109
paul@327 110
def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None,
paul@327 111
    attendee_attr=None, dtstart=None, dtend=None):
paul@222 112
    
paul@222 113
    """
paul@222 114
    Return a calendar node defining the free/busy details described in the given
paul@292 115
    'freebusy' list, employing the given 'uid', for the given 'organiser' and
paul@292 116
    optional 'organiser_attr', with the optional 'attendee' providing recipient
paul@292 117
    details together with the optional 'attendee_attr'.
paul@327 118
paul@327 119
    The result will be constrained to the 'dtstart' and 'dtend' period if these
paul@327 120
    parameters are given.
paul@222 121
    """
paul@222 122
    
paul@222 123
    record = []
paul@222 124
    rwrite = record.append
paul@222 125
    
paul@292 126
    rwrite(("ORGANIZER", organiser_attr or {}, organiser))
paul@222 127
paul@222 128
    if attendee:
paul@292 129
        rwrite(("ATTENDEE", attendee_attr or {}, attendee)) 
paul@222 130
paul@222 131
    rwrite(("UID", {}, uid))
paul@222 132
paul@222 133
    if freebusy:
paul@327 134
paul@327 135
        # Get a constrained view if start and end limits are specified.
paul@327 136
paul@327 137
        periods = dtstart and dtend and period_overlaps(freebusy, (dtstart, dtend), True) or freebusy
paul@327 138
paul@327 139
        # Write the limits of the resource.
paul@327 140
paul@327 141
        rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, periods[0][0]))
paul@327 142
        rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, periods[-1][1]))
paul@327 143
paul@327 144
        for start, end, uid, transp in periods:
paul@222 145
            if transp == "OPAQUE":
paul@222 146
                rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end])))
paul@222 147
paul@222 148
    return ("VFREEBUSY", {}, record)
paul@222 149
paul@213 150
def parse_object(f, encoding, objtype=None):
paul@213 151
paul@213 152
    """
paul@213 153
    Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is
paul@213 154
    given, only objects of that type will be returned. Otherwise, the root of
paul@213 155
    the content will be returned as a dictionary with a single key indicating
paul@213 156
    the object type.
paul@213 157
paul@213 158
    Return None if the content was not readable or suitable.
paul@213 159
    """
paul@213 160
paul@213 161
    try:
paul@213 162
        try:
paul@213 163
            doctype, attrs, elements = obj = parse(f, encoding=encoding)
paul@213 164
            if objtype and doctype == objtype:
paul@213 165
                return to_dict(obj)[objtype][0]
paul@213 166
            elif not objtype:
paul@213 167
                return to_dict(obj)
paul@213 168
        finally:
paul@213 169
            f.close()
paul@213 170
paul@213 171
    # NOTE: Handle parse errors properly.
paul@213 172
paul@213 173
    except (ParseError, ValueError):
paul@213 174
        pass
paul@213 175
paul@213 176
    return None
paul@213 177
paul@213 178
def to_part(method, calendar):
paul@213 179
paul@213 180
    """
paul@213 181
    Write using the given 'method', the 'calendar' details to a MIME
paul@213 182
    text/calendar part.
paul@213 183
    """
paul@213 184
paul@213 185
    encoding = "utf-8"
paul@213 186
    out = StringIO()
paul@213 187
    try:
paul@213 188
        to_stream(out, make_calendar(calendar, method), encoding)
paul@213 189
        part = MIMEText(out.getvalue(), "calendar", encoding)
paul@213 190
        part.set_param("method", method)
paul@213 191
        return part
paul@213 192
paul@213 193
    finally:
paul@213 194
        out.close()
paul@213 195
paul@213 196
def to_stream(out, fragment, encoding="utf-8"):
paul@213 197
    iterwrite(out, encoding=encoding).append(fragment)
paul@213 198
paul@213 199
# Structure access functions.
paul@213 200
paul@213 201
def get_items(d, name, all=True):
paul@213 202
paul@213 203
    """
paul@213 204
    Get all items from 'd' for the given 'name', returning single items if
paul@213 205
    'all' is specified and set to a false value and if only one value is
paul@213 206
    present for the name. Return None if no items are found for the name or if
paul@213 207
    many items are found but 'all' is set to a false value.
paul@213 208
    """
paul@213 209
paul@213 210
    if d.has_key(name):
paul@213 211
        values = d[name]
paul@213 212
        if all:
paul@213 213
            return values
paul@213 214
        elif len(values) == 1:
paul@213 215
            return values[0]
paul@213 216
        else:
paul@213 217
            return None
paul@213 218
    else:
paul@213 219
        return None
paul@213 220
paul@213 221
def get_item(d, name):
paul@213 222
    return get_items(d, name, False)
paul@213 223
paul@213 224
def get_value_map(d, name):
paul@213 225
paul@213 226
    """
paul@213 227
    Return a dictionary for all items in 'd' having the given 'name'. The
paul@213 228
    dictionary will map values for the name to any attributes or qualifiers
paul@213 229
    that may have been present.
paul@213 230
    """
paul@213 231
paul@213 232
    items = get_items(d, name)
paul@213 233
    if items:
paul@213 234
        return dict(items)
paul@213 235
    else:
paul@213 236
        return {}
paul@213 237
paul@213 238
def get_values(d, name, all=True):
paul@213 239
    if d.has_key(name):
paul@213 240
        values = d[name]
paul@213 241
        if not all and len(values) == 1:
paul@213 242
            return values[0][0]
paul@213 243
        else:
paul@213 244
            return map(lambda x: x[0], values)
paul@213 245
    else:
paul@213 246
        return None
paul@213 247
paul@213 248
def get_value(d, name):
paul@213 249
    return get_values(d, name, False)
paul@213 250
paul@213 251
def get_utc_datetime(d, name):
paul@289 252
    dt, attr = get_datetime_item(d, name)
paul@289 253
    return to_utc_datetime(dt)
paul@289 254
paul@289 255
def get_datetime_item(d, name):
paul@213 256
    value, attr = get_item(d, name)
paul@289 257
    return get_datetime(value, attr), attr
paul@213 258
paul@213 259
def get_addresses(values):
paul@213 260
    return [address for name, address in email.utils.getaddresses(values)]
paul@213 261
paul@213 262
def get_address(value):
paul@333 263
    value = value.lower()
paul@333 264
    return value.startswith("mailto:") and value[7:] or value
paul@213 265
paul@213 266
def get_uri(value):
paul@213 267
    return value.lower().startswith("mailto:") and value.lower() or ":" in value and value or "mailto:%s" % value.lower()
paul@213 268
paul@309 269
uri_value = get_uri
paul@309 270
paul@309 271
def uri_values(values):
paul@309 272
    return map(get_uri, values)
paul@309 273
paul@213 274
def uri_dict(d):
paul@213 275
    return dict([(get_uri(key), value) for key, value in d.items()])
paul@213 276
paul@213 277
def uri_item(item):
paul@213 278
    return get_uri(item[0]), item[1]
paul@213 279
paul@213 280
def uri_items(items):
paul@213 281
    return [(get_uri(value), attr) for value, attr in items]
paul@213 282
paul@220 283
# Operations on structure data.
paul@220 284
paul@220 285
def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, partstat_set):
paul@220 286
paul@220 287
    """
paul@220 288
    Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and
paul@220 289
    'new_dtstamp', and the 'partstat_set' indication, whether the object
paul@220 290
    providing the new information is really newer than the object providing the
paul@220 291
    old information.
paul@220 292
    """
paul@220 293
paul@220 294
    have_sequence = old_sequence is not None and new_sequence is not None
paul@220 295
    is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence)
paul@220 296
paul@220 297
    have_dtstamp = old_dtstamp and new_dtstamp
paul@220 298
    is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp
paul@220 299
paul@220 300
    is_old_sequence = have_sequence and (
paul@220 301
        int(new_sequence) < int(old_sequence) or
paul@220 302
        is_same_sequence and is_old_dtstamp
paul@220 303
        )
paul@220 304
paul@220 305
    return is_same_sequence and partstat_set or not is_old_sequence
paul@220 306
paul@256 307
# NOTE: Need to expose the 100 day window for recurring events in the
paul@256 308
# NOTE: configuration.
paul@256 309
paul@318 310
def get_periods(obj, tzid, window_size=100):
paul@256 311
paul@256 312
    """
paul@256 313
    Return periods for the given object 'obj', confining materialised periods
paul@256 314
    to the given 'window_size' in days starting from the present moment.
paul@256 315
    """
paul@256 316
paul@318 317
    # NOTE: Need also RDATE and EXDATE support.
paul@318 318
paul@318 319
    rrule = obj.get_value("RRULE")
paul@318 320
paul@318 321
    if not rrule:
paul@320 322
        return [(obj.get_datetime("DTSTART"), obj.get_datetime("DTEND"))]
paul@318 323
paul@318 324
    # Use localised datetimes.
paul@318 325
paul@318 326
    dtstart, start_attr = obj.get_datetime_item("DTSTART")
paul@318 327
    dtend, end_attr = obj.get_datetime_item("DTEND")
paul@318 328
paul@318 329
    tzid = start_attr.get("TZID") or end_attr.get("TZID") or tzid
paul@256 330
paul@256 331
    # NOTE: Need also DURATION support.
paul@256 332
paul@256 333
    duration = dtend - dtstart
paul@256 334
paul@256 335
    # Recurrence rules create multiple instances to be checked.
paul@256 336
    # Conflicts may only be assessed within a period defined by policy
paul@256 337
    # for the agent, with instances outside that period being considered
paul@256 338
    # unchecked.
paul@256 339
paul@318 340
    window_end = to_timezone(datetime.now(), tzid) + timedelta(window_size)
paul@256 341
paul@318 342
    selector = get_rule(dtstart, rrule)
paul@318 343
    parameters = get_parameters(rrule)
paul@318 344
    periods = []
paul@256 345
paul@318 346
    for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
paul@318 347
        start = to_timezone(datetime(*start), tzid)
paul@318 348
        end = start + duration
paul@318 349
        periods.append((start, end))
paul@256 350
paul@256 351
    return periods
paul@256 352
paul@291 353
def get_periods_for_freebusy(obj, periods, tzid):
paul@291 354
paul@306 355
    """
paul@306 356
    Get free/busy-compliant periods employed by 'obj' from the given 'periods',
paul@306 357
    using the indicated 'tzid' to convert dates to datetimes.
paul@306 358
    """
paul@306 359
paul@291 360
    start, start_attr = obj.get_datetime_item("DTSTART")
paul@291 361
    end, end_attr = obj.get_datetime_item("DTEND")
paul@291 362
paul@291 363
    tzid = start_attr.get("TZID") or end_attr.get("TZID") or tzid
paul@291 364
paul@291 365
    l = []
paul@291 366
paul@291 367
    for start, end in periods:
paul@291 368
        start, end = get_freebusy_period(start, end, tzid)
paul@320 369
        start, end = [to_timezone(x, "UTC") for x in start, end]
paul@291 370
        l.append((format_datetime(start), format_datetime(end)))
paul@291 371
paul@291 372
    return l
paul@291 373
paul@213 374
# vim: tabstop=4 expandtab shiftwidth=4