~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to servers/exchange/lib/e2k-restriction.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
 
 
3
 
/* Copyright (C) 2001-2004 Novell, Inc.
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of version 2 of the GNU Lesser General Public
7
 
 * License as published by the Free Software Foundation.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Lesser General Public
15
 
 * License along with this program; if not, write to the
16
 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
 
 * Boston, MA 02110-1301, USA.
18
 
 */
19
 
 
20
 
/* e2k-restriction.c: message restrictions (WHERE clauses / Rule conditions) */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
 
26
 
#include "e2k-restriction.h"
27
 
#include "e2k-properties.h"
28
 
#include "e2k-rule.h"
29
 
 
30
 
#include <stdarg.h>
31
 
#include <string.h>
32
 
 
33
 
static E2kRestriction *
34
 
conjoin (E2kRestrictionType type, gint nrns, E2kRestriction **rns, gboolean unref)
35
 
{
36
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
37
 
        gint i;
38
 
 
39
 
        ret->type = type;
40
 
        ret->res.and.nrns = nrns;
41
 
        ret->res.and.rns = g_new (E2kRestriction *, nrns);
42
 
        for (i = 0; i < nrns; i++) {
43
 
                ret->res.and.rns[i] = rns[i];
44
 
                if (!unref)
45
 
                        e2k_restriction_ref (rns[i]);
46
 
        }
47
 
 
48
 
        return ret;
49
 
}
50
 
 
51
 
/**
52
 
 * e2k_restriction_and:
53
 
 * @nrns: length of @rns
54
 
 * @rns: an array of #E2kRestriction
55
 
 * @unref: whether or not to unref the restrictions when it is done
56
 
 *
57
 
 * Creates a new restriction which is true if all of the restrictions
58
 
 * in @rns are true.
59
 
 *
60
 
 * If @unref is %TRUE, then e2k_restriction_and() is essentially
61
 
 * stealing the caller's references on the restrictions. If it is
62
 
 * %FALSE, then e2k_restriction_and() will acquire its own references
63
 
 * to each of the restrictions.
64
 
 *
65
 
 * Return value: the new restriction
66
 
 **/
67
 
E2kRestriction *
68
 
e2k_restriction_and (gint nrns, E2kRestriction **rns, gboolean unref)
69
 
{
70
 
        return conjoin (E2K_RESTRICTION_AND, nrns, rns, unref);
71
 
}
72
 
 
73
 
/**
74
 
 * e2k_restriction_or:
75
 
 * @nrns: length of @rns
76
 
 * @rns: an array of #E2kRestriction
77
 
 * @unref: see e2k_restriction_and()
78
 
 *
79
 
 * Creates a new restriction which is true if any of the restrictions
80
 
 * in @rns are true.
81
 
 *
82
 
 * Return value: the new restriction
83
 
 **/
84
 
E2kRestriction *
85
 
e2k_restriction_or (gint nrns, E2kRestriction **rns, gboolean unref)
86
 
{
87
 
        return conjoin (E2K_RESTRICTION_OR, nrns, rns, unref);
88
 
}
89
 
 
90
 
static E2kRestriction *
91
 
conjoinv (E2kRestrictionType type, E2kRestriction *rn, va_list ap)
92
 
{
93
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
94
 
        GPtrArray *rns;
95
 
 
96
 
        rns = g_ptr_array_new ();
97
 
        while (rn) {
98
 
                g_ptr_array_add (rns, rn);
99
 
                rn = va_arg (ap, E2kRestriction *);
100
 
        }
101
 
        va_end (ap);
102
 
 
103
 
        ret->type = type;
104
 
        ret->res.and.nrns = rns->len;
105
 
        ret->res.and.rns = (E2kRestriction **)rns->pdata;
106
 
        g_ptr_array_free (rns, FALSE);
107
 
 
108
 
        return ret;
109
 
}
110
 
 
111
 
/**
112
 
 * e2k_restriction_andv:
113
 
 * @rn: an #E2kRestriction
114
 
 * @...: a %NULL-terminated list of additional #E2kRestrictions
115
 
 *
116
 
 * Creates a new restriction which is true if all of the passed-in
117
 
 * restrictions are true. e2k_restriction_andv() steals the caller's
118
 
 * reference on each of the passed-in restrictions.
119
 
 *
120
 
 * Return value: the new restriction
121
 
 **/
122
 
E2kRestriction *
123
 
e2k_restriction_andv (E2kRestriction *rn, ...)
124
 
{
125
 
        va_list ap;
126
 
 
127
 
        va_start (ap, rn);
128
 
        return conjoinv (E2K_RESTRICTION_AND, rn, ap);
129
 
}
130
 
 
131
 
/**
132
 
 * e2k_restriction_orv:
133
 
 * @rn: an #E2kRestriction
134
 
 * @...: a %NULL-terminated list of additional #E2kRestrictions
135
 
 *
136
 
 * Creates a new restriction which is true if any of the passed-in
137
 
 * restrictions are true. e2k_restriction_orv() steals the caller's
138
 
 * reference on each of the passed-in restrictions.
139
 
 *
140
 
 * Return value: the new restriction
141
 
 **/
142
 
E2kRestriction *
143
 
e2k_restriction_orv (E2kRestriction *rn, ...)
144
 
{
145
 
        va_list ap;
146
 
 
147
 
        va_start (ap, rn);
148
 
        return conjoinv (E2K_RESTRICTION_OR, rn, ap);
149
 
}
150
 
 
151
 
