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"
43 #include "serv_autocompletion.h"
45 #include "ctdl_module.h"
55 * Convert a structured name into a friendly name. Caller must free the
58 char *n_to_fn(char *value) {
62 nnn = malloc(strlen(value) + 10);
64 extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
66 extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
68 extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
70 extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
72 extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
74 for (i=0; i<strlen(nnn); ++i) {
75 if (!strncmp(&nnn[i], " ", 2)) strcpy(&nnn[i], &nnn[i+1]);
85 * Back end for cmd_auto()
87 void hunt_for_autocomplete(long msgnum, char *search_string) {
88 struct CtdlMessage *msg;
95 msg = CtdlFetchMessage(msgnum, 1);
96 if (msg == NULL) return;
98 v = vcard_load(msg->cm_fields['M']);
102 * Try to match from a friendly name (the "fn" field). If there is
103 * a match, return the entry in the form of:
104 * Display Name <user@domain.org>
106 value = vcard_get_prop(v, "fn", 0, 0, 0);
107 if (value != NULL) if (bmstrcasestr(value, search_string)) {
108 value2 = vcard_get_prop(v, "email", 1, 0, 0);
109 if (value2 == NULL) value2 = "";
110 cprintf("%s <%s>\n", value, value2);
116 * Try to match from a structured name (the "n" field). If there is
117 * a match, return the entry in the form of:
118 * Display Name <user@domain.org>
120 value = vcard_get_prop(v, "n", 0, 0, 0);
121 if (value != NULL) if (bmstrcasestr(value, search_string)) {
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);
133 * Try a partial match on all listed email addresses.
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);
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);
148 cprintf("%s\n", value);
161 * Attempt to autocomplete an address based on a partial...
163 void cmd_auto(char *argbuf) {
164 char hold_rm[ROOMNAMELEN];
165 char search_string[256];
166 long *msglist = NULL;
168 long *fts_msgs = NULL;
169 int fts_num_msgs = 0;
170 struct cdbdata *cdbfr;
174 int search_match = 0;
175 char *rooms_to_try[] = { USERCONTACTSROOM, ADDRESS_BOOK_ROOM };
177 if (CtdlAccessCheck(ac_logged_in)) return;
178 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
179 if (IsEmptyStr(search_string)) {
180 cprintf("%d You supplied an empty partial.\n",
181 ERROR + ILLEGAL_VALUE);
185 strcpy(hold_rm, CC->room.QRname); /* save current room */
186 cprintf("%d try these:\n", LISTING_FOLLOWS);
189 * Gather up message pointers in rooms containing vCards
191 for (r=0; r < (sizeof(rooms_to_try) / sizeof(char *)); ++r) {
192 if (getroom(&CC->room, rooms_to_try[r]) == 0) {
193 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
195 msglist = realloc(msglist, (num_msgs * sizeof(long)) + cdbfr->len + 1);
196 memcpy(&msglist[num_msgs], cdbfr->ptr, cdbfr->len);
197 num_msgs += (cdbfr->len / sizeof(long));
204 * Search-reduce the results if we have the full text index available
206 if (config.c_enable_fulltext) {
207 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, search_string, "fulltext");
209 for (i=0; i<num_msgs; ++i) {
211 for (j=0; j<fts_num_msgs; ++j) {
212 if (msglist[i] == fts_msgs[j]) {
214 j = fts_num_msgs + 1; /* end the search */
218 msglist[i] = 0; /* invalidate this result */
224 /* If no results, invalidate the whole list */
232 * Now output the ones that look interesting
234 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
235 if (msglist[i] != 0) {
236 hunt_for_autocomplete(msglist[i], search_string);
241 if (strcmp(CC->room.QRname, hold_rm)) {
242 getroom(&CC->room, hold_rm); /* return to saved room */
252 CTDL_MODULE_INIT(autocompletion) {
253 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");
255 /* return our Subversion id for the Log */