Don't register commands twice, only register them before threading begins.
[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         char *key;
95         int key_len;
96         struct cdbdata *cdb_euid;
97         long msgnum = (-1L);
98
99         CtdlLogPrintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
100
101         key_len = strlen(euid) + sizeof(long) + 1;
102         key = malloc(key_len);
103         memcpy(key, &qrbuf->QRnumber, sizeof(long));
104         strcpy(&key[sizeof(long)], euid);
105
106         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
107         free(key);
108
109         if (cdb_euid == NULL) {
110                 msgnum = (-1L);
111         }
112         else {
113                 /* The first (sizeof long) of the record is what we're
114                  * looking for.  Throw away the rest.
115                  */
116                 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
117                 cdb_free(cdb_euid);
118         }
119         CtdlLogPrintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
120         return(msgnum);
121 }
122
123
124 /*
125  * Store the euid index for a message, which has presumably just been
126  * stored in this room by the caller.
127  */
128 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
129         char *key;
130         int key_len;
131         char *data;
132         int data_len;
133
134         CtdlLogPrintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
135
136         key_len = strlen(euid) + sizeof(long) + 1;
137         key = malloc(key_len);
138         memcpy(key, &qrbuf->QRnumber, sizeof(long));
139         strcpy(&key[sizeof(long)], euid);
140
141         data_len = sizeof(long) + key_len;
142         data = malloc(data_len);
143
144         memcpy(data, &msgnum, sizeof(long));
145         memcpy(&data[sizeof(long)], key, key_len);
146
147         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
148         free(key);
149         free(data);
150 }
151
152
153
154 /*
155  * Called by rebuild_euid_index_for_room() to index one message.
156  */
157 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
158         struct CtdlMessage *msg = NULL;
159
160         msg = CtdlFetchMessage(msgnum, 0);
161         if (msg == NULL) return;
162         if (msg->cm_fields['E'] != NULL) {
163                 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
164         }
165         CtdlFreeMessage(msg);
166 }
167
168
169 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
170         static struct RoomProcList *rplist = NULL;
171         struct RoomProcList *ptr;
172         struct ctdlroom qr;
173
174         /* Lazy programming here.  Call this function as a CtdlForEachRoom backend
175          * in order to queue up the room names, or call it with a null room
176          * to make it do the processing.
177          */
178         if (qrbuf != NULL) {
179                 ptr = (struct RoomProcList *)
180                         malloc(sizeof (struct RoomProcList));
181                 if (ptr == NULL) return;
182
183                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
184                 ptr->next = rplist;
185                 rplist = ptr;
186                 return;
187         }
188
189         while (rplist != NULL) {
190                 if (CtdlGetRoom(&qr, rplist->name) == 0) {
191                         if (DoesThisRoomNeedEuidIndexing(&qr)) {
192                                 CtdlLogPrintf(CTDL_DEBUG,
193                                         "Rebuilding EUID index for <%s>\n",
194                                         rplist->name);
195                                 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL);
196                                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
197                                         rebuild_euid_index_for_msg, NULL);
198                         }
199                 }
200                 ptr = rplist;
201                 rplist = rplist->next;
202                 free(ptr);
203         }
204 }
205
206
207 /*
208  * Globally rebuild the EUID indices in every room.
209  */
210 void rebuild_euid_index(void) {
211         cdb_trunc(CDB_EUIDINDEX);               /* delete the old indices */
212         CtdlForEachRoom(rebuild_euid_index_for_room, NULL);     /* enumerate rm names */
213         rebuild_euid_index_for_room(NULL, NULL);        /* and index them */
214 }
215
216
217
218 /*
219  * Server command to fetch a message number given an euid.
220  */
221 void cmd_euid(char *cmdbuf) {
222         char euid[256];
223         long msgnum;
224         struct cdbdata *cdbfr;
225         long *msglist = NULL;
226         int num_msgs = 0;
227         int i;
228
229         if (CtdlAccessCheck(ac_logged_in)) return;
230
231         extract_token(euid, cmdbuf, 0, '|', sizeof euid);
232         msgnum = locate_message_by_euid(euid, &CC->room);
233         if (msgnum <= 0L) {
234                 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
235                 return;
236         }
237
238         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
239         if (cdbfr != NULL) {
240                 num_msgs = cdbfr->len / sizeof(long);
241                 msglist = (long *) cdbfr->ptr;
242                 for (i = 0; i < num_msgs; ++i) {
243                         if (msglist[i] == msgnum) {
244                                 cdb_free(cdbfr);
245                                 cprintf("%d %ld\n", CIT_OK, msgnum);
246                                 return;
247                         }
248                 }
249                 cdb_free(cdbfr);
250         }
251
252         cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
253 }
254
255 CTDL_MODULE_INIT(euidindex)
256 {
257         if (!threading) {
258                 CtdlRegisterProtoHook(cmd_euid, "EUID", "Autoconverted. TODO: document me.");
259         }
260         /* return our Subversion id for the Log */
261         return "$Id$";
262 }