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