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, "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 * Internet 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;
207 /* Go through the vCard searching for *all* instances of
208 * the "email;internet" key
211 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
213 continue_searching = 1;
216 if (strlen(addr) > 0) {
217 if (IsDirectory(addr)) {
218 continue_searching = 0;
219 safestrncpy(CC->cs_inet_email,
221 sizeof(CC->cs_inet_email)
228 continue_searching = 0;
230 } while(continue_searching);
236 * This handler detects whether the user is attempting to save a new
237 * vCard as part of his/her personal configuration, and handles the replace
238 * function accordingly (delete the user's existing vCard in the config room
239 * and in the global address book).
241 int vcard_upload_beforesave(struct CtdlMessage *msg) {
245 struct usersupp usbuf;
248 if (!CC->logged_in) return(0); /* Only do this if logged in. */
250 /* If this isn't a "My Citadel Config" room, don't bother. */
251 if ( (CC->quickroom.QRflags && QR_MAILBOX)
252 && (!strcasecmp(&CC->quickroom.QRname[11], USERCONFIGROOM)) ) {
253 /* Yes, we want to do this */
259 /* If this isn't a MIME message, don't bother. */
260 if (msg->cm_format_type != 4) return(0);
262 /* Ok, if we got this far, look into the situation further... */
264 ptr = msg->cm_fields['M'];
265 if (ptr == NULL) return(0);
266 while (ptr != NULL) {
268 linelen = strcspn(ptr, "\n");
269 if (linelen == 0) return(0); /* end of headers */
271 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
272 /* Bingo! The user is uploading a new vCard, so
273 * delete the old one. First, figure out which user
274 * is being re-registered...
276 what_user = atol(CC->quickroom.QRname);
278 if (what_user == CC->usersupp.usernum) {
279 /* It's the logged in user. That was easy. */
280 memcpy(&usbuf, &CC->usersupp,
281 sizeof(struct usersupp) );
284 else if (getuserbynumber(&usbuf, what_user) == 0) {
285 /* We fetched a valid user record */
289 /* No user with that number! */
293 /* Delete the user's old vCard. This would probably
294 * get taken care of by the replication check, but we
295 * want to make sure there is absolutely only one
296 * vCard in the user's config room at all times.
298 CtdlDeleteMessages(CC->quickroom.QRname,
301 /* Set the Extended-ID to a standardized one so the
302 * replication always works correctly
304 if (msg->cm_fields['E'] != NULL)
305 phree(msg->cm_fields['E']);
307 if (msg->cm_fields['A'] != NULL)
308 phree(msg->cm_fields['A']);
310 msg->cm_fields['A'] = strdoop(usbuf.fullname);
312 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
313 msg->cm_fields['A'], NODENAME);
314 msg->cm_fields['E'] = strdoop(buf);
316 /* Now allow the save to complete. */
320 ptr = strchr((char *)ptr, '\n');
321 if (ptr != NULL) ++ptr;
330 * This handler detects whether the user is attempting to save a new
331 * vCard as part of his/her personal configuration, and handles the replace
332 * function accordingly (copy the vCard from the config room to the global
335 int vcard_upload_aftersave(struct CtdlMessage *msg) {
341 if (!CC->logged_in) return(0); /* Only do this if logged in. */
343 /* If this isn't the configuration room, or if this isn't a MIME
344 * message, don't bother.
346 if (msg->cm_fields['O'] == NULL) return(0);
347 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
348 if (msg->cm_format_type != 4) return(0);
350 ptr = msg->cm_fields['M'];
351 if (ptr == NULL) return(0);
352 while (ptr != NULL) {
354 linelen = strcspn(ptr, "\n");
355 if (linelen == 0) return(0); /* end of headers */
357 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
358 /* Bingo! The user is uploading a new vCard, so
359 * copy it to the Global Address Book room.
362 I = atol(msg->cm_fields['I']);
363 if (I < 0L) return(0);
365 /* Put it in the Global Address Book room... */
366 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
367 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
369 /* ...and also in the directory database. */
370 vcard_add_to_directory(I, NULL);
372 /* Store our Internet return address in memory */
373 v = vcard_load(msg->cm_fields['M']);
374 vcard_populate_cs_inet_email(v);
380 ptr = strchr((char *)ptr, '\n');
381 if (ptr != NULL) ++ptr;
390 * back end function used for callbacks
392 void vcard_gu_backend(long msgnum, void *userdata) {
398 * If this user has a vcard on disk, read it into memory, otherwise allocate
399 * and return an empty vCard.
401 struct vCard *vcard_get_user(struct usersupp *u) {
402 char hold_rm[ROOMNAMELEN];
403 char config_rm[ROOMNAMELEN];
404 struct CtdlMessage *msg;
407 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
408 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
410 if (getroom(&CC->quickroom, config_rm) != 0) {
411 getroom(&CC->quickroom, hold_rm);
415 /* We want the last (and probably only) vcard in this room */
417 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
418 NULL, vcard_gu_backend, NULL);
419 getroom(&CC->quickroom, hold_rm); /* return to saved room */
421 if (VC->msgnum < 0L) return vcard_new();
423 msg = CtdlFetchMessage(VC->msgnum);
424 if (msg == NULL) return vcard_new();
426 v = vcard_load(msg->cm_fields['M']);
427 CtdlFreeMessage(msg);
433 * Store this user's vCard in the appropriate place
436 * Write our config to disk
438 void vcard_write_user(struct usersupp *u, struct vCard *v) {
443 strcpy(temp, tmpnam(NULL));
444 ser = vcard_serialize(v);
446 fp = fopen(temp, "w");
447 if (fp == NULL) return;
449 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
451 fwrite(ser, strlen(ser), 1, fp);
456 /* This handy API function does all the work for us.
457 * NOTE: normally we would want to set that last argument to 1, to
458 * force the system to delete the user's old vCard. But it doesn't
459 * have to, because the vcard_upload_beforesave() hook above
460 * is going to notice what we're trying to do, and delete the old vCard.
462 CtdlWriteObject(USERCONFIGROOM, /* which room */
463 "text/x-vcard", /* MIME type */
464 temp, /* temp file */
467 0, /* don't delete others of this type */
476 * Old style "enter registration info" command. This function simply honors
477 * the REGI protocol command, translates the entered parameters into a vCard,
478 * and enters the vCard into the user's configuration.
480 void cmd_regi(char *argbuf) {
483 struct vCard *my_vcard;
489 char tmpaddress[SIZ];
490 char tmpcountry[SIZ];
492 if (!(CC->logged_in)) {
493 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
497 my_vcard = vcard_get_user(&CC->usersupp);
500 strcpy(tmpstate, "");
502 strcpy(tmpcountry, "USA");
504 cprintf("%d Send registration...\n", SEND_LISTING);
506 while (client_gets(buf), strcmp(buf,"000")) {
507 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
508 if (a==1) strcpy(tmpaddr, buf);
509 if (a==2) strcpy(tmpcity, buf);
510 if (a==3) strcpy(tmpstate, buf);
512 for (c=0; c<strlen(buf); ++c) {
513 if ((buf[c]>='0') && (buf[c]<='9')) {
520 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
521 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
522 if (a==7) strcpy(tmpcountry, buf);
526 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
527 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
528 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
529 vcard_write_user(&CC->usersupp, my_vcard);
530 vcard_free(my_vcard);
532 lgetuser(&CC->usersupp, CC->curr_user);
533 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
534 lputuser(&CC->usersupp);
536 /* set global flag calling for validation */
537 begin_critical_section(S_CONTROL);
539 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
541 end_critical_section(S_CONTROL);
547 * Protocol command to fetch registration info for a user
549 void cmd_greg(char *argbuf)
551 struct usersupp usbuf;
558 extract(who, argbuf, 0);
560 if (!(CC->logged_in)) {
561 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
565 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
567 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
568 cprintf("%d Higher access required.\n",
569 ERROR+HIGHER_ACCESS_REQUIRED);
573 if (getuser(&usbuf, who) != 0) {
574 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
578 v = vcard_get_user(&usbuf);
580 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
581 cprintf("%ld\n", usbuf.usernum);
582 cprintf("%s\n", usbuf.password);
583 s = vcard_get_prop(v, "n", 0, 0, 0);
584 cprintf("%s\n", s ? s : " "); /* name */
586 s = vcard_get_prop(v, "adr", 0, 0, 0);
587 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
589 extract_token(buf, adr, 2, ';');
590 cprintf("%s\n", buf); /* street */
591 extract_token(buf, adr, 3, ';');
592 cprintf("%s\n", buf); /* city */
593 extract_token(buf, adr, 4, ';');
594 cprintf("%s\n", buf); /* state */
595 extract_token(buf, adr, 5, ';');
596 cprintf("%s\n", buf); /* zip */
598 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
599 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
607 cprintf("%d\n", usbuf.axlevel);
609 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
610 cprintf("%s\n", s ? s : " ");
611 s = vcard_get_prop(v, "adr", 0, 0, 0);
612 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
614 extract_token(buf, adr, 6, ';');
615 cprintf("%s\n", buf); /* country */
621 * When a user is being deleted, we have to remove his/her vCard.
622 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
623 * field, and the same Extended ID as the existing card.
625 void vcard_purge(char *username, long usernum) {
626 struct CtdlMessage *msg;
629 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
630 if (msg == NULL) return;
631 memset(msg, 0, sizeof(struct CtdlMessage));
633 msg->cm_magic = CTDLMESSAGE_MAGIC;
634 msg->cm_anon_type = MES_NORMAL;
635 msg->cm_format_type = 0;
636 msg->cm_fields['A'] = strdoop(username);
637 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
638 msg->cm_fields['N'] = strdoop(NODENAME);
639 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
641 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
642 msg->cm_fields['A'], NODENAME);
643 msg->cm_fields['E'] = strdoop(buf);
645 msg->cm_fields['S'] = strdoop("CANCEL");
647 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
648 CtdlFreeMessage(msg);
653 * Grab vCard directory stuff out of incoming network messages
655 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
659 if (msg == NULL) return(0);
661 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
665 if (msg->cm_format_type != 4) return(0);
667 ptr = msg->cm_fields['M'];
668 if (ptr == NULL) return(0);
669 while (ptr != NULL) {
671 linelen = strcspn(ptr, "\n");
672 if (linelen == 0) return(0); /* end of headers */
674 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
675 /* It's a vCard. Add it to the directory. */
676 vcard_extract_internet_addresses(msg,
677 CtdlDirectoryAddUser);
681 ptr = strchr((char *)ptr, '\n');
682 if (ptr != NULL) ++ptr;
691 * When a vCard is being removed from the Global Address Book room, remove it
692 * from the directory as well.
694 void vcard_delete_remove(char *room, long msgnum) {
695 struct CtdlMessage *msg;
699 if (msgnum <= 0L) return;
701 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
705 msg = CtdlFetchMessage(msgnum);
706 if (msg == NULL) return;
708 ptr = msg->cm_fields['M'];
709 if (ptr == NULL) goto EOH;
710 while (ptr != NULL) {
711 linelen = strcspn(ptr, "\n");
712 if (linelen == 0) goto EOH;
714 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
715 /* Bingo! A vCard is being deleted.
717 vcard_extract_internet_addresses(msg,
718 CtdlDirectoryDelUser);
720 ptr = strchr((char *)ptr, '\n');
721 if (ptr != NULL) ++ptr;
724 EOH: CtdlFreeMessage(msg);
732 void cmd_qdir(char *argbuf) {
733 char citadel_addr[SIZ];
734 char internet_addr[SIZ];
736 if (CtdlAccessCheck(ac_logged_in)) return;
738 extract(internet_addr, argbuf, 0);
740 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
741 cprintf("%d %s was not found.\n",
742 ERROR+NO_SUCH_USER, internet_addr);
746 cprintf("%d %s\n", CIT_OK, citadel_addr);
753 * Session startup, allocate some per-session data
755 void vcard_session_startup_hook(void) {
756 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
761 * When a user logs in...
763 void vcard_session_login_hook(void) {
766 v = vcard_get_user(&CC->usersupp);
767 vcard_populate_cs_inet_email(v);
774 char *Dynamic_Module_Init(void)
776 SYM_VCARD = CtdlGetDynamicSymbol();
777 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
778 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
779 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
780 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
781 CtdlRegisterDeleteHook(vcard_delete_remove);
782 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
783 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
784 CtdlRegisterProtoHook(cmd_igab, "IGAB",
785 "Initialize Global Address Book");
786 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
787 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
788 CtdlRegisterNetprocHook(vcard_extract_from_network);
789 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0);