4 * Index messages by EUID per room.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
34 #include "serv_extensions.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
46 #include "euidindex.h"
49 * The structure of an euidindex record *key* is:
51 * |----room_number----|----------EUID-------------|
52 * (sizeof long) (actual length of euid)
55 * The structure of an euidindex record *value* is:
57 * |-----msg_number----|----room_number----|----------EUID-------------|
58 * (sizeof long) (sizeof long) (actual length of euid)
64 * Locate a message in a given room with a given euid, and return
67 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
70 struct cdbdata *cdb_euid;
73 lprintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
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);
80 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
83 if (cdb_euid == NULL) {
87 /* The first (sizeof long) of the record is what we're
88 * looking for. Throw away the rest.
90 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
93 lprintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
99 * Store the euid index for a message, which has presumably just been
100 * stored in this room by the caller.
102 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
108 lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
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);
115 data_len = sizeof(long) + key_len;
116 data = malloc(data_len);
118 memcpy(data, &msgnum, sizeof(long));
119 memcpy(&data[sizeof(long)], key, key_len);
121 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
129 * Called by rebuild_euid_index_for_room() to index one message.
131 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
132 struct CtdlMessage *msg;
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);
139 CtdlFreeMessage(msg);
143 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
144 static struct RoomProcList *rplist = NULL;
145 struct RoomProcList *ptr;
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.
153 ptr = (struct RoomProcList *)
154 malloc(sizeof (struct RoomProcList));
155 if (ptr == NULL) return;
157 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
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);
170 rplist = rplist->next;
177 * Globally rebuild the EUID indices in every room.
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 */
188 * Server command to fetch a message number given an euid.
190 void cmd_euid(char *cmdbuf) {
193 struct cdbdata *cdbfr;
194 long *msglist = NULL;
198 if (CtdlAccessCheck(ac_logged_in)) return;
200 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
201 msgnum = locate_message_by_euid(euid, &CC->room);
203 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
207 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
209 num_msgs = cdbfr->len / sizeof(long);
210 msglist = (long *) cdbfr->ptr;
211 for (i = 0; i < num_msgs; ++i) {
212 if (msglist[i] == msgnum) {
214 cprintf("%d %ld\n", CIT_OK, msgnum);
221 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);