File: e2k-utils.c

package info (click to toggle)
evolution-data-server 1.6.3-5etch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 59,384 kB
  • ctags: 43,218
  • sloc: ansic: 319,315; tcl: 30,499; xml: 19,166; sh: 18,776; perl: 11,529; cpp: 8,259; java: 7,653; makefile: 6,448; awk: 1,338; yacc: 1,103; sed: 772; cs: 505; lex: 134; asm: 14
file content (665 lines) | stat: -rw-r--r-- 16,614 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Copyright (C) 2001-2004 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * 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 Lesser 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "e2k-utils.h"
#include "e2k-autoconfig.h"
#include "e2k-propnames.h"
#include "e2k-rule.h"

#include <libedataserver/e-time-utils.h>

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

/* Do not internationalize */
const char *e2k_rfc822_months [] = {
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

/**
 * e2k_parse_timestamp:
 * @timestamp: an ISO8601 timestamp returned by the Exchange server
 *
 * Converts @timestamp to a %time_t value. @timestamp must be in one
 * of the two ISO8601 variants used by Exchange.
 *
 * Note that the timestamps used (in most contexts) by Exchange have
 * millisecond resolution, so converting them to %time_t loses
 * resolution. Since ISO8601 timestamps can be compared using
 * strcmp(), it is often best to keep them as strings.
 *
 * Return value: the %time_t corresponding to @timestamp, or -1 on
 * error.
 **/
time_t
e2k_parse_timestamp (const char *timestamp)
{
	struct tm tm;

	tm.tm_year = strtoul (timestamp, (char **)&timestamp, 10) - 1900;
	if (*timestamp++ != '-')
		return -1;
	tm.tm_mon = strtoul (timestamp, (char **)&timestamp, 10) - 1;
	if (*timestamp++ != '-')
		return -1;
	tm.tm_mday = strtoul (timestamp, (char **)&timestamp, 10);
	if (*timestamp++ != 'T')
		return -1;
	tm.tm_hour = strtoul (timestamp, (char **)&timestamp, 10);
	if (*timestamp++ != ':')
		return -1;
	tm.tm_min = strtoul (timestamp, (char **)&timestamp, 10);
	if (*timestamp++ != ':')
		return -1;
	tm.tm_sec = strtoul (timestamp, (char **)&timestamp, 10);
	if (*timestamp != '.' && *timestamp != 'Z')
		return -1;

	return e_mktime_utc (&tm);
}

/**
 * e2k_make_timestamp:
 * @when: the %time_t to convert to an ISO8601 timestamp
 *
 * Creates an ISO8601 timestamp (in an format acceptable to Exchange)
 * corresponding to @when.
 *
 * Return value: the timestamp, which the caller must free.
 **/
char *
e2k_make_timestamp (time_t when)
{
	struct tm *tm;

	tm = gmtime (&when);
	return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02dZ",
				tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
				tm->tm_hour, tm->tm_min, tm->tm_sec);
}

/**
 * e2k_make_timestamp_rfc822:
 * @when: the %time_t to convert to an RFC822 timestamp
 *
 * Creates an RFC822 Date header value corresponding to @when, in the
 * locale timezone.
 *
 * Return value: the timestamp, which the caller must free.
 **/
char *
e2k_make_timestamp_rfc822 (time_t when)
{
	struct tm tm;
	int offset;

	e_localtime_with_offset (when, &tm, &offset);
	offset = (offset / 3600) * 100 + (offset / 60) % 60;

	return g_strdup_printf ("%02d %s %04d %02d:%02d:%02d %+05d",
				tm.tm_mday, e2k_rfc822_months[tm.tm_mon],
				tm.tm_year + 1900,
				tm.tm_hour, tm.tm_min, tm.tm_sec,
				offset);
}

/* SYSTIME_OFFSET is the number of minutes between the Windows epoch
 * (1601-01-01T00:00:00Z) and the time_t epoch (1970-01-01T00:00:00Z):
 * 369 years, 89 of which are leap years.
 */
#define SYSTIME_OFFSET 194074560UL

