*** empty log message ***
[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         return(0);
51 }
52
53 void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
54         char *key;
55         int key_len;
56
57         lprintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
58
59         key_len = strlen(euid) + sizeof(long) + 1;
60         key = malloc(key_len);
61         memcpy(key, &qrbuf->QRnumber, sizeof(long));
62         strcpy(&key[sizeof(long)], euid);
63
64         cdb_store(CDB_EUIDINDEX, key, key_len, &msgnum, sizeof(long));
65 }
66
67
68
69 /*
70  * Called by rebuild_euid_index_for_room() to index one message.
71  */
72 void rebuild_euid_index_for_msg(long msgnum, void *userdata) {
73         struct CtdlMessage *msg;
74
75         msg = CtdlFetchMessage(msgnum, 0);
76         if (msg == NULL) return;
77         if (msg->cm_fields['E'] != NULL) {
78                 index_message_by_euid(msg->cm_fields['E'], &CC->room, msgnum);
79         }
80         CtdlFreeMessage(msg);
81 }
82
83
84 void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
85         static struct RoomProcList *rplist = NULL;
86         struct RoomProcList *ptr;
87         struct ctdlroom qr;
88
89         /* Lazy programming here.  Call this function as a ForEachRoom backend
90          * in order to queue up the room names, or call it with a null room
91          * to make it do the processing.
92          */
93         if (qrbuf != NULL) {
94                 ptr = (struct RoomProcList *)
95                         malloc(sizeof (struct RoomProcList));
96                 if (ptr == NULL) return;
97
98                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
99                 ptr->next = rplist;
100                 rplist = ptr;
101                 return;
102         }
103
104         while (rplist != NULL) {
105                 if (getroom(&qr, rplist->name) == 0) {
106                         lprintf(CTDL_DEBUG, "Rebuilding EUID index for <%s>\n", rplist->name);
107                         usergoto(rplist->name, 0, 0, NULL, NULL);
108                         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, rebuild_euid_index_for_msg, NULL);
109                 }
110                 ptr = rplist;
111                 rplist = rplist->next;
112                 free(ptr);
113         }
114 }
115
116
117 /*
118  * Globally rebuild the EUID indices in every room.
119  */
120 void rebuild_euid_index(void) {
121         cdb_trunc(CDB_EUIDINDEX);
122
123         ForEachRoom(rebuild_euid_index_for_room, NULL);
124         rebuild_euid_index_for_room(NULL, NULL);
125 }