/**
152
 
 * e2k_restriction_not:
153
 
 * @rn: an #E2kRestriction
154
 
 * @unref: see e2k_restriction_and()
155
 
 *
156
 
 * Creates a new restriction which is true if @rn is false.
157
 
 *
158
 
 * Return value: the new restriction
159
 
 **/
160
 
E2kRestriction *
161
 
e2k_restriction_not (E2kRestriction *rn, gboolean unref)
162
 
{
163
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
164
 
 
165
 
        ret->type = E2K_RESTRICTION_NOT;
166
 
        ret->res.not.rn = rn;
167
 
        if (!unref)
168
 
                e2k_restriction_ref (rn);
169
 
 
170
 
        return ret;
171
 
}
172
 
 
173
 
/**
174
 
 * e2k_restriction_content:
175
 
 * @propname: text property to compare against
176
 
 * @fuzzy_level: how to compare
177
 
 * @value: value to compare against
178
 
 *
179
 
 * Creates a new restriction that is true for objects where the
180
 
 * indicated property's value matches @value according to @fuzzy_level.
181
 
 *
182
 
 * For a WebDAV SEARCH, @fuzzy_level should be %E2K_FL_FULLSTRING,
183
 
 * %E2K_FL_SUBSTRING, %E2K_FL_PREFIX, or %E2K_FL_SUFFIX.
184
 
 *
185
 
 * For a MAPI restriction, @fuzzy_level may not be %E2K_FL_SUFFIX, but
186
 
 * may be ORed with any of the additional values %E2K_FL_IGNORECASE,
187
 
 * %E2K_FL_IGNORENONSPACE, or %E2K_FL_LOOSE.
188
 
 *
189
 
 * To compare a property's sort order to another string, use
190
 
 * e2k_restriction_prop_string().
191
 
 *
192
 
 * Return value: the new restriction
193
 
 **/
194
 
E2kRestriction *
195
 
e2k_restriction_content (const gchar *propname,
196
 
                         E2kRestrictionFuzzyLevel fuzzy_level,
197
 
                         const gchar *value)
198
 
{
199
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
200
 
 
201
 
        ret->type = E2K_RESTRICTION_CONTENT;
202
 
        ret->res.content.fuzzy_level = fuzzy_level;
203
 
        e2k_rule_prop_set (&ret->res.content.pv.prop, propname);
204
 
        ret->res.content.pv.type = E2K_PROP_TYPE_STRING;
205
 
        ret->res.content.pv.value = g_strdup (value);
206
 
 
207
 
        return ret;
208
 
}
209
 
 
210
 
/**
211
 
 * e2k_restriction_prop_bool:
212
 
 * @propname: boolean property to compare against
213
 
 * @relop: %E2K_RELOP_EQ or %E2K_RELOP_NE
214
 
 * @value: %TRUE or %FALSE
215
 
 *
216
 
 * Creates a new restriction that is true for objects where the
217
 
 * indicated property matches @relop and @value.
218
 
 *
219
 
 * Return value: the new restriction
220
 
 **/
221
 
E2kRestriction *
222
 
e2k_restriction_prop_bool (const gchar *propname, E2kRestrictionRelop relop,
223
 
                           gboolean value)
224
 
{
225
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
226
 
 
227
 
        ret->type = E2K_RESTRICTION_PROPERTY;
228
 
        ret->res.property.relop = relop;
229
 
        e2k_rule_prop_set (&ret->res.property.pv.prop, propname);
230
 
        ret->res.property.pv.type = E2K_PROP_TYPE_BOOL;
231
 
        ret->res.property.pv.value = GUINT_TO_POINTER (value);
232
 
 
233
 
        return ret;
234
 
}
235
 
 
236
 
/**
237
 
 * e2k_restriction_prop_int:
238
 
 * @propname: integer property to compare against
239
 
 * @relop: an #E2kRestrictionRelop
240
 
 * @value: number to compare against
241
 
 *
242
 
 * Creates a new restriction that is true for objects where the
243
 
 * indicated property matches @value according to @relop.
244
 
 *
245
 
 * Return value: the new restriction
246
 
 **/
247
 
E2kRestriction *
248
 
e2k_restriction_prop_int (const gchar *propname, E2kRestrictionRelop relop,
249
 
                          gint value)
250
 
{
251
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
252
 
 
253
 
        ret->type = E2K_RESTRICTION_PROPERTY;
254
 
        ret->res.property.relop = relop;
255
 
        e2k_rule_prop_set (&ret->res.property.pv.prop, propname);
256
 
        ret->res.property.pv.type = E2K_PROP_TYPE_INT;
257
 
        ret->res.property.pv.value = GINT_TO_POINTER (value);
258
 
 
259
 
        return ret;
260
 
}
261
 
 
262
 
/**
263
 
 * e2k_restriction_prop_date:
264
 
 * @propname: date/time property to compare against
265
 
 * @relop: an #E2kRestrictionRelop
266
 
 * @value: date/time to compare against (as returned by e2k_make_timestamp())
267
 
 *
268
 
 * Creates a new restriction that is true for objects where the
269
 
 * indicated property matches @value according to @relop.
270
 
 *
271
 
 * Return value: the new restriction
272
 
 **/
273
 
E2kRestriction *
274
 
