* Changed the format of the euidindex record to contain the record's key.
[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 EUID *index* is:
50  *
51  * |----room_number----|----------EUID-------------|
52  *    (sizeof long)       (actual length of euid)
53  *
54  *
55  * The structure of an EUID *record* is:
56  *
57  * |-----msg_number----|----room_number----|----------EUID-------------|
58  *    (sizeof long)       (sizeof long)       (actual length of euid)
59  *
60  */
61
62 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
63         char *key;
64         int key_len;
65         struct cdbdata *cdb_euid;
66         long msgnum = (-1L);
67
68         lprintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
69
70         key_len = strlen(euid) + sizeof(long) + 1;
71         key = malloc(key_len);
72         memcpy(key, &qrbuf->QRnumber, sizeof(long));
73         strcpy(&key[sizeof(long)], euid);
74
75         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
76         free(key);
77
78         if (cdb_euid == NULL) {
79                 msgnum = (-1L);
80         }
81         else {
82                 /* The first (sizeof long) of the record is what we're
83                  * looking for.  Throw away the rest.
84                  */
85                 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
86                 cdb_free(cdb_euid);
87         }
88         lprintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
89         return(msgnum);
90 }
91
92 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
93         char *key;
94         int key_len;
95         char *data;
96         int data_len;
97
98         lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, 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         data_len = sizeof(long) + key_len;
106         data = malloc(data_len);
107
108         memcpy(data, &msgnum, sizeof(long));
109         memcpy(&data[sizeof(long)], key, key_len);
110
111         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
112         free(key);
113         free(data);
114 }
115
116
117
118 /*
119  * Called by rebuild_euid_index_for_room() to index one message.
120  */
121 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
122         struct CtdlMessage *msg;
123
124         msg = CtdlFetchMessage(msgnum, 0);
125         if (msg == NULL) return;
126         if (msg->cm_fields['E'] != NULL) {
127                 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
128         }
129         CtdlFreeMessage(msg);
130 }
131
132
133 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
134         static struct RoomProcList *rplist = NULL;
135         struct RoomProcList *ptr;
136         struct ctdlroom qr;
137
138         /* Lazy programming here.  Call this function as a ForEachRoom backend
139          * in order to queue up the room names, or call it with a null room
140          * to make it do the processing.
141          */
142         if (qrbuf != NULL) {
143                 ptr = (struct RoomProcList *)
144                         malloc(sizeof (struct RoomProcList));
145                 if (ptr == NULL) return;
146
147                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
148                 ptr->next = rplist;
149                 rplist = ptr;
150                 return;
151         }
152
153         while (rplist != NULL) {
154                 if (getroom(&qr, rplist->name) == 0) {
155                         lprintf(CTDL_DEBUG, "Rebuilding EUID index for <%s>\n", rplist->name);
156                         usergoto(rplist->name, 0, 0, NULL, NULL);
157                         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, rebuild_euid_index_for_msg, NULL);
158                 }
159                 ptr = rplist;
160                 rplist = rplist->next;
161                 free(ptr);
162         }
163 }
164
165
166 /*
167  * Globally rebuild the EUID indices in every room.
168  */
169 void rebuild_euid_index(void) {
170         cdb_trunc(CDB_EUIDINDEX);                       /* delete the old indices */
171         ForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate the room names */
172         rebuild_euid_index_for_room(NULL, NULL);        /* now do indexing on them */
173 }