bcf0606ed1ea67dc79476c2f6f34c39f2a78d66c
[citadel.git] / citadel / euidindex.c
1 /*
2  * Index messages by EUID per room.
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
13 # include <time.h>
14 #else
15 # if HAVE_SYS_TIME_H
16 #  include <sys/time.h>
17 # else
18 #  include <time.h>
19 # endif
20 #endif
21
22
23 #include <ctype.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <sys/stat.h>
29 #include <libcitadel.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include "database.h"
33 #include "msgbase.h"
34 #include "support.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "room_ops.h"
38 #include "user_ops.h"
39 #include "file_ops.h"
40 #include "config.h"
41 #include "control.h"
42 #include "euidindex.h"
43
44 #include "ctdl_module.h"
45
46 /*
47  * The structure of an euidindex record *key* is:
48  *
49  * |----room_number----|----------EUID-------------|
50  *    (sizeof long)       (actual length of euid)
51  *
52  *
53  * The structure of an euidindex record *value* is:
54  *
55  * |-----msg_number----|----room_number----|----------EUID-------------|
56  *    (sizeof long)       (sizeof long)       (actual length of euid)
57  *
58  */
59
60
61
62 /*
63  * Return nonzero if the supplied room is one which should have
64  * an EUID index.
65  */
66 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
67
68         switch(qrbuf->QRdefaultview) {
69                 case VIEW_BBS:          return(0);
70                 case VIEW_MAILBOX:      return(0);
71                 case VIEW_ADDRESSBOOK:  return(1);
72                 case VIEW_DRAFTS:       return(0);
73                 case VIEW_CALENDAR:     return(1);
74                 case VIEW_TASKS:        return(1);
75                 case VIEW_NOTES:        return(1);
76                 case VIEW_WIKI:         return(1);
77                 case VIEW_BLOG:         return(1);
78         }
79         
80         return(0);
81 }
82
83
84
85
86
87
88 /*
89  * Locate a message in a given room with a given euid, and return
90  * its message number.
91  */
92 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
93         return CtdlLocateMessageByEuid (euid, qrbuf);
94 }
95
96 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
97         char *key;
98         int key_len;
99         struct cdbdata *cdb_euid;
100         long msgnum = (-1L);
101
102         syslog(LOG_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
103
104         key_len = strlen(euid) + sizeof(long) + 1;
105         key = malloc(key_len);
106         memcpy(key, &qrbuf->QRnumber, sizeof(long));
107         strcpy(&key[sizeof(long)], euid);
108
109         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
110         free(key);
111
112         if (cdb_euid == NULL) {
113                 msgnum = (-1L);
114         }
115         else {
116                 /* The first (sizeof long) of the record is what we're
117                  * looking for.  Throw away the rest.
118                  */
119                 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
120                 cdb_free(cdb_euid);
121         }
122         syslog(LOG_DEBUG, "returning msgnum = %ld\n", msgnum);
123         return(msgnum);
124 }
125
126
127 /*
128  * Store the euid index for a message, which has presumably just been
129  * stored in this room by the caller.
130  */
131 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
132         char *key;
133         int key_len;
134         char *data;
135         int data_len;
136
137         syslog(LOG_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
138
139         key_len = strlen(euid) + sizeof(long) + 1;
140         key = malloc(key_len);
141         memcpy(key, &qrbuf->QRnumber, sizeof(long));
142         strcpy(&key[sizeof(long)], euid);
143
144         data_len = sizeof(long) + key_len;
145         data = malloc(data_len);
146
147         memcpy(data, &msgnum, sizeof(long));
148         memcpy(&data[sizeof(long)], key, key_len);
149
150         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
151         free(key);
152         free(data);
153 }
154
155
156
157 /*
158  * Called by rebuild_euid_index_for_room() to index one message.
159  */
160 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
161         struct CtdlMessage *msg = NULL;
162
163         msg = CtdlFetchMessage(msgnum, 0);
164         if (msg == NULL) return;
165         if (msg->cm_fields[eExclusiveID] != NULL) {
166                 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
167         }
168         CtdlFreeMessage(msg);
169 }
170
171
172 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
173         static struct RoomProcList *rplist = NULL;
174         struct RoomProcList *ptr;
175         struct ctdlroom qr;
176
177         /* Lazy programming here.  Call this function as a CtdlForEachRoom backend
178          * in order to queue up the room names, or call it with a null room
179          * to make it do the processing.
180          */
181         if (qrbuf != NULL) {
182                 ptr = (struct RoomProcList *)
183                         malloc(sizeof (struct RoomProcList));
184                 if (ptr == NULL) return;
185
186                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
187                 ptr->next = rplist;
188                 rplist = ptr;
189                 return;
190         }
191
192         while (rplist != NULL) {
193                 if (CtdlGetRoom(&qr, rplist->name) == 0) {
194                         if (DoesThisRoomNeedEuidIndexing(&qr)) {
195                                 syslog(LOG_DEBUG,
196                                         "Rebuilding EUID index for <%s>\n",
197                                         rplist->name);
198                                 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL);
199                                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
200                                         rebuild_euid_index_for_msg, NULL);
201                         }
202                 }
203                 ptr = rplist;
204                 rplist = rplist->next;
205                 free(ptr);
206         }
207 }
208
209
210 /*
211  * Globally rebuild the EUID indices in every room.
212  */
213 void rebuild_euid_index(void) {
214         cdb_trunc(CDB_EUIDINDEX);               /* delete the old indices */
215         CtdlForEachRoom(rebuild_euid_index_for_room, NULL);     /* enumerate rm names */
216         rebuild_euid_index_for_room(NULL, NULL);        /* and index them */
217 }
218
219
220
221 /*
222  * Server command to fetch a message number given an euid.
223  */
224 void cmd_euid(char *cmdbuf) {
225         char euid[256];
226         long msgnum;
227         struct cdbdata *cdbfr;
228         long *msglist = NULL;
229         int num_msgs = 0;
230         int i;
231
232         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
233
234         extract_token(euid, cmdbuf, 0, '|', sizeof euid);
235         msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
236         if (msgnum <= 0L) {
237                 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
238                 return;
239         }
240
241         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
242         if (cdbfr != NULL) {
243                 num_msgs = cdbfr->len / sizeof(long);
244                 msglist = (long *) cdbfr->ptr;
245                 for (i = 0; i < num_msgs; ++i) {
246                         if (msglist[i] == msgnum) {
247                                 cdb_free(cdbfr);
248                                 cprintf("%d %ld\n", CIT_OK, msgnum);
249                                 return;
250                         }
251                 }
252                 cdb_free(cdbfr);
253         }
254
255         cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
256 }
257
258 CTDL_MODULE_INIT(euidindex)
259 {
260         if (!threading) {
261                 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
262         }
263         /* return our Subversion id for the Log */
264         return "euidindex";
265 }