]> code.citadel.org Git - citadel.git/blob - citadel/serv_autocompletion.c
* Bumped internal version number to 5.66
[citadel.git] / citadel / serv_autocompletion.c
1 /*
2  * $Id$
3  *
4  * Autocompletion of email recipients, etc.
5  */
6
7 #ifdef DLL_EXPORT
8 #define IN_LIBCIT
9 #endif
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "tools.h"
44 #include "msgbase.h"
45 #include "user_ops.h"
46 #include "room_ops.h"
47 #include "database.h"
48 #include "vcard.h"
49 #include "serv_autocompletion.h"
50
51
52 #ifndef HAVE_SNPRINTF
53 #include "snprintf.h"
54 #endif
55
56
57 /*
58  * Back end for cmd_auto()
59  */
60 void hunt_for_autocomplete(long msgnum, void *data) {
61         char *search_string;
62         struct CtdlMessage *msg;
63         struct vCard *v;
64
65         search_string = (char *) data;
66
67         msg = CtdlFetchMessage(msgnum, 1);
68         if (msg == NULL) return;
69
70         v = vcard_load(msg->cm_fields['M']);
71         CtdlFreeMessage(msg);
72
73         /*
74          * Try to match from a display name or something like that
75          */
76         if (
77                 (bmstrcasestr(vcard_get_prop(v, "n", 0, 0, 0), search_string))
78         ) {
79                 cprintf("%s\n", vcard_get_prop(v, "email", 1, 0, 0));
80         }
81
82         vcard_free(v);
83 }
84
85
86
87 /*
88  * Attempt to autocomplete an address based on a partial...
89  */
90 void cmd_auto(char *argbuf) {
91         char hold_rm[ROOMNAMELEN];
92         char search_string[256];
93
94         if (CtdlAccessCheck(ac_logged_in)) return;
95         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
96         if (strlen(search_string) == 0) {
97                 cprintf("%d You supplied an empty partial.\n", ERROR + ILLEGAL_VALUE);
98                 return;
99         }
100
101         strcpy(hold_rm, CC->room.QRname);       /* save current room */
102
103         if (getroom(&CC->room, USERCONTACTSROOM) != 0) {
104                 getroom(&CC->room, hold_rm);
105                 lprintf(CTDL_CRIT, "cannot get user contacts room\n");
106                 cprintf("%d Your address book was not found.\n", ERROR + ROOM_NOT_FOUND);
107                 return;
108         }
109
110         cprintf("%d try these:\n", LISTING_FOLLOWS);
111         CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard", NULL, hunt_for_autocomplete, search_string);
112         cprintf("000\n");
113
114         getroom(&CC->room, hold_rm);    /* return to saved room */
115 }
116
117
118 char *serv_autocompletion_init(void)
119 {
120         CtdlRegisterProtoHook(cmd_auto, "AUTO", "Perform recipient autocompletion");
121         return "$Id$";
122 }