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