e2k_restriction_prop_date (const gchar *propname, E2kRestrictionRelop relop,
275
 
                           const gchar *value)
276
 
{
277
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
278
 
 
279
 
        ret->type = E2K_RESTRICTION_PROPERTY;
280
 
        ret->res.property.relop = relop;
281
 
        e2k_rule_prop_set (&ret->res.property.pv.prop, propname);
282
 
        ret->res.property.pv.type = E2K_PROP_TYPE_DATE;
283
 
        ret->res.property.pv.value = g_strdup (value);
284
 
 
285
 
        return ret;
286
 
}
287
 
 
288
 
/**
289
 
 * e2k_restriction_prop_string:
290
 
 * @propname: text property to compare against
291
 
 * @relop: an #E2kRestrictionRelop
292
 
 * @value: text to compare against
293
 
 *
294
 
 * Creates a new restriction that is true for objects where the
295
 
 * indicated property matches @value according to @relop.
296
 
 *
297
 
 * To do a substring match, use e2k_restriction_content().
298
 
 *
299
 
 * Return value: the new restriction
300
 
 **/
301
 
E2kRestriction *
302
 
e2k_restriction_prop_string (const gchar *propname, E2kRestrictionRelop relop,
303
 
                             const gchar *value)
304
 
{
305
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
306
 
 
307
 
        ret->type = E2K_RESTRICTION_PROPERTY;
308
 
        ret->res.property.relop = relop;
309
 
        e2k_rule_prop_set (&ret->res.property.pv.prop, propname);
310
 
        ret->res.property.pv.type = E2K_PROP_TYPE_STRING;
311
 
        ret->res.property.pv.value = g_strdup (value);
312
 
 
313
 
        return ret;
314
 
}
315
 
 
316
 
/**
317
 
 * e2k_restriction_prop_binary:
318
 
 * @propname: binary property to compare against
319
 
 * @relop: %E2K_RELOP_EQ or %E2K_RELOP_NE
320
 
 * @data: data to compare against
321
 
 * @len: length of @data
322
 
 *
323
 
 * Creates a new restriction that is true for objects where the
324
 
 * indicated property matches @value according to @relop.
325
 
 *
326
 
 * Return value: the new restriction
327
 
 **/
328
 
E2kRestriction *
329
 
e2k_restriction_prop_binary (const gchar *propname, E2kRestrictionRelop relop,
330
 
                             gconstpointer data, gint len)
331
 
{
332
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
333
 
 
334
 
        ret->type = E2K_RESTRICTION_PROPERTY;
335
 
        ret->res.property.relop = relop;
336
 
        e2k_rule_prop_set (&ret->res.property.pv.prop, propname);
337
 
        ret->res.property.pv.type = E2K_PROP_TYPE_BINARY;
338
 
        ret->res.property.pv.value = g_byte_array_new ();
339
 
        g_byte_array_append (ret->res.property.pv.value, data, len);
340
 
 
341
 
        return ret;
342
 
}
343
 
 
344
 
/**
345
 
 * e2k_restriction_compare:
346
 
 * @propname1: first property
347
 
 * @relop: an #E2kRestrictionRelop
348
 
 * @propname2: second property
349
 
 *
350
 
 * Creates a new restriction which is true for objects where
351
 
 * @propname1 and @propname2 have the relationship described by
352
 
 * @relop.
353
 
 *
354
 
 * Return value: the new restriction
355
 
 **/
356
 
E2kRestriction *
357
 
e2k_restriction_compare (const gchar *propname1, E2kRestrictionRelop relop,
358
 
                         const gchar *propname2)
359
 
{
360
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
361
 
 
362
 
        ret->type = E2K_RESTRICTION_COMPAREPROPS;
363
 
        ret->res.compare.relop = relop;
364
 
        e2k_rule_prop_set (&ret->res.compare.prop1, propname1);
365
 
        e2k_rule_prop_set (&ret->res.compare.prop2, propname2);
366
 
 
367
 
        return ret;
368
 
}
369
 
 
370
 
/**
371
 
 * e2k_restriction_bitmask:
372
 
 * @propname: integer property to compare
373
 
 * @bitop: an #E2kRestrictionBitop
374
 
 * @mask: mask of bits to compare against
375
 
 *
376
 
 * Creates a new restriction that is true for objects where the
377
 
 * indicated bits of the value of @propname either are or aren't zero,
378
 
 * as indicated by @bitop.
379
 
 *
380
 
 * This cannot be used for WebDAV SEARCH restrictions.
381
 
 *
382
 
 * Return value: the new restriction
383
 
 **/
384
 
E2kRestriction *
385
 
e2k_restriction_bitmask (const gchar *propname, E2kRestrictionBitop bitop,
386
 
                         guint32 mask)
387
 
{
388
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
389
 
 
390
 
        ret->type = E2K_RESTRICTION_BITMASK;
391
 
        ret->res.bitmask.bitop = bitop;
392
 
        e2k_rule_prop_set (&ret->res.bitmask.prop, propname);
393
 
        ret->res.bitmask.mask = mask;
394
 
 
395
 
        return ret;
396
 
}
397
 
 
398
 
