Removed the logging facility from citserver, use syslog instead
[citadel.git] / citadel / euidindex.c
1 /*
2  * Index messages by EUID per room.
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
13 # include <time.h>
14 #else
15 # if HAVE_SYS_TIME_H
16 #  include <sys/time.h>
17 # else
18 #  include <time.h>
19 # endif
20 #endif
21
22
23 #include <ctype.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <sys/stat.h>
29 #include <libcitadel.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include "database.h"
33 #include "msgbase.h"
34 #include "support.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "room_ops.h"
38 #include "user_ops.h"
39 #include "file_ops.h"
40 #include "config.h"
41 #include "control.h"
42 #include "euidindex.h"
43
44 #include "ctdl_module.h"
45
46 /*
47  * The structure of an euidindex record *key* is:
48  *
49  * |----room_number----|----------EUID-------------|
50  *    (sizeof long)       (actual length of euid)
51  *
52  *
53  * The structure of an euidindex record *value* is:
54  *
55  * |-----msg_number----|----room_number----|----------EUID-------------|
56  *    (sizeof long)       (sizeof long)       (actual length of euid)
57  *
58  */
59
60
61
62 /*
63  * Return nonzero if the supplied room is one which should have
64  * an EUID index.
65  */
66 int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
67
68         switch(qrbuf->QRdefaultview) {
69                 case VIEW_BBS:          return(0);
70                 case VIEW_MAILBOX:      return(0);
71                 case VIEW_ADDRESSBOOK:  return(1);
72                 case VIEW_DRAFTS:       return(0);
73                 case VIEW_CALENDAR:     return(1);
74                 case VIEW_TASKS:        return(1);
75                 case VIEW_NOTES:        return(1);
76                 case VIEW_WIKI:         return(1);
77         }
78         
79         return(0);
80 }
81
82
83
84
85
86
87 /*
88  * Locate a message in a given room with a given euid, and return
89  * its message number.
90  */
91 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
92         return CtdlLocateMessageByEuid (euid, qrbuf);
93 }
94
95 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
96         char *key;
97         int key_len;
98         struct cdbdata *cdb_euid;
99         long msgnum = (-1L);
100
101         syslog(LOG_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
102
103         key_len = strlen(euid) + sizeof(long) + 1;
104         key = malloc(key_len);
105         memcpy(key, &qrbuf->QRnumber, sizeof(long));
106         strcpy(&key[sizeof(long)], euid);
107
108         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
109         free(key);
110
111         if (cdb_euid == NULL) {
112                 msgnum = (-1L);
113         }
114         else {
115                 /* The first (sizeof long) of the record is what we're
116                  * looking for.  Throw away the rest.
117                  */
118                 memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
119                 cdb_free(cdb_euid);
120         }
121         syslog(LOG_DEBUG, "returning msgnum = %ld\n", msgnum);
122         return(msgnum);
123 }
124
125
126 /*
127  * Store the euid index for a message, which has presumably just been
128  * stored in this room by the caller.
129  */
130 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
131         char *key;
132         int key_len;
133         char *data;
134         int data_len;
135
136         syslog(LOG_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
137
138         key_len = strlen(euid) + sizeof(long) + 1;
139         key = malloc(key_len);
140         memcpy(key, &qrbuf->QRnumber, sizeof(long));
141         strcpy(&key[sizeof(long)], euid);
142
143         data_len = sizeof(long) + key_len;
144         data = malloc(data_len);
145
146         memcpy(data, &msgnum, sizeof(long));
147         memcpy(&data[sizeof(long)], key, key_len);
148
149         cdb_store(CDB_EUIDINDEX, key, key_len, data, data_len);
150         free(key);
151         free(data);
152 }
153
154
155
156 /*
157  * Called by rebuild_euid_index_for_room() to index one message.
158  */
159 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
160         struct CtdlMessage *msg = NULL;
161
162         msg = CtdlFetchMessage(msgnum, 0);
163         if (msg == NULL) return;
164         if (msg->cm_fields['E'] != NULL) {
165                 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
166         }
167         CtdlFreeMessage(msg);
168 }
169
170
171 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
172         static struct RoomProcList *rplist = NULL;
173         struct RoomProcList *ptr;
174         struct ctdlroom qr;
175
176         /* Lazy programming here.  Call this function as a CtdlForEachRoom backend
177          * in order to queue up the room names, or call it with a null room
178          * to make it do the processing.
179          */
180         if (qrbuf != NULL) {
181                 ptr = (struct RoomProcList *)
182                         malloc(sizeof (struct RoomProcList));
183                 if (ptr == NULL) return;
184
185                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
186                 ptr->next = rplist;
187                 rplist = ptr;
188                 return;
189         }
190
191         while (rplist != NULL) {
192                 if (CtdlGetRoom(&qr, rplist->name) == 0) {
193                         if (DoesThisRoomNeedEuidIndexing(&qr)) {
194                                 syslog(LOG_DEBUG,
195                                         "Rebuilding EUID index for <%s>\n",
196                                         rplist->name);
197                                 CtdlUserGoto(rplist->name, 0, 0, NULL, NULL);
198                                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
199                                         rebuild_euid_index_for_msg, NULL);
200                         }
201                 }
202                 ptr = rplist;
203                 rplist = rplist->next;
204                 free(ptr);
205         }
206 }
207
208
209 /*
210  * Globally rebuild the EUID indices in every room.
211  */
212 void rebuild_euid_index(void) {
213         cdb_trunc(CDB_EUIDINDEX);               /* delete the old indices */
214         CtdlForEachRoom(rebuild_euid_index_for_room, NULL);     /* enumerate rm names */
215         rebuild_euid_index_for_room(NULL, NULL);        /* and index them */
216 }
217
218
219
220 /*
221  * Server command to fetch a message number given an euid.
222  */
223 void cmd_euid(char *cmdbuf) {
224         char euid[256];
225         long msgnum;
226         struct cdbdata *cdbfr;
227         long *msglist = NULL;
228         int num_msgs = 0;
229         int i;
230
231         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
232
233         extract_token(euid, cmdbuf, 0, '|', sizeof euid);
234         msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
235         if (msgnum <= 0L) {
236                 cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
237                 return;
238         }
239
240         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
241         if (cdbfr != NULL) {
242                 num_msgs = cdbfr->len / sizeof(long);
243                 msglist = (long *) cdbfr->ptr;
244                 for (i = 0; i < num_msgs; ++i) {
245                         if (msglist[i] == msgnum) {
246                                 cdb_free(cdbfr);
247                                 cprintf("%d %ld\n", CIT_OK, msgnum);
248                                 return;
249                         }
250                 }
251                 cdb_free(cdbfr);
252         }
253
254         cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
255 }
256
257 CTDL_MODULE_INIT(euidindex)
258 {
259         if (!threading) {
260                 CtdlRegisterProtoHook(cmd_euid, "EUID", "Perform operations on Extended IDs for messages");
261         }
262         /* return our Subversion id for the Log */
263         return "euidindex";
264 }