2 * Index messages by EUID per room.
7 #include <libcitadel.h>
13 * The structure of an euidindex record *key* is:
15 * |----room_number----|----------EUID-------------|
16 * (sizeof long) (actual length of euid)
19 * The structure of an euidindex record *value* is:
21 * |-----msg_number----|----room_number----|----------EUID-------------|
22 * (sizeof long) (sizeof long) (actual length of euid)
29 * Return nonzero if the supplied room is one which should have
32 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
34 switch(qrbuf->QRdefaultview) {
35 case VIEW_BBS: return(0);
36 case VIEW_MAILBOX: return(0);
37 case VIEW_ADDRESSBOOK: return(1);
38 case VIEW_DRAFTS: return(0);
39 case VIEW_CALENDAR: return(1);
40 case VIEW_TASKS: return(1);
41 case VIEW_NOTES: return(1);
42 case VIEW_WIKI: return(1);
43 case VIEW_BLOG: return(1);
55 * Locate a message in a given room with a given euid, and return
58 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
59 return CtdlLocateMessageByEuid (euid, qrbuf);
62 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
65 struct cdbdata *cdb_euid;
68 syslog(LOG_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
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);
75 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
78 if (cdb_euid == NULL) {
82 /* The first (sizeof long) of the record is what we're
83 * looking for. Throw away the rest.
85 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
88 syslog(LOG_DEBUG, "returning msgnum = %ld\n", msgnum);
94 * Store the euid index for a message, which has presumably just been
95 * stored in this room by the caller.
97 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
103 syslog(LOG_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
105 key_len = strlen(euid) + sizeof(long) + 1;
106 key = malloc(key_len);
107 memcpy(key, &qrbuf->QRnumber, sizeof(long));
108 strcpy(&key[sizeof(long)], euid);
110 data_len = sizeof(long) + key_len;
111 data = malloc(data_len);
113 memcpy(data, &msgnum, sizeof(long));
114 memcpy(&data[sizeof(long)], key, key_len);
116 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
124 * Called by rebuild_euid_index_for_room() to index one message.
126 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
127 struct CtdlMessage *msg = NULL;
129 msg = CtdlFetchMessage(msgnum, 0);
130 if (msg == NULL) return;
131 if (!CM_IsEmpty(msg, eExclusiveID)) {
132 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
138 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
139 static struct RoomProcList *rplist = NULL;
140 struct RoomProcList *ptr;
143 /* Lazy programming here. Call this function as a CtdlForEachRoom backend
144 * in order to queue up the room names, or call it with a null room
145 * to make it do the processing.
148 ptr = (struct RoomProcList *)
149 malloc(sizeof (struct RoomProcList));
150 if (ptr == NULL) return;
152 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
158 while (rplist != NULL) {
159 if (CtdlGetRoom(&qr, rplist->name) == 0) {
160 if (DoesThisRoomNeedEuidIndexing(&qr)) {
162 "Rebuilding EUID index for <%s>\n",
164 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL);
165 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
166 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 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
182 rebuild_euid_index_for_room(NULL, NULL); /* and index 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_or_guest)) return;
200 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
201 msgnum = CtdlLocateMessageByEuid(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);
224 CTDL_MODULE_INIT(euidindex)
227 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
229 /* return our Subversion id for the Log */