/**
399
 
 * e2k_restriction_size:
400
 
 * @propname: property to compare
401
 
 * @relop: an #E2kRestrictionRelop
402
 
 * @size: the size to compare @propname to
403
 
 *
404
 
 * Creates a new restriction which is true for objects where the size
405
 
 * of the value of @propname matches @size according to @relop.
406
 
 *
407
 
 * This cannot be used for WebDAV SEARCH restrictions.
408
 
 *
409
 
 * You probably do not want to use this. The standard idiom for
410
 
 * checking the size of a message is to use e2k_restriction_prop_int()
411
 
 * on its %PR_MESSAGE_SIZE property, not to use e2k_restriction_size()
412
 
 * on its %PR_BODY.
413
 
 *
414
 
 * Return value: the new restriction
415
 
 **/
416
 
E2kRestriction *
417
 
e2k_restriction_size (const gchar *propname, E2kRestrictionRelop relop,
418
 
                      guint32 size)
419
 
{
420
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
421
 
 
422
 
        ret->type = E2K_RESTRICTION_SIZE;
423
 
        ret->res.size.relop = relop;
424
 
        e2k_rule_prop_set (&ret->res.size.prop, propname);
425
 
        ret->res.size.size = size;
426
 
 
427
 
        return ret;
428
 
}
429
 
 
430
 
/**
431
 
 * e2k_restriction_exist:
432
 
 * @propname: property to check
433
 
 *
434
 
 * Creates a new restriction which is true for objects that have
435
 
 * a @propname property.
436
 
 *
437
 
 * This cannot be used for WebDAV SEARCH restrictions.
438
 
 *
439
 
 * Return value: the new restriction
440
 
 **/
441
 
E2kRestriction *
442
 
e2k_restriction_exist (const gchar *propname)
443
 
{
444
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
445
 
 
446
 
        ret->type = E2K_RESTRICTION_EXIST;
447
 
        e2k_rule_prop_set (&ret->res.exist.prop, propname);
448
 
 
449
 
        return ret;
450
 
}
451
 
 
452
 
/**
453
 
 * e2k_restriction_sub:
454
 
 * @subtable: the WebDAV name of a MAPI property of type PT_OBJECT
455
 
 * @rn: the restriction to apply against the values of @subtable
456
 
 * @unref: see e2k_restriction_and()
457
 
 *
458
 
 * Creates a new restriction that is true for objects where @rn is
459
 
 * true when applied to the value of @subtable on that object.
460
 
 *
461
 
 * @subtable is generally %PR_MESSAGE_RECIPIENTS (for finding messages
462
 
 * whose recipients match a given restriction) or
463
 
 * %PR_MESSAGE_ATTACHMENTS (for finding messages whose attachments
464
 
 * match a given restriction).
465
 
 *
466
 
 * This cannot be used for WebDAV SEARCH restrictions.
467
 
 *
468
 
 * Return value: the new restriction
469
 
 **/
470
 
E2kRestriction *
471
 
e2k_restriction_sub (const gchar *subtable, E2kRestriction *rn, gboolean unref)
472
 
{
473
 
        E2kRestriction *ret = g_new0 (E2kRestriction, 1);
474
 
 
475
 
        ret->type = E2K_RESTRICTION_SUBRESTRICTION;
476
 
        e2k_rule_prop_set (&ret->res.sub.subtable, subtable);
477
 
        ret->res.sub.rn = rn;
478
 
        if (!unref)
479
 
                e2k_restriction_ref (rn);
480
 
 
481
 
        return ret;
482
 
}
483
 
 
484
 
/**
485
 
 * e2k_restriction_unref:
486
 
 * @rn: a restriction
487
 
 *
488
 
 * Unrefs @rn. If there are no more references to @rn, it is freed.
489
 
 **/
490
 
void
491
 
e2k_restriction_unref (E2kRestriction *rn)
492
 
{
493
 
        gint i;
494
 
 
495
 
        if (rn->ref_count--)
496
 
                return;
497
 
 
498
 
        switch (rn->type) {
499
 
        case E2K_RESTRICTION_AND:
500
 
        case E2K_RESTRICTION_OR:
501
 
                for (i = 0; i < rn->res.and.nrns; i++)
502
 
                        e2k_restriction_unref (rn->res.and.rns[i]);
503
 
                g_free (rn->res.and.rns);
504
 
                break;
505
 
 
506
 
        case E2K_RESTRICTION_NOT:
507
 
                e2k_restriction_unref (rn->res.not.rn);
508
 
                break;
509
 
 
510
 
        case E2K_RESTRICTION_CONTENT:
511
 
                e2k_rule_free_propvalue (&rn->res.content.pv);
512
 
                break;
513
 
 
514
 
        case E2K_RESTRICTION_PROPERTY:
515
 
                e2k_rule_free_propvalue (&rn->res.property.pv);
516
 
                break;
517
 
 
518
 
        default:
519
 
                break;
520
 
        }
521
 
 
522
 
        g_free (rn);
523
 
}
524
 
 
525
 
/**
526
 
 * e2k_restriction_ref:
527
 
 * @rn: a restriction
528
 
 *
529
 
 * Refs @rn.
530
 
 **/
531
 
void
532
 
e2k_restriction_ref (E2kRestriction *rn)
533
 
{
534
 
        rn->ref_count++;
535
 
}
536
 
 
537
 
/* SQL export */
538
 
 
539
 
static gboolean rn_to_sql (E2kRestriction *rn, GString *sql, E2kRestrictionType inside);
540
 
 
541
 
static const gchar *sql_relops[] = { "<", "<=", ">", ">=", "=", "!=" };
542
 
