1 // Index messages by EUID per room.
3 // Copyright (c) 1987-2022 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
15 #include <libcitadel.h>
17 #include "citserver.h"
20 // The structure of an euidindex record *key* is:
22 // |----room_number----|----------EUID-------------|
23 // (sizeof long) (actual length of euid)
26 // The structure of an euidindex record *value* is:
28 // |-----msg_number----|----room_number----|----------EUID-------------|
29 // (sizeof long) (sizeof long) (actual length of euid)
31 // Return nonzero if the supplied room is one which should have
33 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
35 switch(qrbuf->QRdefaultview) {
36 case VIEW_BBS: return(0);
37 case VIEW_MAILBOX: return(0);
38 case VIEW_ADDRESSBOOK: return(1);
39 case VIEW_DRAFTS: return(0);
40 case VIEW_CALENDAR: return(1);
41 case VIEW_TASKS: return(1);
42 case VIEW_NOTES: return(1);
43 case VIEW_WIKI: return(1);
44 case VIEW_BLOG: return(1);
51 // Locate a message in a given room with a given euid, and return
52 // its message number.
53 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
54 return CtdlLocateMessageByEuid (euid, qrbuf);
58 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
61 struct cdbdata *cdb_euid;
64 syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", euid, qrbuf->QRname);
66 key_len = strlen(euid) + sizeof(long) + 1;
67 key = malloc(key_len);
68 memcpy(key, &qrbuf->QRnumber, sizeof(long));
69 strcpy(&key[sizeof(long)], euid);
71 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
74 if (cdb_euid == NULL) {
78 // The first (sizeof long) of the record is what we're looking for. Throw away the rest.
79 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
82 syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", msgnum);
87 // Store the euid index for a message, which has presumably just been
88 // stored in this room by the caller.
89 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
95 syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", msgnum, euid, qrbuf->QRname);
97 key_len = strlen(euid) + sizeof(long) + 1;
98 key = malloc(key_len);
99 memcpy(key, &qrbuf->QRnumber, sizeof(long));
100 strcpy(&key[sizeof(long)], euid);
102 data_len = sizeof(long) + key_len;
103 data = malloc(data_len);
105 memcpy(data, &msgnum, sizeof(long));
106 memcpy(&data[sizeof(long)], key, key_len);
108 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
114 // Called by rebuild_euid_index_for_room() to index one message.
115 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
116 struct CtdlMessage *msg = NULL;
118 msg = CtdlFetchMessage(msgnum, 0);
119 if (msg == NULL) return;
120 if (!CM_IsEmpty(msg, eExclusiveID)) {
121 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
127 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
128 static struct RoomProcList *rplist = NULL;
129 struct RoomProcList *ptr;
132 // Lazy programming here. Call this function as a CtdlForEachRoom backend
133 // in order to queue up the room names, or call it with a null room
134 // to make it do the processing.
136 ptr = (struct RoomProcList *)
137 malloc(sizeof (struct RoomProcList));
138 if (ptr == NULL) return;
140 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
146 while (rplist != NULL) {
147 if (CtdlGetRoom(&qr, rplist->name) == 0) {
148 if (DoesThisRoomNeedEuidIndexing(&qr)) {
150 "euidindex: rebuilding EUID index for <%s>",
152 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
153 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
157 rplist = rplist->next;
163 // Globally rebuild the EUID indices in every room.
164 void rebuild_euid_index(void) {
165 cdb_trunc(CDB_EUIDINDEX); // delete the old indices
166 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); // enumerate room names
167 rebuild_euid_index_for_room(NULL, NULL); // and index them
171 // Server command to fetch a message number given an euid.
172 void cmd_euid(char *cmdbuf) {
175 struct cdbdata *cdbfr;
176 long *msglist = NULL;
180 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
182 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
183 msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
185 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
189 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
191 num_msgs = cdbfr->len / sizeof(long);
192 msglist = (long *) cdbfr->ptr;
193 for (i = 0; i < num_msgs; ++i) {
194 if (msglist[i] == msgnum) {
196 cprintf("%d %ld\n", CIT_OK, msgnum);
203 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
207 CTDL_MODULE_INIT(euidindex)
210 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
212 /* return our Subversion id for the Log */