4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
7 * Copyright (c) 1999-2002 / 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 snprintf(citadel_address, sizeof 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++, 0);
105 if (strlen(addr) > 0) {
106 if (callback != NULL) {
107 callback(addr, citadel_address);
116 } while(found_something);
124 * Callback for vcard_add_to_directory()
125 * (Lotsa ugly nested callbacks. Oh well.)
126 * This little shim function makes sure we're not
128 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
131 /* We have to validate that we're not stepping on someone else's
132 * email address ... but only if we're logged in. Otherwise it's
133 * probably just the networker or something.
136 lprintf(9, "Checking for <%s>...\n", internet_addr);
137 if (CtdlDirectoryLookup(buf, internet_addr) == 0) {
138 if (strcasecmp(buf, citadel_addr)) {
139 /* This address belongs to someone else.
140 * Bail out silently without saving.
142 lprintf(9, "DOOP!\n");
147 lprintf(9, "ADDING!\n");
148 CtdlDirectoryAddUser(internet_addr, citadel_addr);
153 * Back end function for cmd_igab()
155 void vcard_add_to_directory(long msgnum, void *data) {
156 struct CtdlMessage *msg;
158 msg = CtdlFetchMessage(msgnum);
160 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
163 CtdlFreeMessage(msg);
168 * Initialize Global Adress Book
170 void cmd_igab(char *argbuf) {
171 char hold_rm[ROOMNAMELEN];
173 if (CtdlAccessCheck(ac_aide)) return;
175 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
177 if (getroom(&CC->quickroom, ADDRESS_BOOK_ROOM) != 0) {
178 getroom(&CC->quickroom, hold_rm);
179 cprintf("%d cannot get address book room\n", ERROR);
183 /* Empty the existing database first.
187 /* We want the last (and probably only) vcard in this room */
188 CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
189 NULL, vcard_add_to_directory, NULL);
191 getroom(&CC->quickroom, hold_rm); /* return to saved room */
192 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
199 * See if there is a valid Internet address in a vCard to use for outbound
200 * Interet messages. If there is, stick it in CC->cs_inet_email.
202 void vcard_populate_cs_inet_email(struct vCard *v) {
204 int continue_searching = 1;
208 * Clear whatever was in there previously.
210 if (CC->cs_inet_email != NULL) {
211 phree(CC->cs_inet_email);
212 CC->cs_inet_email = NULL;
215 /* Go through the vCard searching for *all* instances of
216 * the "email;internet" key
219 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
221 continue_searching = 1;
224 if (strlen(addr) > 0) {
225 if (IsDirectory(addr)) {
226 continue_searching = 0;
227 CC->cs_inet_email = strdoop(addr);
233 continue_searching = 0;
235 } while(continue_searching);
241 * This handler detects whether the user is attempting to save a new
242 * vCard as part of his/her personal configuration, and handles the replace
243 * function accordingly (delete the user's existing vCard in the config room
244 * and in the global address book).
246 int vcard_upload_beforesave(struct CtdlMessage *msg) {
250 struct usersupp usbuf;
253 if (!CC->logged_in) return(0); /* Only do this if logged in. */
255 /* If this isn't a "My Citadel Config" room, don't bother. */
256 if ( (CC->quickroom.QRflags && QR_MAILBOX)
257 && (!strcasecmp(&CC->quickroom.QRname[11], USERCONFIGROOM)) ) {
258 /* Yes, we want to do this */
264 /* If this isn't a MIME message, don't bother. */
265 if (msg->cm_format_type != 4) return(0);
267 /* Ok, if we got this far, look into the situation further... */
269 ptr = msg->cm_fields['M'];
270 if (ptr == NULL) return(0);
271 while (ptr != NULL) {
273 linelen = strcspn(ptr, "\n");
274 if (linelen == 0) return(0); /* end of headers */
276 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
277 /* Bingo! The user is uploading a new vCard, so
278 * delete the old one. First, figure out which user
279 * is being re-registered...
281 what_user = atol(CC->quickroom.QRname);
283 if (what_user == CC->usersupp.usernum) {
284 /* It's the logged in user. That was easy. */
285 memcpy(&usbuf, &CC->usersupp,
286 sizeof(struct usersupp) );
289 else if (getuserbynumber(&usbuf, what_user) == 0) {
290 /* We fetched a valid user record */
294 /* No user with that number! */
298 /* Delete the user's old vCard. This would probably
299 * get taken care of by the replication check, but we
300 * want to make sure there is absolutely only one
301 * vCard in the user's config room at all times.
303 CtdlDeleteMessages(CC->quickroom.QRname,
306 /* Set the Extended-ID to a standardized one so the
307 * replication always works correctly
309 if (msg->cm_fields['E'] != NULL)
310 phree(msg->cm_fields['E']);
312 if (msg->cm_fields['A'] != NULL)
313 phree(msg->cm_fields['A']);
315 msg->cm_fields['A'] = strdoop(usbuf.fullname);
317 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
318 msg->cm_fields['A'], NODENAME);
319 msg->cm_fields['E'] = strdoop(buf);
321 /* Now allow the save to complete. */
325 ptr = strchr((char *)ptr, '\n');
326 if (ptr != NULL) ++ptr;
335 * This handler detects whether the user is attempting to save a new
336 * vCard as part of his/her personal configuration, and handles the replace
337 * function accordingly (copy the vCard from the config room to the global
340 int vcard_upload_aftersave(struct CtdlMessage *msg) {
346 if (!CC->logged_in) return(0); /* Only do this if logged in. */
348 /* If this isn't the configuration room, or if this isn't a MIME
349 * message, don't bother.
351 if (msg->cm_fields['O'] == NULL) return(0);
352 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
353 if (msg->cm_format_type != 4) return(0);
355 ptr = msg->cm_fields['M'];
356 if (ptr == NULL) return(0);
357 while (ptr != NULL) {
359 linelen = strcspn(ptr, "\n");
360 if (linelen == 0) return(0); /* end of headers */
362 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
363 /* Bingo! The user is uploading a new vCard, so
364 * copy it to the Global Address Book room.
367 I = atol(msg->cm_fields['I']);
368 if (I < 0L) return(0);
370 /* Put it in the Global Address Book room... */
371 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
372 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
374 /* ...and also in the directory database. */
375 vcard_add_to_directory(I, NULL);
377 /* Store our Internet return address in memory */
378 v = vcard_load(msg->cm_fields['M']);
379 vcard_populate_cs_inet_email(v);
385 ptr = strchr((char *)ptr, '\n');
386 if (ptr != NULL) ++ptr;
395 * back end function used for callbacks
397 void vcard_gu_backend(long msgnum, void *userdata) {
403 * If this user has a vcard on disk, read it into memory, otherwise allocate
404 * and return an empty vCard.
406 struct vCard *vcard_get_user(struct usersupp *u) {
407 char hold_rm[ROOMNAMELEN];
408 char config_rm[ROOMNAMELEN];
409 struct CtdlMessage *msg;
412 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
413 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
415 if (getroom(&CC->quickroom, config_rm) != 0) {
416 getroom(&CC->quickroom, hold_rm);
420 /* We want the last (and probably only) vcard in this room */
422 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
423 NULL, vcard_gu_backend, NULL);
424 getroom(&CC->quickroom, hold_rm); /* return to saved room */
426 if (VC->msgnum < 0L) return vcard_new();
428 msg = CtdlFetchMessage(VC->msgnum);
429 if (msg == NULL) return vcard_new();
431 v = vcard_load(msg->cm_fields['M']);
432 CtdlFreeMessage(msg);
438 * Store this user's vCard in the appropriate place
441 * Write our config to disk
443 void vcard_write_user(struct usersupp *u, struct vCard *v) {
448 strcpy(temp, tmpnam(NULL));
449 ser = vcard_serialize(v);
451 fp = fopen(temp, "w");
452 if (fp == NULL) return;
454 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
456 fwrite(ser, strlen(ser), 1, fp);
461 /* This handy API function does all the work for us.
462 * NOTE: normally we would want to set that last argument to 1, to
463 * force the system to delete the user's old vCard. But it doesn't
464 * have to, because the vcard_upload_beforesave() hook above
465 * is going to notice what we're trying to do, and delete the old vCard.
467 CtdlWriteObject(USERCONFIGROOM, /* which room */
468 "text/x-vcard", /* MIME type */
469 temp, /* temp file */
472 0, /* don't delete others of this type */
481 * Old style "enter registration info" command. This function simply honors
482 * the REGI protocol command, translates the entered parameters into a vCard,
483 * and enters the vCard into the user's configuration.
485 void cmd_regi(char *argbuf) {
488 struct vCard *my_vcard;
494 char tmpaddress[SIZ];
495 char tmpcountry[SIZ];
497 if (!(CC->logged_in)) {
498 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
502 my_vcard = vcard_get_user(&CC->usersupp);
505 strcpy(tmpstate, "");
507 strcpy(tmpcountry, "USA");
509 cprintf("%d Send registration...\n", SEND_LISTING);
511 while (client_gets(buf), strcmp(buf,"000")) {
512 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
513 if (a==1) strcpy(tmpaddr, buf);
514 if (a==2) strcpy(tmpcity, buf);
515 if (a==3) strcpy(tmpstate, buf);
517 for (c=0; c<strlen(buf); ++c) {
518 if ((buf[c]>='0') && (buf[c]<='9')) {
525 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
526 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
527 if (a==7) strcpy(tmpcountry, buf);
531 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
532 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
533 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
534 vcard_write_user(&CC->usersupp, my_vcard);
535 vcard_free(my_vcard);
537 lgetuser(&CC->usersupp, CC->curr_user);
538 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
539 lputuser(&CC->usersupp);
541 /* set global flag calling for validation */
542 begin_critical_section(S_CONTROL);
544 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
546 end_critical_section(S_CONTROL);
552 * Protocol command to fetch registration info for a user
554 void cmd_greg(char *argbuf)
556 struct usersupp usbuf;
563 extract(who, argbuf, 0);
565 if (!(CC->logged_in)) {
566 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
570 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
572 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
573 cprintf("%d Higher access required.\n",
574 ERROR+HIGHER_ACCESS_REQUIRED);
578 if (getuser(&usbuf, who) != 0) {
579 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
583 v = vcard_get_user(&usbuf);
585 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
586 cprintf("%ld\n", usbuf.usernum);
587 cprintf("%s\n", usbuf.password);
588 s = vcard_get_prop(v, "n", 0, 0, 0);
589 cprintf("%s\n", s ? s : " "); /* name */
591 s = vcard_get_prop(v, "adr", 0, 0, 0);
592 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
594 extract_token(buf, adr, 2, ';');
595 cprintf("%s\n", buf); /* street */
596 extract_token(buf, adr, 3, ';');
597 cprintf("%s\n", buf); /* city */
598 extract_token(buf, adr, 4, ';');
599 cprintf("%s\n", buf); /* state */
600 extract_token(buf, adr, 5, ';');
601 cprintf("%s\n", buf); /* zip */
603 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
604 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
612 cprintf("%d\n", usbuf.axlevel);
614 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
615 cprintf("%s\n", s ? s : " ");
616 s = vcard_get_prop(v, "adr", 0, 0, 0);
617 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
619 extract_token(buf, adr, 6, ';');
620 cprintf("%s\n", buf); /* country */
626 * When a user is being deleted, we have to remove his/her vCard.
627 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
628 * field, and the same Extended ID as the existing card.
630 void vcard_purge(char *username, long usernum) {
631 struct CtdlMessage *msg;
634 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
635 if (msg == NULL) return;
636 memset(msg, 0, sizeof(struct CtdlMessage));
638 msg->cm_magic = CTDLMESSAGE_MAGIC;
639 msg->cm_anon_type = MES_NORMAL;
640 msg->cm_format_type = 0;
641 msg->cm_fields['A'] = strdoop(username);
642 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
643 msg->cm_fields['N'] = strdoop(NODENAME);
644 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
646 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
647 msg->cm_fields['E'] = strdoop(buf);
649 msg->cm_fields['S'] = strdoop("CANCEL");
651 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
652 CtdlFreeMessage(msg);
657 * Grab vCard directory stuff out of incoming network messages
659 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
663 if (msg == NULL) return(0);
665 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
669 if (msg->cm_format_type != 4) return(0);
671 ptr = msg->cm_fields['M'];
672 if (ptr == NULL) return(0);
673 while (ptr != NULL) {
675 linelen = strcspn(ptr, "\n");
676 if (linelen == 0) return(0); /* end of headers */
678 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
679 /* It's a vCard. Add it to the directory. */
680 vcard_extract_internet_addresses(msg,
681 CtdlDirectoryAddUser);
685 ptr = strchr((char *)ptr, '\n');
686 if (ptr != NULL) ++ptr;
695 * When a vCard is being removed from the Global Address Book room, remove it
696 * from the directory as well.
698 void vcard_delete_remove(char *room, long msgnum) {
699 struct CtdlMessage *msg;
703 if (msgnum <= 0L) return;
705 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
709 msg = CtdlFetchMessage(msgnum);
710 if (msg == NULL) return;
712 ptr = msg->cm_fields['M'];
713 if (ptr == NULL) goto EOH;
714 while (ptr != NULL) {
715 linelen = strcspn(ptr, "\n");
716 if (linelen == 0) goto EOH;
718 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
719 /* Bingo! A vCard is being deleted.
721 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
723 ptr = strchr((char *)ptr, '\n');
724 if (ptr != NULL) ++ptr;
727 EOH: CtdlFreeMessage(msg);
735 void cmd_qdir(char *argbuf) {
736 char citadel_addr[SIZ];
737 char internet_addr[SIZ];
739 if (CtdlAccessCheck(ac_logged_in)) return;
741 extract(internet_addr, argbuf, 0);
743 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
744 cprintf("%d %s was not found.\n",
745 ERROR+NO_SUCH_USER, internet_addr);
749 cprintf("%d %s\n", CIT_OK, citadel_addr);
756 * Session startup, allocate some per-session data
758 void vcard_session_startup_hook(void) {
759 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
764 * When a user logs in...
766 void vcard_session_login_hook(void) {
769 v = vcard_get_user(&CC->usersupp);
770 vcard_populate_cs_inet_email(v);
777 * When a user logs out...
779 void vcard_session_logout_hook(void) {
780 if (CC->cs_inet_email != NULL) {
781 phree(CC->cs_inet_email);
782 CC->cs_inet_email = NULL;
787 char *Dynamic_Module_Init(void)
789 SYM_VCARD = CtdlGetDynamicSymbol();
790 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
791 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
792 CtdlRegisterSessionHook(vcard_session_logout_hook, EVT_LOGOUT);
793 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
794 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
795 CtdlRegisterDeleteHook(vcard_delete_remove);
796 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
797 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
798 CtdlRegisterProtoHook(cmd_igab, "IGAB",
799 "Initialize Global Address Book");
800 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
801 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
802 CtdlRegisterNetprocHook(vcard_extract_from_network);
803 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);