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>
50 #include <libcitadel.h>
53 #include "citserver.h"
62 #include "internet_addressing.h"
63 #include "serv_vcard.h"
65 #include "ctdl_module.h"
70 * set global flag calling for an aide to validate new users
72 void set_mm_valid(void) {
73 begin_critical_section(S_CONTROL);
75 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
77 end_critical_section(S_CONTROL);
83 * Extract Internet e-mail addresses from a message containing a vCard, and
84 * perform a callback for any found.
86 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
87 void (*callback)(char *, char *) ) {
91 char citadel_address[SIZ];
93 int found_something = 0;
95 if (msg->cm_fields['A'] == NULL) return;
96 if (msg->cm_fields['N'] == NULL) return;
97 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
98 msg->cm_fields['A'], msg->cm_fields['N']);
100 v = vcard_load(msg->cm_fields['M']);
101 if (v == NULL) return;
103 /* Go through the vCard searching for *all* instances of
104 * the "email;internet" key
107 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
111 if (!IsEmptyStr(addr)) {
112 if (callback != NULL) {
113 callback(addr, citadel_address);
122 } while(found_something);
129 * vCard-to-LDAP conversions.
131 * If 'op' is set to V2L_WRITE, then write
132 * (add, or change if already exists) a directory entry to the
133 * LDAP server, based on the information supplied in a vCard.
135 * If 'op' is set to V2L_DELETE, then delete the entry from LDAP.
139 void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
140 struct vCard *v = NULL;
145 void *objectlist = NULL;
157 if (msg == NULL) return;
158 if (msg->cm_fields['M'] == NULL) return;
159 if (msg->cm_fields['A'] == NULL) return;
160 if (msg->cm_fields['N'] == NULL) return;
162 /* Initialize variables */
163 strcpy(givenname, "");
165 strcpy(calFBURL, "");
167 sprintf(uid, "%s@%s",
172 sprintf(ldap_dn, "euid=%s,ou=%s", msg->cm_fields['E'], msg->cm_fields['N']);
174 /* Are we just deleting? If so, it's simple... */
175 if (op == V2L_DELETE) {
176 (void) CtdlDoDirectoryServiceFunc (ldap_dn, NULL, NULL, "ldap", DIRECTORY_USER_DEL);
181 * If we get to this point then it must be a V2L_WRITE operation.
184 /* First make sure the OU for the user's home Citadel host is created */
185 (void) CtdlDoDirectoryServiceFunc (NULL, msg->cm_fields['N'], NULL, "ldap", DIRECTORY_CREATE_HOST);
187 /* Next create the directory service object */
188 (void) CtdlDoDirectoryServiceFunc(NULL, NULL, &objectlist, "ldap", DIRECTORY_CREATE_OBJECT);
190 /* The first LDAP attribute will be an 'objectclass' list. Citadel
191 * doesn't do anything with this. It's just there for compatibility
194 (void) CtdlDoDirectoryServiceFunc("objectclass", "citadelInetOrgPerson", &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
196 /* Convert the vCard fields to LDAP properties */
197 v = vcard_load(msg->cm_fields['M']);
198 if (v->numprops) for (i=0; i<(v->numprops); ++i) if (striplt(v->prop[i].value), strlen(v->prop[i].value) > 0) {
200 if (!strcasecmp(v->prop[i].name, "n")) {
201 extract_token(sn, v->prop[i].value, 0, ';', sizeof sn);
202 extract_token(givenname, v->prop[i].value, 1, ';', sizeof givenname);
205 if (!strcasecmp(v->prop[i].name, "fn")) {
206 (void) CtdlDoDirectoryServiceFunc("cn", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
210 if (!strcasecmp(v->prop[i].name, "title")) {
211 (void) CtdlDoDirectoryServiceFunc("title", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
214 if (!strcasecmp(v->prop[i].name, "org")) {
215 (void) CtdlDoDirectoryServiceFunc("o", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
218 if ( (!strcasecmp(v->prop[i].name, "adr"))
219 ||(!strncasecmp(v->prop[i].name, "adr;", 4)) ) {
220 /* Unfortunately, we can only do a single address */
224 extract_token(&street[strlen(street)],
225 v->prop[i].value, 0, ';', (sizeof street - strlen(street))); /* po box */
227 extract_token(&street[strlen(street)],
228 v->prop[i].value, 1, ';', (sizeof street - strlen(street))); /* extend addr */
230 extract_token(&street[strlen(street)],
231 v->prop[i].value, 2, ';', (sizeof street - strlen(street))); /* street */
233 extract_token(city, v->prop[i].value, 3, ';', sizeof city);
234 extract_token(state, v->prop[i].value, 4, ';', sizeof state);
235 extract_token(zipcode, v->prop[i].value, 5, ';', sizeof zipcode);
237 // ldap requires these fields to be something
238 if (IsEmptyStr(street)) strcpy(street, "_");
239 if (IsEmptyStr(zipcode)) strcpy(zipcode, "_");
240 if (IsEmptyStr(city)) strcpy(city, "_");
241 if (IsEmptyStr(state)) strcpy(state, "_");
243 (void) CtdlDoDirectoryServiceFunc("street", street, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
244 (void) CtdlDoDirectoryServiceFunc("l", city, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
245 (void) CtdlDoDirectoryServiceFunc("st", state, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
246 (void) CtdlDoDirectoryServiceFunc("postalcode", zipcode, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
250 if (!strcasecmp(v->prop[i].name, "tel;home"))
251 (void) CtdlDoDirectoryServiceFunc("homePhone", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
253 if (!strcasecmp(v->prop[i].name, "tel;fax"))
254 (void) CtdlDoDirectoryServiceFunc("facsimileTelephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
256 if (!strcasecmp(v->prop[i].name, "tel;cell"))
257 (void) CtdlDoDirectoryServiceFunc("mobile", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
259 if ( (!strcasecmp(v->prop[i].name, "tel"))
260 ||(!strncasecmp(v->prop[i].name, "tel;", 4)) ) {
261 (void) CtdlDoDirectoryServiceFunc("telephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
265 if ( (!strcasecmp(v->prop[i].name, "email"))
266 ||(!strcasecmp(v->prop[i].name, "email;internet")) ) {
267 (void) CtdlDoDirectoryServiceFunc("mail", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
270 /* Calendar free/busy URL (take the first one we find, but if a subsequent
271 * one contains the "pref" designation then we go with that instead.)
273 if ( (!strcasecmp(v->prop[i].name, "fburl"))
274 ||(!strncasecmp(v->prop[i].name, "fburl;", 6)) ) {
275 if ( (IsEmptyStr(calFBURL))
276 || (!strncasecmp(v->prop[i].name, "fburl;pref", 10)) ) {
277 safestrncpy(calFBURL, v->prop[i].value, sizeof calFBURL);
282 vcard_free(v); /* Don't need this anymore. */
284 /* "sn" (surname) based on info in vCard */
285 (void) CtdlDoDirectoryServiceFunc("sn", sn, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
287 /* "givenname" (first name) based on info in vCard */
288 if (IsEmptyStr(givenname)) strcpy(givenname, "_");
289 if (IsEmptyStr(sn)) strcpy(sn, "_");
290 (void) CtdlDoDirectoryServiceFunc("givenname", givenname, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
292 /* "uid" is a Kolab compatibility thing. We just do cituser@citnode */
293 (void) CtdlDoDirectoryServiceFunc("uid", uid, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
295 /* Add a "cn" (Common Name) attribute based on the user's screen name,
296 * but only there was no 'fn' (full name) property in the vCard
299 (void) CtdlDoDirectoryServiceFunc("cn", msg->cm_fields['A'], &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
301 /* Add a "calFBURL" attribute if a calendar free/busy URL exists */
302 if (!IsEmptyStr(calFBURL)) {
303 (void) CtdlDoDirectoryServiceFunc("calFBURL", calFBURL, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
306 // Add this messages EUID as the primary key for this entry.
307 (void) CtdlDoDirectoryServiceFunc("euid", msg->cm_fields['E'], &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
310 (void) CtdlDoDirectoryServiceFunc(ldap_dn, NULL, &objectlist, "ldap", DIRECTORY_SAVE_OBJECT);
312 (void) CtdlDoDirectoryServiceFunc(NULL, NULL, &objectlist, "ldap", DIRECTORY_FREE_OBJECT);
313 lprintf(CTDL_DEBUG, "Directory Services write operation complete.\n");
319 * Callback for vcard_add_to_directory()
320 * (Lotsa ugly nested callbacks. Oh well.)
322 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
325 /* We have to validate that we're not stepping on someone else's
326 * email address ... but only if we're logged in. Otherwise it's
327 * probably just the networker or something.
330 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
331 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
332 if (strcasecmp(buf, citadel_addr)) {
333 /* This address belongs to someone else.
334 * Bail out silently without saving.
336 lprintf(CTDL_DEBUG, "DOOP!\n");
341 lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
342 citadel_addr, internet_addr);
343 CtdlDirectoryAddUser(internet_addr, citadel_addr);
348 * Back end function for cmd_igab()
350 void vcard_add_to_directory(long msgnum, void *data) {
351 struct CtdlMessage *msg;
353 msg = CtdlFetchMessage(msgnum, 1);
355 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
358 ctdl_vcard_to_directory(msg, V2L_WRITE);
360 CtdlFreeMessage(msg);
365 * Initialize Global Adress Book
367 void cmd_igab(char *argbuf) {
368 char hold_rm[ROOMNAMELEN];
370 if (CtdlAccessCheck(ac_aide)) return;
372 strcpy(hold_rm, CC->room.QRname); /* save current room */
374 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
375 getroom(&CC->room, hold_rm);
376 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
380 /* Empty the existing database first.
384 /* We want *all* vCards in this room */
385 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
386 NULL, vcard_add_to_directory, NULL);
388 getroom(&CC->room, hold_rm); /* return to saved room */
389 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
396 * See if there is a valid Internet address in a vCard to use for outbound
397 * Internet messages. If there is, stick it in the buffer.
399 void extract_inet_email_addrs(char *emailaddrbuf, size_t emailaddrbuf_len,
400 char *secemailaddrbuf, size_t secemailaddrbuf_len,
401 struct vCard *v, int local_addrs_only) {
404 int saved_instance = 0;
406 /* Go through the vCard searching for *all* instances of
407 * the "email;internet" key
409 while (s = vcard_get_prop(v, "email;internet", 0, instance++, 0), s != NULL) {
412 if (!IsEmptyStr(addr)) {
413 if ( (IsDirectory(addr, 1)) ||
414 (!local_addrs_only) ) {
416 if ((saved_instance == 1) && (emailaddrbuf != NULL)) {
417 safestrncpy(emailaddrbuf, addr, emailaddrbuf_len);
419 else if ((saved_instance == 2) && (secemailaddrbuf != NULL)) {
420 safestrncpy(secemailaddrbuf, addr, secemailaddrbuf_len);
422 else if ((saved_instance > 2) && (secemailaddrbuf != NULL)) {
423 if ( (strlen(addr) + strlen(secemailaddrbuf) + 2)
424 < secemailaddrbuf_len ) {
425 strcat(secemailaddrbuf, "|");
426 strcat(secemailaddrbuf, addr);
438 * See if there is a name / screen name / friendly name in a vCard to use for outbound
439 * Internet messages. If there is, stick it in the buffer.
441 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
445 s = vcard_get_prop(v, "fn", 0, 0, 0);
447 s = vcard_get_prop(v, "n", 0, 0, 0);
451 safestrncpy(namebuf, s, namebuf_len);
457 * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
459 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
460 void *content, char *cbtype, char *cbcharset, size_t length,
461 char *encoding, void *cbuserdata)
463 struct vCard **v = (struct vCard **) cbuserdata;
465 if ( (!strcasecmp(cbtype, "text/x-vcard"))
466 || (!strcasecmp(cbtype, "text/vcard")) ) {
468 lprintf(CTDL_DEBUG, "Part %s contains a vCard! Loading...\n", partnum);
472 *v = vcard_load(content);
478 * This handler detects whether the user is attempting to save a new
479 * vCard as part of his/her personal configuration, and handles the replace
480 * function accordingly (delete the user's existing vCard in the config room
481 * and in the global address book).
483 int vcard_upload_beforesave(struct CtdlMessage *msg) {
487 struct ctdluser usbuf;
489 struct vCard *v = NULL;
492 int yes_my_citadel_config = 0;
493 int yes_any_vcard_room = 0;
495 if (!CC->logged_in) return(0); /* Only do this if logged in. */
497 /* Is this some user's "My Citadel Config" room? */
498 if ( (CC->room.QRflags && QR_MAILBOX)
499 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
500 /* Yes, we want to do this */
501 yes_my_citadel_config = 1;
503 #ifdef VCARD_SAVES_BY_AIDES_ONLY
504 /* Prevent non-aides from performing registration changes */
505 if (CC->user.axlevel < 6) {
512 /* Is this a room with an address book in it? */
513 if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
514 yes_any_vcard_room = 1;
517 /* If neither condition exists, don't run this hook. */
518 if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
522 /* If this isn't a MIME message, don't bother. */
523 if (msg->cm_format_type != 4) return(0);
525 /* Ok, if we got this far, look into the situation further... */
527 ptr = msg->cm_fields['M'];
528 if (ptr == NULL) return(0);
530 mime_parser(msg->cm_fields['M'],
532 *vcard_extract_vcard,
534 &v, /* user data ptr - put the vcard here */
538 if (v == NULL) return(0); /* no vCards were found in this message */
540 /* If users cannot create their own accounts, they cannot re-register either. */
541 if ( (yes_my_citadel_config) && (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
545 s = vcard_get_prop(v, "FN", 0, 0, 0);
546 if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
548 if (yes_my_citadel_config) {
549 /* Bingo! The user is uploading a new vCard, so
550 * delete the old one. First, figure out which user
551 * is being re-registered...
553 what_user = atol(CC->room.QRname);
555 if (what_user == CC->user.usernum) {
556 /* It's the logged in user. That was easy. */
557 memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
560 else if (getuserbynumber(&usbuf, what_user) == 0) {
561 /* We fetched a valid user record */
565 /* somebody set up us the bomb! */
566 yes_my_citadel_config = 0;
570 if (yes_my_citadel_config) {
571 /* Delete the user's old vCard. This would probably
572 * get taken care of by the replication check, but we
573 * want to make sure there is absolutely only one
574 * vCard in the user's config room at all times.
577 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$");
579 /* Make the author of the message the name of the user. */
580 if (msg->cm_fields['A'] != NULL) {
581 free(msg->cm_fields['A']);
583 msg->cm_fields['A'] = strdup(usbuf.fullname);
586 /* Insert or replace RFC2739-compliant free/busy URL */
587 if (yes_my_citadel_config) {
588 sprintf(buf, "http://%s/%s.vfb",
591 for (i=0; buf[i]; ++i) {
592 if (buf[i] == ' ') buf[i] = '_';
594 vcard_set_prop(v, "FBURL;PREF", buf, 0);
597 /* If the vCard has no UID, then give it one. */
598 s = vcard_get_prop(v, "UID", 0, 0, 0);
601 vcard_set_prop(v, "UID", buf, 0);
604 /* Enforce local UID policy if applicable */
605 if (yes_my_citadel_config) {
606 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
607 vcard_set_prop(v, "UID", buf, 0);
611 * Set the EUID of the message to the UID of the vCard.
613 if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
614 s = vcard_get_prop(v, "UID", 0, 0, 0);
616 msg->cm_fields['E'] = strdup(s);
617 if (msg->cm_fields['U'] == NULL) {
618 msg->cm_fields['U'] = strdup(s);
623 * Set the Subject to the name in the vCard.
625 s = vcard_get_prop(v, "FN", 0, 0, 0);
627 s = vcard_get_prop(v, "N", 0, 0, 0);
630 if (msg->cm_fields['U'] != NULL) {
631 free(msg->cm_fields['U']);
633 msg->cm_fields['U'] = strdup(s);
636 /* Re-serialize it back into the msg body */
637 ser = vcard_serialize(v);
639 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
640 sprintf(msg->cm_fields['M'],
641 "Content-type: " VCARD_MIME_TYPE
642 "\r\n\r\n%s\r\n", ser);
646 /* Now allow the save to complete. */
654 * This handler detects whether the user is attempting to save a new
655 * vCard as part of his/her personal configuration, and handles the replace
656 * function accordingly (copy the vCard from the config room to the global
659 int vcard_upload_aftersave(struct CtdlMessage *msg) {
667 if (!CC->logged_in) return(0); /* Only do this if logged in. */
669 /* If this isn't the configuration room, or if this isn't a MIME
670 * message, don't bother.
672 if (msg->cm_fields['O'] == NULL) return(0);
673 if (!strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) is_UserConf = 1;
674 if (!strcasecmp(msg->cm_fields['O'], ADDRESS_BOOK_ROOM)) is_GAB = 1;
675 if (!is_UserConf && !is_GAB) return(0);
676 if (msg->cm_format_type != 4) return(0);
678 ptr = msg->cm_fields['M'];
679 if (ptr == NULL) return(0);
680 while (ptr != NULL) {
682 linelen = strcspn(ptr, "\n");
683 if (linelen == 0) return(0); /* end of headers */
685 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
686 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
688 * Bingo! The user is uploading a new vCard, so
689 * copy it to the Global Address Book room.
692 I = atol(msg->cm_fields['I']);
693 if (I < 0L) return(0);
695 /* Store our Internet return address in memory */
696 v = vcard_load(msg->cm_fields['M']);
697 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
698 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
700 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
704 { // This is not the GAB
705 /* Put it in the Global Address Book room... */
706 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
709 /* ...and also in the directory database. */
710 vcard_add_to_directory(I, NULL);
712 /* Some sites want an Aide to be notified when a
713 * user registers or re-registers...
717 /* ...which also means we need to flag the user */
718 lgetuser(&CC->user, CC->curr_user);
719 CC->user.flags |= (US_REGIS|US_NEEDVALID);
725 ptr = strchr((char *)ptr, '\n');
726 if (ptr != NULL) ++ptr;
735 * back end function used for callbacks
737 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
740 msgnum = (long *) userdata;
741 *msgnum = supplied_msgnum;
746 * If this user has a vcard on disk, read it into memory, otherwise allocate
747 * and return an empty vCard.
749 struct vCard *vcard_get_user(struct ctdluser *u) {
750 char hold_rm[ROOMNAMELEN];
751 char config_rm[ROOMNAMELEN];
752 struct CtdlMessage *msg = NULL;
756 strcpy(hold_rm, CC->room.QRname); /* save current room */
757 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
759 if (getroom(&CC->room, config_rm) != 0) {
760 getroom(&CC->room, hold_rm);
764 /* We want the last (and probably only) vcard in this room */
766 CtdlForEachMessage(MSGS_LAST, 1, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
767 NULL, vcard_gu_backend, (void *)&VCmsgnum );
768 getroom(&CC->room, hold_rm); /* return to saved room */
770 if (VCmsgnum < 0L) return vcard_new();
772 msg = CtdlFetchMessage(VCmsgnum, 1);
773 if (msg == NULL) return vcard_new();
775 v = vcard_load(msg->cm_fields['M']);
776 CtdlFreeMessage(msg);
782 * Store this user's vCard in the appropriate place
785 * Write our config to disk
787 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
792 CtdlMakeTempFileName(temp, sizeof temp);
793 ser = vcard_serialize(v);
795 fp = fopen(temp, "w");
796 if (fp == NULL) return;
798 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
800 fwrite(ser, strlen(ser), 1, fp);
805 /* This handy API function does all the work for us.
806 * NOTE: normally we would want to set that last argument to 1, to
807 * force the system to delete the user's old vCard. But it doesn't
808 * have to, because the vcard_upload_beforesave() hook above
809 * is going to notice what we're trying to do, and delete the old vCard.
811 CtdlWriteObject(USERCONFIGROOM, /* which room */
812 VCARD_MIME_TYPE,/* MIME type */
813 temp, /* temp file */
816 0, /* don't delete others of this type */
825 * Old style "enter registration info" command. This function simply honors
826 * the REGI protocol command, translates the entered parameters into a vCard,
827 * and enters the vCard into the user's configuration.
829 void cmd_regi(char *argbuf) {
832 struct vCard *my_vcard;
838 char tmpaddress[SIZ];
839 char tmpcountry[SIZ];
843 if (!(CC->logged_in)) {
844 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
848 /* If users cannot create their own accounts, they cannot re-register either. */
849 if ( (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
850 cprintf("%d Self-service registration is not allowed here.\n",
851 ERROR + HIGHER_ACCESS_REQUIRED);
854 my_vcard = vcard_get_user(&CC->user);
857 strcpy(tmpstate, "");
859 strcpy(tmpcountry, "USA");
861 cprintf("%d Send registration...\n", SEND_LISTING);
863 while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
864 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
865 if (a==1) strcpy(tmpaddr, buf);
866 if (a==2) strcpy(tmpcity, buf);
867 if (a==3) strcpy(tmpstate, buf);
869 for (c=0; buf[c]; ++c) {
870 if ((buf[c]>='0') && (buf[c]<='9')) {
877 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
878 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
879 if (a==7) strcpy(tmpcountry, buf);
883 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
884 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
885 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
886 vcard_write_user(&CC->user, my_vcard);
887 vcard_free(my_vcard);
892 * Protocol command to fetch registration info for a user
894 void cmd_greg(char *argbuf)
896 struct ctdluser usbuf;
899 char who[USERNAME_SIZE];
903 extract_token(who, argbuf, 0, '|', sizeof who);
905 if (!(CC->logged_in)) {
906 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
910 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
912 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
913 cprintf("%d Higher access required.\n",
914 ERROR + HIGHER_ACCESS_REQUIRED);
918 if (getuser(&usbuf, who) != 0) {
919 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
923 v = vcard_get_user(&usbuf);
925 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
926 cprintf("%ld\n", usbuf.usernum);
927 cprintf("%s\n", usbuf.password);
928 s = vcard_get_prop(v, "n", 0, 0, 0);
929 cprintf("%s\n", s ? s : " "); /* name */
931 s = vcard_get_prop(v, "adr", 0, 0, 0);
932 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
934 extract_token(buf, adr, 2, ';', sizeof buf);
935 cprintf("%s\n", buf); /* street */
936 extract_token(buf, adr, 3, ';', sizeof buf);
937 cprintf("%s\n", buf); /* city */
938 extract_token(buf, adr, 4, ';', sizeof buf);
939 cprintf("%s\n", buf); /* state */
940 extract_token(buf, adr, 5, ';', sizeof buf);
941 cprintf("%s\n", buf); /* zip */
943 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
944 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
952 cprintf("%d\n", usbuf.axlevel);
954 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
955 cprintf("%s\n", s ? s : " ");
956 s = vcard_get_prop(v, "adr", 0, 0, 0);
957 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
959 extract_token(buf, adr, 6, ';', sizeof buf);
960 cprintf("%s\n", buf); /* country */
968 * When a user is being created, create his/her vCard.
970 void vcard_newuser(struct ctdluser *usbuf) {
976 vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
977 lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
979 /* Create and save the vCard */
981 if (v == NULL) return;
982 sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
983 for (i=0; buf[i]; ++i) {
984 if (buf[i] == ' ') buf[i] = '_';
986 vcard_add_prop(v, "fn", usbuf->fullname);
987 vcard_add_prop(v, "n", vname);
988 vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
989 vcard_add_prop(v, "email;internet", buf);
990 vcard_write_user(usbuf, v);
996 * When a user is being deleted, we have to remove his/her vCard.
997 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
998 * field, and the same Exclusive ID as the existing card.
1000 void vcard_purge(struct ctdluser *usbuf) {
1001 struct CtdlMessage *msg;
1004 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1005 if (msg == NULL) return;
1006 memset(msg, 0, sizeof(struct CtdlMessage));
1008 msg->cm_magic = CTDLMESSAGE_MAGIC;
1009 msg->cm_anon_type = MES_NORMAL;
1010 msg->cm_format_type = 0;
1011 msg->cm_fields['A'] = strdup(usbuf->fullname);
1012 msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
1013 msg->cm_fields['N'] = strdup(NODENAME);
1014 msg->cm_fields['M'] = strdup("Purge this vCard\n");
1016 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
1017 msg->cm_fields['A'], NODENAME);
1018 msg->cm_fields['E'] = strdup(buf);
1020 msg->cm_fields['S'] = strdup("CANCEL");
1022 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
1023 CtdlFreeMessage(msg);
1028 * Grab vCard directory stuff out of incoming network messages
1030 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
1034 if (msg == NULL) return(0);
1036 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
1040 if (msg->cm_format_type != 4) return(0);
1042 ptr = msg->cm_fields['M'];
1043 if (ptr == NULL) return(0);
1044 while (ptr != NULL) {
1046 linelen = strcspn(ptr, "\n");
1047 if (linelen == 0) return(0); /* end of headers */
1049 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
1050 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
1051 /* It's a vCard. Add it to the directory. */
1052 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
1056 ptr = strchr((char *)ptr, '\n');
1057 if (ptr != NULL) ++ptr;
1066 * When a vCard is being removed from the Global Address Book room, remove it
1067 * from the directory as well.
1069 void vcard_delete_remove(char *room, long msgnum) {
1070 struct CtdlMessage *msg;
1074 if (msgnum <= 0L) return;
1076 if (room == NULL) return;
1078 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
1082 msg = CtdlFetchMessage(msgnum, 1);
1083 if (msg == NULL) return;
1085 ptr = msg->cm_fields['M'];
1086 if (ptr == NULL) goto EOH;
1087 while (ptr != NULL) {
1088 linelen = strcspn(ptr, "\n");
1089 if (linelen == 0) goto EOH;
1091 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
1092 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
1093 /* Bingo! A vCard is being deleted. */
1094 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
1095 ctdl_vcard_to_directory(msg, V2L_DELETE);
1097 ptr = strchr((char *)ptr, '\n');
1098 if (ptr != NULL) ++ptr;
1101 EOH: CtdlFreeMessage(msg);
1107 * Get Valid Screen Names
1109 void cmd_gvsn(char *argbuf)
1111 if (CtdlAccessCheck(ac_logged_in)) return;
1113 cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
1114 cprintf("%s\n", CC->user.fullname);
1115 if ( (!IsEmptyStr(CC->cs_inet_fn)) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
1116 cprintf("%s\n", CC->cs_inet_fn);
1123 * Get Valid Email Addresses
1125 void cmd_gvea(char *argbuf)
1127 int num_secondary_emails = 0;
1131 if (CtdlAccessCheck(ac_logged_in)) return;
1133 cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
1134 if (!IsEmptyStr(CC->cs_inet_email)) {
1135 cprintf("%s\n", CC->cs_inet_email);
1137 if (!IsEmptyStr(CC->cs_inet_other_emails)) {
1138 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
1139 for (i=0; i<num_secondary_emails; ++i) {
1140 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
1141 cprintf("%s\n", buf);
1151 * Callback function for cmd_dvca() that hunts for vCard content types
1152 * and outputs any email addresses found within.
1154 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
1155 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1159 char displayname[256];
1160 int displayname_len;
1161 char emailaddr[256];
1165 if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
1169 v = vcard_load(content);
1170 if (v == NULL) return;
1172 extract_friendly_name(displayname, sizeof displayname, v);
1173 extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
1175 displayname_len = strlen(displayname);
1176 for (i=0; i<displayname_len; ++i) {
1177 if (displayname[i] == '\"') displayname[i] = ' ';
1178 if (displayname[i] == ';') displayname[i] = ',';
1179 if (displayname[i] == ',') has_commas = 1;
1181 striplt(displayname);
1183 cprintf("%s%s%s <%s>\n",
1184 (has_commas ? "\"" : ""),
1186 (has_commas ? "\"" : ""),
1195 * Back end callback function for cmd_dvca()
1197 * It's basically just passed a list of message numbers, which we're going
1198 * to fetch off the disk and then pass along to the MIME parser via another
1199 * layer of callback...
1201 void dvca_callback(long msgnum, void *userdata) {
1202 struct CtdlMessage *msg = NULL;
1204 msg = CtdlFetchMessage(msgnum, 1);
1205 if (msg == NULL) return;
1206 mime_parser(msg->cm_fields['M'],
1208 *dvca_mime_callback, /* callback function */
1210 NULL, /* user data */
1213 CtdlFreeMessage(msg);
1218 * Dump VCard Addresses
1220 void cmd_dvca(char *argbuf)
1222 if (CtdlAccessCheck(ac_logged_in)) return;
1224 cprintf("%d addresses:\n", LISTING_FOLLOWS);
1225 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1233 void cmd_qdir(char *argbuf) {
1234 char citadel_addr[256];
1235 char internet_addr[256];
1237 if (CtdlAccessCheck(ac_logged_in)) return;
1239 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1241 if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1242 cprintf("%d %s was not found.\n",
1243 ERROR + NO_SUCH_USER, internet_addr);
1247 cprintf("%d %s\n", CIT_OK, citadel_addr);
1251 * Query Directory, in fact an alias to match postfix tcp auth.
1253 void check_get(void) {
1254 char internet_addr[256];
1259 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1260 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1261 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1265 lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1266 while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1268 if (strcasecmp(cmdbuf, "GET "));
1270 struct recptypes *rcpt;
1271 char *argbuf = &cmdbuf[4];
1273 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1274 rcpt = validate_recipients(internet_addr);
1275 if ((rcpt != NULL)&&
1277 (*rcpt->recp_local != '\0')||
1278 (*rcpt->recp_room != '\0')||
1279 (*rcpt->recp_ignet != '\0')))
1282 cprintf("200 OK %s\n", internet_addr);
1283 lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1287 cprintf("500 REJECT noone here by that name.\n");
1289 lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1291 if (rcpt != NULL) free_recipients(rcpt);
1295 void check_get_greeting(void) {
1296 /* dummy function, we have no greeting in this verry simple protocol. */
1301 * We don't know if the Contacts room exists so we just create it at login
1303 void vcard_create_room(void)
1308 /* Create the calendar room if it doesn't already exist */
1309 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1311 /* Set expiration policy to manual; otherwise objects will be lost! */
1312 if (lgetroom(&qr, USERCONTACTSROOM)) {
1313 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1316 qr.QRep.expire_mode = EXPIRE_MANUAL;
1317 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1320 /* Set the view to a calendar view */
1321 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1322 vbuf.v_view = 2; /* 2 = address book view */
1323 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1332 * When a user logs in...
1334 void vcard_session_login_hook(void) {
1335 struct vCard *v = NULL;
1337 v = vcard_get_user(&CC->user);
1338 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1339 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1341 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1344 vcard_create_room();
1349 * Turn an arbitrary RFC822 address into a struct vCard for possible
1350 * inclusion into an address book.
1352 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1354 char user[256], node[256], name[256], email[256], n[256], uid[256];
1358 if (v == NULL) return(NULL);
1360 process_rfc822_addr(addr, user, node, name);
1361 vcard_set_prop(v, "fn", name, 0);
1363 vcard_fn_to_n(n, name, sizeof n);
1364 vcard_set_prop(v, "n", n, 0);
1366 snprintf(email, sizeof email, "%s@%s", user, node);
1367 vcard_set_prop(v, "email;internet", email, 0);
1369 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1370 for (i=0; uid[i]; ++i) {
1371 if (isspace(uid[i])) uid[i] = '_';
1372 uid[i] = tolower(uid[i]);
1374 vcard_set_prop(v, "UID", uid, 0);
1382 * This is called by store_harvested_addresses() to remove from the
1383 * list any addresses we already have in our address book.
1385 void strip_addresses_already_have(long msgnum, void *userdata) {
1386 char *collected_addresses;
1387 struct CtdlMessage *msg = NULL;
1391 char addr[256], user[256], node[256], name[256];
1393 collected_addresses = (char *)userdata;
1395 msg = CtdlFetchMessage(msgnum, 1);
1396 if (msg == NULL) return;
1397 v = vcard_load(msg->cm_fields['M']);
1398 CtdlFreeMessage(msg);
1401 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1403 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1404 extract_token(addr, collected_addresses, j, ',', sizeof addr);
1406 /* Remove the address if we already have it! */
1407 process_rfc822_addr(addr, user, node, name);
1408 snprintf(addr, sizeof addr, "%s@%s", user, node);
1409 if (!strcasecmp(value, addr)) {
1410 remove_token(collected_addresses, j, ',');
1422 * Back end function for store_harvested_addresses()
1424 void store_this_ha(struct addresses_to_be_filed *aptr) {
1425 struct CtdlMessage *vmsg = NULL;
1426 long vmsgnum = (-1L);
1428 struct vCard *v = NULL;
1429 char recipient[256];
1432 /* First remove any addresses we already have in the address book */
1433 usergoto(aptr->roomname, 0, 0, NULL, NULL);
1434 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1435 strip_addresses_already_have, aptr->collected_addresses);
1437 if (!IsEmptyStr(aptr->collected_addresses))
1438 for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1440 /* Make a vCard out of each address */
1441 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1443 v = vcard_new_from_rfc822_addr(recipient);
1445 vmsg = malloc(sizeof(struct CtdlMessage));
1446 memset(vmsg, 0, sizeof(struct CtdlMessage));
1447 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1448 vmsg->cm_anon_type = MES_NORMAL;
1449 vmsg->cm_format_type = FMT_RFC822;
1450 vmsg->cm_fields['A'] = strdup("Citadel");
1451 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1452 ser = vcard_serialize(v);
1454 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1455 sprintf(vmsg->cm_fields['M'],
1456 "Content-type: " VCARD_MIME_TYPE
1457 "\r\n\r\n%s\r\n", ser);
1462 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1463 vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1464 CtdlFreeMessage(vmsg);
1468 free(aptr->roomname);
1469 free(aptr->collected_addresses);
1475 * When a user sends a message, we may harvest one or more email addresses
1476 * from the recipient list to be added to the user's address book. But we
1477 * want to do this asynchronously so it doesn't keep the user waiting.
1479 void store_harvested_addresses(void) {
1481 struct addresses_to_be_filed *aptr = NULL;
1483 if (atbf == NULL) return;
1485 begin_critical_section(S_ATBF);
1486 while (atbf != NULL) {
1489 end_critical_section(S_ATBF);
1490 store_this_ha(aptr);
1491 begin_critical_section(S_ATBF);
1493 end_critical_section(S_ATBF);
1498 * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
1499 * really this is just so we expose the vCard data to the full text indexer.
1501 void vcard_fixed_output(char *ptr, int len) {
1502 char *serialized_vcard;
1507 serialized_vcard = malloc(len + 1);
1508 safestrncpy(serialized_vcard, ptr, len+1);
1509 v = vcard_load(serialized_vcard);
1510 free(serialized_vcard);
1513 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1514 value = vcard_get_prop(v, "", 0, i++, 0);
1515 cprintf("%s\n", value);
1522 const char *CitadelServiceDICT_TCP="DICT_TCP";
1524 CTDL_MODULE_INIT(vcard)
1532 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1533 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1534 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1535 CtdlRegisterDeleteHook(vcard_delete_remove);
1536 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1537 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1538 CtdlRegisterProtoHook(cmd_igab, "IGAB",
1539 "Initialize Global Address Book");
1540 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1541 CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1542 CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1543 CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1544 CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1545 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1546 CtdlRegisterNetprocHook(vcard_extract_from_network);
1547 CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1548 CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1549 CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1551 /* Create the Global ADdress Book room if necessary */
1552 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1554 /* Set expiration policy to manual; otherwise objects will be lost! */
1555 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1556 qr.QRep.expire_mode = EXPIRE_MANUAL;
1557 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1561 * Also make sure it has a netconfig file, so the networker runs
1562 * on this room even if we don't share it with any other nodes.
1563 * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1566 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1567 fp = fopen(filename, "a");
1568 if (fp != NULL) fclose(fp);
1569 chown(filename, CTDLUID, (-1));
1572 /* for postfix tcpdict */
1573 CtdlRegisterServiceHook(config.c_pftcpdict_port, /* Postfix */
1578 CitadelServiceDICT_TCP);
1581 /* return our Subversion id for the Log */