#include "tools.h"
#include "euidindex.h"
+/*
+ * The structure of an euidindex record *key* is:
+ *
+ * |----room_number----|----------EUID-------------|
+ * (sizeof long) (actual length of euid)
+ *
+ *
+ * The structure of an euidindex record *value* is:
+ *
+ * |-----msg_number----|----room_number----|----------EUID-------------|
+ * (sizeof long) (sizeof long) (actual length of euid)
+ *
+ */
+
+/*
+ * Locate a message in a given room with a given euid, and return
+ * its message number.
+ */
long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
char *key;
int key_len;
free(key);
if (cdb_euid == NULL) {
- return(-1L);
+ msgnum = (-1L);
}
-
- if (cdb_euid->len == sizeof(long)) {
- msgnum = *(long *)cdb_euid->ptr;
+ else {
+ /* The first (sizeof long) of the record is what we're
+ * looking for. Throw away the rest.
+ */
+ memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
+ cdb_free(cdb_euid);
}
-
- cdb_free(cdb_euid);
- lprintf(CTDL_DEBUG, "returning msgnum==%ld\n", msgnum);
+ lprintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
return(msgnum);
}
+
+/*
+ * Store the euid index for a message, which has presumably just been
+ * stored in this room by the caller.
+ */
void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
char *key;
int key_len;
+ char *data;
+ int data_len;
lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
memcpy(key, &qrbuf->QRnumber, sizeof(long));
strcpy(&key[sizeof(long)], euid);
- cdb_store(CDB_EUIDINDEX, key, key_len, &msgnum, sizeof(long));
+ data_len = sizeof(long) + key_len;
+ data = malloc(data_len);
+
+ memcpy(data, &msgnum, sizeof(long));
+ memcpy(&data[sizeof(long)], key, key_len);
+
+ cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
free(key);
+ free(data);
}
* Globally rebuild the EUID indices in every room.
*/
void rebuild_euid_index(void) {
- cdb_trunc(CDB_EUIDINDEX);
+ cdb_trunc(CDB_EUIDINDEX); /* delete the old indices */
+ ForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate the room names */
+ rebuild_euid_index_for_room(NULL, NULL); /* now do indexing on them */
+}
- ForEachRoom(rebuild_euid_index_for_room, NULL);
- rebuild_euid_index_for_room(NULL, NULL);
+
+
+/*
+ * Server command to fetch a message number given an euid.
+ */
+void cmd_euid(char *cmdbuf) {
+ char euid[256];
+ long msgnum;
+
+ if (CtdlAccessCheck(ac_logged_in)) return;
+
+ extract_token(euid, cmdbuf, 0, '|', sizeof euid);
+ msgnum = locate_message_by_euid(euid, &CC->room);
+
+ if (msgnum > 0L) {
+ cprintf("%d %ld\n", CIT_OK, msgnum);
+ }
+ else {
+ cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
+ }
}