removed some debugs
[citadel.git] / citadel / 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; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 3.
7 //
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.
12
13 #include "sysdep.h"
14 #include <stdio.h>
15 #include <libcitadel.h>
16
17 #include "citserver.h"
18 #include "room_ops.h"
19
20 // The structure of an euidindex record *key* is:
21 //
22 // |----room_number----|----------EUID-------------|
23 //    (sizeof long)       (actual length of euid)
24 //
25 //
26 // The structure of an euidindex record *value* is:
27 //
28 // |-----msg_number----|----room_number----|----------EUID-------------|
29 //    (sizeof long)       (sizeof long)       (actual length of euid)
30
31 // Return nonzero if the supplied room is one which should have
32 // an EUID index.
33 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
34
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);
45         }
46         
47         return(0);
48 }
49
50
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);
55 }
56
57
58 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
59         char *key;
60         int key_len;
61         struct cdbdata *cdb_euid;
62         long msgnum = (-1L);
63
64         syslog(LOG_DEBUG, "euidindex: searching for EUID <%s> in <%s>", euid, qrbuf->QRname);
65
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);
70
71         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
72         free(key);
73
74         if (cdb_euid == NULL) {
75                 msgnum = (-1L);
76         }
77         else {
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));
80                 cdb_free(cdb_euid);
81         }
82         syslog(LOG_DEBUG, "euidindex: returning msgnum = %ld", msgnum);
83         return(msgnum);
84 }
85
86
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) {
90         char *key;
91         int key_len;
92         char *data;
93         int data_len;
94
95         syslog(LOG_DEBUG, "euidindex: indexing message #%ld <%s> in <%s>", msgnum, euid, qrbuf->QRname);
96
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);
101
102         data_len = sizeof(long) + key_len;
103         data = malloc(data_len);
104
105         memcpy(data, &msgnum, sizeof(long));
106         memcpy(&data[sizeof(long)], key, key_len);
107
108         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
109         free(key);
110         free(data);
111 }
112
113
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;
117
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);
122         }
123         CM_Free(msg);
124 }
125
126
127 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
128         static struct RoomProcList *rplist = NULL;
129         struct RoomProcList *ptr;
130         struct ctdlroom qr;
131
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.
135         if (qrbuf != NULL) {
136                 ptr = (struct RoomProcList *)
137                         malloc(sizeof (struct RoomProcList));
138                 if (ptr == NULL) return;
139
140                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
141                 ptr->next = rplist;
142                 rplist = ptr;
143                 return;
144         }
145
146         while (rplist != NULL) {
147                 if (CtdlGetRoom(&qr, rplist->name) == 0) {
148                         if (DoesThisRoomNeedEuidIndexing(&qr)) {
149                                 syslog(LOG_DEBUG,
150                                         "euidindex: rebuilding EUID index for <%s>",
151                                         rplist->name);
152                                 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL, NULL, NULL);
153                                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, rebuild_euid_index_for_msg, NULL);
154                         }
155                 }
156                 ptr = rplist;
157                 rplist = rplist->next;
158                 free(ptr);
159         }
160 }
161
162
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
168 }
169
170
171 // Server command to fetch a message number given an euid.
172 void cmd_euid(char *cmdbuf) {
173         char euid[256];
174         long msgnum;
175         struct cdbdata *cdbfr;
176         long *msglist = NULL;
177         int num_msgs = 0;
178         int i;
179
180         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
181
182         extract_token(euid, cmdbuf, 0, '|', sizeof euid);
183         msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
184         if (msgnum <= 0L) {
185                 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
186                 return;
187         }
188
189         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
190         if (cdbfr != NULL) {
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) {
195                                 cdb_free(cdbfr);
196                                 cprintf("%d %ld\n", CIT_OK, msgnum);
197                                 return;
198                         }
199                 }
200                 cdb_free(cdbfr);
201         }
202
203         cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
204 }
205
206
207 CTDL_MODULE_INIT(euidindex)
208 {
209         if (!threading) {
210                 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
211         }
212         /* return our Subversion id for the Log */
213         return "euidindex";
214 }