* Restructured cmd_auto() to be able to search vCards in more than one room.
[citadel.git] / citadel / serv_autocompletion.c
1 /*
2  * $Id$
3  *
4  * Autocompletion of email recipients, etc.
5  */
6
7 #ifdef DLL_EXPORT
8 #define IN_LIBCIT
9 #endif
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "tools.h"
44 #include "msgbase.h"
45 #include "user_ops.h"
46 #include "room_ops.h"
47 #include "database.h"
48 #include "vcard.h"
49 #include "serv_autocompletion.h"
50
51
52 #ifndef HAVE_SNPRINTF
53 #include "snprintf.h"
54 #endif
55
56
57
58 /*
59  * Convert a structured name into a friendly name.  Caller must free the
60  * returned pointer.
61  */
62 char *n_to_fn(char *value) {
63         char *nnn = NULL;
64         int i;
65
66         nnn = malloc(strlen(value) + 10);
67         strcpy(nnn, "");
68         extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);  strcat(nnn, " ");
69         extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);  strcat(nnn, " ");
70         extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);  strcat(nnn, " ");
71         extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);  strcat(nnn, " ");
72         extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);  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, void *data) {
87         char *search_string;
88         struct CtdlMessage *msg;
89         struct vCard *v;
90         char *value = NULL;
91         char *value2 = NULL;
92         int i = 0;
93         char *nnn = NULL;
94
95         search_string = (char *) data;
96
97         msg = CtdlFetchMessage(msgnum, 1);
98         if (msg == NULL) return;
99
100         v = vcard_load(msg->cm_fields['M']);
101         CtdlFreeMessage(msg);
102
103         /*
104          * Try to match from a friendly name (the "fn" field).  If there is a
105          * match, return the entry in the form of:  Display Name <user@domain.org>
106          */
107         value = vcard_get_prop(v, "fn", 0, 0, 0);
108         if (value != NULL) if (bmstrcasestr(value, search_string)) {
109                 value2 = vcard_get_prop(v, "email", 1, 0, 0);
110                 if (value2 == NULL) value2 = "";
111                 cprintf("%s <%s>\n", value, value2);
112                 vcard_free(v);
113                 return;
114         }
115
116         /*
117          * Try to match from a structured name (the "n" field).  If there is a
118          * match, return the entry in the form of:  Display Name <user@domain.org>
119          */
120         value = vcard_get_prop(v, "n", 0, 0, 0);
121         if (value != NULL) if (bmstrcasestr(value, search_string)) {
122
123                 value2 = vcard_get_prop(v, "email", 1, 0, 0);
124                 if (value2 == NULL) value2 = "";
125                 nnn = n_to_fn(value);
126                 cprintf("%s <%s>\n", nnn, value2);
127                 free(nnn);
128                 vcard_free(v);
129                 return;
130         }
131
132         /*
133          * Try a partial match on all listed email addresses.
134          */
135         i = 0;
136         while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
137                 if (bmstrcasestr(value, search_string)) {
138                         if (vcard_get_prop(v, "fn", 0, 0, 0)) {
139                                 cprintf("%s <%s>\n", vcard_get_prop(v, "fn", 0, 0, 0), value);
140                         }
141                         else if (vcard_get_prop(v, "n", 0, 0, 0)) {
142                                 nnn = n_to_fn(vcard_get_prop(v, "n", 0, 0, 0));
143                                 cprintf("%s <%s>\n", nnn, value);
144                                 free(nnn);
145                         
146                         }
147                         else {
148                                 cprintf("%s\n", value);
149                         }
150                         vcard_free(v);
151                         return;
152                 }
153         }
154
155         vcard_free(v);
156 }
157
158
159
160 /*
161  * Attempt to autocomplete an address based on a partial...
162  */
163 void cmd_auto(char *argbuf) {
164         char hold_rm[ROOMNAMELEN];
165         char search_string[256];
166
167         if (CtdlAccessCheck(ac_logged_in)) return;
168         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
169         if (strlen(search_string) == 0) {
170                 cprintf("%d You supplied an empty partial.\n", ERROR + ILLEGAL_VALUE);
171                 return;
172         }
173
174         strcpy(hold_rm, CC->room.QRname);       /* save current room */
175         cprintf("%d try these:\n", LISTING_FOLLOWS);
176
177         /* Take a spin through the user's personal address book */
178         if (getroom(&CC->room, USERCONTACTSROOM) == 0) {
179                 CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard", NULL,
180                                         hunt_for_autocomplete, search_string);
181         }
182         
183         /* FIXME try the global address book */
184         if (getroom(&CC->room, ADDRESS_BOOK_ROOM) == 0) {
185                 CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard", NULL,
186                                         hunt_for_autocomplete, search_string);
187         }
188         
189         cprintf("000\n");
190         if (strcmp(&CC->room.QRname, hold_rm)) {
191                 getroom(&CC->room, hold_rm);    /* return to saved room */
192         }
193 }
194
195
196 char *serv_autocompletion_init(void)
197 {
198         CtdlRegisterProtoHook(cmd_auto, "AUTO", "Perform recipient autocompletion");
199         return "$Id$";
200 }