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>
34 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
45 #include "serv_autocompletion.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, void *data) {
89 struct CtdlMessage *msg;
96 search_string = (char *) data;
98 msg = CtdlFetchMessage(msgnum, 1);
99 if (msg == NULL) return;
101 v = vcard_load(msg->cm_fields['M']);
102 CtdlFreeMessage(msg);
105 * Try to match from a friendly name (the "fn" field). If there is
106 * a match, return the entry in the form of:
107 * Display Name <user@domain.org>
109 value = vcard_get_prop(v, "fn", 0, 0, 0);
110 if (value != NULL) if (bmstrcasestr(value, search_string)) {
111 value2 = vcard_get_prop(v, "email", 1, 0, 0);
112 if (value2 == NULL) value2 = "";
113 cprintf("%s <%s>\n", value, value2);
119 * Try to match from a structured name (the "n" field). If there is
120 * a match, return the entry in the form of:
121 * Display Name <user@domain.org>
123 value = vcard_get_prop(v, "n", 0, 0, 0);
124 if (value != NULL) if (bmstrcasestr(value, search_string)) {
126 value2 = vcard_get_prop(v, "email", 1, 0, 0);
127 if (value2 == NULL) value2 = "";
128 nnn = n_to_fn(value);
129 cprintf("%s <%s>\n", nnn, value2);
136 * Try a partial match on all listed email addresses.
139 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
140 if (bmstrcasestr(value, search_string)) {
141 if (vcard_get_prop(v, "fn", 0, 0, 0)) {
142 cprintf("%s <%s>\n", vcard_get_prop(v, "fn", 0, 0, 0), value);
144 else if (vcard_get_prop(v, "n", 0, 0, 0)) {
145 nnn = n_to_fn(vcard_get_prop(v, "n", 0, 0, 0));
146 cprintf("%s <%s>\n", nnn, value);
151 cprintf("%s\n", value);
164 * Attempt to autocomplete an address based on a partial...
166 void cmd_auto(char *argbuf) {
167 char hold_rm[ROOMNAMELEN];
168 char search_string[256];
170 if (CtdlAccessCheck(ac_logged_in)) return;
171 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
172 if (strlen(search_string) == 0) {
173 cprintf("%d You supplied an empty partial.\n",
174 ERROR + ILLEGAL_VALUE);
178 strcpy(hold_rm, CC->room.QRname); /* save current room */
179 cprintf("%d try these:\n", LISTING_FOLLOWS);
181 /* Take a spin through the user's personal address book */
182 if (getroom(&CC->room, USERCONTACTSROOM) == 0) {
183 CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/x-vcard", NULL,
184 hunt_for_autocomplete, search_string);
187 /* FIXME try the global address book */
188 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) == 0) {
189 CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/x-vcard", NULL,
190 hunt_for_autocomplete, search_string);
194 if (strcmp(CC->room.QRname, hold_rm)) {
195 getroom(&CC->room, hold_rm); /* return to saved room */
200 char *serv_autocompletion_init(void)
202 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");