/**
 * e2k_systime_to_time_t:
 * @systime: a MAPI PT_SYSTIME value (minutes since Windows epoch)
 *
 * Converts the MAPI PT_SYSTIME value @systime to a corresponding
 * %time_t value (assuming it is within the valid range of a %time_t).
 *
 * Return value: a %time_t corresponding to @systime.
 **/
time_t
e2k_systime_to_time_t (guint32 systime)
{
	return (systime - SYSTIME_OFFSET) * 60;
}

/**
 * e2k_systime_from_time_t:
 * @tt: a %time_t value
 *
 * Converts the %time_t value @tt to a corresponding MAPI PT_SYSTIME
 * value, losing some precision if @tt does not fall on a minute
 * boundary.
 *
 * Return value: the Windows systime value corresponding to @tt
 **/
guint32
e2k_systime_from_time_t (time_t tt)
{
	return (tt / 60) + SYSTIME_OFFSET;
}

/**
 * e2k_filetime_to_time_t:
 * @filetime: a Windows FILETIME value (100ns intervals since
 * Windows epoch)
 *
 * Converts the Windows FILETIME value @filetime to a corresponding
 * %time_t value (assuming it is within the valid range of a %time_t),
 * truncating to a second boundary.
 *
 * Return value: a %time_t corresponding to @filetime.
 **/
time_t
e2k_filetime_to_time_t (guint64 filetime)
{
	return (time_t)(filetime / 10000000 - SYSTIME_OFFSET * 60);
}

/**
 * e2k_filetime_from_time_t:
 * @tt: a %time_t value
 *
 * Converts the %time_t value @tt to a corresponding Windows FILETIME
 * value.
 *
 * Return value: the Windows FILETIME value corresponding to @tt
 **/
guint64
e2k_filetime_from_time_t (time_t tt)
{
	return (((guint64)tt) + ((guint64)SYSTIME_OFFSET) * 60) * 10000000;
}

/**
 * e2k_lf_to_crlf:
 * @in: input text in UNIX ("\n") format
 *
 * Creates a copy of @in with all LFs converted to CRLFs.
 *
 * Return value: the converted text, which the caller must free.
 **/
char *
e2k_lf_to_crlf (const char *in)
{
	int len;
	const char *s;
	char *out, *d;

	g_return_val_if_fail (in != NULL, NULL);

	len = strlen (in);
	for (s = strchr (in, '\n'); s; s = strchr (s + 1, '\n'))
		len++;

	out = g_malloc (len + 1);
	for (s = in, d = out; *s; s++) {
		if (*s == '\n')
			*d++ = '\r';
		*d++ = *s;
	}
	*d = '\0';

	return out;
}

/**
 * e2k_crlf_to_lf:
 * @in: input text in network ("\r\n") format
 *
 * Creates a copy of @in with all CRLFs converted to LFs. (Actually,
 * it just strips CRs, so any raw CRs will be removed.)
 *
 * Return value: the converted text, which the caller must free.
 **/
char *
e2k_crlf_to_lf (const char *in)
{
	int len;
	const char *s;
	char *out;
	GString *str;

	g_return_val_if_fail (in != NULL, NULL);

	str = g_string_new ("");

	len = strlen (in);
	for (s = in; *s; s++) {
		if (*s != '\r')
			str = g_string_append_c (str, *s);
	}

	out = str->str;
	g_string_free (str, FALSE);

	return out;
}

/**
 * e2k_strdup_with_trailing_slash:
 * @path: a URI or path
 *
 * Copies @path, appending a "/" to it if and only if it did not
 * already end in "/".
 *
 * Return value: the path, which the caller must free
 **/
char *
e2k_strdup_with_trailing_slash (const char *path)
{
	char *p;

	p = strrchr (path, '/');
	if (p && !p[1])
		return g_strdup (path);
	else
		return g_strdup_printf ("%s/", path);
}

/**
 * e2k_entryid_to_dn:
 * @entryid: an Exchange entryid
 *
 * Finds an Exchange 5.5 DN inside a binary entryid property (such as
 * #PR_STORE_ENTRYID or an element of #PR_DELEGATES_ENTRYIDS).
 *
 * Return value: the entryid, which is a pointer into @entryid's data.
 **/
