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_WIKIMD: return(1);
44 case VIEW_BLOG: return(1);
56 * Locate a message in a given room with a given euid, and return
59 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
60 return CtdlLocateMessageByEuid (euid, qrbuf);
63 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
66 struct cdbdata *cdb_euid;
69 syslog(LOG_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
71 key_len = strlen(euid) + sizeof(long) + 1;
72 key = malloc(key_len);
73 memcpy(key, &qrbuf->QRnumber, sizeof(long));
74 strcpy(&key[sizeof(long)], euid);
76 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
79 if (cdb_euid == NULL) {
83 /* The first (sizeof long) of the record is what we're
84 * looking for. Throw away the rest.
86 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
89 syslog(LOG_DEBUG, "returning msgnum = %ld\n", msgnum);
95 * Store the euid index for a message, which has presumably just been
96 * stored in this room by the caller.
98 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
104 syslog(LOG_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
106 key_len = strlen(euid) + sizeof(long) + 1;
107 key = malloc(key_len);
108 memcpy(key, &qrbuf->QRnumber, sizeof(long));
109 strcpy(&key[sizeof(long)], euid);
111 data_len = sizeof(long) + key_len;
112 data = malloc(data_len);
114 memcpy(data, &msgnum, sizeof(long));
115 memcpy(&data[sizeof(long)], key, key_len);
117 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
125 * Called by rebuild_euid_index_for_room() to index one message.
127 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
128 struct CtdlMessage *msg = NULL;
130 msg = CtdlFetchMessage(msgnum, 0);
131 if (msg == NULL) return;
132 if (!CM_IsEmpty(msg, eExclusiveID)) {
133 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
139 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
140 static struct RoomProcList *rplist = NULL;
141 struct RoomProcList *ptr;
144 /* Lazy programming here. Call this function as a CtdlForEachRoom backend
145 * in order to queue up the room names, or call it with a null room
146 * to make it do the processing.
149 ptr = (struct RoomProcList *)
150 malloc(sizeof (struct RoomProcList));
151 if (ptr == NULL) return;
153 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
159 while (rplist != NULL) {
160 if (CtdlGetRoom(&qr, rplist->name) == 0) {
161 if (DoesThisRoomNeedEuidIndexing(&qr)) {
163 "Rebuilding EUID index for <%s>\n",
165 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
166 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
167 rebuild_euid_index_for_msg, NULL);
171 rplist = rplist->next;
178 * Globally rebuild the EUID indices in every room.
180 void rebuild_euid_index(void) {
181 cdb_trunc(CDB_EUIDINDEX); /* delete the old indices */
182 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
183 rebuild_euid_index_for_room(NULL, NULL); /* and index them */
189 * Server command to fetch a message number given an euid.
191 void cmd_euid(char *cmdbuf) {
194 struct cdbdata *cdbfr;
195 long *msglist = NULL;
199 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
201 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
202 msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
204 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
208 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
210 num_msgs = cdbfr->len / sizeof(long);
211 msglist = (long *) cdbfr->ptr;
212 for (i = 0; i < num_msgs; ++i) {
213 if (msglist[i] == msgnum) {
215 cprintf("%d %ld\n", CIT_OK, msgnum);
222 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
225 CTDL_MODULE_INIT(euidindex)
228 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
230 /* return our Subversion id for the Log */