2 * Index messages by EUID per room.
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
16 # include <sys/time.h>
29 #include <libcitadel.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
42 #include "euidindex.h"
44 #include "ctdl_module.h"
47 * The structure of an euidindex record *key* is:
49 * |----room_number----|----------EUID-------------|
50 * (sizeof long) (actual length of euid)
53 * The structure of an euidindex record *value* is:
55 * |-----msg_number----|----room_number----|----------EUID-------------|
56 * (sizeof long) (sizeof long) (actual length of euid)
63 * Return nonzero if the supplied room is one which should have
66 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
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_DRAFTS: return(0);
73 case VIEW_CALENDAR: return(1);
74 case VIEW_TASKS: return(1);
75 case VIEW_NOTES: return(1);
76 case VIEW_WIKI: return(1);
88 * Locate a message in a given room with a given euid, and return
91 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
92 return CtdlLocateMessageByEuid (euid, qrbuf);
95 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
98 struct cdbdata *cdb_euid;
101 CtdlLogPrintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
103 key_len = strlen(euid) + sizeof(long) + 1;
104 key = malloc(key_len);
105 memcpy(key, &qrbuf->QRnumber, sizeof(long));
106 strcpy(&key[sizeof(long)], euid);
108 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
111 if (cdb_euid == NULL) {
115 /* The first (sizeof long) of the record is what we're
116 * looking for. Throw away the rest.
118 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
121 CtdlLogPrintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
127 * Store the euid index for a message, which has presumably just been
128 * stored in this room by the caller.
130 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
136 CtdlLogPrintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
138 key_len = strlen(euid) + sizeof(long) + 1;
139 key = malloc(key_len);
140 memcpy(key, &qrbuf->QRnumber, sizeof(long));
141 strcpy(&key[sizeof(long)], euid);
143 data_len = sizeof(long) + key_len;
144 data = malloc(data_len);
146 memcpy(data, &msgnum, sizeof(long));
147 memcpy(&data[sizeof(long)], key, key_len);
149 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
157 * Called by rebuild_euid_index_for_room() to index one message.
159 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
160 struct CtdlMessage *msg = NULL;
162 msg = CtdlFetchMessage(msgnum, 0);
163 if (msg == NULL) return;
164 if (msg->cm_fields['E'] != NULL) {
165 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
167 CtdlFreeMessage(msg);
171 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
172 static struct RoomProcList *rplist = NULL;
173 struct RoomProcList *ptr;
176 /* Lazy programming here. Call this function as a CtdlForEachRoom backend
177 * in order to queue up the room names, or call it with a null room
178 * to make it do the processing.
181 ptr = (struct RoomProcList *)
182 malloc(sizeof (struct RoomProcList));
183 if (ptr == NULL) return;
185 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
191 while (rplist != NULL) {
192 if (CtdlGetRoom(&qr, rplist->name) == 0) {
193 if (DoesThisRoomNeedEuidIndexing(&qr)) {
194 CtdlLogPrintf(CTDL_DEBUG,
195 "Rebuilding EUID index for <%s>\n",
197 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL);
198 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
199 rebuild_euid_index_for_msg, NULL);
203 rplist = rplist->next;
210 * Globally rebuild the EUID indices in every room.
212 void rebuild_euid_index(void) {
213 cdb_trunc(CDB_EUIDINDEX); /* delete the old indices */
214 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
215 rebuild_euid_index_for_room(NULL, NULL); /* and index them */
221 * Server command to fetch a message number given an euid.
223 void cmd_euid(char *cmdbuf) {
226 struct cdbdata *cdbfr;
227 long *msglist = NULL;
231 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
233 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
234 msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
236 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
240 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
242 num_msgs = cdbfr->len / sizeof(long);
243 msglist = (long *) cdbfr->ptr;
244 for (i = 0; i < num_msgs; ++i) {
245 if (msglist[i] == msgnum) {
247 cprintf("%d %ld\n", CIT_OK, msgnum);
254 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
257 CTDL_MODULE_INIT(euidindex)
260 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
262 /* return our Subversion id for the Log */