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