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 "citserver.h"
61 #include "internet_addressing.h"
63 #include "mime_parser.h"
65 #include "serv_vcard.h"
66 #include "serv_ldap.h"
68 #include "ctdl_module.h"
73 * set global flag calling for an aide to validate new users
75 void set_mm_valid(void) {
76 begin_critical_section(S_CONTROL);
78 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
80 end_critical_section(S_CONTROL);
86 * Extract Internet e-mail addresses from a message containing a vCard, and
87 * perform a callback for any found.
89 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
90 void (*callback)(char *, char *) ) {
94 char citadel_address[SIZ];
96 int found_something = 0;
98 if (msg->cm_fields['A'] == NULL) return;
99 if (msg->cm_fields['N'] == NULL) return;
100 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
101 msg->cm_fields['A'], msg->cm_fields['N']);
103 v = vcard_load(msg->cm_fields['M']);
104 if (v == NULL) return;
106 /* Go through the vCard searching for *all* instances of
107 * the "email;internet" key
110 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
114 if (!IsEmptyStr(addr)) {
115 if (callback != NULL) {
116 callback(addr, citadel_address);
125 } while(found_something);
133 * Callback for vcard_add_to_directory()
134 * (Lotsa ugly nested callbacks. Oh well.)
136 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
139 /* We have to validate that we're not stepping on someone else's
140 * email address ... but only if we're logged in. Otherwise it's
141 * probably just the networker or something.
144 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
145 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
146 if (strcasecmp(buf, citadel_addr)) {
147 /* This address belongs to someone else.
148 * Bail out silently without saving.
150 lprintf(CTDL_DEBUG, "DOOP!\n");
155 lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
156 citadel_addr, internet_addr);
157 CtdlDirectoryAddUser(internet_addr, citadel_addr);
162 * Back end function for cmd_igab()
164 void vcard_add_to_directory(long msgnum, void *data) {
165 struct CtdlMessage *msg;
167 msg = CtdlFetchMessage(msgnum, 1);
169 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
173 ctdl_vcard_to_ldap(msg, V2L_WRITE);
176 CtdlFreeMessage(msg);
181 * Initialize Global Adress Book
183 void cmd_igab(char *argbuf) {
184 char hold_rm[ROOMNAMELEN];
186 if (CtdlAccessCheck(ac_aide)) return;
188 strcpy(hold_rm, CC->room.QRname); /* save current room */
190 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
191 getroom(&CC->room, hold_rm);
192 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
196 /* Empty the existing database first.
200 /* We want *all* vCards in this room */
201 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
202 NULL, vcard_add_to_directory, NULL);
204 getroom(&CC->room, hold_rm); /* return to saved room */
205 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
212 * See if there is a valid Internet address in a vCard to use for outbound
213 * Internet messages. If there is, stick it in the buffer.
215 void extract_inet_email_addrs(char *emailaddrbuf, size_t emailaddrbuf_len,
216 char *secemailaddrbuf, size_t secemailaddrbuf_len,
217 struct vCard *v, int local_addrs_only) {
220 int saved_instance = 0;
222 /* Go through the vCard searching for *all* instances of
223 * the "email;internet" key
225 while (s = vcard_get_prop(v, "email;internet", 0, instance++, 0), s != NULL) {
228 if (!IsEmptyStr(addr)) {
229 if ( (IsDirectory(addr, 1)) ||
230 (!local_addrs_only) ) {
232 if ((saved_instance == 1) && (emailaddrbuf != NULL)) {
233 safestrncpy(emailaddrbuf, addr, emailaddrbuf_len);
235 else if ((saved_instance == 2) && (secemailaddrbuf != NULL)) {
236 safestrncpy(secemailaddrbuf, addr, secemailaddrbuf_len);
238 else if ((saved_instance > 2) && (secemailaddrbuf != NULL)) {
239 if ( (strlen(addr) + strlen(secemailaddrbuf) + 2)
240 < secemailaddrbuf_len ) {
241 strcat(secemailaddrbuf, "|");
242 strcat(secemailaddrbuf, addr);
254 * See if there is a name / screen name / friendly name in a vCard to use for outbound
255 * Internet messages. If there is, stick it in the buffer.
257 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
261 s = vcard_get_prop(v, "fn", 0, 0, 0);
263 s = vcard_get_prop(v, "n", 0, 0, 0);
267 safestrncpy(namebuf, s, namebuf_len);
273 * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
275 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
276 void *content, char *cbtype, char *cbcharset, size_t length,
277 char *encoding, void *cbuserdata)
279 struct vCard **v = (struct vCard **) cbuserdata;
281 if ( (!strcasecmp(cbtype, "text/x-vcard"))
282 || (!strcasecmp(cbtype, "text/vcard")) ) {
284 lprintf(CTDL_DEBUG, "Part %s contains a vCard! Loading...\n", partnum);
288 *v = vcard_load(content);
294 * This handler detects whether the user is attempting to save a new
295 * vCard as part of his/her personal configuration, and handles the replace
296 * function accordingly (delete the user's existing vCard in the config room
297 * and in the global address book).
299 int vcard_upload_beforesave(struct CtdlMessage *msg) {
303 struct ctdluser usbuf;
305 struct vCard *v = NULL;
308 int yes_my_citadel_config = 0;
309 int yes_any_vcard_room = 0;
311 if (!CC->logged_in) return(0); /* Only do this if logged in. */
313 /* Is this some user's "My Citadel Config" room? */
314 if ( (CC->room.QRflags && QR_MAILBOX)
315 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
316 /* Yes, we want to do this */
317 yes_my_citadel_config = 1;
319 #ifdef VCARD_SAVES_BY_AIDES_ONLY
320 /* Prevent non-aides from performing registration changes */
321 if (CC->user.axlevel < 6) {
328 /* Is this a room with an address book in it? */
329 if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
330 yes_any_vcard_room = 1;
333 /* If neither condition exists, don't run this hook. */
334 if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
338 /* If this isn't a MIME message, don't bother. */
339 if (msg->cm_format_type != 4) return(0);
341 /* Ok, if we got this far, look into the situation further... */
343 ptr = msg->cm_fields['M'];
344 if (ptr == NULL) return(0);
346 mime_parser(msg->cm_fields['M'],
348 *vcard_extract_vcard,
350 &v, /* user data ptr - put the vcard here */
354 if (v == NULL) return(0); /* no vCards were found in this message */
356 /* If users cannot create their own accounts, they cannot re-register either. */
357 if ( (yes_my_citadel_config) && (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
361 s = vcard_get_prop(v, "FN", 0, 0, 0);
362 if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
364 if (yes_my_citadel_config) {
365 /* Bingo! The user is uploading a new vCard, so
366 * delete the old one. First, figure out which user
367 * is being re-registered...
369 what_user = atol(CC->room.QRname);
371 if (what_user == CC->user.usernum) {
372 /* It's the logged in user. That was easy. */
373 memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
376 else if (getuserbynumber(&usbuf, what_user) == 0) {
377 /* We fetched a valid user record */
381 /* somebody set up us the bomb! */
382 yes_my_citadel_config = 0;
386 if (yes_my_citadel_config) {
387 /* Delete the user's old vCard. This would probably
388 * get taken care of by the replication check, but we
389 * want to make sure there is absolutely only one
390 * vCard in the user's config room at all times.
393 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$");
395 /* Make the author of the message the name of the user. */
396 if (msg->cm_fields['A'] != NULL) {
397 free(msg->cm_fields['A']);
399 msg->cm_fields['A'] = strdup(usbuf.fullname);
402 /* Insert or replace RFC2739-compliant free/busy URL */
403 if (yes_my_citadel_config) {
404 sprintf(buf, "http://%s/%s.vfb",
407 for (i=0; buf[i]; ++i) {
408 if (buf[i] == ' ') buf[i] = '_';
410 vcard_set_prop(v, "FBURL;PREF", buf, 0);
413 /* If the vCard has no UID, then give it one. */
414 s = vcard_get_prop(v, "UID", 0, 0, 0);
417 vcard_set_prop(v, "UID", buf, 0);
420 /* Enforce local UID policy if applicable */
421 if (yes_my_citadel_config) {
422 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
423 vcard_set_prop(v, "UID", buf, 0);
427 * Set the EUID of the message to the UID of the vCard.
429 if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
430 s = vcard_get_prop(v, "UID", 0, 0, 0);
432 msg->cm_fields['E'] = strdup(s);
433 if (msg->cm_fields['U'] == NULL) {
434 msg->cm_fields['U'] = strdup(s);
439 * Set the Subject to the name in the vCard.
441 s = vcard_get_prop(v, "FN", 0, 0, 0);
443 s = vcard_get_prop(v, "N", 0, 0, 0);
446 if (msg->cm_fields['U'] != NULL) {
447 free(msg->cm_fields['U']);
449 msg->cm_fields['U'] = strdup(s);
452 /* Re-serialize it back into the msg body */
453 ser = vcard_serialize(v);
455 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
456 sprintf(msg->cm_fields['M'],
457 "Content-type: " VCARD_MIME_TYPE
458 "\r\n\r\n%s\r\n", ser);
462 /* Now allow the save to complete. */
470 * This handler detects whether the user is attempting to save a new
471 * vCard as part of his/her personal configuration, and handles the replace
472 * function accordingly (copy the vCard from the config room to the global
475 int vcard_upload_aftersave(struct CtdlMessage *msg) {
481 if (!CC->logged_in) return(0); /* Only do this if logged in. */
483 /* If this isn't the configuration room, or if this isn't a MIME
484 * message, don't bother.
486 if (msg->cm_fields['O'] == NULL) return(0);
487 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
488 if (msg->cm_format_type != 4) return(0);
490 ptr = msg->cm_fields['M'];
491 if (ptr == NULL) return(0);
492 while (ptr != NULL) {
494 linelen = strcspn(ptr, "\n");
495 if (linelen == 0) return(0); /* end of headers */
497 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
498 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
500 * Bingo! The user is uploading a new vCard, so
501 * copy it to the Global Address Book room.
504 I = atol(msg->cm_fields['I']);
505 if (I < 0L) return(0);
507 /* Store our Internet return address in memory */
508 v = vcard_load(msg->cm_fields['M']);
509 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
510 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
512 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
515 /* Put it in the Global Address Book room... */
516 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
518 /* ...and also in the directory database. */
519 vcard_add_to_directory(I, NULL);
521 /* Some sites want an Aide to be notified when a
522 * user registers or re-registers...
526 /* ...which also means we need to flag the user */
527 lgetuser(&CC->user, CC->curr_user);
528 CC->user.flags |= (US_REGIS|US_NEEDVALID);
534 ptr = strchr((char *)ptr, '\n');
535 if (ptr != NULL) ++ptr;
544 * back end function used for callbacks
546 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
549 msgnum = (long *) userdata;
550 *msgnum = supplied_msgnum;
555 * If this user has a vcard on disk, read it into memory, otherwise allocate
556 * and return an empty vCard.
558 struct vCard *vcard_get_user(struct ctdluser *u) {
559 char hold_rm[ROOMNAMELEN];
560 char config_rm[ROOMNAMELEN];
561 struct CtdlMessage *msg = NULL;
565 strcpy(hold_rm, CC->room.QRname); /* save current room */
566 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
568 if (getroom(&CC->room, config_rm) != 0) {
569 getroom(&CC->room, hold_rm);
573 /* We want the last (and probably only) vcard in this room */
575 CtdlForEachMessage(MSGS_LAST, 1, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
576 NULL, vcard_gu_backend, (void *)&VCmsgnum );
577 getroom(&CC->room, hold_rm); /* return to saved room */
579 if (VCmsgnum < 0L) return vcard_new();
581 msg = CtdlFetchMessage(VCmsgnum, 1);
582 if (msg == NULL) return vcard_new();
584 v = vcard_load(msg->cm_fields['M']);
585 CtdlFreeMessage(msg);
591 * Store this user's vCard in the appropriate place
594 * Write our config to disk
596 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
601 CtdlMakeTempFileName(temp, sizeof temp);
602 ser = vcard_serialize(v);
604 fp = fopen(temp, "w");
605 if (fp == NULL) return;
607 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
609 fwrite(ser, strlen(ser), 1, fp);
614 /* This handy API function does all the work for us.
615 * NOTE: normally we would want to set that last argument to 1, to
616 * force the system to delete the user's old vCard. But it doesn't
617 * have to, because the vcard_upload_beforesave() hook above
618 * is going to notice what we're trying to do, and delete the old vCard.
620 CtdlWriteObject(USERCONFIGROOM, /* which room */
621 VCARD_MIME_TYPE,/* MIME type */
622 temp, /* temp file */
625 0, /* don't delete others of this type */
634 * Old style "enter registration info" command. This function simply honors
635 * the REGI protocol command, translates the entered parameters into a vCard,
636 * and enters the vCard into the user's configuration.
638 void cmd_regi(char *argbuf) {
641 struct vCard *my_vcard;
647 char tmpaddress[SIZ];
648 char tmpcountry[SIZ];
652 if (!(CC->logged_in)) {
653 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
657 /* If users cannot create their own accounts, they cannot re-register either. */
658 if ( (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
659 cprintf("%d Self-service registration is not allowed here.\n",
660 ERROR + HIGHER_ACCESS_REQUIRED);
663 my_vcard = vcard_get_user(&CC->user);
666 strcpy(tmpstate, "");
668 strcpy(tmpcountry, "USA");
670 cprintf("%d Send registration...\n", SEND_LISTING);
672 while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
673 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
674 if (a==1) strcpy(tmpaddr, buf);
675 if (a==2) strcpy(tmpcity, buf);
676 if (a==3) strcpy(tmpstate, buf);
678 for (c=0; buf[c]; ++c) {
679 if ((buf[c]>='0') && (buf[c]<='9')) {
686 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
687 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
688 if (a==7) strcpy(tmpcountry, buf);
692 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
693 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
694 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
695 vcard_write_user(&CC->user, my_vcard);
696 vcard_free(my_vcard);
701 * Protocol command to fetch registration info for a user
703 void cmd_greg(char *argbuf)
705 struct ctdluser usbuf;
708 char who[USERNAME_SIZE];
712 extract_token(who, argbuf, 0, '|', sizeof who);
714 if (!(CC->logged_in)) {
715 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
719 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
721 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
722 cprintf("%d Higher access required.\n",
723 ERROR + HIGHER_ACCESS_REQUIRED);
727 if (getuser(&usbuf, who) != 0) {
728 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
732 v = vcard_get_user(&usbuf);
734 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
735 cprintf("%ld\n", usbuf.usernum);
736 cprintf("%s\n", usbuf.password);
737 s = vcard_get_prop(v, "n", 0, 0, 0);
738 cprintf("%s\n", s ? s : " "); /* name */
740 s = vcard_get_prop(v, "adr", 0, 0, 0);
741 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
743 extract_token(buf, adr, 2, ';', sizeof buf);
744 cprintf("%s\n", buf); /* street */
745 extract_token(buf, adr, 3, ';', sizeof buf);
746 cprintf("%s\n", buf); /* city */
747 extract_token(buf, adr, 4, ';', sizeof buf);
748 cprintf("%s\n", buf); /* state */
749 extract_token(buf, adr, 5, ';', sizeof buf);
750 cprintf("%s\n", buf); /* zip */
752 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
753 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
761 cprintf("%d\n", usbuf.axlevel);
763 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
764 cprintf("%s\n", s ? s : " ");
765 s = vcard_get_prop(v, "adr", 0, 0, 0);
766 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
768 extract_token(buf, adr, 6, ';', sizeof buf);
769 cprintf("%s\n", buf); /* country */
777 * When a user is being created, create his/her vCard.
779 void vcard_newuser(struct ctdluser *usbuf) {
785 vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
786 lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
788 /* Create and save the vCard */
790 if (v == NULL) return;
791 sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
792 for (i=0; buf[i]; ++i) {
793 if (buf[i] == ' ') buf[i] = '_';
795 vcard_add_prop(v, "fn", usbuf->fullname);
796 vcard_add_prop(v, "n", vname);
797 vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
798 vcard_add_prop(v, "email;internet", buf);
799 vcard_write_user(usbuf, v);
805 * When a user is being deleted, we have to remove his/her vCard.
806 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
807 * field, and the same Exclusive ID as the existing card.
809 void vcard_purge(struct ctdluser *usbuf) {
810 struct CtdlMessage *msg;
813 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
814 if (msg == NULL) return;
815 memset(msg, 0, sizeof(struct CtdlMessage));
817 msg->cm_magic = CTDLMESSAGE_MAGIC;
818 msg->cm_anon_type = MES_NORMAL;
819 msg->cm_format_type = 0;
820 msg->cm_fields['A'] = strdup(usbuf->fullname);
821 msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
822 msg->cm_fields['N'] = strdup(NODENAME);
823 msg->cm_fields['M'] = strdup("Purge this vCard\n");
825 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
826 msg->cm_fields['A'], NODENAME);
827 msg->cm_fields['E'] = strdup(buf);
829 msg->cm_fields['S'] = strdup("CANCEL");
831 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
832 CtdlFreeMessage(msg);
837 * Grab vCard directory stuff out of incoming network messages
839 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
843 if (msg == NULL) return(0);
845 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
849 if (msg->cm_format_type != 4) return(0);
851 ptr = msg->cm_fields['M'];
852 if (ptr == NULL) return(0);
853 while (ptr != NULL) {
855 linelen = strcspn(ptr, "\n");
856 if (linelen == 0) return(0); /* end of headers */
858 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
859 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
860 /* It's a vCard. Add it to the directory. */
861 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
865 ptr = strchr((char *)ptr, '\n');
866 if (ptr != NULL) ++ptr;
875 * When a vCard is being removed from the Global Address Book room, remove it
876 * from the directory as well.
878 void vcard_delete_remove(char *room, long msgnum) {
879 struct CtdlMessage *msg;
883 if (msgnum <= 0L) return;
885 if (room == NULL) return;
887 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
891 msg = CtdlFetchMessage(msgnum, 1);
892 if (msg == NULL) return;
894 ptr = msg->cm_fields['M'];
895 if (ptr == NULL) goto EOH;
896 while (ptr != NULL) {
897 linelen = strcspn(ptr, "\n");
898 if (linelen == 0) goto EOH;
900 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
901 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
902 /* Bingo! A vCard is being deleted. */
903 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
905 ctdl_vcard_to_ldap(msg, V2L_DELETE);
908 ptr = strchr((char *)ptr, '\n');
909 if (ptr != NULL) ++ptr;
912 EOH: CtdlFreeMessage(msg);
918 * Get Valid Screen Names
920 void cmd_gvsn(char *argbuf)
922 if (CtdlAccessCheck(ac_logged_in)) return;
924 cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
925 cprintf("%s\n", CC->user.fullname);
926 if ( (!IsEmptyStr(CC->cs_inet_fn)) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
927 cprintf("%s\n", CC->cs_inet_fn);
934 * Get Valid Email Addresses
936 void cmd_gvea(char *argbuf)
938 int num_secondary_emails = 0;
942 if (CtdlAccessCheck(ac_logged_in)) return;
944 cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
945 if (!IsEmptyStr(CC->cs_inet_email)) {
946 cprintf("%s\n", CC->cs_inet_email);
948 if (!IsEmptyStr(CC->cs_inet_other_emails)) {
949 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
950 for (i=0; i<num_secondary_emails; ++i) {
951 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
952 cprintf("%s\n", buf);
962 * Callback function for cmd_dvca() that hunts for vCard content types
963 * and outputs any email addresses found within.
965 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
966 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
970 char displayname[256];
976 if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
980 v = vcard_load(content);
981 if (v == NULL) return;
983 extract_friendly_name(displayname, sizeof displayname, v);
984 extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
986 displayname_len = strlen(displayname);
987 for (i=0; i<displayname_len; ++i) {
988 if (displayname[i] == '\"') displayname[i] = ' ';
989 if (displayname[i] == ';') displayname[i] = ',';
990 if (displayname[i] == ',') has_commas = 1;
992 striplt(displayname);
994 cprintf("%s%s%s <%s>\n",
995 (has_commas ? "\"" : ""),
997 (has_commas ? "\"" : ""),
1006 * Back end callback function for cmd_dvca()
1008 * It's basically just passed a list of message numbers, which we're going
1009 * to fetch off the disk and then pass along to the MIME parser via another
1010 * layer of callback...
1012 void dvca_callback(long msgnum, void *userdata) {
1013 struct CtdlMessage *msg = NULL;
1015 msg = CtdlFetchMessage(msgnum, 1);
1016 if (msg == NULL) return;
1017 mime_parser(msg->cm_fields['M'],
1019 *dvca_mime_callback, /* callback function */
1021 NULL, /* user data */
1024 CtdlFreeMessage(msg);
1029 * Dump VCard Addresses
1031 void cmd_dvca(char *argbuf)
1033 if (CtdlAccessCheck(ac_logged_in)) return;
1035 cprintf("%d addresses:\n", LISTING_FOLLOWS);
1036 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1044 void cmd_qdir(char *argbuf) {
1045 char citadel_addr[256];
1046 char internet_addr[256];
1048 if (CtdlAccessCheck(ac_logged_in)) return;
1050 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1052 if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1053 cprintf("%d %s was not found.\n",
1054 ERROR + NO_SUCH_USER, internet_addr);
1058 cprintf("%d %s\n", CIT_OK, citadel_addr);
1062 * Query Directory, in fact an alias to match postfix tcp auth.
1064 void check_get(void) {
1065 char internet_addr[256];
1070 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1071 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1072 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1076 lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1077 while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1079 if (strcasecmp(cmdbuf, "GET "));
1081 struct recptypes *rcpt;
1082 char *argbuf = &cmdbuf[4];
1084 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1085 rcpt = validate_recipients(internet_addr);
1086 if ((rcpt != NULL)&&
1088 (*rcpt->recp_local != '\0')||
1089 (*rcpt->recp_room != '\0')||
1090 (*rcpt->recp_ignet != '\0')))
1093 cprintf("200 OK %s\n", internet_addr);
1094 lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1098 cprintf("500 REJECT noone here by that name.\n");
1100 lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1102 if (rcpt != NULL) free_recipients(rcpt);
1106 void check_get_greeting(void) {
1107 /* dummy function, we have no greeting in this verry simple protocol. */
1112 * We don't know if the Contacts room exists so we just create it at login
1114 void vcard_create_room(void)
1119 /* Create the calendar room if it doesn't already exist */
1120 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1122 /* Set expiration policy to manual; otherwise objects will be lost! */
1123 if (lgetroom(&qr, USERCONTACTSROOM)) {
1124 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1127 qr.QRep.expire_mode = EXPIRE_MANUAL;
1128 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1131 /* Set the view to a calendar view */
1132 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1133 vbuf.v_view = 2; /* 2 = address book view */
1134 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1143 * When a user logs in...
1145 void vcard_session_login_hook(void) {
1146 struct vCard *v = NULL;
1148 v = vcard_get_user(&CC->user);
1149 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1150 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1152 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1155 vcard_create_room();
1160 * Turn an arbitrary RFC822 address into a struct vCard for possible
1161 * inclusion into an address book.
1163 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1165 char user[256], node[256], name[256], email[256], n[256], uid[256];
1169 if (v == NULL) return(NULL);
1171 process_rfc822_addr(addr, user, node, name);
1172 vcard_set_prop(v, "fn", name, 0);
1174 vcard_fn_to_n(n, name, sizeof n);
1175 vcard_set_prop(v, "n", n, 0);
1177 snprintf(email, sizeof email, "%s@%s", user, node);
1178 vcard_set_prop(v, "email;internet", email, 0);
1180 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1181 for (i=0; uid[i]; ++i) {
1182 if (isspace(uid[i])) uid[i] = '_';
1183 uid[i] = tolower(uid[i]);
1185 vcard_set_prop(v, "UID", uid, 0);
1193 * This is called by store_harvested_addresses() to remove from the
1194 * list any addresses we already have in our address book.
1196 void strip_addresses_already_have(long msgnum, void *userdata) {
1197 char *collected_addresses;
1198 struct CtdlMessage *msg = NULL;
1202 char addr[256], user[256], node[256], name[256];
1204 collected_addresses = (char *)userdata;
1206 msg = CtdlFetchMessage(msgnum, 1);
1207 if (msg == NULL) return;
1208 v = vcard_load(msg->cm_fields['M']);
1209 CtdlFreeMessage(msg);
1212 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1214 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1215 extract_token(addr, collected_addresses, j, ',', sizeof addr);
1217 /* Remove the address if we already have it! */
1218 process_rfc822_addr(addr, user, node, name);
1219 snprintf(addr, sizeof addr, "%s@%s", user, node);
1220 if (!strcasecmp(value, addr)) {
1221 remove_token(collected_addresses, j, ',');
1233 * Back end function for store_harvested_addresses()
1235 void store_this_ha(struct addresses_to_be_filed *aptr) {
1236 struct CtdlMessage *vmsg = NULL;
1237 long vmsgnum = (-1L);
1239 struct vCard *v = NULL;
1240 char recipient[256];
1243 /* First remove any addresses we already have in the address book */
1244 usergoto(aptr->roomname, 0, 0, NULL, NULL);
1245 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1246 strip_addresses_already_have, aptr->collected_addresses);
1248 if (!IsEmptyStr(aptr->collected_addresses))
1249 for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1251 /* Make a vCard out of each address */
1252 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1254 v = vcard_new_from_rfc822_addr(recipient);
1256 vmsg = malloc(sizeof(struct CtdlMessage));
1257 memset(vmsg, 0, sizeof(struct CtdlMessage));
1258 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1259 vmsg->cm_anon_type = MES_NORMAL;
1260 vmsg->cm_format_type = FMT_RFC822;
1261 vmsg->cm_fields['A'] = strdup("Citadel");
1262 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1263 ser = vcard_serialize(v);
1265 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1266 sprintf(vmsg->cm_fields['M'],
1267 "Content-type: " VCARD_MIME_TYPE
1268 "\r\n\r\n%s\r\n", ser);
1273 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1274 vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1275 CtdlFreeMessage(vmsg);
1279 free(aptr->roomname);
1280 free(aptr->collected_addresses);
1286 * When a user sends a message, we may harvest one or more email addresses
1287 * from the recipient list to be added to the user's address book. But we
1288 * want to do this asynchronously so it doesn't keep the user waiting.
1290 void store_harvested_addresses(void) {
1292 struct addresses_to_be_filed *aptr = NULL;
1294 if (atbf == NULL) return;
1296 begin_critical_section(S_ATBF);
1297 while (atbf != NULL) {
1300 end_critical_section(S_ATBF);
1301 store_this_ha(aptr);
1302 begin_critical_section(S_ATBF);
1304 end_critical_section(S_ATBF);
1309 * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
1310 * really this is just so we expose the vCard data to the full text indexer.
1312 void vcard_fixed_output(char *ptr, int len) {
1313 char *serialized_vcard;
1318 serialized_vcard = malloc(len + 1);
1319 safestrncpy(serialized_vcard, ptr, len+1);
1320 v = vcard_load(serialized_vcard);
1321 free(serialized_vcard);
1324 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1325 value = vcard_get_prop(v, "", 0, i++, 0);
1326 cprintf("%s\n", value);
1333 const char *CitadelServiceDICT_TCP="DICT_TCP";
1335 CTDL_MODULE_INIT(vcard)
1341 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1342 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1343 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1344 CtdlRegisterDeleteHook(vcard_delete_remove);
1345 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1346 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1347 CtdlRegisterProtoHook(cmd_igab, "IGAB",
1348 "Initialize Global Address Book");
1349 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1350 CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1351 CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1352 CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1353 CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1354 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1355 CtdlRegisterNetprocHook(vcard_extract_from_network);
1356 CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1357 CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1358 CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1360 /* Create the Global ADdress Book room if necessary */
1361 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1363 /* Set expiration policy to manual; otherwise objects will be lost! */
1364 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1365 qr.QRep.expire_mode = EXPIRE_MANUAL;
1366 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1370 * Also make sure it has a netconfig file, so the networker runs
1371 * on this room even if we don't share it with any other nodes.
1372 * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1375 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1376 fp = fopen(filename, "a");
1377 if (fp != NULL) fclose(fp);
1378 chown(filename, CTDLUID, (-1));
1381 /* for postfix tcpdict */
1382 CtdlRegisterServiceHook(config.c_pftcpdict_port, /* Postfix */
1387 CitadelServiceDICT_TCP);
1389 /* return our Subversion id for the Log */