4 * Autocompletion of email recipients, etc.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
38 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
40 #include "citserver.h"
49 #include "serv_autocompletion.h"
59 * Convert a structured name into a friendly name. Caller must free the
62 char *n_to_fn(char *value) {
66 nnn = malloc(strlen(value) + 10);
68 extract_token(&nnn[strlen(nnn)] , value, 3, ';', 999);
70 extract_token(&nnn[strlen(nnn)] , value, 1, ';', 999);
72 extract_token(&nnn[strlen(nnn)] , value, 2, ';', 999);
74 extract_token(&nnn[strlen(nnn)] , value, 0, ';', 999);
76 extract_token(&nnn[strlen(nnn)] , value, 4, ';', 999);
78 for (i=0; i<strlen(nnn); ++i) {
79 if (!strncmp(&nnn[i], " ", 2)) strcpy(&nnn[i], &nnn[i+1]);
89 * Back end for cmd_auto()
91 void hunt_for_autocomplete(long msgnum, void *data) {
93 struct CtdlMessage *msg;
100 search_string = (char *) data;
102 msg = CtdlFetchMessage(msgnum, 1);
103 if (msg == NULL) return;
105 v = vcard_load(msg->cm_fields['M']);
106 CtdlFreeMessage(msg);
109 * Try to match from a friendly name (the "fn" field). If there is
110 * a match, return the entry in the form of:
111 * Display Name <user@domain.org>
113 value = vcard_get_prop(v, "fn", 0, 0, 0);
114 if (value != NULL) if (bmstrcasestr(value, search_string)) {
115 value2 = vcard_get_prop(v, "email", 1, 0, 0);
116 if (value2 == NULL) value2 = "";
117 cprintf("%s <%s>\n", value, value2);
123 * Try to match from a structured name (the "n" field). If there is
124 * a match, return the entry in the form of:
125 * Display Name <user@domain.org>
127 value = vcard_get_prop(v, "n", 0, 0, 0);
128 if (value != NULL) if (bmstrcasestr(value, search_string)) {
130 value2 = vcard_get_prop(v, "email", 1, 0, 0);
131 if (value2 == NULL) value2 = "";
132 nnn = n_to_fn(value);
133 cprintf("%s <%s>\n", nnn, value2);
140 * Try a partial match on all listed email addresses.
143 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
144 if (bmstrcasestr(value, search_string)) {
145 if (vcard_get_prop(v, "fn", 0, 0, 0)) {
146 cprintf("%s <%s>\n", vcard_get_prop(v, "fn", 0, 0, 0), value);
148 else if (vcard_get_prop(v, "n", 0, 0, 0)) {
149 nnn = n_to_fn(vcard_get_prop(v, "n", 0, 0, 0));
150 cprintf("%s <%s>\n", nnn, value);
155 cprintf("%s\n", value);
168 * Attempt to autocomplete an address based on a partial...
170 void cmd_auto(char *argbuf) {
171 char hold_rm[ROOMNAMELEN];
172 char search_string[256];
174 if (CtdlAccessCheck(ac_logged_in)) return;
175 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
176 if (strlen(search_string) == 0) {
177 cprintf("%d You supplied an empty partial.\n",
178 ERROR + ILLEGAL_VALUE);
182 strcpy(hold_rm, CC->room.QRname); /* save current room */
183 cprintf("%d try these:\n", LISTING_FOLLOWS);
185 /* Take a spin through the user's personal address book */
186 if (getroom(&CC->room, USERCONTACTSROOM) == 0) {
187 CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard", NULL,
188 hunt_for_autocomplete, search_string);
191 /* FIXME try the global address book */
192 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) == 0) {
193 CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard", NULL,
194 hunt_for_autocomplete, search_string);
198 if (strcmp(&CC->room.QRname, hold_rm)) {
199 getroom(&CC->room, hold_rm); /* return to saved room */
204 char *serv_autocompletion_init(void)
206 CtdlRegisterProtoHook(cmd_auto, "AUTO", "Do recipient autocompletion");