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", 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) {
249 char config_rm[ROOMNAMELEN];
252 if (!CC->logged_in) return(0); /* Only do this if logged in. */
254 /* If this isn't the configuration room, or if this isn't a MIME
255 * message, don't bother. (Check for NULL room first, otherwise
256 * some messages will cause it to crash!!)
258 if (msg->cm_fields['O'] == NULL) return(0);
259 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
260 if (msg->cm_format_type != 4) return(0);
262 ptr = msg->cm_fields['M'];
263 if (ptr == NULL) return(0);
264 while (ptr != NULL) {
266 linelen = strcspn(ptr, "\n");
267 if (linelen == 0) return(0); /* end of headers */
269 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
270 /* Bingo! The user is uploading a new vCard, so
271 * delete the old one.
274 /* Delete the user's old vCard. This would probably
275 * get taken care of by the replication check, but we
276 * want to make sure there is absolutely only one
277 * vCard in the user's config room at all times.
279 * FIXME ... this needs to be tweaked to allow an admin
280 * to make changes to another user's vCard instead of
281 * assuming that it's always the user saving his own.
283 MailboxName(config_rm, sizeof config_rm,
284 &CC->usersupp, USERCONFIGROOM);
285 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
287 /* Set the Extended-ID to a standardized one so the
288 * replication always works correctly
290 if (msg->cm_fields['E'] != NULL)
291 phree(msg->cm_fields['E']);
293 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
294 msg->cm_fields['A'], NODENAME);
295 msg->cm_fields['E'] = strdoop(buf);
297 /* Now allow the save to complete. */
301 ptr = strchr((char *)ptr, '\n');
302 if (ptr != NULL) ++ptr;
311 * This handler detects whether the user is attempting to save a new
312 * vCard as part of his/her personal configuration, and handles the replace
313 * function accordingly (copy the vCard from the config room to the global
316 int vcard_upload_aftersave(struct CtdlMessage *msg) {
322 if (!CC->logged_in) return(0); /* Only do this if logged in. */
324 /* If this isn't the configuration room, or if this isn't a MIME
325 * message, don't bother.
327 if (msg->cm_fields['O'] == NULL) return(0);
328 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
329 if (msg->cm_format_type != 4) return(0);
331 ptr = msg->cm_fields['M'];
332 if (ptr == NULL) return(0);
333 while (ptr != NULL) {
335 linelen = strcspn(ptr, "\n");
336 if (linelen == 0) return(0); /* end of headers */
338 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
339 /* Bingo! The user is uploading a new vCard, so
340 * copy it to the Global Address Book room.
343 I = atol(msg->cm_fields['I']);
344 if (I < 0L) return(0);
346 /* Put it in the Global Address Book room... */
347 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
348 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
350 /* ...and also in the directory database. */
351 vcard_add_to_directory(I, NULL);
353 /* Store our Internet return address in memory */
354 v = vcard_load(msg->cm_fields['M']);
355 vcard_populate_cs_inet_email(v);
361 ptr = strchr((char *)ptr, '\n');
362 if (ptr != NULL) ++ptr;
371 * back end function used for callbacks
373 void vcard_gu_backend(long msgnum, void *userdata) {
379 * If this user has a vcard on disk, read it into memory, otherwise allocate
380 * and return an empty vCard.
382 struct vCard *vcard_get_user(struct usersupp *u) {
383 char hold_rm[ROOMNAMELEN];
384 char config_rm[ROOMNAMELEN];
385 struct CtdlMessage *msg;
388 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
389 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
391 if (getroom(&CC->quickroom, config_rm) != 0) {
392 getroom(&CC->quickroom, hold_rm);
396 /* We want the last (and probably only) vcard in this room */
398 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
399 NULL, vcard_gu_backend, NULL);
400 getroom(&CC->quickroom, hold_rm); /* return to saved room */
402 if (VC->msgnum < 0L) return vcard_new();
404 msg = CtdlFetchMessage(VC->msgnum);
405 if (msg == NULL) return vcard_new();
407 v = vcard_load(msg->cm_fields['M']);
408 CtdlFreeMessage(msg);
414 * Store this user's vCard in the appropriate place
417 * Write our config to disk
419 void vcard_write_user(struct usersupp *u, struct vCard *v) {
424 strcpy(temp, tmpnam(NULL));
425 ser = vcard_serialize(v);
427 fp = fopen(temp, "w");
428 if (fp == NULL) return;
430 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
432 fwrite(ser, strlen(ser), 1, fp);
437 /* This handy API function does all the work for us.
438 * NOTE: normally we would want to set that last argument to 1, to
439 * force the system to delete the user's old vCard. But it doesn't
440 * have to, because the vcard_upload_beforesave() hook above
441 * is going to notice what we're trying to do, and delete the old vCard.
443 CtdlWriteObject(USERCONFIGROOM, /* which room */
444 "text/x-vcard", /* MIME type */
445 temp, /* temp file */
448 0, /* don't delete others of this type */
457 * Old style "enter registration info" command. This function simply honors
458 * the REGI protocol command, translates the entered parameters into a vCard,
459 * and enters the vCard into the user's configuration.
461 void cmd_regi(char *argbuf) {
464 struct vCard *my_vcard;
470 char tmpaddress[SIZ];
471 char tmpcountry[SIZ];
473 if (!(CC->logged_in)) {
474 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
478 my_vcard = vcard_get_user(&CC->usersupp);
481 strcpy(tmpstate, "");
483 strcpy(tmpcountry, "USA");
485 cprintf("%d Send registration...\n", SEND_LISTING);
487 while (client_gets(buf), strcmp(buf,"000")) {
488 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
489 if (a==1) strcpy(tmpaddr, buf);
490 if (a==2) strcpy(tmpcity, buf);
491 if (a==3) strcpy(tmpstate, buf);
493 for (c=0; c<strlen(buf); ++c) {
494 if ((buf[c]>='0') && (buf[c]<='9')) {
501 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
502 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
503 if (a==7) strcpy(tmpcountry, buf);
507 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
508 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
509 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
510 vcard_write_user(&CC->usersupp, my_vcard);
511 vcard_free(my_vcard);
513 lgetuser(&CC->usersupp, CC->curr_user);
514 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
515 lputuser(&CC->usersupp);
517 /* set global flag calling for validation */
518 begin_critical_section(S_CONTROL);
520 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
522 end_critical_section(S_CONTROL);
528 * Protocol command to fetch registration info for a user
530 void cmd_greg(char *argbuf)
532 struct usersupp usbuf;
539 extract(who, argbuf, 0);
541 if (!(CC->logged_in)) {
542 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
546 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
548 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
549 cprintf("%d Higher access required.\n",
550 ERROR+HIGHER_ACCESS_REQUIRED);
554 if (getuser(&usbuf, who) != 0) {
555 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
559 v = vcard_get_user(&usbuf);
561 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
562 cprintf("%ld\n", usbuf.usernum);
563 cprintf("%s\n", usbuf.password);
564 s = vcard_get_prop(v, "n", 0, 0, 0);
565 cprintf("%s\n", s ? s : " "); /* name */
567 s = vcard_get_prop(v, "adr", 0, 0, 0);
568 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
570 extract_token(buf, adr, 2, ';');
571 cprintf("%s\n", buf); /* street */
572 extract_token(buf, adr, 3, ';');
573 cprintf("%s\n", buf); /* city */
574 extract_token(buf, adr, 4, ';');
575 cprintf("%s\n", buf); /* state */
576 extract_token(buf, adr, 5, ';');
577 cprintf("%s\n", buf); /* zip */
579 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
580 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
588 cprintf("%d\n", usbuf.axlevel);
590 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
591 cprintf("%s\n", s ? s : " ");
592 s = vcard_get_prop(v, "adr", 0, 0, 0);
593 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
595 extract_token(buf, adr, 6, ';');
596 cprintf("%s\n", buf); /* country */
602 * When a user is being deleted, we have to remove his/her vCard.
603 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
604 * field, and the same Extended ID as the existing card.
606 void vcard_purge(char *username, long usernum) {
607 struct CtdlMessage *msg;
610 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
611 if (msg == NULL) return;
612 memset(msg, 0, sizeof(struct CtdlMessage));
614 msg->cm_magic = CTDLMESSAGE_MAGIC;
615 msg->cm_anon_type = MES_NORMAL;
616 msg->cm_format_type = 0;
617 msg->cm_fields['A'] = strdoop(username);
618 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
619 msg->cm_fields['N'] = strdoop(NODENAME);
620 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
622 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
623 msg->cm_fields['E'] = strdoop(buf);
625 msg->cm_fields['S'] = strdoop("CANCEL");
627 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
628 CtdlFreeMessage(msg);
633 * Grab vCard directory stuff out of incoming network messages
635 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
639 if (msg == NULL) return(0);
641 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
645 if (msg->cm_format_type != 4) return(0);
647 ptr = msg->cm_fields['M'];
648 if (ptr == NULL) return(0);
649 while (ptr != NULL) {
651 linelen = strcspn(ptr, "\n");
652 if (linelen == 0) return(0); /* end of headers */
654 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
655 /* It's a vCard. Add it to the directory. */
656 vcard_extract_internet_addresses(msg,
657 CtdlDirectoryAddUser);
661 ptr = strchr((char *)ptr, '\n');
662 if (ptr != NULL) ++ptr;
671 * When a vCard is being removed from the Global Address Book room, remove it
672 * from the directory as well.
674 void vcard_delete_remove(char *room, long msgnum) {
675 struct CtdlMessage *msg;
679 if (msgnum <= 0L) return;
681 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
685 msg = CtdlFetchMessage(msgnum);
686 if (msg == NULL) return;
688 ptr = msg->cm_fields['M'];
689 if (ptr == NULL) goto EOH;
690 while (ptr != NULL) {
691 linelen = strcspn(ptr, "\n");
692 if (linelen == 0) goto EOH;
694 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
695 /* Bingo! A vCard is being deleted.
697 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
699 ptr = strchr((char *)ptr, '\n');
700 if (ptr != NULL) ++ptr;
703 EOH: CtdlFreeMessage(msg);
711 void cmd_qdir(char *argbuf) {
712 char citadel_addr[SIZ];
713 char internet_addr[SIZ];
715 if (CtdlAccessCheck(ac_logged_in)) return;
717 extract(internet_addr, argbuf, 0);
719 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
720 cprintf("%d %s was not found.\n",
721 ERROR+NO_SUCH_USER, internet_addr);
725 cprintf("%d %s\n", OK, citadel_addr);
732 * Session startup, allocate some per-session data
734 void vcard_session_startup_hook(void) {
735 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
740 * When a user logs in...
742 void vcard_session_login_hook(void) {
745 v = vcard_get_user(&CC->usersupp);
746 vcard_populate_cs_inet_email(v);
753 * When a user logs out...
755 void vcard_session_logout_hook(void) {
756 if (CC->cs_inet_email != NULL) {
757 phree(CC->cs_inet_email);
758 CC->cs_inet_email = NULL;
763 char *Dynamic_Module_Init(void)
765 SYM_VCARD = CtdlGetDynamicSymbol();
766 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
767 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
768 CtdlRegisterSessionHook(vcard_session_logout_hook, EVT_LOGOUT);
769 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
770 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
771 CtdlRegisterDeleteHook(vcard_delete_remove);
772 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
773 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
774 CtdlRegisterProtoHook(cmd_igab, "IGAB",
775 "Initialize Global Address Book");
776 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
777 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
778 CtdlRegisterNetprocHook(vcard_extract_from_network);
779 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);