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