62bc53c1206d2ab25ce2b5165cf5a7e0c0fcf829
[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 "citadel.h"
33 #include "server.h"
34 #include "serv_extensions.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 "tools.h"
46 #include "euidindex.h"
47
48
49 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
50         char *key;
51         int key_len;
52         struct cdbdata *cdb_euid;
53         long msgnum = (-1L);
54
55         lprintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
56
57         key_len = strlen(euid) + sizeof(long) + 1;
58         key = malloc(key_len);
59         memcpy(key, &qrbuf->QRnumber, sizeof(long));
60         strcpy(&key[sizeof(long)], euid);
61
62         cdb_euid = cdb_fetch(CDB_EUIDINDEX, key, key_len);
63         free(key);
64
65         if (cdb_euid == NULL) {
66                 return(-1L);
67         }
68
69         if (cdb_euid->len == sizeof(long)) {
70                 msgnum = *(long *)cdb_euid->ptr;
71         }
72
73         cdb_free(cdb_euid);
74         lprintf(CTDL_DEBUG, "returning msgnum==%ld\n", msgnum);
75         return(msgnum);
76 }
77
78 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
79         char *key;
80         int key_len;
81
82         lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
83
84         key_len = strlen(euid) + sizeof(long) + 1;
85         key = malloc(key_len);
86         memcpy(key, &qrbuf->QRnumber, sizeof(long));
87         strcpy(&key[sizeof(long)], euid);
88
89         cdb_store(CDB_EUIDINDEX, key, key_len, &msgnum, sizeof(long));
90         free(key);
91 }
92
93
94
95 /*
96  * Called by rebuild_euid_index_for_room() to index one message.
97  */
98 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
99         struct CtdlMessage *msg;
100
101         msg = CtdlFetchMessage(msgnum, 0);
102         if (msg == NULL) return;
103         if (msg->cm_fields['E'] != NULL) {
104                 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
105         }
106         CtdlFreeMessage(msg);
107 }
108
109
110 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
111         static struct RoomProcList *rplist = NULL;
112         struct RoomProcList *ptr;
113         struct ctdlroom qr;
114
115         /* Lazy programming here.  Call this function as a ForEachRoom backend
116          * in order to queue up the room names, or call it with a null room
117          * to make it do the processing.
118          */
119         if (qrbuf != NULL) {
120                 ptr = (struct RoomProcList *)
121                         malloc(sizeof (struct RoomProcList));
122                 if (ptr == NULL) return;
123
124                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
125                 ptr->next = rplist;
126                 rplist = ptr;
127                 return;
128         }
129
130         while (rplist != NULL) {
131                 if (getroom(&qr, rplist->name) == 0) {
132                         lprintf(CTDL_DEBUG, "Rebuilding EUID index for <%s>\n", rplist->name);
133                         usergoto(rplist->name, 0, 0, NULL, NULL);
134                         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, rebuild_euid_index_for_msg, NULL);
135                 }
136                 ptr = rplist;
137                 rplist = rplist->next;
138                 free(ptr);
139         }
140 }
141
142
143 /*
144  * Globally rebuild the EUID indices in every room.
145  */
146 void rebuild_euid_index(void) {
147         cdb_trunc(CDB_EUIDINDEX);
148
149         ForEachRoom(rebuild_euid_index_for_room, NULL);
150         rebuild_euid_index_for_room(NULL, NULL);
151 }