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
22 #include "ctdl_module.h"
25 #include "serv_autocompletion.h"
30 * Convert a structured name into a friendly name. Caller must free the
33 char *n_to_fn(char *value) {
37 nnn = malloc(strlen(value) + 10);
39 extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
41 extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
43 extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
45 extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
47 extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
49 for (i=0; i<strlen(nnn); ++i) {
50 if (!strncmp(&nnn[i], " ", 2)) strcpy(&nnn[i], &nnn[i+1]);
60 * Back end for cmd_auto()
62 void hunt_for_autocomplete(long msgnum, char *search_string) {
63 struct CtdlMessage *msg;
70 msg = CtdlFetchMessage(msgnum, 1);
71 if (msg == NULL) return;
73 v = vcard_load(msg->cm_fields['M']);
77 * Try to match from a friendly name (the "fn" field). If there is
78 * a match, return the entry in the form of:
79 * Display Name <user@domain.org>
81 value = vcard_get_prop(v, "fn", 0, 0, 0);
82 if (value != NULL) if (bmstrcasestr(value, search_string)) {
83 value2 = vcard_get_prop(v, "email", 1, 0, 0);
84 if (value2 == NULL) value2 = "";
85 cprintf("%s <%s>\n", value, value2);
91 * Try to match from a structured name (the "n" field). If there is
92 * a match, return the entry in the form of:
93 * Display Name <user@domain.org>
95 value = vcard_get_prop(v, "n", 0, 0, 0);
96 if (value != NULL) if (bmstrcasestr(value, search_string)) {
98 value2 = vcard_get_prop(v, "email", 1, 0, 0);
99 if (value2 == NULL) value2 = "";
100 nnn = n_to_fn(value);
101 cprintf("%s <%s>\n", nnn, value2);
108 * Try a partial match on all listed email addresses.
111 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
112 if (bmstrcasestr(value, search_string)) {
113 if (vcard_get_prop(v, "fn", 0, 0, 0)) {
114 cprintf("%s <%s>\n", vcard_get_prop(v, "fn", 0, 0, 0), value);
116 else if (vcard_get_prop(v, "n", 0, 0, 0)) {
117 nnn = n_to_fn(vcard_get_prop(v, "n", 0, 0, 0));
118 cprintf("%s <%s>\n", nnn, value);
123 cprintf("%s\n", value);
136 * Attempt to autocomplete an address based on a partial...
138 void cmd_auto(char *argbuf) {
139 char hold_rm[ROOMNAMELEN];
140 char search_string[256];
141 long *msglist = NULL;
143 long *fts_msgs = NULL;
144 int fts_num_msgs = 0;
145 struct cdbdata *cdbfr;
149 int search_match = 0;
150 char *rooms_to_try[] = { USERCONTACTSROOM, ADDRESS_BOOK_ROOM };
152 if (CtdlAccessCheck(ac_logged_in)) return;
153 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
154 if (IsEmptyStr(search_string)) {
155 cprintf("%d You supplied an empty partial.\n",
156 ERROR + ILLEGAL_VALUE);
160 strcpy(hold_rm, CC->room.QRname); /* save current room */
161 cprintf("%d try these:\n", LISTING_FOLLOWS);
164 * Gather up message pointers in rooms containing vCards
166 for (r=0; r < (sizeof(rooms_to_try) / sizeof(char *)); ++r) {
167 if (CtdlGetRoom(&CC->room, rooms_to_try[r]) == 0) {
168 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
170 msglist = realloc(msglist, (num_msgs * sizeof(long)) + cdbfr->len + 1);
171 memcpy(&msglist[num_msgs], cdbfr->ptr, cdbfr->len);
172 num_msgs += (cdbfr->len / sizeof(long));
179 * Search-reduce the results if we have the full text index available
181 if (config.c_enable_fulltext) {
182 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, search_string, "fulltext");
184 for (i=0; i<num_msgs; ++i) {
186 for (j=0; j<fts_num_msgs; ++j) {
187 if (msglist[i] == fts_msgs[j]) {
189 j = fts_num_msgs + 1; /* end the search */
193 msglist[i] = 0; /* invalidate this result */
199 /* If no results, invalidate the whole list */
207 * Now output the ones that look interesting
209 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
210 if (msglist[i] != 0) {
211 hunt_for_autocomplete(msglist[i], search_string);
216 if (strcmp(CC->room.QRname, hold_rm)) {
217 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
227 CTDL_MODULE_INIT(autocompletion) {
230 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");
232 /* return our Subversion id for the Log */