const char *
e2k_entryid_to_dn (GByteArray *entryid)
{
	char *p;

	p = ((char *)entryid->data) + entryid->len - 1;
	if (*p == 0) {
		while (*(p - 1) && p > (char *)entryid->data)
			p--;
		if (*p == '/')
			return p;
	}
	return NULL;
}

static void
append_permanenturl_section (GString *url, guint8 *entryid)
{
	int i = 0;

	/* First part */
	while (i < 16)
		g_string_append_printf (url, "%02x", entryid[i++]);

	/* Replace 0s with a single '-' */
	g_string_append_c (url, '-');
	while (i < 22 && entryid[i] == 0)
		i++;

	/* Last part; note that if the first non-0 byte can be
	 * expressed in a single hex digit, we do so. (ie, the 0
	 * in the 16's place was also accumulated into the
	 * preceding '-'.)
	 */
	if (i < 22 && entryid[i] < 0x10)
		g_string_append_printf (url, "%01x", entryid[i++]);
	while (i < 22)
		g_string_append_printf (url, "%02x", entryid[i++]);
}

#define E2K_PERMANENTURL_INFIX "-FlatUrlSpace-"
#define E2K_PERMANENTURL_INFIX_LEN (sizeof (E2K_PERMANENTURL_INFIX) - 1)

/**
 * e2k_entryid_to_permanenturl:
 * @entryid: an ENTRYID (specifically, a PR_SOURCE_KEY)
 * @base_uri: base URI of the store containing @entryid
 *
 * Creates a permanenturl based on @entryid and @base_uri.
 *
 * Return value: the permanenturl, which the caller must free.
 **/
char *
e2k_entryid_to_permanenturl (GByteArray *entryid, const char *base_uri)
{
	GString *url;
	char *ret;

	g_return_val_if_fail (entryid->len == 22 || entryid->len == 44, NULL);

	url = g_string_new (base_uri);
	if (url->str[url->len - 1] != '/')
		g_string_append_c (url, '/');
	g_string_append (url, E2K_PERMANENTURL_INFIX);
	g_string_append_c (url, '/');

	append_permanenturl_section (url, entryid->data);
	if (entryid->len > 22) {
		g_string_append_c (url, '/');
		append_permanenturl_section (url, entryid->data + 22);
	}

	ret = url->str;
	g_string_free (url, FALSE);
	return ret;
}

#define HEXVAL(c) (isdigit (c) ? (c) - '0' : g_ascii_tolower (c) - 'a' + 10)

static gboolean
append_entryid_section (GByteArray *entryid, const char **permanenturl)
{
	const char *p;
	char buf[44], byte;
	int endlen;

	p = *permanenturl;
	if (strspn (p, "0123456789abcdefABCDEF") != 32)
		return FALSE;
	if (p[32] != '-')
		return FALSE;
	endlen = strspn (p + 33, "0123456789abcdefABCDEF");
	if (endlen > 6)
		return FALSE;

	/* Expand to the full form by replacing the "-" with "0"s */
	memcpy (buf, p, 32);
	memset (buf + 32, '0', sizeof (buf) - 32 - endlen);
	memcpy (buf + sizeof (buf) - endlen, p + 33, endlen);

	p = buf;
	while (p < buf + sizeof (buf)) {
		byte = (HEXVAL (*p) << 4) + HEXVAL (*(p + 1));
		g_byte_array_append (entryid, &byte, 1);
		p += 2;
	}

	*permanenturl += 33 + endlen;
	return TRUE;
}

/**
 * e2k_permanenturl_to_entryid:
 * @permanenturl: an Exchange permanenturl
 *
 * Creates an ENTRYID (specifically, a PR_SOURCE_KEY) based on
 * @permanenturl
 *
 * Return value: the entryid
 **/
GByteArray *
e2k_permanenturl_to_entryid (const char *permanenturl)
{
	GByteArray *entryid;

	permanenturl = strstr (permanenturl, E2K_PERMANENTURL_INFIX);
	if (!permanenturl)
		return NULL;
	permanenturl += E2K_PERMANENTURL_INFIX_LEN;

	entryid = g_byte_array_new ();
	while (*permanenturl++ == '/') {
		if (!append_entryid_section (entryid, &permanenturl)) {
			g_byte_array_free (entryid, TRUE);
			return NULL;
		}
	}

	return entryid;
}

