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