2 * Index messages by EUID per room.
4 * Copyright (c) 1987-2020 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <libcitadel.h>
19 #include "citserver.h"
23 * The structure of an euidindex record *key* is:
25 * |----room_number----|----------EUID-------------|
26 * (sizeof long) (actual length of euid)
29 * The structure of an euidindex record *value* is:
31 * |-----msg_number----|----room_number----|----------EUID-------------|
32 * (sizeof long) (sizeof long) (actual length of euid)
37 * Return nonzero if the supplied room is one which should have
40 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
42 switch(qrbuf->QRdefaultview) {
43 case VIEW_BBS: return(0);
44 case VIEW_MAILBOX: return(0);
45 case VIEW_ADDRESSBOOK: return(1);
46 case VIEW_DRAFTS: return(0);
47 case VIEW_CALENDAR: return(1);
48 case VIEW_TASKS: return(1);
49 case VIEW_NOTES: return(1);
50 case VIEW_WIKI: return(1);
51 case VIEW_BLOG: return(1);
59 * Locate a message in a given room with a given euid, and return
62 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
63 return CtdlLocateMessageByEuid (euid, qrbuf);
67 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
70 struct cdbdata *cdb_euid;
73 syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", 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 syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", 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 syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", 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);
128 * Called by rebuild_euid_index_for_room() to index one message.
130 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
131 struct CtdlMessage *msg = NULL;
133 msg = CtdlFetchMessage(msgnum, 0);
134 if (msg == NULL) return;
135 if (!CM_IsEmpty(msg, eExclusiveID)) {
136 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
142 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
143 static struct RoomProcList *rplist = NULL;
144 struct RoomProcList *ptr;
147 /* Lazy programming here. Call this function as a CtdlForEachRoom backend
148 * in order to queue up the room names, or call it with a null room
149 * to make it do the processing.
152 ptr = (struct RoomProcList *)
153 malloc(sizeof (struct RoomProcList));
154 if (ptr == NULL) return;
156 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
162 while (rplist != NULL) {
163 if (CtdlGetRoom(&qr, rplist->name) == 0) {
164 if (DoesThisRoomNeedEuidIndexing(&qr)) {
166 "euidindex: rebuilding EUID index for <%s>",
168 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
169 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
173 rplist = rplist->next;
180 * Globally rebuild the EUID indices in every room.
182 void rebuild_euid_index(void) {
183 cdb_trunc(CDB_EUIDINDEX); /* delete the old indices */
184 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
185 rebuild_euid_index_for_room(NULL, NULL); /* and index them */
190 * Server command to fetch a message number given an euid.
192 void cmd_euid(char *cmdbuf) {
195 struct cdbdata *cdbfr;
196 long *msglist = NULL;
200 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
202 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
203 msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
205 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
209 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
211 num_msgs = cdbfr->len / sizeof(long);
212 msglist = (long *) cdbfr->ptr;
213 for (i = 0; i < num_msgs; ++i) {
214 if (msglist[i] == msgnum) {
216 cprintf("%d %ld\n", CIT_OK, msgnum);
223 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
227 CTDL_MODULE_INIT(euidindex)
230 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
232 /* return our Subversion id for the Log */