static const gint n_sql_relops = G_N_ELEMENTS (sql_relops);
543
 
 
544
 
static gboolean
545
 
rns_to_sql (E2kRestrictionType type, E2kRestriction **rns, gint nrns, GString *sql)
546
 
{
547
 
        gint i;
548
 
        gboolean need_op = FALSE;
549
 
        gboolean rv = FALSE;
550
 
 
551
 
        for (i = 0; i < nrns; i++) {
552
 
                if (need_op) {
553
 
                        g_string_append (sql, type == E2K_RESTRICTION_AND ?
554
 
                                         " AND " : " OR ");
555
 
                        need_op = FALSE;
556
 
                }
557
 
                if (rn_to_sql (rns[i], sql, type)) {
558
 
                        need_op = TRUE;
559
 
                        rv = TRUE;
560
 
                }
561
 
        }
562
 
        return rv;
563
 
}
564
 
 
565
 
static void
566
 
append_sql_quoted (GString *sql, const gchar *string)
567
 
{
568
 
        while (*string) {
569
 
                if (*string == '\'')
570
 
                        g_string_append (sql, "''");
571
 
                else
572
 
                        g_string_append_c (sql, *string);
573
 
                string++;
574
 
        }
575
 
}
576
 
 
577
 
static gboolean
578
 
rn_to_sql (E2kRestriction *rn, GString *sql, E2kRestrictionType inside)
579
 
{
580
 
        E2kPropValue *pv;
581
 
 
582
 
        switch (rn->type) {
583
 
        case E2K_RESTRICTION_AND:
584
 
        case E2K_RESTRICTION_OR: {
585
 
                GString *subsql = g_string_new ("");
586
 
                gboolean rv;
587
 
                if ((rv = rns_to_sql (rn->type, rn->res.and.rns, rn->res.and.nrns, subsql))) {
588
 
                        if (rn->type != inside)
589
 
                                g_string_append (sql, "(");
590
 
                        g_string_append (sql, subsql->str);
591
 
                        if (rn->type != inside)
592
 
                                g_string_append (sql, ")");
593
 
                }
594
 
                g_string_free (subsql, TRUE);
595
 
 
596
 
                return rv;
597
 
        }
598
 
 
599
 
        case E2K_RESTRICTION_NOT: {
600
 
                GString *subsql = g_string_new ("");
601
 
                gboolean rv;
602
 
                if ((rv = rn_to_sql (rn->res.not.rn, subsql, rn->type))) {
603
 
                        g_string_append (sql, "NOT (");
604
 
                        g_string_append (sql, subsql->str);
605
 
                        g_string_append (sql, ")");
606
 
                }
607
 
                g_string_free (subsql, TRUE);
608
 
 
609
 
                return rv;
610
 
        }
611
 
 
612
 
        case E2K_RESTRICTION_CONTENT:
613
 
                pv = &rn->res.content.pv;
614
 
                g_string_append_printf (sql, "\"%s\" ", pv->prop.name);
615
 
 
616
 
                switch (E2K_FL_MATCH_TYPE (rn->res.content.fuzzy_level)) {
617
 
                case E2K_FL_SUFFIX:
618
 
                        /* make suffix same as substring; it'll be checked for suffixes only later */
619
 
                case E2K_FL_SUBSTRING:
620
 
                        g_string_append (sql, "LIKE '%");
621
 
                        append_sql_quoted (sql, pv->value);
622
 
                        g_string_append (sql, "%'");
623
 
                        break;
624
 
 
625
 
                case E2K_FL_PREFIX:
626
 
                        g_string_append (sql, "LIKE '");
627
 
                        append_sql_quoted (sql, pv->value);
628
 
                        g_string_append (sql, "%'");
629
 
                        break;
630
 
 
631
 
                case E2K_FL_FULLSTRING:
632
 
                default:
633
 
                        g_string_append (sql, "= '");
634
 
                        append_sql_quoted (sql, pv->value);
635
 
                        g_string_append_c (sql, '\'');
636
 
                        break;
637
 
                }
638
 
                return TRUE;
639
 
 
640
 
        case E2K_RESTRICTION_PROPERTY:
641
 
                if (rn->res.property.relop >= n_sql_relops)
642
 
                        return FALSE;
643
 
 
644
 
                pv = &rn->res.property.pv;
645
 
                g_string_append_printf (sql, "\"%s\" %s ", pv->prop.name,
646
 
                                        sql_relops[rn->res.property.relop]);
647
 
 
648
 
                switch (pv->type) {
649
 
                case E2K_PROP_TYPE_INT:
650
 
                        g_string_append_printf (sql, "%d",
651
 
                                                GPOINTER_TO_UINT (pv->value));
652
 
                        break;
653
 
 
654
 
                case E2K_PROP_TYPE_BOOL:
655
 
                        g_string_append (sql, pv->value ? "True" : "False");
656
 
                        break;
657
 
 
658
 
                case E2K_PROP_TYPE_DATE:
659
 
                        g_string_append_printf (sql,
660
 
                                                "cast (\"%s\" as 'dateTime.tz')",
661
 
                                                (gchar *)pv->value);
662
 
                        break;
663
 
 
664
 
                default:
665
 
                        g_string_append_c (sql, '\'');
666
 
                        append_sql_quoted (sql, pv->value);
667
 
                        g_string_append_c (sql, '\'');
668
 
                        break;
669
 
                }
670
 
                return TRUE;
671
 
 
672
 
        case E2K_RESTRICTION_COMPAREPROPS:
673
 
                if (rn->res.compare.relop >= n_sql_relops)
674
 
                        return FALSE;
675
 
 
676
 
                g_string_append_printf (sql, "\"%s\" %s \"%s\"",
677
 
                                        rn->res.compare.prop1.name,
678
 
                                        sql_relops[rn->res.compare.relop],
679
 
                                        rn->res.compare.prop2.name);
680
 
                return TRUE;
681
 
 
682
 
        case E2K_RESTRICTION_COMMENT:
683
 
                return TRUE;
684
 
 
685
 
        case E2K_RESTRICTION_BITMASK:
686
 
        case E2K_RESTRICTION_EXIST:
687
 
        case E2K_RESTRICTION_SIZE:
688
 
        case E2K_RESTRICTION_SUBRESTRICTION:
689
 
        default:
690
 
                return FALSE;
691
 
 
692
 
        }
693
 
}
694
 
 
695
 
