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"
67 struct vcard_internal_info {
71 /* Message number symbol used internally by these functions */
72 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
76 * set global flag calling for an aide to validate new users
78 void set_mm_valid(void) {
79 begin_critical_section(S_CONTROL);
81 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
83 end_critical_section(S_CONTROL);
89 * Extract Internet e-mail addresses from a message containing a vCard, and
90 * perform a callback for any found.
92 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
93 void (*callback)(char *, char *) ) {
97 char citadel_address[SIZ];
99 int found_something = 0;
101 if (msg->cm_fields['A'] == NULL) return;
102 if (msg->cm_fields['N'] == NULL) return;
103 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
104 msg->cm_fields['A'], msg->cm_fields['N']);
106 v = vcard_load(msg->cm_fields['M']);
107 if (v == NULL) return;
109 /* Go through the vCard searching for *all* instances of
110 * the "email;internet" key
113 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
117 if (strlen(addr) > 0) {
118 if (callback != NULL) {
119 callback(addr, citadel_address);
128 } while(found_something);
136 * Callback for vcard_add_to_directory()
137 * (Lotsa ugly nested callbacks. Oh well.)
138 * This little shim function makes sure we're not
140 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
143 /* We have to validate that we're not stepping on someone else's
144 * email address ... but only if we're logged in. Otherwise it's
145 * probably just the networker or something.
148 lprintf(9, "Checking for <%s>...\n", internet_addr);
149 if (CtdlDirectoryLookup(buf, internet_addr) == 0) {
150 if (strcasecmp(buf, citadel_addr)) {
151 /* This address belongs to someone else.
152 * Bail out silently without saving.
154 lprintf(9, "DOOP!\n");
159 lprintf(9, "ADDING!\n");
160 CtdlDirectoryAddUser(internet_addr, citadel_addr);
165 * Back end function for cmd_igab()
167 void vcard_add_to_directory(long msgnum, void *data) {
168 struct CtdlMessage *msg;
170 msg = CtdlFetchMessage(msgnum);
172 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
175 CtdlFreeMessage(msg);
180 * Initialize Global Adress Book
182 void cmd_igab(char *argbuf) {
183 char hold_rm[ROOMNAMELEN];
185 if (CtdlAccessCheck(ac_aide)) return;
187 strcpy(hold_rm, CC->room.QRname); /* save current room */
189 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
190 getroom(&CC->room, hold_rm);
191 cprintf("%d cannot get address book room\n", ERROR);
195 /* Empty the existing database first.
199 /* We want the last (and probably only) vcard in this room */
200 CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard",
201 NULL, vcard_add_to_directory, NULL);
203 getroom(&CC->room, hold_rm); /* return to saved room */
204 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
211 * See if there is a valid Internet address in a vCard to use for outbound
212 * Internet messages. If there is, stick it in CC->cs_inet_email.
214 void vcard_populate_cs_inet_email(struct vCard *v) {
216 int continue_searching = 1;
219 /* Go through the vCard searching for *all* instances of
220 * the "email;internet" key
223 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
225 continue_searching = 1;
228 if (strlen(addr) > 0) {
229 if (IsDirectory(addr)) {
230 continue_searching = 0;
231 safestrncpy(CC->cs_inet_email,
233 sizeof(CC->cs_inet_email)
240 continue_searching = 0;
242 } while(continue_searching);
248 * This handler detects whether the user is attempting to save a new
249 * vCard as part of his/her personal configuration, and handles the replace
250 * function accordingly (delete the user's existing vCard in the config room
251 * and in the global address book).
253 int vcard_upload_beforesave(struct CtdlMessage *msg) {
257 struct ctdluser usbuf;
260 if (!CC->logged_in) return(0); /* Only do this if logged in. */
262 /* If this isn't a "My Citadel Config" room, don't bother. */
263 if ( (CC->room.QRflags && QR_MAILBOX)
264 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
265 /* Yes, we want to do this */
271 /* If this isn't a MIME message, don't bother. */
272 if (msg->cm_format_type != 4) return(0);
274 /* Ok, if we got this far, look into the situation further... */
276 ptr = msg->cm_fields['M'];
277 if (ptr == NULL) return(0);
278 while (ptr != NULL) {
280 linelen = strcspn(ptr, "\n");
281 if (linelen == 0) return(0); /* end of headers */
283 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
284 /* Bingo! The user is uploading a new vCard, so
285 * delete the old one. First, figure out which user
286 * is being re-registered...
288 what_user = atol(CC->room.QRname);
290 if (what_user == CC->user.usernum) {
291 /* It's the logged in user. That was easy. */
292 memcpy(&usbuf, &CC->user,
293 sizeof(struct ctdluser) );
296 else if (getuserbynumber(&usbuf, what_user) == 0) {
297 /* We fetched a valid user record */
301 /* No user with that number! */
305 /* Delete the user's old vCard. This would probably
306 * get taken care of by the replication check, but we
307 * want to make sure there is absolutely only one
308 * vCard in the user's config room at all times.
310 CtdlDeleteMessages(CC->room.QRname,
313 /* Set the Extended-ID to a standardized one so the
314 * replication always works correctly
316 if (msg->cm_fields['E'] != NULL)
317 phree(msg->cm_fields['E']);
319 if (msg->cm_fields['A'] != NULL)
320 phree(msg->cm_fields['A']);
322 msg->cm_fields['A'] = strdoop(usbuf.fullname);
324 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
325 msg->cm_fields['A'], NODENAME);
326 msg->cm_fields['E'] = strdoop(buf);
328 /* Now allow the save to complete. */
332 ptr = strchr((char *)ptr, '\n');
333 if (ptr != NULL) ++ptr;
342 * This handler detects whether the user is attempting to save a new
343 * vCard as part of his/her personal configuration, and handles the replace
344 * function accordingly (copy the vCard from the config room to the global
347 int vcard_upload_aftersave(struct CtdlMessage *msg) {
353 if (!CC->logged_in) return(0); /* Only do this if logged in. */
355 /* If this isn't the configuration room, or if this isn't a MIME
356 * message, don't bother.
358 if (msg->cm_fields['O'] == NULL) return(0);
359 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
360 if (msg->cm_format_type != 4) return(0);
362 ptr = msg->cm_fields['M'];
363 if (ptr == NULL) return(0);
364 while (ptr != NULL) {
366 linelen = strcspn(ptr, "\n");
367 if (linelen == 0) return(0); /* end of headers */
369 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
370 /* Bingo! The user is uploading a new vCard, so
371 * copy it to the Global Address Book room.
374 I = atol(msg->cm_fields['I']);
375 if (I < 0L) return(0);
377 /* Put it in the Global Address Book room... */
378 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
379 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
381 /* ...and also in the directory database. */
382 vcard_add_to_directory(I, NULL);
384 /* Store our Internet return address in memory */
385 v = vcard_load(msg->cm_fields['M']);
386 vcard_populate_cs_inet_email(v);
389 /* Some sites want an Aide to be notified when a
390 * user registers or re-registers...
394 /* ...which also means we need to flag the user */
395 lgetuser(&CC->user, CC->curr_user);
396 CC->user.flags |= (US_REGIS|US_NEEDVALID);
402 ptr = strchr((char *)ptr, '\n');
403 if (ptr != NULL) ++ptr;
412 * back end function used for callbacks
414 void vcard_gu_backend(long msgnum, void *userdata) {
420 * If this user has a vcard on disk, read it into memory, otherwise allocate
421 * and return an empty vCard.
423 struct vCard *vcard_get_user(struct ctdluser *u) {
424 char hold_rm[ROOMNAMELEN];
425 char config_rm[ROOMNAMELEN];
426 struct CtdlMessage *msg;
429 strcpy(hold_rm, CC->room.QRname); /* save current room */
430 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
432 if (getroom(&CC->room, config_rm) != 0) {
433 getroom(&CC->room, hold_rm);
437 /* We want the last (and probably only) vcard in this room */
439 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
440 NULL, vcard_gu_backend, NULL);
441 getroom(&CC->room, hold_rm); /* return to saved room */
443 if (VC->msgnum < 0L) return vcard_new();
445 msg = CtdlFetchMessage(VC->msgnum);
446 if (msg == NULL) return vcard_new();
448 v = vcard_load(msg->cm_fields['M']);
449 CtdlFreeMessage(msg);
455 * Store this user's vCard in the appropriate place
458 * Write our config to disk
460 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
465 strcpy(temp, tmpnam(NULL));
466 ser = vcard_serialize(v);
468 fp = fopen(temp, "w");
469 if (fp == NULL) return;
471 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
473 fwrite(ser, strlen(ser), 1, fp);
478 /* This handy API function does all the work for us.
479 * NOTE: normally we would want to set that last argument to 1, to
480 * force the system to delete the user's old vCard. But it doesn't
481 * have to, because the vcard_upload_beforesave() hook above
482 * is going to notice what we're trying to do, and delete the old vCard.
484 CtdlWriteObject(USERCONFIGROOM, /* which room */
485 "text/x-vcard", /* MIME type */
486 temp, /* temp file */
489 0, /* don't delete others of this type */
498 * Old style "enter registration info" command. This function simply honors
499 * the REGI protocol command, translates the entered parameters into a vCard,
500 * and enters the vCard into the user's configuration.
502 void cmd_regi(char *argbuf) {
505 struct vCard *my_vcard;
511 char tmpaddress[SIZ];
512 char tmpcountry[SIZ];
514 if (!(CC->logged_in)) {
515 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
519 my_vcard = vcard_get_user(&CC->user);
522 strcpy(tmpstate, "");
524 strcpy(tmpcountry, "USA");
526 cprintf("%d Send registration...\n", SEND_LISTING);
528 while (client_gets(buf), strcmp(buf,"000")) {
529 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
530 if (a==1) strcpy(tmpaddr, buf);
531 if (a==2) strcpy(tmpcity, buf);
532 if (a==3) strcpy(tmpstate, buf);
534 for (c=0; c<strlen(buf); ++c) {
535 if ((buf[c]>='0') && (buf[c]<='9')) {
542 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
543 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
544 if (a==7) strcpy(tmpcountry, buf);
548 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
549 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
550 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
551 vcard_write_user(&CC->user, my_vcard);
552 vcard_free(my_vcard);
557 * Protocol command to fetch registration info for a user
559 void cmd_greg(char *argbuf)
561 struct ctdluser usbuf;
568 extract(who, argbuf, 0);
570 if (!(CC->logged_in)) {
571 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
575 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
577 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
578 cprintf("%d Higher access required.\n",
579 ERROR+HIGHER_ACCESS_REQUIRED);
583 if (getuser(&usbuf, who) != 0) {
584 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
588 v = vcard_get_user(&usbuf);
590 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
591 cprintf("%ld\n", usbuf.usernum);
592 cprintf("%s\n", usbuf.password);
593 s = vcard_get_prop(v, "n", 0, 0, 0);
594 cprintf("%s\n", s ? s : " "); /* name */
596 s = vcard_get_prop(v, "adr", 0, 0, 0);
597 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
599 extract_token(buf, adr, 2, ';');
600 cprintf("%s\n", buf); /* street */
601 extract_token(buf, adr, 3, ';');
602 cprintf("%s\n", buf); /* city */
603 extract_token(buf, adr, 4, ';');
604 cprintf("%s\n", buf); /* state */
605 extract_token(buf, adr, 5, ';');
606 cprintf("%s\n", buf); /* zip */
608 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
609 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
617 cprintf("%d\n", usbuf.axlevel);
619 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
620 cprintf("%s\n", s ? s : " ");
621 s = vcard_get_prop(v, "adr", 0, 0, 0);
622 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
624 extract_token(buf, adr, 6, ';');
625 cprintf("%s\n", buf); /* country */
631 * When a user is being deleted, we have to remove his/her vCard.
632 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
633 * field, and the same Extended ID as the existing card.
635 void vcard_purge(char *username, long usernum) {
636 struct CtdlMessage *msg;
639 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
640 if (msg == NULL) return;
641 memset(msg, 0, sizeof(struct CtdlMessage));
643 msg->cm_magic = CTDLMESSAGE_MAGIC;
644 msg->cm_anon_type = MES_NORMAL;
645 msg->cm_format_type = 0;
646 msg->cm_fields['A'] = strdoop(username);
647 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
648 msg->cm_fields['N'] = strdoop(NODENAME);
649 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
651 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
652 msg->cm_fields['A'], NODENAME);
653 msg->cm_fields['E'] = strdoop(buf);
655 msg->cm_fields['S'] = strdoop("CANCEL");
657 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
658 CtdlFreeMessage(msg);
663 * Grab vCard directory stuff out of incoming network messages
665 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
669 if (msg == NULL) return(0);
671 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
675 if (msg->cm_format_type != 4) return(0);
677 ptr = msg->cm_fields['M'];
678 if (ptr == NULL) return(0);
679 while (ptr != NULL) {
681 linelen = strcspn(ptr, "\n");
682 if (linelen == 0) return(0); /* end of headers */
684 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
685 /* It's a vCard. Add it to the directory. */
686 vcard_extract_internet_addresses(msg,
687 CtdlDirectoryAddUser);
691 ptr = strchr((char *)ptr, '\n');
692 if (ptr != NULL) ++ptr;
701 * When a vCard is being removed from the Global Address Book room, remove it
702 * from the directory as well.
704 void vcard_delete_remove(char *room, long msgnum) {
705 struct CtdlMessage *msg;
709 if (msgnum <= 0L) return;
711 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
715 msg = CtdlFetchMessage(msgnum);
716 if (msg == NULL) return;
718 ptr = msg->cm_fields['M'];
719 if (ptr == NULL) goto EOH;
720 while (ptr != NULL) {
721 linelen = strcspn(ptr, "\n");
722 if (linelen == 0) goto EOH;
724 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
725 /* Bingo! A vCard is being deleted.
727 vcard_extract_internet_addresses(msg,
728 CtdlDirectoryDelUser);
730 ptr = strchr((char *)ptr, '\n');
731 if (ptr != NULL) ++ptr;
734 EOH: CtdlFreeMessage(msg);
742 void cmd_qdir(char *argbuf) {
743 char citadel_addr[SIZ];
744 char internet_addr[SIZ];
746 if (CtdlAccessCheck(ac_logged_in)) return;
748 extract(internet_addr, argbuf, 0);
750 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
751 cprintf("%d %s was not found.\n",
752 ERROR+NO_SUCH_USER, internet_addr);
756 cprintf("%d %s\n", CIT_OK, citadel_addr);
763 * Session startup, allocate some per-session data
765 void vcard_session_startup_hook(void) {
766 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
771 * When a user logs in...
773 void vcard_session_login_hook(void) {
776 v = vcard_get_user(&CC->user);
777 vcard_populate_cs_inet_email(v);
784 char *serv_vcard_init(void)
786 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
787 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
788 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
789 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
790 CtdlRegisterDeleteHook(vcard_delete_remove);
791 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
792 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
793 CtdlRegisterProtoHook(cmd_igab, "IGAB",
794 "Initialize Global Address Book");
795 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
796 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
797 CtdlRegisterNetprocHook(vcard_extract_from_network);
798 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0);