4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
7 * Copyright (c) 1999-2001 / released under the GNU General Public License
11 * Where we keep messages containing the vCards that source our directory. It
12 * makes no sense to change this, because you'd have to change it on every
13 * system on the network. That would be stupid.
15 #define ADDRESS_BOOK_ROOM "Global Address Book"
18 * Format of the "Extended ID" field of the message containing a user's
19 * vCard. Doesn't matter what it really looks like as long as it's both
20 * unique and consistent (because we use it for replication checking to
21 * delete the old vCard network-wide when the user enters a new one).
23 #define VCARD_EXT_FORMAT "Citadel vCard: personal card for %s at %s"
34 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
57 #include "dynloader.h"
63 #include "internet_addressing.h"
67 struct vcard_internal_info {
71 /* Message number symbol used internally by these functions */
72 unsigned long SYM_VCARD;
73 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
77 * Extract Internet e-mail addresses from a message containing a vCard, and
78 * perform a callback for any found.
80 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
81 void (*callback)(char *, char *) ) {
85 char citadel_address[SIZ];
87 int found_something = 0;
89 if (msg->cm_fields['A'] == NULL) return;
90 if (msg->cm_fields['N'] == NULL) return;
91 sprintf(citadel_address, "%s @ %s",
92 msg->cm_fields['A'], msg->cm_fields['N']);
94 v = vcard_load(msg->cm_fields['M']);
95 if (v == NULL) return;
97 /* Go through the vCard searching for *all* instances of
98 * the "email;internet" key
101 s = vcard_get_prop(v, "email;internet", 0, instance++);
105 if (strlen(addr) > 0) {
106 if (callback != NULL) {
107 callback(addr, citadel_address);
116 } while(found_something);
122 * Back end function for cmd_igab()
124 void vcard_add_to_directory(long msgnum, void *data) {
125 struct CtdlMessage *msg;
127 msg = CtdlFetchMessage(msgnum);
129 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
132 CtdlFreeMessage(msg);
137 * Initialize Global Adress Book
139 void cmd_igab(char *argbuf) {
140 char hold_rm[ROOMNAMELEN];
142 if (CtdlAccessCheck(ac_aide)) return;
144 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
146 if (getroom(&CC->quickroom, ADDRESS_BOOK_ROOM) != 0) {
147 getroom(&CC->quickroom, hold_rm);
148 cprintf("%d cannot get address book room\n", ERROR);
152 /* Empty the existing database first.
156 /* We want the last (and probably only) vcard in this room */
157 CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
158 NULL, vcard_add_to_directory, NULL);
160 getroom(&CC->quickroom, hold_rm); /* return to saved room */
161 cprintf("%d Directory has been rebuilt.\n", OK);
168 * See if there is a valid Internet address in a vCard to use for outbound
169 * Interet messages. If there is, stick it in CC->cs_inet_email.
171 void vcard_populate_cs_inet_email(struct vCard *v) {
173 int continue_searching = 1;
177 * Clear whatever was in there previously.
179 if (CC->cs_inet_email != NULL) {
180 phree(CC->cs_inet_email);
181 CC->cs_inet_email = NULL;
184 /* Go through the vCard searching for *all* instances of
185 * the "email;internet" key
188 s = vcard_get_prop(v, "email;internet", 0, instance++);
190 continue_searching = 1;
193 if (strlen(addr) > 0) {
194 if (IsDirectory(addr)) {
195 continue_searching = 0;
196 CC->cs_inet_email = strdoop(addr);
202 continue_searching = 0;
204 } while(continue_searching);
210 * This handler detects whether the user is attempting to save a new
211 * vCard as part of his/her personal configuration, and handles the replace
212 * function accordingly (delete the user's existing vCard in the config room
213 * and in the global address book).
215 int vcard_upload_beforesave(struct CtdlMessage *msg) {
218 char config_rm[ROOMNAMELEN];
221 if (!CC->logged_in) return(0); /* Only do this if logged in. */
223 /* If this isn't the configuration room, or if this isn't a MIME
224 * message, don't bother. (Check for NULL room first, otherwise
225 * some messages will cause it to crash!!)
227 if (msg->cm_fields['O'] == NULL) return(0);
228 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
229 if (msg->cm_format_type != 4) return(0);
231 ptr = msg->cm_fields['M'];
232 if (ptr == NULL) return(0);
233 while (ptr != NULL) {
235 linelen = strcspn(ptr, "\n");
236 if (linelen == 0) return(0); /* end of headers */
238 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
239 /* Bingo! The user is uploading a new vCard, so
240 * delete the old one.
243 /* Delete the user's old vCard. This would probably
244 * get taken care of by the replication check, but we
245 * want to make sure there is absolutely only one
246 * vCard in the user's config room at all times.
248 * FIXME ... this needs to be tweaked to allow an admin
249 * to make changes to another user's vCard instead of
250 * assuming that it's always the user saving his own.
252 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
253 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
255 /* Set the Extended-ID to a standardized one so the
256 * replication always works correctly
258 if (msg->cm_fields['E'] != NULL)
259 phree(msg->cm_fields['E']);
261 sprintf(buf, VCARD_EXT_FORMAT,
262 msg->cm_fields['A'], NODENAME);
263 msg->cm_fields['E'] = strdoop(buf);
265 /* Now allow the save to complete. */
269 ptr = strchr((char *)ptr, '\n');
270 if (ptr != NULL) ++ptr;
279 * This handler detects whether the user is attempting to save a new
280 * vCard as part of his/her personal configuration, and handles the replace
281 * function accordingly (copy the vCard from the config room to the global
284 int vcard_upload_aftersave(struct CtdlMessage *msg) {
290 if (!CC->logged_in) return(0); /* Only do this if logged in. */
292 /* If this isn't the configuration room, or if this isn't a MIME
293 * message, don't bother.
295 if (msg->cm_fields['O'] == NULL) return(0);
296 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
297 if (msg->cm_format_type != 4) return(0);
299 ptr = msg->cm_fields['M'];
300 if (ptr == NULL) return(0);
301 while (ptr != NULL) {
303 linelen = strcspn(ptr, "\n");
304 if (linelen == 0) return(0); /* end of headers */
306 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
307 /* Bingo! The user is uploading a new vCard, so
308 * copy it to the Global Address Book room.
311 I = atol(msg->cm_fields['I']);
312 if (I < 0L) return(0);
314 /* Put it in the Global Address Book room... */
315 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
316 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
318 /* ...and also in the directory database. */
319 vcard_add_to_directory(I, NULL);
321 /* Store our Internet return address in memory */
322 v = vcard_load(msg->cm_fields['M']);
323 vcard_populate_cs_inet_email(v);
329 ptr = strchr((char *)ptr, '\n');
330 if (ptr != NULL) ++ptr;
339 * back end function used for callbacks
341 void vcard_gu_backend(long msgnum, void *userdata) {
347 * If this user has a vcard on disk, read it into memory, otherwise allocate
348 * and return an empty vCard.
350 struct vCard *vcard_get_user(struct usersupp *u) {
351 char hold_rm[ROOMNAMELEN];
352 char config_rm[ROOMNAMELEN];
353 struct CtdlMessage *msg;
356 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
357 MailboxName(config_rm, u, USERCONFIGROOM);
359 if (getroom(&CC->quickroom, config_rm) != 0) {
360 getroom(&CC->quickroom, hold_rm);
364 /* We want the last (and probably only) vcard in this room */
366 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
367 NULL, vcard_gu_backend, NULL);
368 getroom(&CC->quickroom, hold_rm); /* return to saved room */
370 if (VC->msgnum < 0L) return vcard_new();
372 msg = CtdlFetchMessage(VC->msgnum);
373 if (msg == NULL) return vcard_new();
375 v = vcard_load(msg->cm_fields['M']);
376 CtdlFreeMessage(msg);
382 * Store this user's vCard in the appropriate place
385 * Write our config to disk
387 void vcard_write_user(struct usersupp *u, struct vCard *v) {
392 strcpy(temp, tmpnam(NULL));
393 ser = vcard_serialize(v);
395 fp = fopen(temp, "w");
396 if (fp == NULL) return;
398 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
400 fwrite(ser, strlen(ser), 1, fp);
405 /* This handy API function does all the work for us.
406 * NOTE: normally we would want to set that last argument to 1, to
407 * force the system to delete the user's old vCard. But it doesn't
408 * have to, because the vcard_upload_beforesave() hook above
409 * is going to notice what we're trying to do, and delete the old vCard.
411 CtdlWriteObject(USERCONFIGROOM, /* which room */
412 "text/x-vcard", /* MIME type */
413 temp, /* temp file */
416 0, /* don't delete others of this type */
425 * Old style "enter registration info" command. This function simply honors
426 * the REGI protocol command, translates the entered parameters into a vCard,
427 * and enters the vCard into the user's configuration.
429 void cmd_regi(char *argbuf) {
432 struct vCard *my_vcard;
438 char tmpaddress[SIZ];
439 char tmpcountry[SIZ];
441 if (!(CC->logged_in)) {
442 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
446 my_vcard = vcard_get_user(&CC->usersupp);
449 strcpy(tmpstate, "");
451 strcpy(tmpcountry, "USA");
453 cprintf("%d Send registration...\n", SEND_LISTING);
455 while (client_gets(buf), strcmp(buf,"000")) {
456 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
457 if (a==1) strcpy(tmpaddr, buf);
458 if (a==2) strcpy(tmpcity, buf);
459 if (a==3) strcpy(tmpstate, buf);
461 for (c=0; c<strlen(buf); ++c) {
462 if ((buf[c]>='0') && (buf[c]<='9')) {
469 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
470 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
471 if (a==7) strcpy(tmpcountry, buf);
475 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
476 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
477 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
478 vcard_write_user(&CC->usersupp, my_vcard);
479 vcard_free(my_vcard);
481 lgetuser(&CC->usersupp, CC->curr_user);
482 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
483 lputuser(&CC->usersupp);
485 /* set global flag calling for validation */
486 begin_critical_section(S_CONTROL);
488 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
490 end_critical_section(S_CONTROL);
496 * Protocol command to fetch registration info for a user
498 void cmd_greg(char *argbuf)
500 struct usersupp usbuf;
507 extract(who, argbuf, 0);
509 if (!(CC->logged_in)) {
510 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
514 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
516 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
517 cprintf("%d Higher access required.\n",
518 ERROR+HIGHER_ACCESS_REQUIRED);
522 if (getuser(&usbuf, who) != 0) {
523 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
527 v = vcard_get_user(&usbuf);
529 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
530 cprintf("%ld\n", usbuf.usernum);
531 cprintf("%s\n", usbuf.password);
532 s = vcard_get_prop(v, "n", 0, 0);
533 cprintf("%s\n", s ? s : " "); /* name */
535 s = vcard_get_prop(v, "adr", 0, 0);
536 sprintf(adr, "%s", s ? s : " ");/* address... */
538 extract_token(buf, adr, 2, ';');
539 cprintf("%s\n", buf); /* street */
540 extract_token(buf, adr, 3, ';');
541 cprintf("%s\n", buf); /* city */
542 extract_token(buf, adr, 4, ';');
543 cprintf("%s\n", buf); /* state */
544 extract_token(buf, adr, 5, ';');
545 cprintf("%s\n", buf); /* zip */
547 s = vcard_get_prop(v, "tel;home", 0, 0);
548 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0);
556 cprintf("%d\n", usbuf.axlevel);
558 s = vcard_get_prop(v, "email;internet", 0, 0);
559 cprintf("%s\n", s ? s : " ");
560 s = vcard_get_prop(v, "adr", 0, 0);
561 sprintf(adr, "%s", s ? s : " ");/* address... */
563 extract_token(buf, adr, 6, ';');
564 cprintf("%s\n", buf); /* country */
570 * When a user is being deleted, we have to remove his/her vCard.
571 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
572 * field, and the same Extended ID as the existing card.
574 void vcard_purge(char *username, long usernum) {
575 struct CtdlMessage *msg;
578 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
579 if (msg == NULL) return;
580 memset(msg, 0, sizeof(struct CtdlMessage));
582 msg->cm_magic = CTDLMESSAGE_MAGIC;
583 msg->cm_anon_type = MES_NORMAL;
584 msg->cm_format_type = 0;
585 msg->cm_fields['A'] = strdoop(username);
586 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
587 msg->cm_fields['N'] = strdoop(NODENAME);
588 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
590 sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
591 msg->cm_fields['E'] = strdoop(buf);
593 msg->cm_fields['S'] = strdoop("CANCEL");
595 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
596 CtdlFreeMessage(msg);
601 * Grab vCard directory stuff out of incoming network messages
603 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
607 if (msg == NULL) return(0);
609 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
613 if (msg->cm_format_type != 4) return(0);
615 ptr = msg->cm_fields['M'];
616 if (ptr == NULL) return(0);
617 while (ptr != NULL) {
619 linelen = strcspn(ptr, "\n");
620 if (linelen == 0) return(0); /* end of headers */
622 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
623 /* It's a vCard. Add it to the directory. */
624 vcard_extract_internet_addresses(msg,
625 CtdlDirectoryAddUser);
629 ptr = strchr((char *)ptr, '\n');
630 if (ptr != NULL) ++ptr;
639 * When a vCard is being removed from the Global Address Book room, remove it
640 * from the directory as well.
642 void vcard_delete_remove(char *room, long msgnum) {
643 struct CtdlMessage *msg;
645 if (msgnum <= 0L) return;
647 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
651 msg = CtdlFetchMessage(msgnum);
653 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
656 CtdlFreeMessage(msg);
663 void cmd_qdir(char *argbuf) {
664 char citadel_addr[SIZ];
665 char internet_addr[SIZ];
667 if (CtdlAccessCheck(ac_logged_in)) return;
669 extract(internet_addr, argbuf, 0);
671 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
672 cprintf("%d %s was not found.\n",
673 ERROR+NO_SUCH_USER, internet_addr);
677 cprintf("%d %s\n", OK, citadel_addr);
684 * Session startup, allocate some per-session data
686 void vcard_session_startup_hook(void) {
687 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
692 * When a user logs in...
694 void vcard_session_login_hook(void) {
697 v = vcard_get_user(&CC->usersupp);
698 vcard_populate_cs_inet_email(v);
705 * When a user logs out...
707 void vcard_session_logout_hook(void) {
708 if (CC->cs_inet_email != NULL) {
709 phree(CC->cs_inet_email);
710 CC->cs_inet_email = NULL;
715 char *Dynamic_Module_Init(void)
717 SYM_VCARD = CtdlGetDynamicSymbol();
718 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
719 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
720 CtdlRegisterSessionHook(vcard_session_logout_hook, EVT_LOGOUT);
721 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
722 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
723 CtdlRegisterDeleteHook(vcard_delete_remove);
724 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
725 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
726 CtdlRegisterProtoHook(cmd_igab, "IGAB",
727 "Initialize Global Address Book");
728 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
729 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
730 CtdlRegisterNetprocHook(vcard_extract_from_network);
731 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);