4 * Autocompletion of email recipients, etc.
6 * Copyright (c) 1987-2009 by the citadel.org team
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.
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.
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
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
48 #include <libcitadel.h>
51 #include "serv_autocompletion.h"
53 #include "ctdl_module.h"
63 * Convert a structured name into a friendly name. Caller must free the
66 char *n_to_fn(char *value) {
70 nnn = malloc(strlen(value) + 10);
72 extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
74 extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
76 extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
78 extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
80 extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
82 for (i=0; i<strlen(nnn); ++i) {
83 if (!strncmp(&nnn[i], " ", 2)) strcpy(&nnn[i], &nnn[i+1]);
93 * Back end for cmd_auto()
95 void hunt_for_autocomplete(long msgnum, char *search_string) {
96 struct CtdlMessage *msg;
103 msg = CtdlFetchMessage(msgnum, 1);
104 if (msg == NULL) return;
106 v = vcard_load(msg->cm_fields['M']);
107 CtdlFreeMessage(msg);
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>
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);
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>
128 value = vcard_get_prop(v, "n", 0, 0, 0);
129 if (value != NULL) if (bmstrcasestr(value, search_string)) {
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);
141 * Try a partial match on all listed email addresses.
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);
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);
156 cprintf("%s\n", value);
169 * Attempt to autocomplete an address based on a partial...
171 void cmd_auto(char *argbuf) {
172 char hold_rm[ROOMNAMELEN];
173 char search_string[256];
174 long *msglist = NULL;
176 long *fts_msgs = NULL;
177 int fts_num_msgs = 0;
178 struct cdbdata *cdbfr;
182 int search_match = 0;
183 char *rooms_to_try[] = { USERCONTACTSROOM, ADDRESS_BOOK_ROOM };
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);
193 strcpy(hold_rm, CC->room.QRname); /* save current room */
194 cprintf("%d try these:\n", LISTING_FOLLOWS);
197 * Gather up message pointers in rooms containing vCards
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));
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));
212 * Search-reduce the results if we have the full text index available
214 if (config.c_enable_fulltext) {
215 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, search_string, "fulltext");
217 for (i=0; i<num_msgs; ++i) {
219 for (j=0; j<fts_num_msgs; ++j) {
220 if (msglist[i] == fts_msgs[j]) {
222 j = fts_num_msgs + 1; /* end the search */
226 msglist[i] = 0; /* invalidate this result */
232 /* If no results, invalidate the whole list */
240 * Now output the ones that look interesting
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);
249 if (strcmp(CC->room.QRname, hold_rm)) {
250 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
260 CTDL_MODULE_INIT(autocompletion) {
263 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");
265 /* return our Subversion id for the Log */