]> code.citadel.org Git - citadel.git/blob - citadel/server/euidindex.c
Can you tell I'm REALLY avoiding another task right now?
[citadel.git] / citadel / server / euidindex.c
1 // Index messages by EUID per room.
2 //
3 // Copyright (c) 1987-2023 by the citadel.org team
4 //
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.
7
8 #include "sysdep.h"
9 #include <stdio.h>
10 #include <libcitadel.h>
11 #include "citserver.h"
12 #include "room_ops.h"
13
14 // The structure of an euidindex record *key* is:
15 //
16 // |----room_number----|----------EUID-------------|
17 //    (sizeof long)       (actual length of euid)
18 //
19 //
20 // The structure of an euidindex record *value* is:
21 //
22 // |-----msg_number----|----room_number----|----------EUID-------------|
23 //    (sizeof long)       (sizeof long)       (actual length of euid)
24
25 // Return nonzero if the supplied room is one which should have
26 // an EUID index.
27 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
28
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);
39         }
40         
41         return(0);
42 }
43
44
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);
49 }
50
51
52 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
53         char *key;
54         int key_len;
55         struct cdbdata cdb_euid;
56         long msgnum = (-1L);
57
58         syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", euid, qrbuf->QRname);
59
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);
64
65         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
66         free(key);
67
68         if (cdb_euid.ptr == NULL) {
69                 msgnum = (-1L);
70         }
71         else {
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));
74         }
75         syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", msgnum);
76         return(msgnum);
77 }
78
79
80 // Store the euid index for a message, which has presumably just been
81 // stored in this room by the caller.
82 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
83         char *key;
84         int key_len;
85         char *data;
86         int data_len;
87
88         syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", msgnum, euid, qrbuf->QRname);
89
90         key_len = strlen(euid) + sizeof(long) + 1;
91         key = malloc(key_len);
92         memcpy(key, &qrbuf->QRnumber, sizeof(long));
93         strcpy(&key[sizeof(long)], euid);
94
95         data_len = sizeof(long) + key_len;
96         data = malloc(data_len);
97
98         memcpy(data, &msgnum, sizeof(long));
99         memcpy(&data[sizeof(long)], key, key_len);
100
101         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
102         free(key);
103         free(data);
104 }
105
106
107 // Called by rebuild_euid_index_for_room() to index one message.
108 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
109         struct CtdlMessage *msg = NULL;
110
111         msg = CtdlFetchMessage(msgnum, 0);
112         if (msg == NULL) return;
113         if (!CM_IsEmpty(msg, eExclusiveID)) {
114                 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgnum);
115         }
116         CM_Free(msg);
117 }
118
119
120 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
121         static struct RoomProcList *rplist = NULL;
122         struct RoomProcList *ptr;
123         struct ctdlroom qr;
124
125         // Lazy programming here.  Call this function as a CtdlForEachRoom backend
126         // in order to queue up the room names, or call it with a null room
127         // to make it do the processing.
128         if (qrbuf != NULL) {
129                 ptr = (struct RoomProcList *)
130                         malloc(sizeof (struct RoomProcList));
131                 if (ptr == NULL) return;
132
133                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
134                 ptr->next = rplist;
135                 rplist = ptr;
136                 return;
137         }
138
139         while (rplist != NULL) {
140                 if (CtdlGetRoom(&qr, rplist->name) == 0) {
141                         if (DoesThisRoomNeedEuidIndexing(&qr)) {
142                                 syslog(LOG_DEBUG,
143                                         "euidindex: rebuilding EUID index for <%s>",
144                                         rplist->name);
145                                 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
146                                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
147                         }
148                 }
149                 ptr = rplist;
150                 rplist = rplist->next;
151                 free(ptr);
152         }
153 }
154
155
156 // Globally rebuild the EUID indices in every room.
157 void rebuild_euid_index(void) {
158         cdb_trunc(CDB_EUIDINDEX);                               // delete the old indices
159         CtdlForEachRoom(rebuild_euid_index_for_room, NULL);     // enumerate room names
160         rebuild_euid_index_for_room(NULL, NULL);                // and index them
161 }
162
163
164 // Server command to fetch a message number given an euid.
165 void cmd_euid(char *cmdbuf) {
166         char euid[256];
167         long msgnum;
168         long *msglist = NULL;
169         int num_msgs = 0;
170         int i;
171
172         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
173
174         extract_token(euid, cmdbuf, 0, '|', sizeof euid);
175         msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
176         if (msgnum <= 0L) {
177                 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
178                 return;
179         }
180
181         num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
182         if (num_msgs > 0) {
183                 for (i = 0; i < num_msgs; ++i) {
184                         if (msglist[i] == msgnum) {
185                                 free(msglist);
186                                 cprintf("%d %ld\n", CIT_OK, msgnum);
187                                 return;
188                         }
189                 }
190         }
191         free(msglist);
192
193         cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
194 }
195
196
197 char *ctdl_module_init_euidindex(void) {
198         if (!threading) {
199                 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
200         }
201         // return our id for the log
202         return "euidindex";
203 }