/**
 * e2k_ascii_strcase_equal
 * @v: a string
 * @v2: another string
 *
 * ASCII-case-insensitive comparison function for use with #GHashTable.
 *
 * Return value: %TRUE if @v and @v2 are ASCII-case-insensitively
 * equal, %FALSE if not.
 **/
gint
e2k_ascii_strcase_equal (gconstpointer v, gconstpointer v2)
{
	return !g_ascii_strcasecmp (v, v2);
}

/**
 * e2k_ascii_strcase_hash
 * @v: a string
 *
 * ASCII-case-insensitive hash function for use with #GHashTable.
 *
 * Return value: An ASCII-case-insensitive hashing of @v.
 **/
guint
e2k_ascii_strcase_hash (gconstpointer v)
{
	/* case-insensitive g_str_hash */

	const unsigned char *p = v;
	guint h = g_ascii_tolower (*p);

	if (h) {
		for (p += 1; *p != '\0'; p++)
			h = (h << 5) - h + g_ascii_tolower (*p);
	}

	return h;
}

/**
 * e2k_restriction_folders_only:
 * @rn: a restriction
 *
 * Examines @rn, and determines if it can only return folders
 *
 * Return value: %TRUE if @rn will cause only folders to be returned
 **/
gboolean
e2k_restriction_folders_only (E2kRestriction *rn)
{
	int i;

	if (!rn)
		return FALSE;

	switch (rn->type) {
	case E2K_RESTRICTION_PROPERTY:
		if (strcmp (rn->res.property.pv.prop.name,
			    E2K_PR_DAV_IS_COLLECTION) != 0)
			return FALSE;

		/* return TRUE if it's "= TRUE" or "!= FALSE" */
		return (rn->res.property.relop == E2K_RELOP_EQ) ==
			(rn->res.property.pv.value != NULL);

	case E2K_RESTRICTION_AND:
		for (i = 0; i < rn->res.and.nrns; i++) {
			if (e2k_restriction_folders_only (rn->res.and.rns[i]))
				return TRUE;
		}
		return FALSE;

	case E2K_RESTRICTION_OR:
		for (i = 0; i < rn->res.or.nrns; i++) {
			if (!e2k_restriction_folders_only (rn->res.or.rns[i]))
				return FALSE;
		}
		return TRUE;

	case E2K_RESTRICTION_NOT:
		return !e2k_restriction_folders_only (rn->res.not.rn);

	case E2K_RESTRICTION_COMMENT:
		return e2k_restriction_folders_only (rn->res.comment.rn);

	default:
		return FALSE;
	}
}

/* From MAPIDEFS.H */
static const char MAPI_ONE_OFF_UID[] = {
	0x81, 0x2b, 0x1f, 0xa4, 0xbe, 0xa3, 0x10, 0x19,
	0x9d, 0x6e, 0x00, 0xdd, 0x01, 0x0f, 0x54, 0x02
};
#define MAPI_ONE_OFF_UNICODE	  0x8000
#define MAPI_ONE_OFF_NO_RICH_INFO 0x0001
#define MAPI_ONE_OFF_MYSTERY_FLAG 0x1000

/**
 * e2k_entryid_generate_oneoff:
 * @display_name: the display name of the user
 * @email: the email address
 * @unicode: %TRUE to generate a Unicode ENTRYID (in which case
 * @display_name should be UTF-8), %FALSE for an ASCII ENTRYID.
 *
 * Constructs a "one-off" ENTRYID value that can be used as a MAPI
 * recipient (eg, for a message forwarding server-side rule),
 * corresponding to @display_name and @email.
 *
 * Return value: the recipient ENTRYID
 **/
