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 "serv_extensions.h"
63 #include "internet_addressing.h"
66 #include "serv_ldap.h"
68 struct vcard_internal_info {
72 /* Message number symbol used internally by these functions */
73 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
77 * set global flag calling for an aide to validate new users
79 void set_mm_valid(void) {
80 begin_critical_section(S_CONTROL);
82 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
84 end_critical_section(S_CONTROL);
90 * Extract Internet e-mail addresses from a message containing a vCard, and
91 * perform a callback for any found.
93 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
94 void (*callback)(char *, char *) ) {
98 char citadel_address[SIZ];
100 int found_something = 0;
102 if (msg->cm_fields['A'] == NULL) return;
103 if (msg->cm_fields['N'] == NULL) return;
104 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
105 msg->cm_fields['A'], msg->cm_fields['N']);
107 v = vcard_load(msg->cm_fields['M']);
108 if (v == NULL) return;
110 /* Go through the vCard searching for *all* instances of
111 * the "email;internet" key
114 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
118 if (strlen(addr) > 0) {
119 if (callback != NULL) {
120 callback(addr, citadel_address);
129 } while(found_something);
137 * Callback for vcard_add_to_directory()
138 * (Lotsa ugly nested callbacks. Oh well.)
139 * This little shim function makes sure we're not
141 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
144 /* We have to validate that we're not stepping on someone else's
145 * email address ... but only if we're logged in. Otherwise it's
146 * probably just the networker or something.
149 lprintf(9, "Checking for <%s>...\n", internet_addr);
150 if (CtdlDirectoryLookup(buf, internet_addr) == 0) {
151 if (strcasecmp(buf, citadel_addr)) {
152 /* This address belongs to someone else.
153 * Bail out silently without saving.
155 lprintf(9, "DOOP!\n");
160 lprintf(9, "ADDING!\n");
161 CtdlDirectoryAddUser(internet_addr, citadel_addr);
166 * Back end function for cmd_igab()
168 void vcard_add_to_directory(long msgnum, void *data) {
169 struct CtdlMessage *msg;
171 msg = CtdlFetchMessage(msgnum);
173 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
177 ctdl_vcard_to_ldap(msg, V2L_WRITE);
180 CtdlFreeMessage(msg);
185 * Initialize Global Adress Book
187 void cmd_igab(char *argbuf) {
188 char hold_rm[ROOMNAMELEN];
190 if (CtdlAccessCheck(ac_aide)) return;
192 strcpy(hold_rm, CC->room.QRname); /* save current room */
194 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
195 getroom(&CC->room, hold_rm);
196 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
200 /* Empty the existing database first.
204 /* We want *all* vCards in this room */
205 CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard",
206 NULL, vcard_add_to_directory, NULL);
208 getroom(&CC->room, hold_rm); /* return to saved room */
209 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
216 * See if there is a valid Internet address in a vCard to use for outbound
217 * Internet messages. If there is, stick it in CC->cs_inet_email.
219 void vcard_populate_cs_inet_email(struct vCard *v) {
221 int continue_searching = 1;
224 /* Go through the vCard searching for *all* instances of
225 * the "email;internet" key
228 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
230 continue_searching = 1;
233 if (strlen(addr) > 0) {
234 if (IsDirectory(addr)) {
235 continue_searching = 0;
236 safestrncpy(CC->cs_inet_email,
238 sizeof(CC->cs_inet_email)
245 continue_searching = 0;
247 } while(continue_searching);
253 * This handler detects whether the user is attempting to save a new
254 * vCard as part of his/her personal configuration, and handles the replace
255 * function accordingly (delete the user's existing vCard in the config room
256 * and in the global address book).
258 int vcard_upload_beforesave(struct CtdlMessage *msg) {
262 struct ctdluser usbuf;
265 if (!CC->logged_in) return(0); /* Only do this if logged in. */
267 /* If this isn't a "My Citadel Config" room, don't bother. */
268 if ( (CC->room.QRflags && QR_MAILBOX)
269 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
270 /* Yes, we want to do this */
276 /* If this isn't a MIME message, don't bother. */
277 if (msg->cm_format_type != 4) return(0);
279 /* Ok, if we got this far, look into the situation further... */
281 ptr = msg->cm_fields['M'];
282 if (ptr == NULL) return(0);
283 while (ptr != NULL) {
285 linelen = strcspn(ptr, "\n");
286 if (linelen == 0) return(0); /* end of headers */
288 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
289 /* Bingo! The user is uploading a new vCard, so
290 * delete the old one. First, figure out which user
291 * is being re-registered...
293 what_user = atol(CC->room.QRname);
295 if (what_user == CC->user.usernum) {
296 /* It's the logged in user. That was easy. */
297 memcpy(&usbuf, &CC->user,
298 sizeof(struct ctdluser) );
301 else if (getuserbynumber(&usbuf, what_user) == 0) {
302 /* We fetched a valid user record */
306 /* No user with that number! */
310 /* Delete the user's old vCard. This would probably
311 * get taken care of by the replication check, but we
312 * want to make sure there is absolutely only one
313 * vCard in the user's config room at all times.
315 CtdlDeleteMessages(CC->room.QRname,
318 /* Set the Extended-ID to a standardized one so the
319 * replication always works correctly
321 if (msg->cm_fields['E'] != NULL)
322 phree(msg->cm_fields['E']);
324 if (msg->cm_fields['A'] != NULL)
325 phree(msg->cm_fields['A']);
327 msg->cm_fields['A'] = strdoop(usbuf.fullname);
329 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
330 msg->cm_fields['A'], NODENAME);
331 msg->cm_fields['E'] = strdoop(buf);
333 /* Now allow the save to complete. */
337 ptr = strchr((char *)ptr, '\n');
338 if (ptr != NULL) ++ptr;
347 * This handler detects whether the user is attempting to save a new
348 * vCard as part of his/her personal configuration, and handles the replace
349 * function accordingly (copy the vCard from the config room to the global
352 int vcard_upload_aftersave(struct CtdlMessage *msg) {
358 if (!CC->logged_in) return(0); /* Only do this if logged in. */
360 /* If this isn't the configuration room, or if this isn't a MIME
361 * message, don't bother.
363 if (msg->cm_fields['O'] == NULL) return(0);
364 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
365 if (msg->cm_format_type != 4) return(0);
367 ptr = msg->cm_fields['M'];
368 if (ptr == NULL) return(0);
369 while (ptr != NULL) {
371 linelen = strcspn(ptr, "\n");
372 if (linelen == 0) return(0); /* end of headers */
374 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
375 /* Bingo! The user is uploading a new vCard, so
376 * copy it to the Global Address Book room.
379 I = atol(msg->cm_fields['I']);
380 if (I < 0L) return(0);
382 /* Put it in the Global Address Book room... */
383 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
384 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
386 /* ...and also in the directory database. */
387 vcard_add_to_directory(I, NULL);
389 /* Store our Internet return address in memory */
390 v = vcard_load(msg->cm_fields['M']);
391 vcard_populate_cs_inet_email(v);
394 /* Some sites want an Aide to be notified when a
395 * user registers or re-registers...
399 /* ...which also means we need to flag the user */
400 lgetuser(&CC->user, CC->curr_user);
401 CC->user.flags |= (US_REGIS|US_NEEDVALID);
407 ptr = strchr((char *)ptr, '\n');
408 if (ptr != NULL) ++ptr;
417 * back end function used for callbacks
419 void vcard_gu_backend(long msgnum, void *userdata) {
425 * If this user has a vcard on disk, read it into memory, otherwise allocate
426 * and return an empty vCard.
428 struct vCard *vcard_get_user(struct ctdluser *u) {
429 char hold_rm[ROOMNAMELEN];
430 char config_rm[ROOMNAMELEN];
431 struct CtdlMessage *msg;
434 strcpy(hold_rm, CC->room.QRname); /* save current room */
435 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
437 if (getroom(&CC->room, config_rm) != 0) {
438 getroom(&CC->room, hold_rm);
442 /* We want the last (and probably only) vcard in this room */
444 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
445 NULL, vcard_gu_backend, NULL);
446 getroom(&CC->room, hold_rm); /* return to saved room */
448 if (VC->msgnum < 0L) return vcard_new();
450 msg = CtdlFetchMessage(VC->msgnum);
451 if (msg == NULL) return vcard_new();
453 v = vcard_load(msg->cm_fields['M']);
454 CtdlFreeMessage(msg);
460 * Store this user's vCard in the appropriate place
463 * Write our config to disk
465 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
470 strcpy(temp, tmpnam(NULL));
471 ser = vcard_serialize(v);
473 fp = fopen(temp, "w");
474 if (fp == NULL) return;
476 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
478 fwrite(ser, strlen(ser), 1, fp);
483 /* This handy API function does all the work for us.
484 * NOTE: normally we would want to set that last argument to 1, to
485 * force the system to delete the user's old vCard. But it doesn't
486 * have to, because the vcard_upload_beforesave() hook above
487 * is going to notice what we're trying to do, and delete the old vCard.
489 CtdlWriteObject(USERCONFIGROOM, /* which room */
490 "text/x-vcard", /* MIME type */
491 temp, /* temp file */
494 0, /* don't delete others of this type */
503 * Old style "enter registration info" command. This function simply honors
504 * the REGI protocol command, translates the entered parameters into a vCard,
505 * and enters the vCard into the user's configuration.
507 void cmd_regi(char *argbuf) {
510 struct vCard *my_vcard;
516 char tmpaddress[SIZ];
517 char tmpcountry[SIZ];
519 if (!(CC->logged_in)) {
520 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
524 my_vcard = vcard_get_user(&CC->user);
527 strcpy(tmpstate, "");
529 strcpy(tmpcountry, "USA");
531 cprintf("%d Send registration...\n", SEND_LISTING);
533 while (client_gets(buf), strcmp(buf,"000")) {
534 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
535 if (a==1) strcpy(tmpaddr, buf);
536 if (a==2) strcpy(tmpcity, buf);
537 if (a==3) strcpy(tmpstate, buf);
539 for (c=0; c<strlen(buf); ++c) {
540 if ((buf[c]>='0') && (buf[c]<='9')) {
547 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
548 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
549 if (a==7) strcpy(tmpcountry, buf);
553 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
554 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
555 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
556 vcard_write_user(&CC->user, my_vcard);
557 vcard_free(my_vcard);
562 * Protocol command to fetch registration info for a user
564 void cmd_greg(char *argbuf)
566 struct ctdluser usbuf;
573 extract(who, argbuf, 0);
575 if (!(CC->logged_in)) {
576 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
580 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
582 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
583 cprintf("%d Higher access required.\n",
584 ERROR + HIGHER_ACCESS_REQUIRED);
588 if (getuser(&usbuf, who) != 0) {
589 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
593 v = vcard_get_user(&usbuf);
595 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
596 cprintf("%ld\n", usbuf.usernum);
597 cprintf("%s\n", usbuf.password);
598 s = vcard_get_prop(v, "n", 0, 0, 0);
599 cprintf("%s\n", s ? s : " "); /* name */
601 s = vcard_get_prop(v, "adr", 0, 0, 0);
602 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
604 extract_token(buf, adr, 2, ';');
605 cprintf("%s\n", buf); /* street */
606 extract_token(buf, adr, 3, ';');
607 cprintf("%s\n", buf); /* city */
608 extract_token(buf, adr, 4, ';');
609 cprintf("%s\n", buf); /* state */
610 extract_token(buf, adr, 5, ';');
611 cprintf("%s\n", buf); /* zip */
613 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
614 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
622 cprintf("%d\n", usbuf.axlevel);
624 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
625 cprintf("%s\n", s ? s : " ");
626 s = vcard_get_prop(v, "adr", 0, 0, 0);
627 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
629 extract_token(buf, adr, 6, ';');
630 cprintf("%s\n", buf); /* country */
636 * When a user is being deleted, we have to remove his/her vCard.
637 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
638 * field, and the same Extended ID as the existing card.
640 void vcard_purge(char *username, long usernum) {
641 struct CtdlMessage *msg;
644 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
645 if (msg == NULL) return;
646 memset(msg, 0, sizeof(struct CtdlMessage));
648 msg->cm_magic = CTDLMESSAGE_MAGIC;
649 msg->cm_anon_type = MES_NORMAL;
650 msg->cm_format_type = 0;
651 msg->cm_fields['A'] = strdoop(username);
652 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
653 msg->cm_fields['N'] = strdoop(NODENAME);
654 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
656 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
657 msg->cm_fields['A'], NODENAME);
658 msg->cm_fields['E'] = strdoop(buf);
660 msg->cm_fields['S'] = strdoop("CANCEL");
662 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
663 CtdlFreeMessage(msg);
668 * Grab vCard directory stuff out of incoming network messages
670 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
674 if (msg == NULL) return(0);
676 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
680 if (msg->cm_format_type != 4) return(0);
682 ptr = msg->cm_fields['M'];
683 if (ptr == NULL) return(0);
684 while (ptr != NULL) {
686 linelen = strcspn(ptr, "\n");
687 if (linelen == 0) return(0); /* end of headers */
689 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
690 /* It's a vCard. Add it to the directory. */
691 vcard_extract_internet_addresses(msg,
692 CtdlDirectoryAddUser);
696 ptr = strchr((char *)ptr, '\n');
697 if (ptr != NULL) ++ptr;
706 * When a vCard is being removed from the Global Address Book room, remove it
707 * from the directory as well.
709 void vcard_delete_remove(char *room, long msgnum) {
710 struct CtdlMessage *msg;
714 if (msgnum <= 0L) return;
716 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
720 msg = CtdlFetchMessage(msgnum);
721 if (msg == NULL) return;
723 ptr = msg->cm_fields['M'];
724 if (ptr == NULL) goto EOH;
725 while (ptr != NULL) {
726 linelen = strcspn(ptr, "\n");
727 if (linelen == 0) goto EOH;
729 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
730 /* Bingo! A vCard is being deleted.
732 vcard_extract_internet_addresses(msg,
733 CtdlDirectoryDelUser);
735 ctdl_vcard_to_ldap(msg, V2L_DELETE);
738 ptr = strchr((char *)ptr, '\n');
739 if (ptr != NULL) ++ptr;
742 EOH: CtdlFreeMessage(msg);
750 void cmd_qdir(char *argbuf) {
751 char citadel_addr[SIZ];
752 char internet_addr[SIZ];
754 if (CtdlAccessCheck(ac_logged_in)) return;
756 extract(internet_addr, argbuf, 0);
758 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
759 cprintf("%d %s was not found.\n",
760 ERROR + NO_SUCH_USER, internet_addr);
764 cprintf("%d %s\n", CIT_OK, citadel_addr);
769 * We don't know if the Contacts room exists so we just create it at login
771 void vcard_create_room(void)
776 /* Create the calendar room if it doesn't already exist */
777 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0);
779 /* Set expiration policy to manual; otherwise objects will be lost! */
780 if (lgetroom(&qr, USERCONTACTSROOM)) {
781 lprintf(3, "Couldn't get the user CONTACTS room!\n");
784 qr.QRep.expire_mode = EXPIRE_MANUAL;
785 qr.QRdefaultview = 2; /* 2 = address book view */
788 /* Set the view to a calendar view */
789 CtdlGetRelationship(&vbuf, &CC->user, &qr);
790 vbuf.v_view = 2; /* 2 = address book view */
791 CtdlSetRelationship(&vbuf, &CC->user, &qr);
800 * Session startup, allocate some per-session data
802 void vcard_session_startup_hook(void) {
803 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
808 * When a user logs in...
810 void vcard_session_login_hook(void) {
813 v = vcard_get_user(&CC->user);
814 vcard_populate_cs_inet_email(v);
823 char *serv_vcard_init(void)
827 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
828 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
829 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
830 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
831 CtdlRegisterDeleteHook(vcard_delete_remove);
832 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
833 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
834 CtdlRegisterProtoHook(cmd_igab, "IGAB",
835 "Initialize Global Address Book");
836 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
837 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
838 CtdlRegisterNetprocHook(vcard_extract_from_network);
840 /* Create the Global ADdress Book room if necessary */
841 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0);
843 /* Set expiration policy to manual; otherwise objects will be lost! */
844 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
845 qr.QRep.expire_mode = EXPIRE_MANUAL;
846 qr.QRdefaultview = 2; /* 2 = address book view */