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