Move vcard.c into libcitadel.
[citadel.git] / citadel / modules / autocompletion / serv_autocompletion.c
1 /*
2  * $Id$
3  *
4  * Autocompletion of email recipients, etc.
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "msgbase.h"
39 #include "user_ops.h"
40 #include "room_ops.h"
41 #include "database.h"
42 #include "serv_autocompletion.h"
43
44 #include "ctdl_module.h"
45
46
47 #ifndef HAVE_SNPRINTF
48 #include "snprintf.h"
49 #endif
50
51
52
53 /*
54  * Convert a structured name into a friendly name.  Caller must free the
55  * returned pointer.
56  */
57 char *n_to_fn(char *value) {
58         char *nnn = NULL;
59         int i;
60
61         nnn = malloc(strlen(value) + 10);
62         strcpy(nnn, "");
63         extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
64         strcat(nnn, " ");
65         extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
66         strcat(nnn, " ");
67         extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
68         strcat(nnn, " ");
69         extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
70         strcat(nnn, " ");
71         extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
72         strcat(nnn, " ");
73         for (i=0; i<strlen(nnn); ++i) {
74                 if (!strncmp(&nnn[i], "  ", 2)) strcpy(&nnn[i], &nnn[i+1]);
75         }
76         striplt(nnn);
77         return(nnn);
78 }
79
80
81
82
83 /*
84  * Back end for cmd_auto()
85  */
86 void hunt_for_autocomplete(long msgnum, char *search_string) {
87         struct CtdlMessage *msg;
88         struct vCard *v;
89         char *value = NULL;
90         char *value2 = NULL;
91         int i = 0;
92         char *nnn = NULL;
93
94         msg = CtdlFetchMessage(msgnum, 1);
95         if (msg == NULL) return;
96
97         v = vcard_load(msg->cm_fields['M']);
98         CtdlFreeMessage(msg);
99
100         /*
101          * Try to match from a friendly name (the "fn" field).  If there is
102          * a match, return the entry in the form of:
103          *     Display Name <user@domain.org>
104          */
105         value = vcard_get_prop(v, "fn", 0, 0, 0);
106         if (value != NULL) if (bmstrcasestr(value, search_string)) {
107                 value2 = vcard_get_prop(v, "email", 1, 0, 0);
108                 if (value2 == NULL) value2 = "";
109                 cprintf("%s <%s>\n", value, value2);
110                 vcard_free(v);
111                 return;
112         }
113
114         /*
115          * Try to match from a structured name (the "n" field).  If there is
116          * a match, return the entry in the form of:
117          *     Display Name <user@domain.org>
118          */
119         value = vcard_get_prop(v, "n", 0, 0, 0);
120         if (value != NULL) if (bmstrcasestr(value, search_string)) {
121
122                 value2 = vcard_get_prop(v, "email", 1, 0, 0);
123                 if (value2 == NULL) value2 = "";
124                 nnn = n_to_fn(value);
125                 cprintf("%s <%s>\n", nnn, value2);
126                 free(nnn);
127                 vcard_free(v);
128                 return;
129         }
130
131         /*
132          * Try a partial match on all listed email addresses.
133          */
134         i = 0;
135         while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
136                 if (bmstrcasestr(value, search_string)) {
137                         if (vcard_get_prop(v, "fn", 0, 0, 0)) {
138                                 cprintf("%s <%s>\n", vcard_get_prop(v, "fn", 0, 0, 0), value);
139                         }
140                         else if (vcard_get_prop(v, "n", 0, 0, 0)) {
141                                 nnn = n_to_fn(vcard_get_prop(v, "n", 0, 0, 0));
142                                 cprintf("%s <%s>\n", nnn, value);
143                                 free(nnn);
144                         
145                         }
146                         else {
147                                 cprintf("%s\n", value);
148                         }
149                         vcard_free(v);
150                         return;
151                 }
152         }
153
154         vcard_free(v);
155 }
156
157
158
159 /*
160  * Attempt to autocomplete an address based on a partial...
161  */
162 void cmd_auto(char *argbuf) {
163         char hold_rm[ROOMNAMELEN];
164         char search_string[256];
165         long *msglist = NULL;
166         int num_msgs = 0;
167         long *fts_msgs = NULL;
168         int fts_num_msgs = 0;
169         struct cdbdata *cdbfr;
170         int r = 0;
171         int i = 0;
172         int j = 0;
173         int search_match = 0;
174         char *rooms_to_try[] = { USERCONTACTSROOM, ADDRESS_BOOK_ROOM };
175                 
176         if (CtdlAccessCheck(ac_logged_in)) return;
177         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
178         if (IsEmptyStr(search_string)) {
179                 cprintf("%d You supplied an empty partial.\n",
180                         ERROR + ILLEGAL_VALUE);
181                 return;
182         }
183
184         strcpy(hold_rm, CC->room.QRname);       /* save current room */
185         cprintf("%d try these:\n", LISTING_FOLLOWS);
186
187         /*
188          * Gather up message pointers in rooms containing vCards
189          */
190         for (r=0; r < (sizeof(rooms_to_try) / sizeof(char *)); ++r) {
191                 if (getroom(&CC->room, rooms_to_try[r]) == 0) {
192                         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
193                         if (cdbfr != NULL) {
194                                 msglist = realloc(msglist, (num_msgs * sizeof(long)) + cdbfr->len + 1);
195                                 memcpy(&msglist[num_msgs], cdbfr->ptr, cdbfr->len);
196                                 num_msgs += (cdbfr->len / sizeof(long));
197                                 cdb_free(cdbfr);
198                         }
199                 }
200         }
201
202         /*
203          * Search-reduce the results if we have the full text index available
204          */
205         if (config.c_enable_fulltext) {
206                 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, search_string, "fulltext");
207                 if (fts_msgs) {
208                         for (i=0; i<num_msgs; ++i) {
209                                 search_match = 0;
210                                 for (j=0; j<fts_num_msgs; ++j) {
211                                         if (msglist[i] == fts_msgs[j]) {
212                                                 search_match = 1;
213                                                 j = fts_num_msgs + 1;   /* end the search */
214                                         }
215                                 }
216                                 if (!search_match) {
217                                         msglist[i] = 0;         /* invalidate this result */
218                                 }
219                         }
220                         free(fts_msgs);
221                 }
222                 else {
223                         /* If no results, invalidate the whole list */
224                         free(msglist);
225                         msglist = NULL;
226                         num_msgs = 0;
227                 }
228         }
229
230         /*
231          * Now output the ones that look interesting
232          */
233         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
234                 if (msglist[i] != 0) {
235                         hunt_for_autocomplete(msglist[i], search_string);
236                 }
237         }
238         
239         cprintf("000\n");
240         if (strcmp(CC->room.QRname, hold_rm)) {
241                 getroom(&CC->room, hold_rm);    /* return to saved room */
242         }
243
244         if (msglist) {
245                 free(msglist);
246         }
247         
248 }
249
250
251 CTDL_MODULE_INIT(autocompletion) {
252         CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");
253
254         /* return our Subversion id for the Log */
255         return "$Id$";
256 }