2 * IMAP METADATA extension
4 * This is an implementation of the Bynari variant of the METADATA extension.
6 * Copyright (c) 2007-2009 by the citadel.org team
8 * This program is open source 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.
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.
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
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "serv_imap.h"
61 #include "imap_tools.h"
62 #include "imap_fetch.h"
63 #include "imap_misc.h"
66 #include "ctdl_module.h"
69 * Implements the SETMETADATA command.
71 * Again, the only thing we're interested in setting here is the folder type.
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.
76 void imap_setmetadata(int num_parms, ConstStr *Params) {
77 char roomname[ROOMNAMELEN];
78 char savedroom[ROOMNAMELEN];
81 int setting_user_value = 0;
83 int set_view = VIEW_BBS;
87 IReply("BAD usage error");
92 * Don't allow other types of metadata to be set
94 if (strcasecmp(Params[3].Key, "/vendor/kolab/folder-type")) {
95 IReply("NO [METADATA TOOMANY] SETMETADATA failed");
99 if (!strcasecmp(Params[4].Key, "(value.shared")) {
100 setting_user_value = 0; /* global view */
102 else if (!strcasecmp(Params[4].Key, "(value.priv")) {
103 setting_user_value = 1; /* per-user view */
106 IReply("NO [METADATA TOOMANY] SETMETADATA failed");
111 * Extract the folder type without any parentheses. Then learn
112 * the Citadel view type based on the supplied folder type.
114 extract_token(set_value, Params[5].Key, 0, ')', sizeof set_value);
115 if (!strncasecmp(set_value, "mail", 4)) {
116 set_view = VIEW_MAILBOX;
118 else if (!strncasecmp(set_value, "event", 5)) {
119 set_view = VIEW_CALENDAR;
121 else if (!strncasecmp(set_value, "contact", 7)) {
122 set_view = VIEW_ADDRESSBOOK;
124 else if (!strncasecmp(set_value, "journal", 7)) {
125 set_view = VIEW_JOURNAL;
127 else if (!strncasecmp(set_value, "note", 4)) {
128 set_view = VIEW_NOTES;
130 else if (!strncasecmp(set_value, "task", 4)) {
131 set_view = VIEW_TASKS;
134 set_view = VIEW_MAILBOX;
137 ret = imap_grabroom(roomname, Params[2].Key, 1);
139 IReply("NO Invalid mailbox name or access denied");
144 * CtdlUserGoto() formally takes us to the desired room. (If another
145 * folder is selected, save its name so we can return there!!!!!)
147 if (IMAP->selected) {
148 strcpy(savedroom, CC->room.QRname);
150 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
153 * Always set the per-user view to the requested one.
155 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
156 vbuf.v_view = set_view;
157 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
159 /* If this is a "value.priv" set operation, we're done. */
161 if (setting_user_value)
163 IReply("OK SETANNOTATION complete");
166 /* If this is a "value.shared" set operation, we are allowed to perform it
167 * under certain conditions.
169 else if ( (is_room_aide()) /* aide or room aide */
170 || ( (CC->room.QRflags & QR_MAILBOX)
171 && (CC->user.usernum == atol(CC->room.QRname)) /* mailbox owner */
173 || (msgs == 0) /* hack: if room is empty, assume we just created it */
175 CtdlGetRoomLock(&CC->room, CC->room.QRname);
176 CC->room.QRdefaultview = set_view;
177 CtdlPutRoomLock(&CC->room);
178 IReply("OK SETANNOTATION complete");
181 /* If we got to this point, we don't have permission to set the default view. */
183 IReply("NO [METADATA TOOMANY] SETMETADATA failed");
187 * If a different folder was previously selected, return there now.
189 if ( (IMAP->selected) && (strcasecmp(roomname, savedroom)) ) {
190 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
197 * Implements the GETMETADATA command.
199 * Regardless of what the client asked for, we are going to supply them with
200 * the folder type. It's the only metadata we have anyway.
202 void imap_getmetadata(int num_parms, ConstStr *Params) {
203 char roomname[ROOMNAMELEN];
204 char savedroom[ROOMNAMELEN];
209 /* this doesn't work if you have rooms/floors with spaces.
210 we need this for the bynari connector.
212 IReply("BAD usage error");
216 ret = imap_grabroom(roomname, Params[2].Key, 1);
218 IReply("NO Invalid mailbox name or access denied");
223 * CtdlUserGoto() formally takes us to the desired room. (If another
224 * folder is selected, save its name so we can return there!!!!!)
226 if (IMAP->selected) {
227 strcpy(savedroom, CC->room.QRname);
229 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
231 IAPuts("* METADATA ");
233 IAPuts(" \"/vendor/kolab/folder-type\" (\"value.shared\" \"");
235 /* If it's one of our hard-coded default rooms, we know what to do... */
237 if (CC->room.QRname[10] == '.')
239 if (!strcasecmp(&CC->room.QRname[11], MAILROOM)) {
241 IAPuts("mail.inbox");
243 else if (!strcasecmp(&CC->room.QRname[11], SENTITEMS)) {
245 IAPuts("mail.sentitems");
247 else if (!strcasecmp(&CC->room.QRname[11], USERDRAFTROOM)) {
249 IAPuts("mail.drafts");
251 else if (!strcasecmp(&CC->room.QRname[11], USERCALENDARROOM)) {
253 IAPuts("event.default");
255 else if (!strcasecmp(&CC->room.QRname[11], USERCONTACTSROOM)) {
257 IAPuts("contact.default");
259 else if (!strcasecmp(&CC->room.QRname[11], USERNOTESROOM)) {
261 IAPuts("note.default");
263 else if (!strcasecmp(&CC->room.QRname[11], USERTASKSROOM)) {
265 IAPuts("task.default");
269 /* Otherwise, use the view for this room to determine the type of data.
270 * We are going with the default view rather than the user's view, because
271 * the default view almost always defines the actual contents, while the
272 * user's view might only make changes to presentation. It also saves us
273 * an extra database access because we don't need to load the visit record.
277 if (CC->room.QRdefaultview == VIEW_CALENDAR) {
280 else if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
283 else if (CC->room.QRdefaultview == VIEW_TASKS) {
286 else if (CC->room.QRdefaultview == VIEW_NOTES) {
289 else if (CC->room.QRdefaultview == VIEW_JOURNAL) {
293 /* If none of the above conditions were met, consider it an ordinary mailbox. */
299 /* "mail.outbox" and "junkemail" are not implemented. */
304 * If a different folder was previously selected, return there now.
306 if ( (IMAP->selected) && (strcasecmp(roomname, savedroom)) ) {
307 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
310 IReply("OK GETMETADATA complete");