/**
696
 
 * e2k_restriction_to_sql:
697
 
 * @rn: a restriction
698
 
 *
699
 
 * Converts @rn to an SQL WHERE clause to be used with the WebDAV
700
 
 * SEARCH method. Note that certain restriction types cannot be used
701
 
 * in SQL, as mentioned in their descriptions above.
702
 
 *
703
 
 * If the restriction matches all objects, the return value will
704
 
 * be the empty string. Otherwise it will start with "WHERE ".
705
 
 *
706
 
 * Return value: the SQL WHERE clause, which the caller must free,
707
 
 * or %NULL if @rn could not be converted to SQL.
708
 
 **/
709
 
gchar *
710
 
e2k_restriction_to_sql (E2kRestriction *rn)
711
 
{
712
 
        GString *sql;
713
 
        gchar *ret;
714
 
 
715
 
        sql = g_string_new (NULL);
716
 
        if (!rn_to_sql (rn, sql, E2K_RESTRICTION_AND)) {
717
 
                g_string_free (sql, TRUE);
718
 
                return NULL;
719
 
        }
720
 
 
721
 
        if (sql->len)
722
 
                g_string_prepend (sql, "WHERE ");
723
 
 
724
 
        ret = sql->str;
725
 
        g_string_free (sql, FALSE);
726
 
        return ret;
727
 
}
728
 
 
729
 
/* Binary import/export */
730
 
 
731
 
static gboolean
732
 
extract_restriction (guint8 **data, gint *len, E2kRestriction **rn)
733
 
