namespace.c
4.46 KB
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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2001 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*FIXME: should be global? */
typedef int (*nsfp_t) __P((void *closure, int ns, char *path, int delim));
struct namespace_t
{
int subdir_c;
char **subdir_v;
};
struct namespace_t namespace[NS_MAX];
/* Note: str is not supposed to be NULL */
int
set_namespace(int i, char *str)
{
char *p, *save;
struct namespace_t ns;
/* first, estimate the number of items in subdir_v array: */
ns.subdir_c = 1;
for (p = strchr(str, ':'); p && *p; p = strchr(p+1, ':'))
ns.subdir_c++;
/* Now allocate the memory */
ns.subdir_v = calloc(ns.subdir_c, sizeof(ns.subdir_v[0]));
/* Fill in the array */
if (ns.subdir_c == 1)
{
ns.subdir_v[0] = util_normalize_path(strdup(str), "/");
}
else
{
ns.subdir_c = 0;
for (p = strtok_r(str, ":", &save); p; p = strtok_r(NULL, ":", &save))
{
ns.subdir_v[ns.subdir_c++] = util_normalize_path(strdup(p), "/");
}
}
namespace[i] = ns;
return 0;
}
static char *
printable_pathname(char *str)
{
if (strncmp(str, homedir, strlen(homedir)) == 0)
{
str += strlen(homedir);
if (str[0] == '/')
str++;
}
return str;
}
static void
print_namespace(int n)
{
int i;
if (namespace[n].subdir_c == 0)
{
util_send("NIL");
return;
}
util_send("(");
for (i = 0; i < namespace[n].subdir_c; i++)
{
util_send("(\"%s\" \"/\")", printable_pathname(namespace[n].subdir_v[i]));
}
util_send(")");
}
static int
namespace_enumerate(int ns, nsfp_t f, void *closure)
{
int i, rc;
for (i = 0; i < namespace[ns].subdir_c; i++)
if ((rc = (*f)(closure, ns, namespace[ns].subdir_v[i], '/')))
return rc;
return 0;
}
static int
namespace_enumerate_all(nsfp_t f, void *closure)
{
return namespace_enumerate(NS_PRIVATE, f, closure)
|| namespace_enumerate(NS_OTHER, f, closure)
|| namespace_enumerate(NS_SHARED, f, closure);
}
int
imap4d_namespace(struct imap4d_command *command, char *arg)
{
if (! (command->states & state))
return util_finish (command, RESP_BAD, "Wrong state");
if (*arg)
return util_finish (command, RESP_BAD, "Too many arguments");
util_send("* NAMESPACE ");
print_namespace(NS_PRIVATE);
util_send(" ");
print_namespace(NS_OTHER);
util_send(" ");
print_namespace(NS_SHARED);
util_send("\r\n");
return util_finish (command, RESP_OK, "Completed");
}
struct namespace_info
{
char *name;
int namelen;
int ns;
int exact;
};
static int
check_namespace (void *closure, int ns, char *path, int delim)
{
struct namespace_info *p = (struct namespace_info *)closure;
int len = strlen(path);
if ((len == 0 && p->namelen == len)
|| (len > 0 && strncmp(path, p->name, strlen(path)) == 0))
{
p->ns = ns;
p->exact = len == p->namelen;
return 1;
}
return 0;
}
static int
risky_pattern(const char *pattern, int delim)
{
for (; *pattern && *pattern != delim; pattern++)
{
if (*pattern == '%' || *pattern == '*')
return 1;
}
return 0;
}
char *
namespace_checkfullpath (char *name, const char *pattern, const char *delim)
{
struct namespace_info info;
char *path = util_getfullpath(name, delim);
if (!path)
return path;
util_normalize_path(path, "/");
info.name = path;
info.namelen = strlen(path);
if (!namespace_enumerate_all(check_namespace, &info))
{
free(path);
return NULL;
}
if (pattern &&
info.ns == NS_OTHER && info.exact && risky_pattern(pattern, '/'))
{
free(path);
return NULL;
}
return path;
}
char *
namespace_getfullpath (char *name, const char *delim)
{
return namespace_checkfullpath (name, NULL, delim);
}
int
namespace_init(char *path)
{
set_namespace(NS_PRIVATE, path);
return 0;
}