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