{
734
 
        gint type;
735
 
 
736
 
        if (*len == 0)
737
 
                return FALSE;
738
 
        type = (*data)[0];
739
 
        (*data)++;
740
 
        (*len)--;
741
 
 
742
 
        switch (type) {
743
 
        case E2K_RESTRICTION_AND:
744
 
        case E2K_RESTRICTION_OR:
745
 
        {
746
 
                E2kRestriction **rns;
747
 
                guint16 nrns;
748
 
                gint i;
749
 
 
750
 
                if (!e2k_rule_extract_uint16 (data, len, &nrns))
751
 
                        return FALSE;
752
 
                rns = g_new0 (E2kRestriction *, nrns);
753
 
                for (i = 0; i < nrns; i++) {
754
 
                        if (!extract_restriction (data, len, &rns[i])) {
755
 
                                while (i--)
756
 
                                        e2k_restriction_unref (rns[i]);
757
 
                                g_free (rns);
758
 
                                return FALSE;
759
 
                        }
760
 
                }
761
 
 
762
 
                *rn = conjoin (type, nrns, rns, TRUE);
763
 
                return TRUE;
764
 
        }
765
 
 
766
 
        case E2K_RESTRICTION_NOT:
767
 
        {
768
 
                E2kRestriction *subrn;
769
 
 
770
 
                if (!extract_restriction (data, len, &subrn))
771
 
                        return FALSE;
772
 
                *rn = e2k_restriction_not (subrn, TRUE);
773
 
                return TRUE;
774
 
        }
775
 
 
776
 
        case E2K_RESTRICTION_CONTENT:
777
 
        {
778
 
                guint32 fuzzy_level;
779
 
                E2kRuleProp prop;
780
 
                E2kPropValue pv;
781
 
 
782
 
                if (!e2k_rule_extract_uint32 (data, len, &fuzzy_level) ||
783
 
                    !e2k_rule_extract_proptag (data, len, &prop) ||
784
 
                    !e2k_rule_extract_propvalue (data, len, &pv))
785
 
                        return FALSE;
786
 
 
787
 
                pv.prop = prop;
788
 
 
789
 
                *rn = g_new0 (E2kRestriction, 1);
790
 
                (*rn)->type = type;
791
 
                (*rn)->res.content.fuzzy_level = fuzzy_level;
792
 
                (*rn)->res.content.pv = pv;
793
 
                return TRUE;
794
 
        }
795
 
 
796
 
        case E2K_RESTRICTION_PROPERTY:
797
 
        {
798
 
                guint8 relop;
799
 
                E2kRuleProp prop;
800
 
                E2kPropValue pv;
801
 
 
802
 
                if (!e2k_rule_extract_byte (data, len, &relop) ||
803
 
                    !e2k_rule_extract_proptag (data, len, &prop) ||
804
 
                    !e2k_rule_extract_propvalue (data, len, &pv))
805
 
                        return FALSE;
806
 
 
807
 
                pv.prop = prop;
808
 
 
809
 
                *rn = g_new0 (E2kRestriction, 1);
810
 
                (*rn)->type = type;
811
 
                (*rn)->res.property.relop = relop;
812
 
                (*rn)->res.property.pv = pv;
813
 
                return TRUE;
814
 
        }
815
 
 
816
 
        case E2K_RESTRICTION_COMPAREPROPS:
817
 
        {
818
 
                /* FIXME */
819
 
                return FALSE;
820
 
        }
821
 
 
822
 
        case E2K_RESTRICTION_BITMASK:
823
 
        {
824
 
                guint8 bitop;
825
 
                guint32 mask;
826
 
                E2kRuleProp prop;
827
 
 
828
 
                if (!e2k_rule_extract_byte (data, len, &bitop) ||
829
 
                    !e2k_rule_extract_proptag (data, len, &prop) ||
830
 
                    !e2k_rule_extract_uint32 (data, len, &mask))
831
 
                        return FALSE;
832
 
 
833
 
                *rn = g_new0 (E2kRestriction, 1);
834
 
                (*rn)->type = type;
835
 
                (*rn)->res.bitmask.bitop = bitop;
836
 
                (*rn)->res.bitmask.prop = prop;
837
 
                (*rn)->res.bitmask.mask = mask;
838
 
                return TRUE;
839
 
        }
840
 
 
841
 
        case E2K_RESTRICTION_SIZE:
842
 
        {
843
 
                /* FIXME */
844
 
                return FALSE;
845
 
        }
846
 
 
847
 
        case E2K_RESTRICTION_EXIST:
848
 
        {
849
 
                E2kRuleProp prop;
850
 
 
851
 
                if (!e2k_rule_extract_proptag (data, len, &prop))
852
 
                        return FALSE;
853
 
 
854
 
                *rn = g_new0 (E2kRestriction, 1);
855
 
                (*rn)->type = type;
856
 
                (*rn)->res.exist.prop = prop;
857
 
                return TRUE;
858
 
        }
859
 
 
860
 
        case E2K_RESTRICTION_SUBRESTRICTION:
861
 
        {
862
 
                E2kRuleProp subtable;
863
 
                E2kRestriction *subrn;
864
 
 
865
 
                if (!e2k_rule_extract_proptag (data, len, &subtable) ||
866
 
                    !extract_restriction (data, len, &subrn))
867
 
                        return FALSE;
868
 
 
869
 
                *rn = g_new0 (E2kRestriction, 1);
870
 
                (*rn)->type = type;
871
 
                (*rn)->res.sub.subtable = subtable;
872
 
                (*rn)->res.sub.rn = subrn;
873
 
                return TRUE;
874
 
        }
875
 
 
876
 
        case E2K_RESTRICTION_COMMENT:
877
 
        {
878
 
                guint8 nprops, dummy;
879
 
                E2kPropValue *props;
880
 
                gint i;
881
 
 
882
 
                if (!e2k_rule_extract_byte (data, len, &nprops))
883
 
                        return FALSE;
884
 
 
885
 
                props = g_new0 (E2kPropValue, nprops);
886
 
                for (i = 0; i < nprops; i++) {
887
 
                        if (!e2k_rule_extract_propvalue (data, len, &props[i])) {
888
 
                                while (i--)
889
 
                                        e2k_rule_free_propvalue (&props[i]);
890
 
                                g_free (props);
891
 
                                return FALSE;
892
 
                        }
893
 
                }
894
 
 
895
 
                *rn = g_new0 (E2kRestriction, 1);
896
 
                (*rn)->type = type;
897
 
                (*rn)->res.comment.nprops = nprops;
898
 
                (*rn)->res.comment.props = props;
899
 
 
900
 
                /* FIXME: There is always a "1" byte here, but I don't
901
 
                 * know why.
902
 
                 */
903
 
                if (!e2k_rule_extract_byte (data, len, &dummy) || dummy != 1) {
904
 
                        e2k_restriction_unref (*rn);
905
 
                        return FALSE;
906
 
                }
907
 
 
908
 
                if (!extract_restriction (data, len, &(*rn)->res.comment.rn)) {
909
 
                        e2k_restriction_unref (*rn);
910
 
                        return FALSE;
911
 
                }
912
 
 
913
 
                return TRUE;
914
 
        }
915
 
 
916
 
        default:
917
 
                return FALSE;
918
 
        }
919
 
}
920
 
 
921
 
/**
922
 
 * e2k_restriction_extract:
923
 
 * @data: pointer to data pointer
924
 
 * @len: pointer to data length
925
 
 * @rn: pointer to variable to store the extracted restriction in
926
 
 *
927
 
 * Attempts to extract a restriction from *@data, which contains
928
 
 * a binary-encoded restriction from a server-side rule.
929
 
 *
930
 
 * On success, *@rn will contain the extracted restriction, *@data
931
 
 * will be advanced past the end of the restriction data, and *@len
932
 
 * will be decremented accordingly.
933
 
 *
934
 
 * Return value: success or failure
935
 
 **/
936
 
gboolean
937
 
e2k_restriction_extract (guint8 **data, gint *len, E2kRestriction **rn)
938
 
