File: namespace.c

package info (click to toggle)
imapfilter 1%3A1.2.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 300 kB
  • ctags: 315
  • sloc: ansic: 3,392; sh: 182; makefile: 103
file content (78 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download
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
#include <stdio.h>
#include <string.h>

#include "imapfilter.h"
#include "buffer.h"


buffer nbuf;			/* Namespace buffer. */


/*
 * Convert the names of personal mailboxes, using the namespace specified
 * by the mail server, from internal to mail server format.
 */
const char *
apply_namespace(const char *mbox, char *prefix, char delim)
{
	int n;
	char *c;

	if ((prefix == NULL && delim == '\0') ||
	    (prefix == NULL && delim == '/') ||
	    !strcasecmp(mbox, "INBOX"))
		return mbox;

	buffer_reset(&nbuf);

	n = snprintf(nbuf.data, nbuf.size + 1, "%s%s", (prefix ? prefix : ""),
	    mbox);
	if (n > (int)nbuf.size) {
		buffer_check(&nbuf, n);
		snprintf(nbuf.data, nbuf.size + 1, "%s%s",
		    (prefix ? prefix : ""), mbox);
	}
	c = nbuf.data;
	while ((c = strchr(c, '/')))
		*(c++) = delim;

	debug("namespace: '%s' -> '%s'\n", mbox, nbuf.data);

	return nbuf.data;
}


/*
 * Convert the names of personal mailboxes, using the namespace specified by
 * the mail server, from mail server format to internal format.
 */
const char *
reverse_namespace(const char *mbox, char *prefix, char delim)
{
	int n, o;
	char *c;

	if ((prefix == NULL && delim == '\0') ||
	    (prefix == NULL && delim == '/') ||
	    !strcasecmp(mbox, "INBOX"))
		return mbox;

	buffer_reset(&nbuf);

	o = 0;
	if (!strncasecmp(mbox, prefix, strlen(prefix)))
		o = strlen(prefix);

	n = snprintf(nbuf.data, nbuf.size + 1, "%s", mbox + o);
	if (n > (int)nbuf.size) {
		buffer_check(&nbuf, n);
		snprintf(nbuf.data, nbuf.size + 1, "%s", mbox + o);
	}
	c = nbuf.data;
	while ((c = strchr(c, delim)))
		*(c++) = '/';

	debug("namespace: '%s' <- '%s'\n", mbox, nbuf.data);

	return nbuf.data;
}