4 * Autocompletion of email recipients, etc.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
32 #include <libcitadel.h>
35 #include "citserver.h"
42 #include "serv_autocompletion.h"
44 #include "ctdl_module.h"
54 * Convert a structured name into a friendly name. Caller must free the
57 char *n_to_fn(char *value) {
61 nnn = malloc(strlen(value) + 10);
63 extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
65 extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
67 extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
69 extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
71 extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
73 for (i=0; i<strlen(nnn); ++i) {
74 if (!strncmp(&nnn[i], " ", 2)) strcpy(&nnn[i], &nnn[i+1]);
84 * Back end for cmd_auto()
86 void hunt_for_autocomplete(long msgnum, char *search_string) {
87 struct CtdlMessage *msg;
94 msg = CtdlFetchMessage(msgnum, 1);
95 if (msg == NULL) return;
97 v = vcard_load(msg->cm_fields['M']);
101 * Try to match from a friendly name (the "fn" field). If there is
102 * a match, return the entry in the form of:
103 * Display Name <user@domain.org>
105 value = vcard_get_prop(v, "fn", 0, 0, 0);
106 if (value != NULL) if (bmstrcasestr(value, search_string)) {
107 value2 = vcard_get_prop(v, "email", 1, 0, 0);
108 if (value2 == NULL) value2 = "";
109 cprintf("%s <%s>\n", value, value2);
115 * Try to match from a structured name (the "n" field). If there is
116 * a match, return the entry in the form of:
117 * Display Name <user@domain.org>
119 value = vcard_get_prop(v, "n", 0, 0, 0);
120 if (value != NULL) if (bmstrcasestr(value, search_string)) {
122 value2 = vcard_get_prop(v, "email", 1, 0, 0);
123 if (value2 == NULL) value2 = "";
124 nnn = n_to_fn(value);
125 cprintf("%s <%s>\n", nnn, value2);
132 * Try a partial match on all listed email addresses.
135 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
136 if (bmstrcasestr(value, search_string)) {
137 if (vcard_get_prop(v, "fn", 0, 0, 0)) {
138 cprintf("%s <%s>\n", vcard_get_prop(v, "fn", 0, 0, 0), value);
140 else if (vcard_get_prop(v, "n", 0, 0, 0)) {
141 nnn = n_to_fn(vcard_get_prop(v, "n", 0, 0, 0));
142 cprintf("%s <%s>\n", nnn, value);
147 cprintf("%s\n", value);
160 * Attempt to autocomplete an address based on a partial...
162 void cmd_auto(char *argbuf) {
163 char hold_rm[ROOMNAMELEN];
164 char search_string[256];
165 long *msglist = NULL;
167 long *fts_msgs = NULL;
168 int fts_num_msgs = 0;
169 struct cdbdata *cdbfr;
173 int search_match = 0;
174 char *rooms_to_try[] = { USERCONTACTSROOM, ADDRESS_BOOK_ROOM };
176 if (CtdlAccessCheck(ac_logged_in)) return;
177 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
178 if (IsEmptyStr(search_string)) {
179 cprintf("%d You supplied an empty partial.\n",
180 ERROR + ILLEGAL_VALUE);
184 strcpy(hold_rm, CC->room.QRname); /* save current room */
185 cprintf("%d try these:\n", LISTING_FOLLOWS);
188 * Gather up message pointers in rooms containing vCards
190 for (r=0; r < (sizeof(rooms_to_try) / sizeof(char *)); ++r) {
191 if (getroom(&CC->room, rooms_to_try[r]) == 0) {
192 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
194 msglist = realloc(msglist, (num_msgs * sizeof(long)) + cdbfr->len + 1);
195 memcpy(&msglist[num_msgs], cdbfr->ptr, cdbfr->len);
196 num_msgs += (cdbfr->len / sizeof(long));
203 * Search-reduce the results if we have the full text index available
205 if (config.c_enable_fulltext) {
206 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, search_string, "fulltext");
208 for (i=0; i<num_msgs; ++i) {
210 for (j=0; j<fts_num_msgs; ++j) {
211 if (msglist[i] == fts_msgs[j]) {
213 j = fts_num_msgs + 1; /* end the search */
217 msglist[i] = 0; /* invalidate this result */
223 /* If no results, invalidate the whole list */
231 * Now output the ones that look interesting
233 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
234 if (msglist[i] != 0) {
235 hunt_for_autocomplete(msglist[i], search_string);
240 if (strcmp(CC->room.QRname, hold_rm)) {
241 getroom(&CC->room, hold_rm); /* return to saved room */
251 CTDL_MODULE_INIT(autocompletion) {
252 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");
254 /* return our Subversion id for the Log */