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 "citserver.h"
58 #include "serv_autocompletion.h"
60 #include "ctdl_module.h"
70 * Convert a structured name into a friendly name. Caller must free the
73 char *n_to_fn(char *value) {
77 nnn = malloc(strlen(value) + 10);
79 extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
81 extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
83 extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
85 extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
87 extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
89 for (i=0; i<strlen(nnn); ++i) {
90 if (!strncmp(&nnn[i], " ", 2)) strcpy(&nnn[i], &nnn[i+1]);
100 * Back end for cmd_auto()
102 void hunt_for_autocomplete(long msgnum, char *search_string) {
103 struct CtdlMessage *msg;
110 msg = CtdlFetchMessage(msgnum, 1);
111 if (msg == NULL) return;
113 v = vcard_load(msg->cm_fields['M']);
114 CtdlFreeMessage(msg);
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>
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);
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>
135 value = vcard_get_prop(v, "n", 0, 0, 0);
136 if (value != NULL) if (bmstrcasestr(value, search_string)) {
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);
148 * Try a partial match on all listed email addresses.
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);
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);
163 cprintf("%s\n", value);
176 * Attempt to autocomplete an address based on a partial...
178 void cmd_auto(char *argbuf) {
179 char hold_rm[ROOMNAMELEN];
180 char search_string[256];
181 long *msglist = NULL;
183 long *fts_msgs = NULL;
184 int fts_num_msgs = 0;
185 struct cdbdata *cdbfr;
189 int search_match = 0;
190 char *rooms_to_try[] = { USERCONTACTSROOM, ADDRESS_BOOK_ROOM };
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);
200 strcpy(hold_rm, CC->room.QRname); /* save current room */
201 cprintf("%d try these:\n", LISTING_FOLLOWS);
204 * Gather up message pointers in rooms containing vCards
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));
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));
219 * Search-reduce the results if we have the full text index available
221 if (config.c_enable_fulltext) {
222 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, search_string, "fulltext");
224 for (i=0; i<num_msgs; ++i) {
226 for (j=0; j<fts_num_msgs; ++j) {
227 if (msglist[i] == fts_msgs[j]) {
229 j = fts_num_msgs + 1; /* end the search */
233 msglist[i] = 0; /* invalidate this result */
239 /* If no results, invalidate the whole list */
247 * Now output the ones that look interesting
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);
256 if (strcmp(CC->room.QRname, hold_rm)) {
257 getroom(&CC->room, hold_rm); /* return to saved room */
267 CTDL_MODULE_INIT(autocompletion) {
270 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");
272 /* return our Subversion id for the Log */