1 // Index messages by EUID per room.
3 // Copyright (c) 1987-2022 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
10 #include <libcitadel.h>
11 #include "citserver.h"
14 // The structure of an euidindex record *key* is:
16 // |----room_number----|----------EUID-------------|
17 // (sizeof long) (actual length of euid)
20 // The structure of an euidindex record *value* is:
22 // |-----msg_number----|----room_number----|----------EUID-------------|
23 // (sizeof long) (sizeof long) (actual length of euid)
25 // Return nonzero if the supplied room is one which should have
27 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
29 switch(qrbuf->QRdefaultview) {
30 case VIEW_BBS: return(0);
31 case VIEW_MAILBOX: return(0);
32 case VIEW_ADDRESSBOOK: return(1);
33 case VIEW_DRAFTS: return(0);
34 case VIEW_CALENDAR: return(1);
35 case VIEW_TASKS: return(1);
36 case VIEW_NOTES: return(1);
37 case VIEW_WIKI: return(1);
38 case VIEW_BLOG: return(1);
45 // Locate a message in a given room with a given euid, and return
46 // its message number.
47 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
48 return CtdlLocateMessageByEuid (euid, qrbuf);
52 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
55 struct cdbdata *cdb_euid;
58 syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", euid, qrbuf->QRname);
60 key_len = strlen(euid) + sizeof(long) + 1;
61 key = malloc(key_len);
62 memcpy(key, &qrbuf->QRnumber, sizeof(long));
63 strcpy(&key[sizeof(long)], euid);
65 cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
68 if (cdb_euid == NULL) {
72 // The first (sizeof long) of the record is what we're looking for. Throw away the rest.
73 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
76 syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", msgnum);
81 // Store the euid index for a message, which has presumably just been
82 // stored in this room by the caller.
83 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
89 syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", msgnum, euid, qrbuf->QRname);
91 key_len = strlen(euid) + sizeof(long) + 1;
92 key = malloc(key_len);
93 memcpy(key, &qrbuf->QRnumber, sizeof(long));
94 strcpy(&key[sizeof(long)], euid);
96 data_len = sizeof(long) + key_len;
97 data = malloc(data_len);
99 memcpy(data, &msgnum, sizeof(long));
100 memcpy(&data[sizeof(long)], key, key_len);
102 cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
108 // Called by rebuild_euid_index_for_room() to index one message.
109 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
110 struct CtdlMessage *msg = NULL;
112 msg = CtdlFetchMessage(msgnum, 0);
113 if (msg == NULL) return;
114 if (!CM_IsEmpty(msg, eExclusiveID)) {
115 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
121 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
122 static struct RoomProcList *rplist = NULL;
123 struct RoomProcList *ptr;
126 // Lazy programming here. Call this function as a CtdlForEachRoom backend
127 // in order to queue up the room names, or call it with a null room
128 // to make it do the processing.
130 ptr = (struct RoomProcList *)
131 malloc(sizeof (struct RoomProcList));
132 if (ptr == NULL) return;
134 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
140 while (rplist != NULL) {
141 if (CtdlGetRoom(&qr, rplist->name) == 0) {
142 if (DoesThisRoomNeedEuidIndexing(&qr)) {
144 "euidindex: rebuilding EUID index for <%s>",
146 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
147 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
151 rplist = rplist->next;
157 // Globally rebuild the EUID indices in every room.
158 void rebuild_euid_index(void) {
159 cdb_trunc(CDB_EUIDINDEX); // delete the old indices
160 CtdlForEachRoom(rebuild_euid_index_for_room, NULL); // enumerate room names
161 rebuild_euid_index_for_room(NULL, NULL); // and index them
165 // Server command to fetch a message number given an euid.
166 void cmd_euid(char *cmdbuf) {
169 long *msglist = NULL;
173 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
175 extract_token(euid, cmdbuf, 0, '|', sizeof euid);
176 msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
178 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
182 num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
184 for (i = 0; i < num_msgs; ++i) {
185 if (msglist[i] == msgnum) {
187 cprintf("%d %ld\n", CIT_OK, msgnum);
194 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
198 char *ctdl_module_init_euidindex(void) {
200 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
202 // return our id for the log