cada83ac7591476a300c2c23feba6cefec0d2948
[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  * Locate a message in a given room with a given euid, and return
65  * its message number.
66  */
67 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
68         char *key;
69         int key_len;
70         struct cdbdata *cdb_euid;
71         long msgnum = (-1L);
72
73         lprintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
74
75         key_len = strlen(euid) + sizeof(long) + 1;
76         key = malloc(key_len);
77         memcpy(key, &qrbuf->QRnumber, sizeof(long));
78         strcpy(&key[sizeof(long)], euid);
79
80         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
81         free(key);
82
83         if (cdb_euid == NULL) {
84                 msgnum = (-1L);
85         }
86         else {
87                 /* The first (sizeof long) of the record is what we're
88                  * looking for.  Throw away the rest.
89                  */
90                 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
91                 cdb_free(cdb_euid);
92         }
93         lprintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
94         return(msgnum);
95 }
96
97
98 /*
99  * Store the euid index for a message, which has presumably just been
100  * stored in this room by the caller.
101  */
102 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
103         char *key;
104         int key_len;
105         char *data;
106         int data_len;
107
108         lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
109
110         key_len = strlen(euid) + sizeof(long) + 1;
111         key = malloc(key_len);
112         memcpy(key, &qrbuf->QRnumber, sizeof(long));
113         strcpy(&key[sizeof(long)], euid);
114
115         data_len = sizeof(long) + key_len;
116         data = malloc(data_len);
117
118         memcpy(data, &msgnum, sizeof(long));
119         memcpy(&data[sizeof(long)], key, key_len);
120
121         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
122         free(key);
123         free(data);
124 }
125
126
127
128 /*
129  * Called by rebuild_euid_index_for_room() to index one message.
130  */
131 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
132         struct CtdlMessage *msg;
133
134         msg = CtdlFetchMessage(msgnum, 0);
135         if (msg == NULL) return;
136         if (msg->cm_fields['E'] != NULL) {
137                 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
138         }
139         CtdlFreeMessage(msg);
140 }
141
142
143 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
144         static struct RoomProcList *rplist = NULL;
145         struct RoomProcList *ptr;
146         struct ctdlroom qr;
147
148         /* Lazy programming here.  Call this function as a ForEachRoom backend
149          * in order to queue up the room names, or call it with a null room
150          * to make it do the processing.
151          */
152         if (qrbuf != NULL) {
153                 ptr = (struct RoomProcList *)
154                         malloc(sizeof (struct RoomProcList));
155                 if (ptr == NULL) return;
156
157                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
158                 ptr->next = rplist;
159                 rplist = ptr;
160                 return;
161         }
162
163         while (rplist != NULL) {
164                 if (getroom(&qr, rplist->name) == 0) {
165                         lprintf(CTDL_DEBUG, "Rebuilding EUID index for <%s>\n", rplist->name);
166                         usergoto(rplist->name, 0, 0, NULL, NULL);
167                         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, rebuild_euid_index_for_msg, NULL);
168                 }
169                 ptr = rplist;
170                 rplist = rplist->next;
171                 free(ptr);
172         }
173 }
174
175
176 /*
177  * Globally rebuild the EUID indices in every room.
178  */
179 void rebuild_euid_index(void) {
180         cdb_trunc(CDB_EUIDINDEX);                       /* delete the old indices */
181         ForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate the room names */
182         rebuild_euid_index_for_room(NULL, NULL);        /* now do indexing on them */
183 }
184
185
186
187 /*
188  * Server command to fetch a message number given an euid.
189  */
190 void cmd_euid(char *cmdbuf) {
191         char euid[256];
192         long msgnum;
193
194         if (CtdlAccessCheck(ac_logged_in)) return;
195
196         extract_token(euid, cmdbuf, 0, '|', sizeof euid);
197         msgnum = locate_message_by_euid(euid, &CC->room);
198
199         if (msgnum > 0L) {
200                 cprintf("%d %ld\n", CIT_OK, msgnum);
201         }
202         else {
203                 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
204         }
205 }