GByteArray *
e2k_entryid_generate_oneoff (const char *display_name, const char *email, gboolean unicode)
{
	GByteArray *entryid;

	entryid = g_byte_array_new ();

	e2k_rule_append_uint32 (entryid, 0);
	g_byte_array_append (entryid, MAPI_ONE_OFF_UID, sizeof (MAPI_ONE_OFF_UID));
	e2k_rule_append_uint16 (entryid, 0);
	e2k_rule_append_uint16 (entryid,
				MAPI_ONE_OFF_NO_RICH_INFO |
				MAPI_ONE_OFF_MYSTERY_FLAG |
				(unicode ? MAPI_ONE_OFF_UNICODE : 0));

	if (unicode) {
		e2k_rule_append_unicode (entryid, display_name);
		e2k_rule_append_unicode (entryid, "SMTP");
		e2k_rule_append_unicode (entryid, email);
	} else {
		e2k_rule_append_string (entryid, display_name);
		e2k_rule_append_string (entryid, "SMTP");
		e2k_rule_append_string (entryid, email);
	}

	return entryid;
}

static const char MAPI_LOCAL_UID[] = {
	0xdc, 0xa7, 0x40, 0xc8, 0xc0, 0x42, 0x10, 0x1a,
	0xb4, 0xb9, 0x08, 0x00, 0x2b, 0x2f, 0xe1, 0x82
};

/**
 * e2k_entryid_generate_local:
 * @exchange_dn: the Exchange 5.5-style DN of the local user
 *
 * Constructs an ENTRYID value that can be used as a MAPI
 * recipient (eg, for a message forwarding server-side rule),
 * corresponding to the local user identified by @exchange_dn.
 *
 * Return value: the recipient ENTRYID
 **/
GByteArray *
e2k_entryid_generate_local (const char *exchange_dn)
{
	GByteArray *entryid;

	entryid = g_byte_array_new ();

	e2k_rule_append_uint32 (entryid, 0);
	g_byte_array_append (entryid, MAPI_LOCAL_UID, sizeof (MAPI_LOCAL_UID));
	e2k_rule_append_uint16 (entryid, 1);
	e2k_rule_append_uint16 (entryid, 0);
	e2k_rule_append_string (entryid, exchange_dn);

	return entryid;
}

static const char MAPI_CONTACT_UID[] = {
	0xfe, 0x42, 0xaa, 0x0a, 0x18, 0xc7, 0x1a, 0x10,
	0xe8, 0x85, 0x0b, 0x65, 0x1c, 0x24, 0x00, 0x00
};

/**
 * e2k_entryid_generate_contact:
 * @contact_entryid: the #PR_ENTRYID of an item in the user's Contacts
 * folder.
 * @nth_address: which of the contact's email addresses to use.
 *
 * Constructs an ENTRYID value that can be used as a MAPI recipient
 * (eg, for a message forwarding server-side rule), corresponding to
 * the Contacts folder entry identified by @contact_entryid.
 *
 * Return value: the recipient ENTRYID
 **/
GByteArray *
e2k_entryid_generate_contact (GByteArray *contact_entryid, int nth_address)
{
	GByteArray *entryid;

	entryid = g_byte_array_new ();

	e2k_rule_append_uint32 (entryid, 0);
	g_byte_array_append (entryid, MAPI_CONTACT_UID, sizeof (MAPI_CONTACT_UID));
	e2k_rule_append_uint32 (entryid, 3);
	e2k_rule_append_uint32 (entryid, 4);
	e2k_rule_append_uint32 (entryid, nth_address);
	e2k_rule_append_uint32 (entryid, contact_entryid->len);
	g_byte_array_append (entryid, contact_entryid->data, contact_entryid->len);

	return entryid;
}

/**
 * e2k_search_key_generate:
 * @addrtype: the type of @address (usually "SMTP" or "EX")
 * @address: the address data
 *
 * Constructs a PR_SEARCH_KEY value for @address
 *
 * Return value: the search key
 **/
GByteArray *
e2k_search_key_generate (const char *addrtype, const char *address)
{
	GByteArray *search_key;
	guint8 *p;

	search_key = g_byte_array_new ();
	g_byte_array_append (search_key, addrtype, strlen (addrtype));
	g_byte_array_append (search_key, ":", 1);
	g_byte_array_append (search_key, address, strlen (address));
	g_byte_array_append (search_key, "", 1);

	for (p = search_key->data; *p; p++)
		*p = g_ascii_toupper (*p);

	return search_key;
}