{
939
 
        guint32 rnlen;
940
 
 
941
 
        if (!e2k_rule_extract_uint32 (data, len, &rnlen))
942
 
                return FALSE;
943
 
        if (rnlen > *len)
944
 
                return FALSE;
945
 
 
946
 
        if (rnlen == 1 && (*data)[0] == 0xFF) {
947
 
                (*data)++;
948
 
                (*len)--;
949
 
                *rn = NULL;
950
 
                return TRUE;
951
 
        }
952
 
 
953
 
        if (*len < 2)
954
 
                return FALSE;
955
 
        if ((*data)[0] != 0 || (*data)[1] != 0)
956
 
                return FALSE;
957
 
        (*data) += 2;
958
 
        (*len) -= 2;
959
 
 
960
 
        return extract_restriction (data, len, rn);
961
 
}
962
 
 
963
 
static void
964
 
append_restriction (GByteArray *ba, E2kRestriction *rn)
965
 
{
966
 
        gint i;
967
 
 
968
 
        e2k_rule_append_byte (ba, rn->type);
969
 
 
970
 
        switch (rn->type) {
971
 
        case E2K_RESTRICTION_AND:
972
 
        case E2K_RESTRICTION_OR:
973
 
                e2k_rule_append_uint16 (ba, rn->res.and.nrns);
974
 
                for (i = 0; i < rn->res.and.nrns; i++)
975
 
                        append_restriction (ba, rn->res.and.rns[i]);
976
 
                break;
977
 
 
978
 
        case E2K_RESTRICTION_NOT:
979
 
                append_restriction (ba, rn->res.not.rn);
980
 
                break;
981
 
 
982
 
        case E2K_RESTRICTION_CONTENT:
983
 
                e2k_rule_append_uint32 (ba, rn->res.content.fuzzy_level);
984
 
                e2k_rule_append_proptag (ba, &rn->res.content.pv.prop);
985
 
                e2k_rule_append_propvalue (ba, &rn->res.content.pv);
986
 
                break;
987
 
 
988
 
        case E2K_RESTRICTION_PROPERTY:
989
 
                e2k_rule_append_byte (ba, rn->res.property.relop);
990
 
                e2k_rule_append_proptag (ba, &rn->res.property.pv.prop);
991
 
                e2k_rule_append_propvalue (ba, &rn->res.property.pv);
992
 
                break;
993
 
 
994
 
        case E2K_RESTRICTION_COMPAREPROPS:
995
 
                /* FIXME */
996
 
                break;
997
 
 
998
 
        case E2K_RESTRICTION_BITMASK:
999
 
                e2k_rule_append_byte (ba, rn->res.bitmask.bitop);
1000
 
                e2k_rule_append_proptag (ba, &rn->res.bitmask.prop);
1001
 
                e2k_rule_append_uint32 (ba, rn->res.bitmask.mask);
1002
 
                break;
1003
 
 
1004
 
        case E2K_RESTRICTION_SIZE:
1005
 
                break;
1006
 
 
1007
 
        case E2K_RESTRICTION_EXIST:
1008
 
                e2k_rule_append_proptag (ba, &rn->res.exist.prop);
1009
 
                break;
1010
 
 
1011
 
        case E2K_RESTRICTION_SUBRESTRICTION:
1012
 
                e2k_rule_append_proptag (ba, &rn->res.sub.subtable);
1013
 
                append_restriction (ba, rn->res.sub.rn);
1014
 
                break;
1015
 
 
1016
 
        case E2K_RESTRICTION_COMMENT:
1017
 
                e2k_rule_append_byte (ba, rn->res.comment.nprops);
1018
 
 
1019
 
                for (i = 0; i < rn->res.comment.nprops; i++)
1020
 
                        e2k_rule_append_propvalue (ba, &rn->res.comment.props[i]);
1021
 
 
1022
 
                /* FIXME: There is always a "1" byte here, but I don't
1023
 
                 * know why.
1024
 
                 */
1025
 
                e2k_rule_append_byte (ba, 1);
1026
 
 
1027
 
                append_restriction (ba, rn->res.comment.rn);
1028
 
                break;
1029
 
 
1030
 
        default:
1031
 
                break;
1032
 
        }
1033
 
}
1034
 
 
1035
 
/**
1036
 
 * e2k_restriction_append:
1037
 
 * @ba: a buffer into which a server-side rule is being constructed
1038
 
 * @rn: the restriction to append to @ba
1039
 
 *
1040
 
 * Appends @rn to @ba as part of a server-side rule.
1041
 
 **/
1042
 
void
1043
 
e2k_restriction_append (GByteArray *ba, E2kRestriction *rn)
1044
 
{
1045
 
        gint rnlen_offset, rnlen;
1046
 
 
1047
 
        if (!rn) {
1048
 
                e2k_rule_append_uint32 (ba, 1);
1049
 
                e2k_rule_append_byte (ba, 0xFF);
1050
 
                return;
1051
 
        }
1052
 
 
1053
 
        /* Save space for the length field */
1054
 
        rnlen_offset = ba->len;
1055
 
        e2k_rule_append_uint32 (ba, 0);
1056
 
 
1057
 
        /* FIXME: ??? */
1058
 
        e2k_rule_append_uint16 (ba, 0);
1059
 
 
1060
 
        append_restriction (ba, rn);
1061
 
 
1062
 
        rnlen = ba->len - rnlen_offset - 4;
1063
 
        e2k_rule_write_uint32 (ba->data + rnlen_offset, rnlen);
1064
 
}