4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
11 #define ADDRESS_BOOK_ROOM "Global Address Book"
21 #include <sys/types.h>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
38 #include "dynloader.h"
47 struct vcard_internal_info {
51 /* Message number symbol used internally by these functions */
52 unsigned long SYM_VCARD;
53 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
57 * back end function used by vcard_personal_upload()
59 void vcard_replace_backend(long msgnum) {
60 lprintf(9, "doing the replace thing for <%ld>\n", msgnum);
61 CtdlDeleteMessages(CONFIGROOM, msgnum, NULL);
62 CtdlDeleteMessages(ADDRESS_BOOK_ROOM, msgnum, NULL);
67 * This handler detects whether the user is attempting to save a new
68 * vCard as part of his/her personal configuration, and handles the replace
69 * function accordingly.
71 int vcard_personal_upload(struct CtdlMessage *msg) {
74 char hold_rm[ROOMNAMELEN];
75 char config_rm[ROOMNAMELEN];
77 /* If this isn't the configuration room, or if this isn't a MIME
78 * message, don't bother.
80 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
81 if (msg->cm_format_type != 4) return(0);
83 ptr = msg->cm_fields['M'];
86 linelen = strcspn(ptr, "\n");
87 if (linelen == 0) return(0); /* end of headers */
89 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
90 /* Bingo! The user is uploading a new vCard, so
94 strcpy(hold_rm, CC->quickroom.QRname); /* save rm */
95 MailboxName(config_rm, &CC->usersupp, CONFIGROOM);
97 if (getroom(&CC->quickroom, config_rm) != 0) {
98 getroom(&CC->quickroom, hold_rm);
99 return(1); /* abort */
101 CtdlForEachMessage(MSGS_ALL, 0,
102 "text/x-vcard", vcard_replace_backend);
103 getroom(&CC->quickroom, hold_rm); /* return rm */
107 ptr = strchr((char *)ptr, '\n');
108 if (ptr != NULL) ++ptr;
117 * back end function used by vcard_get_user()
119 void vcard_gm_backend(long msgnum) {
125 * If this user has a vcard on disk, read it into memory, otherwise allocate
126 * and return an empty vCard.
128 struct vCard *vcard_get_user(struct usersupp *u) {
129 char hold_rm[ROOMNAMELEN];
130 char config_rm[ROOMNAMELEN];
131 struct CtdlMessage *msg;
134 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
135 MailboxName(config_rm, u, CONFIGROOM);
137 if (getroom(&CC->quickroom, config_rm) != 0) {
138 getroom(&CC->quickroom, hold_rm);
142 /* We want the last (and probably only) vcard in this room */
144 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard", vcard_gm_backend);
145 getroom(&CC->quickroom, hold_rm); /* return to saved room */
147 if (VC->msgnum < 0L) return vcard_new();
149 msg = CtdlFetchMessage(VC->msgnum);
150 if (msg == NULL) return vcard_new();
152 v = vcard_load(msg->cm_fields['M']);
153 CtdlFreeMessage(msg);
159 * Store this user's vCard in the appropriate place
162 * Write our config to disk
164 void vcard_write_user(struct usersupp *u, struct vCard *v) {
169 strcpy(temp, tmpnam(NULL));
170 ser = vcard_serialize(v);
172 fp = fopen(temp, "w");
173 if (fp == NULL) return;
174 fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
176 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
178 fwrite(ser, strlen(ser), 1, fp);
183 /* This handy API function does all the work for us.
184 * NOTE: normally we would want to set that last argument to 1, to
185 * force the system to delete the user's old vCard. But it doesn't
186 * have to, because the vcard_personal_upload() hook above is going to
187 * notice what we're trying to do, and delete the old vCard.
189 CtdlWriteObject(CONFIGROOM, /* which room */
190 "text/x-vcard", /* MIME type */
191 temp, /* temp file */
194 0); /* don't delete others of this type */
202 * old style "enter registration info" command
204 void cmd_regi(char *argbuf) {
207 struct vCard *my_vcard;
214 char tmpaddress[512];
216 if (!(CC->logged_in)) {
217 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
221 my_vcard = vcard_get_user(&CC->usersupp);
224 strcpy(tmpstate, "");
227 cprintf("%d Send registration...\n", SEND_LISTING);
229 while (client_gets(buf), strcmp(buf,"000")) {
230 if (a==0) vcard_set_prop(my_vcard, "n", buf);
231 if (a==1) strcpy(tmpaddr,buf);
232 if (a==2) strcpy(tmpcity,buf);
233 if (a==3) strcpy(tmpstate,buf);
235 for (c=0; c<strlen(buf); ++c) {
236 if ((buf[c]>='0')&&(buf[c]<='9')) {
244 strcpy(tmpphone, "");
245 for (c=0; c<strlen(buf); ++c) {
246 if ((buf[c]>='0')&&(buf[c]<='9')) {
252 vcard_set_prop(my_vcard, "tel;home", tmpphone);
254 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
257 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
258 tmpaddr, tmpcity, tmpstate, tmpzip);
259 vcard_set_prop(my_vcard, "adr", tmpaddress);
260 vcard_write_user(&CC->usersupp, my_vcard);
261 vcard_free(my_vcard);
263 lgetuser(&CC->usersupp, CC->curr_user);
264 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
265 lputuser(&CC->usersupp);
267 /* set global flag calling for validation */
268 begin_critical_section(S_CONTROL);
270 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
272 end_critical_section(S_CONTROL);
278 * get registration info for a user
280 void cmd_greg(char *argbuf)
282 struct usersupp usbuf;
289 extract(who, argbuf, 0);
291 if (!(CC->logged_in)) {
292 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
296 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
298 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
299 cprintf("%d Higher access required.\n",
300 ERROR+HIGHER_ACCESS_REQUIRED);
304 if (getuser(&usbuf, who) != 0) {
305 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
309 v = vcard_get_user(&usbuf);
311 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
312 cprintf("%ld\n", usbuf.usernum);
313 cprintf("%s\n", usbuf.password);
314 cprintf("%s\n", vcard_get_prop(v, "n", 0)); /* name */
316 sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
318 extract_token(buf, adr, 2, ';');
319 cprintf("%s\n", buf); /* street */
320 extract_token(buf, adr, 3, ';');
321 cprintf("%s\n", buf); /* city */
322 extract_token(buf, adr, 4, ';');
323 cprintf("%s\n", buf); /* state */
324 extract_token(buf, adr, 5, ';');
325 cprintf("%s\n", buf); /* zip */
327 tel = vcard_get_prop(v, "tel;home", 0);
328 if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
330 cprintf("%s\n", tel);
336 cprintf("%d\n", usbuf.axlevel);
338 cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
344 void vcard_session_startup_hook(void) {
345 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
350 char *Dynamic_Module_Init(void)
352 SYM_VCARD = CtdlGetDynamicSymbol();
353 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
354 CtdlRegisterMessageHook(vcard_personal_upload, EVT_BEFORESAVE);
355 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
356 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");