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_WIKIMD: return(1);
52 case VIEW_BLOG: return(1);
60 * Locate a message in a given room with a given euid, and return
63 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
64 return CtdlLocateMessageByEuid (euid, qrbuf);
68 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
71 struct cdbdata *cdb_euid;
74 syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", euid, qrbuf->QRname);
76 key_len = strlen(euid) + sizeof(long) + 1;
77 key = malloc(key_len);
78 memcpy(key, &qrbuf->QRnumber, sizeof(long));
79 strcpy(&key[sizeof(long)], euid);
81 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
84 if (cdb_euid == NULL) {
88 /* The first (sizeof long) of the record is what we're
89 * looking for. Throw away the rest.
91 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
94 syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", msgnum);
100 * Store the euid index for a message, which has presumably just been
101 * stored in this room by the caller.
103 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
109 syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", msgnum, euid, qrbuf->QRname);
111 key_len = strlen(euid) + sizeof(long) + 1;
112 key = malloc(key_len);
113 memcpy(key, &qrbuf->QRnumber, sizeof(long));
114 strcpy(&key[sizeof(long)], euid);
116 data_len = sizeof(long) + key_len;
117 data = malloc(data_len);
119 memcpy(data, &msgnum, sizeof(long));
120 memcpy(&data[sizeof(long)], key, key_len);
122 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 = NULL;
134 msg = CtdlFetchMessage(msgnum, 0);
135 if (msg == NULL) return;
136 if (!CM_IsEmpty(msg, eExclusiveID)) {
137 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
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 CtdlForEachRoom 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 (CtdlGetRoom(&qr, rplist->name) == 0) {
165 if (DoesThisRoomNeedEuidIndexing(&qr)) {
167 "euidindex: rebuilding EUID index for <%s>",
169 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
170 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
174 rplist = rplist->next;
181 * Globally rebuild the EUID indices in every room.
183 void rebuild_euid_index(void) {
184 cdb_trunc(CDB_EUIDINDEX); /* delete the old indices */
185 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
186 rebuild_euid_index_for_room(NULL, NULL); /* and index them */
191 * Server command to fetch a message number given an euid.
193 void cmd_euid(char *cmdbuf) {
196 struct cdbdata *cdbfr;
197 long *msglist = NULL;
201 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
203 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
204 msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
206 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
210 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
212 num_msgs = cdbfr->len / sizeof(long);
213 msglist = (long *) cdbfr->ptr;
214 for (i = 0; i < num_msgs; ++i) {
215 if (msglist[i] == msgnum) {
217 cprintf("%d %ld\n", CIT_OK, msgnum);
224 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
228 CTDL_MODULE_INIT(euidindex)
231 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
233 /* return our Subversion id for the Log */