More removal of $Id$ tags
[citadel.git] / citadel / modules / imap / imap_metadata.c
1 /*
2  * IMAP METADATA extension
3  *
4  * This is an implementation of the Bynari variant of the METADATA extension.
5  *
6  * Copyright (c) 2007-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <ctype.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <libcitadel.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "sysdep_decls.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "user_ops.h"
57 #include "database.h"
58 #include "msgbase.h"
59 #include "internet_addressing.h"
60 #include "imap_tools.h"
61 #include "serv_imap.h"
62 #include "imap_fetch.h"
63 #include "imap_misc.h"
64 #include "genstamp.h"
65
66 #include "ctdl_module.h"
67
68 /*
69  * Implements the SETMETADATA command.
70  *
71  * Again, the only thing we're interested in setting here is the folder type.
72  *
73  * Attempting to set anything else calls a stub which fools the client into
74  * thinking that there is no remaining space available to store annotations.
75  */
76 void imap_setmetadata(int num_parms, ConstStr *Params) {
77         char roomname[ROOMNAMELEN];
78         char savedroom[ROOMNAMELEN];
79         int msgs, new;
80         int ret;
81         int setting_user_value = 0;
82         char set_value[32];
83         int set_view = VIEW_BBS;
84         visit vbuf;
85
86         if (num_parms != 6) {
87                 cprintf("%s BAD usage error\r\n", Params[0].Key);
88                 return;
89         }
90
91         /*
92          * Don't allow other types of metadata to be set
93          */
94         if (strcasecmp(Params[3].Key, "/vendor/kolab/folder-type")) {
95                 cprintf("%s NO [METADATA TOOMANY] SETMETADATA failed\r\n", Params[0].Key);
96                 return;
97         }
98
99         if (!strcasecmp(Params[4].Key, "(value.shared")) {
100                 setting_user_value = 0;                         /* global view */
101         }
102         else if (!strcasecmp(Params[4].Key, "(value.priv")) {
103                 setting_user_value = 1;                         /* per-user view */
104         }
105         else {
106                 cprintf("%s NO [METADATA TOOMANY] SETMETADATA failed\r\n", Params[0].Key);
107                 return;
108         }
109
110         /*
111          * Extract the folder type without any parentheses.  Then learn
112          * the Citadel view type based on the supplied folder type.
113          */
114         extract_token(set_value, Params[5].Key, 0, ')', sizeof set_value);
115         if (!strncasecmp(set_value, "mail", 4)) {
116                 set_view = VIEW_MAILBOX;
117         }
118         else if (!strncasecmp(set_value, "event", 5)) {
119                 set_view = VIEW_CALENDAR;
120         }
121         else if (!strncasecmp(set_value, "contact", 7)) {
122                 set_view = VIEW_ADDRESSBOOK;
123         }
124         else if (!strncasecmp(set_value, "journal", 7)) {
125                 set_view = VIEW_JOURNAL;
126         }
127         else if (!strncasecmp(set_value, "note", 4)) {
128                 set_view = VIEW_NOTES;
129         }
130         else if (!strncasecmp(set_value, "task", 4)) {
131                 set_view = VIEW_TASKS;
132         }
133         else {
134                 set_view = VIEW_MAILBOX;
135         }
136
137         ret = imap_grabroom(roomname, Params[2].Key, 1);
138         if (ret != 0) {
139                 cprintf("%s NO Invalid mailbox name or access denied\r\n",
140                         Params[0].Key);
141                 return;
142         }
143
144         /*
145          * CtdlUserGoto() formally takes us to the desired room.  (If another
146          * folder is selected, save its name so we can return there!!!!!)
147          */
148         if (IMAP->selected) {
149                 strcpy(savedroom, CC->room.QRname);
150         }
151         CtdlUserGoto(roomname, 0, 0, &msgs, &new);
152
153         /*
154          * Always set the per-user view to the requested one.
155          */
156         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
157         vbuf.v_view = set_view;
158         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
159
160         /* If this is a "value.priv" set operation, we're done. */
161
162         if (setting_user_value)
163         {
164                 cprintf("%s OK SETANNOTATION complete\r\n", Params[0].Key);
165         }
166
167         /* If this is a "value.shared" set operation, we are allowed to perform it
168          * under certain conditions.
169          */
170         else if (       (is_room_aide())                                        /* aide or room aide */
171                 ||      (       (CC->room.QRflags & QR_MAILBOX)
172                         &&      (CC->user.usernum == atol(CC->room.QRname))     /* mailbox owner */
173                         )
174                 ||      (msgs == 0)             /* hack: if room is empty, assume we just created it */
175         ) {
176                 CtdlGetRoomLock(&CC->room, CC->room.QRname);
177                 CC->room.QRdefaultview = set_view;
178                 CtdlPutRoomLock(&CC->room);
179                 cprintf("%s OK SETANNOTATION complete\r\n", Params[0].Key);
180         }
181
182         /* If we got to this point, we don't have permission to set the default view. */
183         else {
184                 cprintf("%s NO [METADATA TOOMANY] SETMETADATA failed\r\n", Params[0].Key);
185         }
186
187         /*
188          * If a different folder was previously selected, return there now.
189          */
190         if ( (IMAP->selected) && (strcasecmp(roomname, savedroom)) ) {
191                 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
192         }
193         return;
194 }
195
196
197 /*
198  * Implements the GETMETADATA command.
199  *
200  * Regardless of what the client asked for, we are going to supply them with
201  * the folder type.  It's the only metadata we have anyway.
202  */
203 void imap_getmetadata(int num_parms, ConstStr *Params) {
204         char roomname[ROOMNAMELEN];
205         char savedroom[ROOMNAMELEN];
206         int msgs, new;
207         int ret;
208
209         if (num_parms > 5) {
210                 cprintf("%s BAD usage error\r\n", Params[0].Key);
211                 return;
212         }
213
214         ret = imap_grabroom(roomname, Params[2].Key, 1);
215         if (ret != 0) {
216                 cprintf("%s NO Invalid mailbox name or access denied\r\n",
217                         Params[0].Key);
218                 return;
219         }
220
221         /*
222          * CtdlUserGoto() formally takes us to the desired room.  (If another
223          * folder is selected, save its name so we can return there!!!!!)
224          */
225         if (IMAP->selected) {
226                 strcpy(savedroom, CC->room.QRname);
227         }
228         CtdlUserGoto(roomname, 0, 0, &msgs, &new);
229
230         cprintf("* METADATA ");
231         imap_strout(&Params[2]);
232         cprintf(" \"/vendor/kolab/folder-type\" (\"value.shared\" \"");
233
234         /* If it's one of our hard-coded default rooms, we know what to do... */
235
236         if (!strcasecmp(&CC->room.QRname[11], MAILROOM)) {
237                 cprintf("mail.inbox");
238         }
239         else if (!strcasecmp(&CC->room.QRname[11], SENTITEMS)) {
240                 cprintf("mail.sentitems");
241         }
242         else if (!strcasecmp(&CC->room.QRname[11], USERDRAFTROOM)) {
243                 cprintf("mail.drafts");
244         }
245         else if (!strcasecmp(&CC->room.QRname[11], USERCALENDARROOM)) {
246                 cprintf("event.default");
247         }
248         else if (!strcasecmp(&CC->room.QRname[11], USERCONTACTSROOM)) {
249                 cprintf("contact.default");
250         }
251         else if (!strcasecmp(&CC->room.QRname[11], USERNOTESROOM)) {
252                 cprintf("note.default");
253         }
254         else if (!strcasecmp(&CC->room.QRname[11], USERTASKSROOM)) {
255                 cprintf("task.default");
256         }
257
258         /* Otherwise, use the view for this room to determine the type of data.
259          * We are going with the default view rather than the user's view, because
260          * the default view almost always defines the actual contents, while the
261          * user's view might only make changes to presentation.  It also saves us
262          * an extra database access because we don't need to load the visit record.
263          */
264
265         else if (CC->room.QRdefaultview == VIEW_CALENDAR) {
266                 cprintf("event");
267         }
268         else if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
269                 cprintf("contact");
270         }
271         else if (CC->room.QRdefaultview == VIEW_TASKS) {
272                 cprintf("task");
273         }
274         else if (CC->room.QRdefaultview == VIEW_NOTES) {
275                 cprintf("note");
276         }
277         else if (CC->room.QRdefaultview == VIEW_JOURNAL) {
278                 cprintf("journal");
279         }
280
281         /* If none of the above conditions were met, consider it an ordinary mailbox. */
282         else {
283                 cprintf("mail");
284         }
285
286         /* "mail.outbox" and "junkemail" are not implemented. */
287
288         cprintf("\")\r\n");
289
290         /*
291          * If a different folder was previously selected, return there now.
292          */
293         if ( (IMAP->selected) && (strcasecmp(roomname, savedroom)) ) {
294                 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
295         }
296
297         cprintf("%s OK GETMETADATA complete\r\n", Params[0].Key);
298         return;
299 }
300