4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
7 * Copyright (c) 1999-2007 / released under the GNU General Public License
11 * Format of the "Exclusive ID" field of the message containing a user's
12 * vCard. Doesn't matter what it really looks like as long as it's both
13 * unique and consistent (because we use it for replication checking to
14 * delete the old vCard network-wide when the user enters a new one).
16 #define VCARD_EXT_FORMAT "Citadel vCard: personal card for %s at %s"
19 * Citadel will accept either text/vcard or text/x-vcard as the MIME type
20 * for a vCard. The following definition determines which one it *generates*
23 #define VCARD_MIME_TYPE "text/x-vcard"
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"
65 #include "mime_parser.h"
67 #include "serv_ldap.h"
68 #include "serv_vcard.h"
71 * set global flag calling for an aide to validate new users
73 void set_mm_valid(void) {
74 begin_critical_section(S_CONTROL);
76 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
78 end_critical_section(S_CONTROL);
84 * Extract Internet e-mail addresses from a message containing a vCard, and
85 * perform a callback for any found.
87 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
88 void (*callback)(char *, char *) ) {
92 char citadel_address[SIZ];
94 int found_something = 0;
96 if (msg->cm_fields['A'] == NULL) return;
97 if (msg->cm_fields['N'] == NULL) return;
98 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
99 msg->cm_fields['A'], msg->cm_fields['N']);
101 v = vcard_load(msg->cm_fields['M']);
102 if (v == NULL) return;
104 /* Go through the vCard searching for *all* instances of
105 * the "email;internet" key
108 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
112 if (strlen(addr) > 0) {
113 if (callback != NULL) {
114 callback(addr, citadel_address);
123 } while(found_something);
131 * Callback for vcard_add_to_directory()
132 * (Lotsa ugly nested callbacks. Oh well.)
134 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
137 /* We have to validate that we're not stepping on someone else's
138 * email address ... but only if we're logged in. Otherwise it's
139 * probably just the networker or something.
142 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
143 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
144 if (strcasecmp(buf, citadel_addr)) {
145 /* This address belongs to someone else.
146 * Bail out silently without saving.
148 lprintf(CTDL_DEBUG, "DOOP!\n");
153 lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
154 citadel_addr, internet_addr);
155 CtdlDirectoryAddUser(internet_addr, citadel_addr);
160 * Back end function for cmd_igab()
162 void vcard_add_to_directory(long msgnum, void *data) {
163 struct CtdlMessage *msg;
165 msg = CtdlFetchMessage(msgnum, 1);
167 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
171 ctdl_vcard_to_ldap(msg, V2L_WRITE);
174 CtdlFreeMessage(msg);
179 * Initialize Global Adress Book
181 void cmd_igab(char *argbuf) {
182 char hold_rm[ROOMNAMELEN];
184 if (CtdlAccessCheck(ac_aide)) return;
186 strcpy(hold_rm, CC->room.QRname); /* save current room */
188 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
189 getroom(&CC->room, hold_rm);
190 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
194 /* Empty the existing database first.
198 /* We want *all* vCards in this room */
199 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
200 NULL, vcard_add_to_directory, NULL);
202 getroom(&CC->room, hold_rm); /* return to saved room */
203 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
210 * See if there is a valid Internet address in a vCard to use for outbound
211 * Internet messages. If there is, stick it in the buffer.
213 void extract_inet_email_addrs(char *emailaddrbuf, size_t emailaddrbuf_len,
214 char *secemailaddrbuf, size_t secemailaddrbuf_len,
215 struct vCard *v, int local_addrs_only) {
218 int saved_instance = 0;
220 /* Go through the vCard searching for *all* instances of
221 * the "email;internet" key
223 while (s = vcard_get_prop(v, "email;internet", 0, instance++, 0), s != NULL) {
226 if (strlen(addr) > 0) {
227 if ( (IsDirectory(addr, 1)) ||
228 (!local_addrs_only) ) {
230 if ((saved_instance == 1) && (emailaddrbuf != NULL)) {
231 safestrncpy(emailaddrbuf, addr, emailaddrbuf_len);
233 else if ((saved_instance == 2) && (secemailaddrbuf != NULL)) {
234 safestrncpy(secemailaddrbuf, addr, secemailaddrbuf_len);
236 else if ((saved_instance > 2) && (secemailaddrbuf != NULL)) {
237 if ( (strlen(addr) + strlen(secemailaddrbuf) + 2)
238 < secemailaddrbuf_len ) {
239 strcat(secemailaddrbuf, "|");
240 strcat(secemailaddrbuf, addr);
252 * See if there is a name / screen name / friendly name in a vCard to use for outbound
253 * Internet messages. If there is, stick it in the buffer.
255 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
259 s = vcard_get_prop(v, "fn", 0, 0, 0);
261 s = vcard_get_prop(v, "n", 0, 0, 0);
265 safestrncpy(namebuf, s, namebuf_len);
271 * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
273 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
274 void *content, char *cbtype, char *cbcharset, size_t length,
275 char *encoding, void *cbuserdata)
277 struct vCard **v = (struct vCard **) cbuserdata;
279 if ( (!strcasecmp(cbtype, "text/x-vcard"))
280 || (!strcasecmp(cbtype, "text/vcard")) ) {
282 lprintf(CTDL_DEBUG, "Part %s contains a vCard! Loading...\n", partnum);
286 *v = vcard_load(content);
292 * This handler detects whether the user is attempting to save a new
293 * vCard as part of his/her personal configuration, and handles the replace
294 * function accordingly (delete the user's existing vCard in the config room
295 * and in the global address book).
297 int vcard_upload_beforesave(struct CtdlMessage *msg) {
301 struct ctdluser usbuf;
303 struct vCard *v = NULL;
306 int yes_my_citadel_config = 0;
307 int yes_any_vcard_room = 0;
309 if (!CC->logged_in) return(0); /* Only do this if logged in. */
311 /* Is this some user's "My Citadel Config" room? */
312 if ( (CC->room.QRflags && QR_MAILBOX)
313 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
314 /* Yes, we want to do this */
315 yes_my_citadel_config = 1;
317 #ifdef VCARD_SAVES_BY_AIDES_ONLY
318 /* Prevent non-aides from performing registration changes */
319 if (CC->user.axlevel < 6) {
326 /* Is this a room with an address book in it? */
327 if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
328 yes_any_vcard_room = 1;
331 /* If neither condition exists, don't run this hook. */
332 if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
336 /* If this isn't a MIME message, don't bother. */
337 if (msg->cm_format_type != 4) return(0);
339 /* Ok, if we got this far, look into the situation further... */
341 ptr = msg->cm_fields['M'];
342 if (ptr == NULL) return(0);
344 mime_parser(msg->cm_fields['M'],
346 *vcard_extract_vcard,
348 &v, /* user data ptr - put the vcard here */
352 if (v == NULL) return(0); /* no vCards were found in this message */
354 /* If users cannot create their own accounts, they cannot re-register either. */
355 if ( (yes_my_citadel_config) && (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
359 s = vcard_get_prop(v, "FN", 0, 0, 0);
360 if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
362 if (yes_my_citadel_config) {
363 /* Bingo! The user is uploading a new vCard, so
364 * delete the old one. First, figure out which user
365 * is being re-registered...
367 what_user = atol(CC->room.QRname);
369 if (what_user == CC->user.usernum) {
370 /* It's the logged in user. That was easy. */
371 memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
374 else if (getuserbynumber(&usbuf, what_user) == 0) {
375 /* We fetched a valid user record */
379 /* somebody set up us the bomb! */
380 yes_my_citadel_config = 0;
384 if (yes_my_citadel_config) {
385 /* Delete the user's old vCard. This would probably
386 * get taken care of by the replication check, but we
387 * want to make sure there is absolutely only one
388 * vCard in the user's config room at all times.
391 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$");
393 /* Make the author of the message the name of the user. */
394 if (msg->cm_fields['A'] != NULL) {
395 free(msg->cm_fields['A']);
397 msg->cm_fields['A'] = strdup(usbuf.fullname);
400 /* Insert or replace RFC2739-compliant free/busy URL */
401 if (yes_my_citadel_config) {
402 sprintf(buf, "http://%s/%s.vfb",
405 for (i=0; i<strlen(buf); ++i) {
406 if (buf[i] == ' ') buf[i] = '_';
408 vcard_set_prop(v, "FBURL;PREF", buf, 0);
411 /* If the vCard has no UID, then give it one. */
412 s = vcard_get_prop(v, "UID", 0, 0, 0);
415 vcard_set_prop(v, "UID", buf, 0);
418 /* Enforce local UID policy if applicable */
419 if (yes_my_citadel_config) {
420 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
421 vcard_set_prop(v, "UID", buf, 0);
425 * Set the EUID of the message to the UID of the vCard.
427 if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
428 s = vcard_get_prop(v, "UID", 0, 0, 0);
430 msg->cm_fields['E'] = strdup(s);
431 if (msg->cm_fields['U'] == NULL) {
432 msg->cm_fields['U'] = strdup(s);
437 * Set the Subject to the name in the vCard.
439 s = vcard_get_prop(v, "FN", 0, 0, 0);
441 s = vcard_get_prop(v, "N", 0, 0, 0);
444 if (msg->cm_fields['U'] != NULL) {
445 free(msg->cm_fields['U']);
447 msg->cm_fields['U'] = strdup(s);
450 /* Re-serialize it back into the msg body */
451 ser = vcard_serialize(v);
453 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
454 sprintf(msg->cm_fields['M'],
455 "Content-type: " VCARD_MIME_TYPE
456 "\r\n\r\n%s\r\n", ser);
460 /* Now allow the save to complete. */
468 * This handler detects whether the user is attempting to save a new
469 * vCard as part of his/her personal configuration, and handles the replace
470 * function accordingly (copy the vCard from the config room to the global
473 int vcard_upload_aftersave(struct CtdlMessage *msg) {
479 if (!CC->logged_in) return(0); /* Only do this if logged in. */
481 /* If this isn't the configuration room, or if this isn't a MIME
482 * message, don't bother.
484 if (msg->cm_fields['O'] == NULL) return(0);
485 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
486 if (msg->cm_format_type != 4) return(0);
488 ptr = msg->cm_fields['M'];
489 if (ptr == NULL) return(0);
490 while (ptr != NULL) {
492 linelen = strcspn(ptr, "\n");
493 if (linelen == 0) return(0); /* end of headers */
495 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
496 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
498 * Bingo! The user is uploading a new vCard, so
499 * copy it to the Global Address Book room.
502 I = atol(msg->cm_fields['I']);
503 if (I < 0L) return(0);
505 /* Store our Internet return address in memory */
506 v = vcard_load(msg->cm_fields['M']);
507 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
508 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
510 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
513 /* Put it in the Global Address Book room... */
514 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
516 /* ...and also in the directory database. */
517 vcard_add_to_directory(I, NULL);
519 /* Some sites want an Aide to be notified when a
520 * user registers or re-registers...
524 /* ...which also means we need to flag the user */
525 lgetuser(&CC->user, CC->curr_user);
526 CC->user.flags |= (US_REGIS|US_NEEDVALID);
532 ptr = strchr((char *)ptr, '\n');
533 if (ptr != NULL) ++ptr;
542 * back end function used for callbacks
544 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
547 msgnum = (long *) userdata;
548 *msgnum = supplied_msgnum;
553 * If this user has a vcard on disk, read it into memory, otherwise allocate
554 * and return an empty vCard.
556 struct vCard *vcard_get_user(struct ctdluser *u) {
557 char hold_rm[ROOMNAMELEN];
558 char config_rm[ROOMNAMELEN];
559 struct CtdlMessage *msg = NULL;
563 strcpy(hold_rm, CC->room.QRname); /* save current room */
564 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
566 if (getroom(&CC->room, config_rm) != 0) {
567 getroom(&CC->room, hold_rm);
571 /* We want the last (and probably only) vcard in this room */
573 CtdlForEachMessage(MSGS_LAST, 1, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
574 NULL, vcard_gu_backend, (void *)&VCmsgnum );
575 getroom(&CC->room, hold_rm); /* return to saved room */
577 if (VCmsgnum < 0L) return vcard_new();
579 msg = CtdlFetchMessage(VCmsgnum, 1);
580 if (msg == NULL) return vcard_new();
582 v = vcard_load(msg->cm_fields['M']);
583 CtdlFreeMessage(msg);
589 * Store this user's vCard in the appropriate place
592 * Write our config to disk
594 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
599 CtdlMakeTempFileName(temp, sizeof temp);
600 ser = vcard_serialize(v);
602 fp = fopen(temp, "w");
603 if (fp == NULL) return;
605 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
607 fwrite(ser, strlen(ser), 1, fp);
612 /* This handy API function does all the work for us.
613 * NOTE: normally we would want to set that last argument to 1, to
614 * force the system to delete the user's old vCard. But it doesn't
615 * have to, because the vcard_upload_beforesave() hook above
616 * is going to notice what we're trying to do, and delete the old vCard.
618 CtdlWriteObject(USERCONFIGROOM, /* which room */
619 VCARD_MIME_TYPE,/* MIME type */
620 temp, /* temp file */
623 0, /* don't delete others of this type */
632 * Old style "enter registration info" command. This function simply honors
633 * the REGI protocol command, translates the entered parameters into a vCard,
634 * and enters the vCard into the user's configuration.
636 void cmd_regi(char *argbuf) {
639 struct vCard *my_vcard;
645 char tmpaddress[SIZ];
646 char tmpcountry[SIZ];
650 if (!(CC->logged_in)) {
651 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
655 /* If users cannot create their own accounts, they cannot re-register either. */
656 if ( (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
657 cprintf("%d Self-service registration is not allowed here.\n",
658 ERROR + HIGHER_ACCESS_REQUIRED);
661 my_vcard = vcard_get_user(&CC->user);
664 strcpy(tmpstate, "");
666 strcpy(tmpcountry, "USA");
668 cprintf("%d Send registration...\n", SEND_LISTING);
670 while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
671 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
672 if (a==1) strcpy(tmpaddr, buf);
673 if (a==2) strcpy(tmpcity, buf);
674 if (a==3) strcpy(tmpstate, buf);
676 for (c=0; c<strlen(buf); ++c) {
677 if ((buf[c]>='0') && (buf[c]<='9')) {
684 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
685 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
686 if (a==7) strcpy(tmpcountry, buf);
690 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
691 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
692 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
693 vcard_write_user(&CC->user, my_vcard);
694 vcard_free(my_vcard);
699 * Protocol command to fetch registration info for a user
701 void cmd_greg(char *argbuf)
703 struct ctdluser usbuf;
706 char who[USERNAME_SIZE];
710 extract_token(who, argbuf, 0, '|', sizeof who);
712 if (!(CC->logged_in)) {
713 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
717 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
719 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
720 cprintf("%d Higher access required.\n",
721 ERROR + HIGHER_ACCESS_REQUIRED);
725 if (getuser(&usbuf, who) != 0) {
726 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
730 v = vcard_get_user(&usbuf);
732 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
733 cprintf("%ld\n", usbuf.usernum);
734 cprintf("%s\n", usbuf.password);
735 s = vcard_get_prop(v, "n", 0, 0, 0);
736 cprintf("%s\n", s ? s : " "); /* name */
738 s = vcard_get_prop(v, "adr", 0, 0, 0);
739 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
741 extract_token(buf, adr, 2, ';', sizeof buf);
742 cprintf("%s\n", buf); /* street */
743 extract_token(buf, adr, 3, ';', sizeof buf);
744 cprintf("%s\n", buf); /* city */
745 extract_token(buf, adr, 4, ';', sizeof buf);
746 cprintf("%s\n", buf); /* state */
747 extract_token(buf, adr, 5, ';', sizeof buf);
748 cprintf("%s\n", buf); /* zip */
750 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
751 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
759 cprintf("%d\n", usbuf.axlevel);
761 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
762 cprintf("%s\n", s ? s : " ");
763 s = vcard_get_prop(v, "adr", 0, 0, 0);
764 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
766 extract_token(buf, adr, 6, ';', sizeof buf);
767 cprintf("%s\n", buf); /* country */
775 * When a user is being created, create his/her vCard.
777 void vcard_newuser(struct ctdluser *usbuf) {
783 vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
784 lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
786 /* Create and save the vCard */
788 if (v == NULL) return;
789 sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
790 for (i=0; i<strlen(buf); ++i) {
791 if (buf[i] == ' ') buf[i] = '_';
793 vcard_add_prop(v, "fn", usbuf->fullname);
794 vcard_add_prop(v, "n", vname);
795 vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
796 vcard_add_prop(v, "email;internet", buf);
797 vcard_write_user(usbuf, v);
803 * When a user is being deleted, we have to remove his/her vCard.
804 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
805 * field, and the same Exclusive ID as the existing card.
807 void vcard_purge(struct ctdluser *usbuf) {
808 struct CtdlMessage *msg;
811 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
812 if (msg == NULL) return;
813 memset(msg, 0, sizeof(struct CtdlMessage));
815 msg->cm_magic = CTDLMESSAGE_MAGIC;
816 msg->cm_anon_type = MES_NORMAL;
817 msg->cm_format_type = 0;
818 msg->cm_fields['A'] = strdup(usbuf->fullname);
819 msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
820 msg->cm_fields['N'] = strdup(NODENAME);
821 msg->cm_fields['M'] = strdup("Purge this vCard\n");
823 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
824 msg->cm_fields['A'], NODENAME);
825 msg->cm_fields['E'] = strdup(buf);
827 msg->cm_fields['S'] = strdup("CANCEL");
829 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
830 CtdlFreeMessage(msg);
835 * Grab vCard directory stuff out of incoming network messages
837 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
841 if (msg == NULL) return(0);
843 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
847 if (msg->cm_format_type != 4) return(0);
849 ptr = msg->cm_fields['M'];
850 if (ptr == NULL) return(0);
851 while (ptr != NULL) {
853 linelen = strcspn(ptr, "\n");
854 if (linelen == 0) return(0); /* end of headers */
856 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
857 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
858 /* It's a vCard. Add it to the directory. */
859 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
863 ptr = strchr((char *)ptr, '\n');
864 if (ptr != NULL) ++ptr;
873 * When a vCard is being removed from the Global Address Book room, remove it
874 * from the directory as well.
876 void vcard_delete_remove(char *room, long msgnum) {
877 struct CtdlMessage *msg;
881 if (msgnum <= 0L) return;
883 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
887 msg = CtdlFetchMessage(msgnum, 1);
888 if (msg == NULL) return;
890 ptr = msg->cm_fields['M'];
891 if (ptr == NULL) goto EOH;
892 while (ptr != NULL) {
893 linelen = strcspn(ptr, "\n");
894 if (linelen == 0) goto EOH;
896 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
897 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
898 /* Bingo! A vCard is being deleted. */
899 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
901 ctdl_vcard_to_ldap(msg, V2L_DELETE);
904 ptr = strchr((char *)ptr, '\n');
905 if (ptr != NULL) ++ptr;
908 EOH: CtdlFreeMessage(msg);
914 * Get Valid Screen Names
916 void cmd_gvsn(char *argbuf)
918 if (CtdlAccessCheck(ac_logged_in)) return;
920 cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
921 cprintf("%s\n", CC->user.fullname);
922 if ( (strlen(CC->cs_inet_fn) > 0) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
923 cprintf("%s\n", CC->cs_inet_fn);
930 * Get Valid Email Addresses
932 void cmd_gvea(char *argbuf)
934 int num_secondary_emails = 0;
938 if (CtdlAccessCheck(ac_logged_in)) return;
940 cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
941 if (strlen(CC->cs_inet_email) > 0) {
942 cprintf("%s\n", CC->cs_inet_email);
944 if (strlen(CC->cs_inet_other_emails) > 0) {
945 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
946 for (i=0; i<num_secondary_emails; ++i) {
947 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
948 cprintf("%s\n", buf);
958 * Callback function for cmd_dvca() that hunts for vCard content types
959 * and outputs any email addresses found within.
961 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
962 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
966 char displayname[256];
972 if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
976 v = vcard_load(content);
977 if (v == NULL) return;
979 extract_friendly_name(displayname, sizeof displayname, v);
980 extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
982 displayname_len = strlen(displayname);
983 for (i=0; i<displayname_len; ++i) {
984 if (displayname[i] == '\"') displayname[i] = ' ';
985 if (displayname[i] == ';') displayname[i] = ',';
986 if (displayname[i] == ',') has_commas = 1;
988 striplt(displayname);
990 cprintf("%s%s%s <%s>\n",
991 (has_commas ? "\"" : ""),
993 (has_commas ? "\"" : ""),
1002 * Back end callback function for cmd_dvca()
1004 * It's basically just passed a list of message numbers, which we're going
1005 * to fetch off the disk and then pass along to the MIME parser via another
1006 * layer of callback...
1008 void dvca_callback(long msgnum, void *userdata) {
1009 struct CtdlMessage *msg = NULL;
1011 msg = CtdlFetchMessage(msgnum, 1);
1012 if (msg == NULL) return;
1013 mime_parser(msg->cm_fields['M'],
1015 *dvca_mime_callback, /* callback function */
1017 NULL, /* user data */
1020 CtdlFreeMessage(msg);
1025 * Dump VCard Addresses
1027 void cmd_dvca(char *argbuf)
1029 if (CtdlAccessCheck(ac_logged_in)) return;
1031 cprintf("%d addresses:\n", LISTING_FOLLOWS);
1032 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1040 void cmd_qdir(char *argbuf) {
1041 char citadel_addr[256];
1042 char internet_addr[256];
1044 if (CtdlAccessCheck(ac_logged_in)) return;
1046 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1048 if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1049 cprintf("%d %s was not found.\n",
1050 ERROR + NO_SUCH_USER, internet_addr);
1054 cprintf("%d %s\n", CIT_OK, citadel_addr);
1058 * Query Directory, in fact an alias to match postfix tcp auth.
1060 void check_get(void) {
1061 char internet_addr[256];
1066 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1067 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1068 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1072 lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1073 while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1075 if (strcasecmp(cmdbuf, "GET "));
1077 struct recptypes *rcpt;
1078 char *argbuf = &cmdbuf[4];
1080 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1081 rcpt = validate_recipients(internet_addr);
1082 if ((rcpt != NULL)&&
1084 (*rcpt->recp_local != '\0')||
1085 (*rcpt->recp_room != '\0')||
1086 (*rcpt->recp_ignet != '\0')))
1089 cprintf("200 OK %s\n", internet_addr);
1090 lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1094 cprintf("500 REJECT noone here by that name.\n");
1096 lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1098 if (rcpt != NULL) free_recipients(rcpt);
1102 void check_get_greeting(void) {
1103 /* dummy function, we have no greeting in this verry simple protocol. */
1108 * We don't know if the Contacts room exists so we just create it at login
1110 void vcard_create_room(void)
1115 /* Create the calendar room if it doesn't already exist */
1116 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1118 /* Set expiration policy to manual; otherwise objects will be lost! */
1119 if (lgetroom(&qr, USERCONTACTSROOM)) {
1120 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1123 qr.QRep.expire_mode = EXPIRE_MANUAL;
1124 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1127 /* Set the view to a calendar view */
1128 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1129 vbuf.v_view = 2; /* 2 = address book view */
1130 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1139 * When a user logs in...
1141 void vcard_session_login_hook(void) {
1142 struct vCard *v = NULL;
1144 v = vcard_get_user(&CC->user);
1145 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1146 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1148 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1151 vcard_create_room();
1156 * Turn an arbitrary RFC822 address into a struct vCard for possible
1157 * inclusion into an address book.
1159 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1161 char user[256], node[256], name[256], email[256], n[256], uid[256];
1165 if (v == NULL) return(NULL);
1167 process_rfc822_addr(addr, user, node, name);
1168 vcard_set_prop(v, "fn", name, 0);
1170 vcard_fn_to_n(n, name, sizeof n);
1171 vcard_set_prop(v, "n", n, 0);
1173 snprintf(email, sizeof email, "%s@%s", user, node);
1174 vcard_set_prop(v, "email;internet", email, 0);
1176 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1177 for (i=0; i<strlen(uid); ++i) {
1178 if (isspace(uid[i])) uid[i] = '_';
1179 uid[i] = tolower(uid[i]);
1181 vcard_set_prop(v, "UID", uid, 0);
1189 * This is called by store_harvested_addresses() to remove from the
1190 * list any addresses we already have in our address book.
1192 void strip_addresses_already_have(long msgnum, void *userdata) {
1193 char *collected_addresses;
1194 struct CtdlMessage *msg = NULL;
1198 char addr[256], user[256], node[256], name[256];
1200 collected_addresses = (char *)userdata;
1202 msg = CtdlFetchMessage(msgnum, 1);
1203 if (msg == NULL) return;
1204 v = vcard_load(msg->cm_fields['M']);
1205 CtdlFreeMessage(msg);
1208 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1210 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1211 extract_token(addr, collected_addresses, j, ',', sizeof addr);
1213 /* Remove the address if we already have it! */
1214 process_rfc822_addr(addr, user, node, name);
1215 snprintf(addr, sizeof addr, "%s@%s", user, node);
1216 if (!strcasecmp(value, addr)) {
1217 remove_token(collected_addresses, j, ',');
1229 * Back end function for store_harvested_addresses()
1231 void store_this_ha(struct addresses_to_be_filed *aptr) {
1232 struct CtdlMessage *vmsg = NULL;
1233 long vmsgnum = (-1L);
1235 struct vCard *v = NULL;
1236 char recipient[256];
1239 /* First remove any addresses we already have in the address book */
1240 usergoto(aptr->roomname, 0, 0, NULL, NULL);
1241 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1242 strip_addresses_already_have, aptr->collected_addresses);
1244 if (strlen(aptr->collected_addresses) > 0)
1245 for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1247 /* Make a vCard out of each address */
1248 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1250 v = vcard_new_from_rfc822_addr(recipient);
1252 vmsg = malloc(sizeof(struct CtdlMessage));
1253 memset(vmsg, 0, sizeof(struct CtdlMessage));
1254 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1255 vmsg->cm_anon_type = MES_NORMAL;
1256 vmsg->cm_format_type = FMT_RFC822;
1257 vmsg->cm_fields['A'] = strdup("Citadel");
1258 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1259 ser = vcard_serialize(v);
1261 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1262 sprintf(vmsg->cm_fields['M'],
1263 "Content-type: " VCARD_MIME_TYPE
1264 "\r\n\r\n%s\r\n", ser);
1269 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1270 vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1271 CtdlFreeMessage(vmsg);
1275 free(aptr->roomname);
1276 free(aptr->collected_addresses);
1282 * When a user sends a message, we may harvest one or more email addresses
1283 * from the recipient list to be added to the user's address book. But we
1284 * want to do this asynchronously so it doesn't keep the user waiting.
1286 void store_harvested_addresses(void) {
1288 struct addresses_to_be_filed *aptr = NULL;
1290 if (atbf == NULL) return;
1292 begin_critical_section(S_ATBF);
1293 while (atbf != NULL) {
1296 end_critical_section(S_ATBF);
1297 store_this_ha(aptr);
1298 begin_critical_section(S_ATBF);
1300 end_critical_section(S_ATBF);
1305 * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
1306 * really this is just so we expose the vCard data to the full text indexer.
1308 void vcard_fixed_output(char *ptr, int len) {
1309 char *serialized_vcard;
1314 serialized_vcard = malloc(len + 1);
1315 safestrncpy(serialized_vcard, ptr, len+1);
1316 v = vcard_load(serialized_vcard);
1317 free(serialized_vcard);
1320 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1321 value = vcard_get_prop(v, "", 0, i++, 0);
1322 cprintf("%s\n", value);
1329 char *serv_postfix_tcpdict(void)
1331 CtdlRegisterServiceHook(config.c_pftcpdict_port, /* Postfix */
1341 char *serv_vcard_init(void)
1347 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1348 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1349 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1350 CtdlRegisterDeleteHook(vcard_delete_remove);
1351 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1352 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1353 CtdlRegisterProtoHook(cmd_igab, "IGAB",
1354 "Initialize Global Address Book");
1355 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1356 CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1357 CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1358 CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1359 CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1360 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1361 CtdlRegisterNetprocHook(vcard_extract_from_network);
1362 CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1363 CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1364 CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1366 /* Create the Global ADdress Book room if necessary */
1367 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1369 /* Set expiration policy to manual; otherwise objects will be lost! */
1370 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1371 qr.QRep.expire_mode = EXPIRE_MANUAL;
1372 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1376 * Also make sure it has a netconfig file, so the networker runs
1377 * on this room even if we don't share it with any other nodes.
1378 * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1381 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1382 fp = fopen(filename, "a");
1383 if (fp != NULL) fclose(fp);
1384 chown(filename, CTDLUID, (-1));
1387 /* return our Subversion id for the Log */