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 sprintf(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++);
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++);
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, &CC->usersupp, USERCONFIGROOM);
284 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
286 /* Set the Extended-ID to a standardized one so the
287 * replication always works correctly
289 if (msg->cm_fields['E'] != NULL)
290 phree(msg->cm_fields['E']);
292 sprintf(buf, VCARD_EXT_FORMAT,
293 msg->cm_fields['A'], NODENAME);
294 msg->cm_fields['E'] = strdoop(buf);
296 /* Now allow the save to complete. */
300 ptr = strchr((char *)ptr, '\n');
301 if (ptr != NULL) ++ptr;
310 * This handler detects whether the user is attempting to save a new
311 * vCard as part of his/her personal configuration, and handles the replace
312 * function accordingly (copy the vCard from the config room to the global
315 int vcard_upload_aftersave(struct CtdlMessage *msg) {
321 if (!CC->logged_in) return(0); /* Only do this if logged in. */
323 /* If this isn't the configuration room, or if this isn't a MIME
324 * message, don't bother.
326 if (msg->cm_fields['O'] == NULL) return(0);
327 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
328 if (msg->cm_format_type != 4) return(0);
330 ptr = msg->cm_fields['M'];
331 if (ptr == NULL) return(0);
332 while (ptr != NULL) {
334 linelen = strcspn(ptr, "\n");
335 if (linelen == 0) return(0); /* end of headers */
337 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
338 /* Bingo! The user is uploading a new vCard, so
339 * copy it to the Global Address Book room.
342 I = atol(msg->cm_fields['I']);
343 if (I < 0L) return(0);
345 /* Put it in the Global Address Book room... */
346 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
347 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
349 /* ...and also in the directory database. */
350 vcard_add_to_directory(I, NULL);
352 /* Store our Internet return address in memory */
353 v = vcard_load(msg->cm_fields['M']);
354 vcard_populate_cs_inet_email(v);
360 ptr = strchr((char *)ptr, '\n');
361 if (ptr != NULL) ++ptr;
370 * back end function used for callbacks
372 void vcard_gu_backend(long msgnum, void *userdata) {
378 * If this user has a vcard on disk, read it into memory, otherwise allocate
379 * and return an empty vCard.
381 struct vCard *vcard_get_user(struct usersupp *u) {
382 char hold_rm[ROOMNAMELEN];
383 char config_rm[ROOMNAMELEN];
384 struct CtdlMessage *msg;
387 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
388 MailboxName(config_rm, u, USERCONFIGROOM);
390 if (getroom(&CC->quickroom, config_rm) != 0) {
391 getroom(&CC->quickroom, hold_rm);
395 /* We want the last (and probably only) vcard in this room */
397 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
398 NULL, vcard_gu_backend, NULL);
399 getroom(&CC->quickroom, hold_rm); /* return to saved room */
401 if (VC->msgnum < 0L) return vcard_new();
403 msg = CtdlFetchMessage(VC->msgnum);
404 if (msg == NULL) return vcard_new();
406 v = vcard_load(msg->cm_fields['M']);
407 CtdlFreeMessage(msg);
413 * Store this user's vCard in the appropriate place
416 * Write our config to disk
418 void vcard_write_user(struct usersupp *u, struct vCard *v) {
423 strcpy(temp, tmpnam(NULL));
424 ser = vcard_serialize(v);
426 fp = fopen(temp, "w");
427 if (fp == NULL) return;
429 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
431 fwrite(ser, strlen(ser), 1, fp);
436 /* This handy API function does all the work for us.
437 * NOTE: normally we would want to set that last argument to 1, to
438 * force the system to delete the user's old vCard. But it doesn't
439 * have to, because the vcard_upload_beforesave() hook above
440 * is going to notice what we're trying to do, and delete the old vCard.
442 CtdlWriteObject(USERCONFIGROOM, /* which room */
443 "text/x-vcard", /* MIME type */
444 temp, /* temp file */
447 0, /* don't delete others of this type */
456 * Old style "enter registration info" command. This function simply honors
457 * the REGI protocol command, translates the entered parameters into a vCard,
458 * and enters the vCard into the user's configuration.
460 void cmd_regi(char *argbuf) {
463 struct vCard *my_vcard;
469 char tmpaddress[SIZ];
470 char tmpcountry[SIZ];
472 if (!(CC->logged_in)) {
473 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
477 my_vcard = vcard_get_user(&CC->usersupp);
480 strcpy(tmpstate, "");
482 strcpy(tmpcountry, "USA");
484 cprintf("%d Send registration...\n", SEND_LISTING);
486 while (client_gets(buf), strcmp(buf,"000")) {
487 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
488 if (a==1) strcpy(tmpaddr, buf);
489 if (a==2) strcpy(tmpcity, buf);
490 if (a==3) strcpy(tmpstate, buf);
492 for (c=0; c<strlen(buf); ++c) {
493 if ((buf[c]>='0') && (buf[c]<='9')) {
500 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
501 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
502 if (a==7) strcpy(tmpcountry, buf);
506 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
507 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
508 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
509 vcard_write_user(&CC->usersupp, my_vcard);
510 vcard_free(my_vcard);
512 lgetuser(&CC->usersupp, CC->curr_user);
513 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
514 lputuser(&CC->usersupp);
516 /* set global flag calling for validation */
517 begin_critical_section(S_CONTROL);
519 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
521 end_critical_section(S_CONTROL);
527 * Protocol command to fetch registration info for a user
529 void cmd_greg(char *argbuf)
531 struct usersupp usbuf;
538 extract(who, argbuf, 0);
540 if (!(CC->logged_in)) {
541 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
545 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
547 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
548 cprintf("%d Higher access required.\n",
549 ERROR+HIGHER_ACCESS_REQUIRED);
553 if (getuser(&usbuf, who) != 0) {
554 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
558 v = vcard_get_user(&usbuf);
560 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
561 cprintf("%ld\n", usbuf.usernum);
562 cprintf("%s\n", usbuf.password);
563 s = vcard_get_prop(v, "n", 0, 0);
564 cprintf("%s\n", s ? s : " "); /* name */
566 s = vcard_get_prop(v, "adr", 0, 0);
567 sprintf(adr, "%s", s ? s : " ");/* address... */
569 extract_token(buf, adr, 2, ';');
570 cprintf("%s\n", buf); /* street */
571 extract_token(buf, adr, 3, ';');
572 cprintf("%s\n", buf); /* city */
573 extract_token(buf, adr, 4, ';');
574 cprintf("%s\n", buf); /* state */
575 extract_token(buf, adr, 5, ';');
576 cprintf("%s\n", buf); /* zip */
578 s = vcard_get_prop(v, "tel;home", 0, 0);
579 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0);
587 cprintf("%d\n", usbuf.axlevel);
589 s = vcard_get_prop(v, "email;internet", 0, 0);
590 cprintf("%s\n", s ? s : " ");
591 s = vcard_get_prop(v, "adr", 0, 0);
592 sprintf(adr, "%s", s ? s : " ");/* address... */
594 extract_token(buf, adr, 6, ';');
595 cprintf("%s\n", buf); /* country */
601 * When a user is being deleted, we have to remove his/her vCard.
602 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
603 * field, and the same Extended ID as the existing card.
605 void vcard_purge(char *username, long usernum) {
606 struct CtdlMessage *msg;
609 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
610 if (msg == NULL) return;
611 memset(msg, 0, sizeof(struct CtdlMessage));
613 msg->cm_magic = CTDLMESSAGE_MAGIC;
614 msg->cm_anon_type = MES_NORMAL;
615 msg->cm_format_type = 0;
616 msg->cm_fields['A'] = strdoop(username);
617 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
618 msg->cm_fields['N'] = strdoop(NODENAME);
619 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
621 sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
622 msg->cm_fields['E'] = strdoop(buf);
624 msg->cm_fields['S'] = strdoop("CANCEL");
626 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
627 CtdlFreeMessage(msg);
632 * Grab vCard directory stuff out of incoming network messages
634 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
638 if (msg == NULL) return(0);
640 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
644 if (msg->cm_format_type != 4) return(0);
646 ptr = msg->cm_fields['M'];
647 if (ptr == NULL) return(0);
648 while (ptr != NULL) {
650 linelen = strcspn(ptr, "\n");
651 if (linelen == 0) return(0); /* end of headers */
653 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
654 /* It's a vCard. Add it to the directory. */
655 vcard_extract_internet_addresses(msg,
656 CtdlDirectoryAddUser);
660 ptr = strchr((char *)ptr, '\n');
661 if (ptr != NULL) ++ptr;
670 * When a vCard is being removed from the Global Address Book room, remove it
671 * from the directory as well.
673 void vcard_delete_remove(char *room, long msgnum) {
674 struct CtdlMessage *msg;
676 if (msgnum <= 0L) return;
678 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
682 msg = CtdlFetchMessage(msgnum);
684 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
687 CtdlFreeMessage(msg);
694 void cmd_qdir(char *argbuf) {
695 char citadel_addr[SIZ];
696 char internet_addr[SIZ];
698 if (CtdlAccessCheck(ac_logged_in)) return;
700 extract(internet_addr, argbuf, 0);
702 if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
703 cprintf("%d %s was not found.\n",
704 ERROR+NO_SUCH_USER, internet_addr);
708 cprintf("%d %s\n", OK, citadel_addr);
715 * Session startup, allocate some per-session data
717 void vcard_session_startup_hook(void) {
718 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
723 * When a user logs in...
725 void vcard_session_login_hook(void) {
728 v = vcard_get_user(&CC->usersupp);
729 vcard_populate_cs_inet_email(v);
736 * When a user logs out...
738 void vcard_session_logout_hook(void) {
739 if (CC->cs_inet_email != NULL) {
740 phree(CC->cs_inet_email);
741 CC->cs_inet_email = NULL;
746 char *Dynamic_Module_Init(void)
748 SYM_VCARD = CtdlGetDynamicSymbol();
749 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
750 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
751 CtdlRegisterSessionHook(vcard_session_logout_hook, EVT_LOGOUT);
752 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
753 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
754 CtdlRegisterDeleteHook(vcard_delete_remove);
755 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
756 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
757 CtdlRegisterProtoHook(cmd_igab, "IGAB",
758 "Initialize Global Address Book");
759 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
760 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
761 CtdlRegisterNetprocHook(vcard_extract_from_network);
762 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);