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"
67 #include "ctdl_module.h"
72 * set global flag calling for an aide to validate new users
74 void set_mm_valid(void) {
75 begin_critical_section(S_CONTROL);
77 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
79 end_critical_section(S_CONTROL);
85 * Extract Internet e-mail addresses from a message containing a vCard, and
86 * perform a callback for any found.
88 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
89 void (*callback)(char *, char *) ) {
93 char citadel_address[SIZ];
95 int found_something = 0;
97 if (msg->cm_fields['A'] == NULL) return;
98 if (msg->cm_fields['N'] == NULL) return;
99 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
100 msg->cm_fields['A'], msg->cm_fields['N']);
102 v = vcard_load(msg->cm_fields['M']);
103 if (v == NULL) return;
105 /* Go through the vCard searching for *all* instances of
106 * the "email;internet" key
109 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
113 if (!IsEmptyStr(addr)) {
114 if (callback != NULL) {
115 callback(addr, citadel_address);
124 } while(found_something);
131 * vCard-to-LDAP conversions.
133 * If 'op' is set to V2L_WRITE, then write
134 * (add, or change if already exists) a directory entry to the
135 * LDAP server, based on the information supplied in a vCard.
137 * If 'op' is set to V2L_DELETE, then delete the entry from LDAP.
141 void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
142 struct vCard *v = NULL;
149 void *objectlist = NULL;
160 if (msg == NULL) return;
161 if (msg->cm_fields['M'] == NULL) return;
162 if (msg->cm_fields['A'] == NULL) return;
163 if (msg->cm_fields['N'] == NULL) return;
165 /* Initialize variables */
166 strcpy(givenname, "");
168 strcpy(calFBURL, "");
170 sprintf(uid, "%s@%s",
175 /* Are we just deleting? If so, it's simple... */
176 if (op == V2L_DELETE) {
177 (void) CtdlDoDirectoryServiceFunc (msg->cm_fields['A'], msg->cm_fields['N'], NULL, "ldap", DIRECTORY_USER_DEL);
182 * If we get to this point then it must be a V2L_WRITE operation.
185 /* First make sure the OU for the user's home Citadel host is created */
186 (void) CtdlDoDirectoryServiceFunc (NULL, msg->cm_fields['N'], NULL, "ldap", DIRECTORY_CREATE_HOST);
188 /* Next create the directory service object */
189 (void) CtdlDoDirectoryServiceFunc(NULL, NULL, &objectlist, "ldap", DIRECTORY_CREATE_OBJECT);
191 /* The first LDAP attribute will be an 'objectclass' list. Citadel
192 * doesn't do anything with this. It's just there for compatibility
195 (void) CtdlDoDirectoryServiceFunc("objectclass", "citadelInetOrgPerson", &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
197 /* Convert the vCard fields to LDAP properties */
198 v = vcard_load(msg->cm_fields['M']);
199 if (v->numprops) for (i=0; i<(v->numprops); ++i) if (striplt(v->prop[i].value), strlen(v->prop[i].value) > 0) {
201 if (!strcasecmp(v->prop[i].name, "n")) {
202 extract_token(sn, v->prop[i].value, 0, ';', sizeof sn);
203 extract_token(givenname, v->prop[i].value, 1, ';', sizeof givenname);
206 if (!strcasecmp(v->prop[i].name, "fn")) {
207 (void) CtdlDoDirectoryServiceFunc("cn", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
211 if (!strcasecmp(v->prop[i].name, "title")) {
212 (void) CtdlDoDirectoryServiceFunc("title", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
215 if (!strcasecmp(v->prop[i].name, "org")) {
216 (void) CtdlDoDirectoryServiceFunc("o", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
219 if ( (!strcasecmp(v->prop[i].name, "adr"))
220 ||(!strncasecmp(v->prop[i].name, "adr;", 4)) ) {
221 /* Unfortunately, we can only do a single address */
225 extract_token(&street[strlen(street)],
226 v->prop[i].value, 0, ';', (sizeof street - strlen(street))); /* po box */
228 extract_token(&street[strlen(street)],
229 v->prop[i].value, 1, ';', (sizeof street - strlen(street))); /* extend addr */
231 extract_token(&street[strlen(street)],
232 v->prop[i].value, 2, ';', (sizeof street - strlen(street))); /* street */
234 extract_token(city, v->prop[i].value, 3, ';', sizeof city);
235 extract_token(state, v->prop[i].value, 4, ';', sizeof state);
236 extract_token(zipcode, v->prop[i].value, 5, ';', sizeof zipcode);
238 (void) CtdlDoDirectoryServiceFunc("street", street, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
239 (void) CtdlDoDirectoryServiceFunc("l", city, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
240 (void) CtdlDoDirectoryServiceFunc("st", state, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
241 (void) CtdlDoDirectoryServiceFunc("postalcode", zipcode, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
245 if ( (!strcasecmp(v->prop[i].name, "tel"))
246 ||(!strncasecmp(v->prop[i].name, "tel;", 4)) ) {
248 /* The first 'tel' property creates the 'telephoneNumber' attribute */
249 if (num_phones == 1) {
250 (void) CtdlDoDirectoryServiceFunc("telephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
252 /* Subsequent 'tel' properties *add to* the 'telephoneNumber' attribute */
254 (void) CtdlDoDirectoryServiceFunc("telephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
259 if ( (!strcasecmp(v->prop[i].name, "email"))
260 ||(!strcasecmp(v->prop[i].name, "email;internet")) ) {
263 lprintf(CTDL_DEBUG, "email addr %d\n", num_emails);
265 /* The first email address creates the 'mail' attribute */
266 if (num_emails == 1) {
267 (void) CtdlDoDirectoryServiceFunc("mail", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
269 /* The second and subsequent email address creates the 'alias' attribute */
270 else if (num_emails >= 2) {
271 (void) CtdlDoDirectoryServiceFunc("alias", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
275 /* Calendar free/busy URL (take the first one we find, but if a subsequent
276 * one contains the "pref" designation then we go with that instead.)
278 if ( (!strcasecmp(v->prop[i].name, "fburl"))
279 ||(!strncasecmp(v->prop[i].name, "fburl;", 6)) ) {
280 if ( (IsEmptyStr(calFBURL))
281 || (!strncasecmp(v->prop[i].name, "fburl;pref", 10)) ) {
282 safestrncpy(calFBURL, v->prop[i].value, sizeof calFBURL);
287 vcard_free(v); /* Don't need this anymore. */
289 /* "sn" (surname) based on info in vCard */
290 (void) CtdlDoDirectoryServiceFunc("sn", sn, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
292 /* "givenname" (first name) based on info in vCard */
293 if (IsEmptyStr(givenname)) strcpy(givenname, "_");
294 if (IsEmptyStr(sn)) strcpy(sn, "_");
295 (void) CtdlDoDirectoryServiceFunc("givenname", givenname, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
297 /* "uid" is a Kolab compatibility thing. We just do cituser@citnode */
298 (void) CtdlDoDirectoryServiceFunc("uid", uid, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
300 /* Add a "cn" (Common Name) attribute based on the user's screen name,
301 * but only there was no 'fn' (full name) property in the vCard
304 (void) CtdlDoDirectoryServiceFunc("cn", msg->cm_fields['A'], &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
307 /* Add a "calFBURL" attribute if a calendar free/busy URL exists */
308 if (!IsEmptyStr(calFBURL)) {
309 (void) CtdlDoDirectoryServiceFunc("calFBURL", calFBURL, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
312 (void) CtdlDoDirectoryServiceFunc(msg->cm_fields['A'], msg->cm_fields['N'], &objectlist, "ldap", DIRECTORY_SAVE_OBJECT);
314 (void) CtdlDoDirectoryServiceFunc(NULL, NULL, &objectlist, "ldap", DIRECTORY_FREE_OBJECT);
315 lprintf(CTDL_DEBUG, "LDAP write operation complete.\n");
321 * Callback for vcard_add_to_directory()
322 * (Lotsa ugly nested callbacks. Oh well.)
324 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
327 /* We have to validate that we're not stepping on someone else's
328 * email address ... but only if we're logged in. Otherwise it's
329 * probably just the networker or something.
332 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
333 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
334 if (strcasecmp(buf, citadel_addr)) {
335 /* This address belongs to someone else.
336 * Bail out silently without saving.
338 lprintf(CTDL_DEBUG, "DOOP!\n");
343 lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
344 citadel_addr, internet_addr);
345 CtdlDirectoryAddUser(internet_addr, citadel_addr);
350 * Back end function for cmd_igab()
352 void vcard_add_to_directory(long msgnum, void *data) {
353 struct CtdlMessage *msg;
355 msg = CtdlFetchMessage(msgnum, 1);
357 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
360 ctdl_vcard_to_directory(msg, V2L_WRITE);
362 CtdlFreeMessage(msg);
367 * Initialize Global Adress Book
369 void cmd_igab(char *argbuf) {
370 char hold_rm[ROOMNAMELEN];
372 if (CtdlAccessCheck(ac_aide)) return;
374 strcpy(hold_rm, CC->room.QRname); /* save current room */
376 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
377 getroom(&CC->room, hold_rm);
378 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
382 /* Empty the existing database first.
386 /* We want *all* vCards in this room */
387 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
388 NULL, vcard_add_to_directory, NULL);
390 getroom(&CC->room, hold_rm); /* return to saved room */
391 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
398 * See if there is a valid Internet address in a vCard to use for outbound
399 * Internet messages. If there is, stick it in the buffer.
401 void extract_inet_email_addrs(char *emailaddrbuf, size_t emailaddrbuf_len,
402 char *secemailaddrbuf, size_t secemailaddrbuf_len,
403 struct vCard *v, int local_addrs_only) {
406 int saved_instance = 0;
408 /* Go through the vCard searching for *all* instances of
409 * the "email;internet" key
411 while (s = vcard_get_prop(v, "email;internet", 0, instance++, 0), s != NULL) {
414 if (!IsEmptyStr(addr)) {
415 if ( (IsDirectory(addr, 1)) ||
416 (!local_addrs_only) ) {
418 if ((saved_instance == 1) && (emailaddrbuf != NULL)) {
419 safestrncpy(emailaddrbuf, addr, emailaddrbuf_len);
421 else if ((saved_instance == 2) && (secemailaddrbuf != NULL)) {
422 safestrncpy(secemailaddrbuf, addr, secemailaddrbuf_len);
424 else if ((saved_instance > 2) && (secemailaddrbuf != NULL)) {
425 if ( (strlen(addr) + strlen(secemailaddrbuf) + 2)
426 < secemailaddrbuf_len ) {
427 strcat(secemailaddrbuf, "|");
428 strcat(secemailaddrbuf, addr);
440 * See if there is a name / screen name / friendly name in a vCard to use for outbound
441 * Internet messages. If there is, stick it in the buffer.
443 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
447 s = vcard_get_prop(v, "fn", 0, 0, 0);
449 s = vcard_get_prop(v, "n", 0, 0, 0);
453 safestrncpy(namebuf, s, namebuf_len);
459 * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
461 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
462 void *content, char *cbtype, char *cbcharset, size_t length,
463 char *encoding, void *cbuserdata)
465 struct vCard **v = (struct vCard **) cbuserdata;
467 if ( (!strcasecmp(cbtype, "text/x-vcard"))
468 || (!strcasecmp(cbtype, "text/vcard")) ) {
470 lprintf(CTDL_DEBUG, "Part %s contains a vCard! Loading...\n", partnum);
474 *v = vcard_load(content);
480 * This handler detects whether the user is attempting to save a new
481 * vCard as part of his/her personal configuration, and handles the replace
482 * function accordingly (delete the user's existing vCard in the config room
483 * and in the global address book).
485 int vcard_upload_beforesave(struct CtdlMessage *msg) {
489 struct ctdluser usbuf;
491 struct vCard *v = NULL;
494 int yes_my_citadel_config = 0;
495 int yes_any_vcard_room = 0;
497 if (!CC->logged_in) return(0); /* Only do this if logged in. */
499 /* Is this some user's "My Citadel Config" room? */
500 if ( (CC->room.QRflags && QR_MAILBOX)
501 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
502 /* Yes, we want to do this */
503 yes_my_citadel_config = 1;
505 #ifdef VCARD_SAVES_BY_AIDES_ONLY
506 /* Prevent non-aides from performing registration changes */
507 if (CC->user.axlevel < 6) {
514 /* Is this a room with an address book in it? */
515 if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
516 yes_any_vcard_room = 1;
519 /* If neither condition exists, don't run this hook. */
520 if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
524 /* If this isn't a MIME message, don't bother. */
525 if (msg->cm_format_type != 4) return(0);
527 /* Ok, if we got this far, look into the situation further... */
529 ptr = msg->cm_fields['M'];
530 if (ptr == NULL) return(0);
532 mime_parser(msg->cm_fields['M'],
534 *vcard_extract_vcard,
536 &v, /* user data ptr - put the vcard here */
540 if (v == NULL) return(0); /* no vCards were found in this message */
542 /* If users cannot create their own accounts, they cannot re-register either. */
543 if ( (yes_my_citadel_config) && (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
547 s = vcard_get_prop(v, "FN", 0, 0, 0);
548 if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
550 if (yes_my_citadel_config) {
551 /* Bingo! The user is uploading a new vCard, so
552 * delete the old one. First, figure out which user
553 * is being re-registered...
555 what_user = atol(CC->room.QRname);
557 if (what_user == CC->user.usernum) {
558 /* It's the logged in user. That was easy. */
559 memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
562 else if (getuserbynumber(&usbuf, what_user) == 0) {
563 /* We fetched a valid user record */
567 /* somebody set up us the bomb! */
568 yes_my_citadel_config = 0;
572 if (yes_my_citadel_config) {
573 /* Delete the user's old vCard. This would probably
574 * get taken care of by the replication check, but we
575 * want to make sure there is absolutely only one
576 * vCard in the user's config room at all times.
579 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$");
581 /* Make the author of the message the name of the user. */
582 if (msg->cm_fields['A'] != NULL) {
583 free(msg->cm_fields['A']);
585 msg->cm_fields['A'] = strdup(usbuf.fullname);
588 /* Insert or replace RFC2739-compliant free/busy URL */
589 if (yes_my_citadel_config) {
590 sprintf(buf, "http://%s/%s.vfb",
593 for (i=0; buf[i]; ++i) {
594 if (buf[i] == ' ') buf[i] = '_';
596 vcard_set_prop(v, "FBURL;PREF", buf, 0);
599 /* If the vCard has no UID, then give it one. */
600 s = vcard_get_prop(v, "UID", 0, 0, 0);
603 vcard_set_prop(v, "UID", buf, 0);
606 /* Enforce local UID policy if applicable */
607 if (yes_my_citadel_config) {
608 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
609 vcard_set_prop(v, "UID", buf, 0);
613 * Set the EUID of the message to the UID of the vCard.
615 if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
616 s = vcard_get_prop(v, "UID", 0, 0, 0);
618 msg->cm_fields['E'] = strdup(s);
619 if (msg->cm_fields['U'] == NULL) {
620 msg->cm_fields['U'] = strdup(s);
625 * Set the Subject to the name in the vCard.
627 s = vcard_get_prop(v, "FN", 0, 0, 0);
629 s = vcard_get_prop(v, "N", 0, 0, 0);
632 if (msg->cm_fields['U'] != NULL) {
633 free(msg->cm_fields['U']);
635 msg->cm_fields['U'] = strdup(s);
638 /* Re-serialize it back into the msg body */
639 ser = vcard_serialize(v);
641 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
642 sprintf(msg->cm_fields['M'],
643 "Content-type: " VCARD_MIME_TYPE
644 "\r\n\r\n%s\r\n", ser);
648 /* Now allow the save to complete. */
656 * This handler detects whether the user is attempting to save a new
657 * vCard as part of his/her personal configuration, and handles the replace
658 * function accordingly (copy the vCard from the config room to the global
661 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)) return(0);
674 if (msg->cm_format_type != 4) return(0);
676 ptr = msg->cm_fields['M'];
677 if (ptr == NULL) return(0);
678 while (ptr != NULL) {
680 linelen = strcspn(ptr, "\n");
681 if (linelen == 0) return(0); /* end of headers */
683 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
684 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
686 * Bingo! The user is uploading a new vCard, so
687 * copy it to the Global Address Book room.
690 I = atol(msg->cm_fields['I']);
691 if (I < 0L) return(0);
693 /* Store our Internet return address in memory */
694 v = vcard_load(msg->cm_fields['M']);
695 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
696 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
698 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
701 /* Put it in the Global Address Book room... */
702 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
704 /* ...and also in the directory database. */
705 vcard_add_to_directory(I, NULL);
707 /* Some sites want an Aide to be notified when a
708 * user registers or re-registers...
712 /* ...which also means we need to flag the user */
713 lgetuser(&CC->user, CC->curr_user);
714 CC->user.flags |= (US_REGIS|US_NEEDVALID);
720 ptr = strchr((char *)ptr, '\n');
721 if (ptr != NULL) ++ptr;
730 * back end function used for callbacks
732 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
735 msgnum = (long *) userdata;
736 *msgnum = supplied_msgnum;
741 * If this user has a vcard on disk, read it into memory, otherwise allocate
742 * and return an empty vCard.
744 struct vCard *vcard_get_user(struct ctdluser *u) {
745 char hold_rm[ROOMNAMELEN];
746 char config_rm[ROOMNAMELEN];
747 struct CtdlMessage *msg = NULL;
751 strcpy(hold_rm, CC->room.QRname); /* save current room */
752 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
754 if (getroom(&CC->room, config_rm) != 0) {
755 getroom(&CC->room, hold_rm);
759 /* We want the last (and probably only) vcard in this room */
761 CtdlForEachMessage(MSGS_LAST, 1, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
762 NULL, vcard_gu_backend, (void *)&VCmsgnum );
763 getroom(&CC->room, hold_rm); /* return to saved room */
765 if (VCmsgnum < 0L) return vcard_new();
767 msg = CtdlFetchMessage(VCmsgnum, 1);
768 if (msg == NULL) return vcard_new();
770 v = vcard_load(msg->cm_fields['M']);
771 CtdlFreeMessage(msg);
777 * Store this user's vCard in the appropriate place
780 * Write our config to disk
782 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
787 CtdlMakeTempFileName(temp, sizeof temp);
788 ser = vcard_serialize(v);
790 fp = fopen(temp, "w");
791 if (fp == NULL) return;
793 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
795 fwrite(ser, strlen(ser), 1, fp);
800 /* This handy API function does all the work for us.
801 * NOTE: normally we would want to set that last argument to 1, to
802 * force the system to delete the user's old vCard. But it doesn't
803 * have to, because the vcard_upload_beforesave() hook above
804 * is going to notice what we're trying to do, and delete the old vCard.
806 CtdlWriteObject(USERCONFIGROOM, /* which room */
807 VCARD_MIME_TYPE,/* MIME type */
808 temp, /* temp file */
811 0, /* don't delete others of this type */
820 * Old style "enter registration info" command. This function simply honors
821 * the REGI protocol command, translates the entered parameters into a vCard,
822 * and enters the vCard into the user's configuration.
824 void cmd_regi(char *argbuf) {
827 struct vCard *my_vcard;
833 char tmpaddress[SIZ];
834 char tmpcountry[SIZ];
838 if (!(CC->logged_in)) {
839 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
843 /* If users cannot create their own accounts, they cannot re-register either. */
844 if ( (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
845 cprintf("%d Self-service registration is not allowed here.\n",
846 ERROR + HIGHER_ACCESS_REQUIRED);
849 my_vcard = vcard_get_user(&CC->user);
852 strcpy(tmpstate, "");
854 strcpy(tmpcountry, "USA");
856 cprintf("%d Send registration...\n", SEND_LISTING);
858 while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
859 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
860 if (a==1) strcpy(tmpaddr, buf);
861 if (a==2) strcpy(tmpcity, buf);
862 if (a==3) strcpy(tmpstate, buf);
864 for (c=0; buf[c]; ++c) {
865 if ((buf[c]>='0') && (buf[c]<='9')) {
872 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
873 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
874 if (a==7) strcpy(tmpcountry, buf);
878 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
879 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
880 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
881 vcard_write_user(&CC->user, my_vcard);
882 vcard_free(my_vcard);
887 * Protocol command to fetch registration info for a user
889 void cmd_greg(char *argbuf)
891 struct ctdluser usbuf;
894 char who[USERNAME_SIZE];
898 extract_token(who, argbuf, 0, '|', sizeof who);
900 if (!(CC->logged_in)) {
901 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
905 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
907 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
908 cprintf("%d Higher access required.\n",
909 ERROR + HIGHER_ACCESS_REQUIRED);
913 if (getuser(&usbuf, who) != 0) {
914 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
918 v = vcard_get_user(&usbuf);
920 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
921 cprintf("%ld\n", usbuf.usernum);
922 cprintf("%s\n", usbuf.password);
923 s = vcard_get_prop(v, "n", 0, 0, 0);
924 cprintf("%s\n", s ? s : " "); /* name */
926 s = vcard_get_prop(v, "adr", 0, 0, 0);
927 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
929 extract_token(buf, adr, 2, ';', sizeof buf);
930 cprintf("%s\n", buf); /* street */
931 extract_token(buf, adr, 3, ';', sizeof buf);
932 cprintf("%s\n", buf); /* city */
933 extract_token(buf, adr, 4, ';', sizeof buf);
934 cprintf("%s\n", buf); /* state */
935 extract_token(buf, adr, 5, ';', sizeof buf);
936 cprintf("%s\n", buf); /* zip */
938 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
939 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
947 cprintf("%d\n", usbuf.axlevel);
949 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
950 cprintf("%s\n", s ? s : " ");
951 s = vcard_get_prop(v, "adr", 0, 0, 0);
952 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
954 extract_token(buf, adr, 6, ';', sizeof buf);
955 cprintf("%s\n", buf); /* country */
963 * When a user is being created, create his/her vCard.
965 void vcard_newuser(struct ctdluser *usbuf) {
971 vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
972 lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
974 /* Create and save the vCard */
976 if (v == NULL) return;
977 sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
978 for (i=0; buf[i]; ++i) {
979 if (buf[i] == ' ') buf[i] = '_';
981 vcard_add_prop(v, "fn", usbuf->fullname);
982 vcard_add_prop(v, "n", vname);
983 vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
984 vcard_add_prop(v, "email;internet", buf);
985 vcard_write_user(usbuf, v);
991 * When a user is being deleted, we have to remove his/her vCard.
992 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
993 * field, and the same Exclusive ID as the existing card.
995 void vcard_purge(struct ctdluser *usbuf) {
996 struct CtdlMessage *msg;
999 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1000 if (msg == NULL) return;
1001 memset(msg, 0, sizeof(struct CtdlMessage));
1003 msg->cm_magic = CTDLMESSAGE_MAGIC;
1004 msg->cm_anon_type = MES_NORMAL;
1005 msg->cm_format_type = 0;
1006 msg->cm_fields['A'] = strdup(usbuf->fullname);
1007 msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
1008 msg->cm_fields['N'] = strdup(NODENAME);
1009 msg->cm_fields['M'] = strdup("Purge this vCard\n");
1011 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
1012 msg->cm_fields['A'], NODENAME);
1013 msg->cm_fields['E'] = strdup(buf);
1015 msg->cm_fields['S'] = strdup("CANCEL");
1017 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
1018 CtdlFreeMessage(msg);
1023 * Grab vCard directory stuff out of incoming network messages
1025 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
1029 if (msg == NULL) return(0);
1031 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
1035 if (msg->cm_format_type != 4) return(0);
1037 ptr = msg->cm_fields['M'];
1038 if (ptr == NULL) return(0);
1039 while (ptr != NULL) {
1041 linelen = strcspn(ptr, "\n");
1042 if (linelen == 0) return(0); /* end of headers */
1044 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
1045 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
1046 /* It's a vCard. Add it to the directory. */
1047 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
1051 ptr = strchr((char *)ptr, '\n');
1052 if (ptr != NULL) ++ptr;
1061 * When a vCard is being removed from the Global Address Book room, remove it
1062 * from the directory as well.
1064 void vcard_delete_remove(char *room, long msgnum) {
1065 struct CtdlMessage *msg;
1069 if (msgnum <= 0L) return;
1071 if (room == NULL) return;
1073 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
1077 msg = CtdlFetchMessage(msgnum, 1);
1078 if (msg == NULL) return;
1080 ptr = msg->cm_fields['M'];
1081 if (ptr == NULL) goto EOH;
1082 while (ptr != NULL) {
1083 linelen = strcspn(ptr, "\n");
1084 if (linelen == 0) goto EOH;
1086 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
1087 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
1088 /* Bingo! A vCard is being deleted. */
1089 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
1090 ctdl_vcard_to_directory(msg, V2L_DELETE);
1092 ptr = strchr((char *)ptr, '\n');
1093 if (ptr != NULL) ++ptr;
1096 EOH: CtdlFreeMessage(msg);
1102 * Get Valid Screen Names
1104 void cmd_gvsn(char *argbuf)
1106 if (CtdlAccessCheck(ac_logged_in)) return;
1108 cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
1109 cprintf("%s\n", CC->user.fullname);
1110 if ( (!IsEmptyStr(CC->cs_inet_fn)) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
1111 cprintf("%s\n", CC->cs_inet_fn);
1118 * Get Valid Email Addresses
1120 void cmd_gvea(char *argbuf)
1122 int num_secondary_emails = 0;
1126 if (CtdlAccessCheck(ac_logged_in)) return;
1128 cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
1129 if (!IsEmptyStr(CC->cs_inet_email)) {
1130 cprintf("%s\n", CC->cs_inet_email);
1132 if (!IsEmptyStr(CC->cs_inet_other_emails)) {
1133 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
1134 for (i=0; i<num_secondary_emails; ++i) {
1135 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
1136 cprintf("%s\n", buf);
1146 * Callback function for cmd_dvca() that hunts for vCard content types
1147 * and outputs any email addresses found within.
1149 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
1150 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1154 char displayname[256];
1155 int displayname_len;
1156 char emailaddr[256];
1160 if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
1164 v = vcard_load(content);
1165 if (v == NULL) return;
1167 extract_friendly_name(displayname, sizeof displayname, v);
1168 extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
1170 displayname_len = strlen(displayname);
1171 for (i=0; i<displayname_len; ++i) {
1172 if (displayname[i] == '\"') displayname[i] = ' ';
1173 if (displayname[i] == ';') displayname[i] = ',';
1174 if (displayname[i] == ',') has_commas = 1;
1176 striplt(displayname);
1178 cprintf("%s%s%s <%s>\n",
1179 (has_commas ? "\"" : ""),
1181 (has_commas ? "\"" : ""),
1190 * Back end callback function for cmd_dvca()
1192 * It's basically just passed a list of message numbers, which we're going
1193 * to fetch off the disk and then pass along to the MIME parser via another
1194 * layer of callback...
1196 void dvca_callback(long msgnum, void *userdata) {
1197 struct CtdlMessage *msg = NULL;
1199 msg = CtdlFetchMessage(msgnum, 1);
1200 if (msg == NULL) return;
1201 mime_parser(msg->cm_fields['M'],
1203 *dvca_mime_callback, /* callback function */
1205 NULL, /* user data */
1208 CtdlFreeMessage(msg);
1213 * Dump VCard Addresses
1215 void cmd_dvca(char *argbuf)
1217 if (CtdlAccessCheck(ac_logged_in)) return;
1219 cprintf("%d addresses:\n", LISTING_FOLLOWS);
1220 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1228 void cmd_qdir(char *argbuf) {
1229 char citadel_addr[256];
1230 char internet_addr[256];
1232 if (CtdlAccessCheck(ac_logged_in)) return;
1234 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1236 if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1237 cprintf("%d %s was not found.\n",
1238 ERROR + NO_SUCH_USER, internet_addr);
1242 cprintf("%d %s\n", CIT_OK, citadel_addr);
1246 * Query Directory, in fact an alias to match postfix tcp auth.
1248 void check_get(void) {
1249 char internet_addr[256];
1254 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1255 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1256 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1260 lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1261 while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1263 if (strcasecmp(cmdbuf, "GET "));
1265 struct recptypes *rcpt;
1266 char *argbuf = &cmdbuf[4];
1268 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1269 rcpt = validate_recipients(internet_addr);
1270 if ((rcpt != NULL)&&
1272 (*rcpt->recp_local != '\0')||
1273 (*rcpt->recp_room != '\0')||
1274 (*rcpt->recp_ignet != '\0')))
1277 cprintf("200 OK %s\n", internet_addr);
1278 lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1282 cprintf("500 REJECT noone here by that name.\n");
1284 lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1286 if (rcpt != NULL) free_recipients(rcpt);
1290 void check_get_greeting(void) {
1291 /* dummy function, we have no greeting in this verry simple protocol. */
1296 * We don't know if the Contacts room exists so we just create it at login
1298 void vcard_create_room(void)
1303 /* Create the calendar room if it doesn't already exist */
1304 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1306 /* Set expiration policy to manual; otherwise objects will be lost! */
1307 if (lgetroom(&qr, USERCONTACTSROOM)) {
1308 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1311 qr.QRep.expire_mode = EXPIRE_MANUAL;
1312 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1315 /* Set the view to a calendar view */
1316 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1317 vbuf.v_view = 2; /* 2 = address book view */
1318 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1327 * When a user logs in...
1329 void vcard_session_login_hook(void) {
1330 struct vCard *v = NULL;
1332 v = vcard_get_user(&CC->user);
1333 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1334 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1336 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1339 vcard_create_room();
1344 * Turn an arbitrary RFC822 address into a struct vCard for possible
1345 * inclusion into an address book.
1347 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1349 char user[256], node[256], name[256], email[256], n[256], uid[256];
1353 if (v == NULL) return(NULL);
1355 process_rfc822_addr(addr, user, node, name);
1356 vcard_set_prop(v, "fn", name, 0);
1358 vcard_fn_to_n(n, name, sizeof n);
1359 vcard_set_prop(v, "n", n, 0);
1361 snprintf(email, sizeof email, "%s@%s", user, node);
1362 vcard_set_prop(v, "email;internet", email, 0);
1364 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1365 for (i=0; uid[i]; ++i) {
1366 if (isspace(uid[i])) uid[i] = '_';
1367 uid[i] = tolower(uid[i]);
1369 vcard_set_prop(v, "UID", uid, 0);
1377 * This is called by store_harvested_addresses() to remove from the
1378 * list any addresses we already have in our address book.
1380 void strip_addresses_already_have(long msgnum, void *userdata) {
1381 char *collected_addresses;
1382 struct CtdlMessage *msg = NULL;
1386 char addr[256], user[256], node[256], name[256];
1388 collected_addresses = (char *)userdata;
1390 msg = CtdlFetchMessage(msgnum, 1);
1391 if (msg == NULL) return;
1392 v = vcard_load(msg->cm_fields['M']);
1393 CtdlFreeMessage(msg);
1396 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1398 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1399 extract_token(addr, collected_addresses, j, ',', sizeof addr);
1401 /* Remove the address if we already have it! */
1402 process_rfc822_addr(addr, user, node, name);
1403 snprintf(addr, sizeof addr, "%s@%s", user, node);
1404 if (!strcasecmp(value, addr)) {
1405 remove_token(collected_addresses, j, ',');
1417 * Back end function for store_harvested_addresses()
1419 void store_this_ha(struct addresses_to_be_filed *aptr) {
1420 struct CtdlMessage *vmsg = NULL;
1421 long vmsgnum = (-1L);
1423 struct vCard *v = NULL;
1424 char recipient[256];
1427 /* First remove any addresses we already have in the address book */
1428 usergoto(aptr->roomname, 0, 0, NULL, NULL);
1429 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1430 strip_addresses_already_have, aptr->collected_addresses);
1432 if (!IsEmptyStr(aptr->collected_addresses))
1433 for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1435 /* Make a vCard out of each address */
1436 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1438 v = vcard_new_from_rfc822_addr(recipient);
1440 vmsg = malloc(sizeof(struct CtdlMessage));
1441 memset(vmsg, 0, sizeof(struct CtdlMessage));
1442 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1443 vmsg->cm_anon_type = MES_NORMAL;
1444 vmsg->cm_format_type = FMT_RFC822;
1445 vmsg->cm_fields['A'] = strdup("Citadel");
1446 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1447 ser = vcard_serialize(v);
1449 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1450 sprintf(vmsg->cm_fields['M'],
1451 "Content-type: " VCARD_MIME_TYPE
1452 "\r\n\r\n%s\r\n", ser);
1457 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1458 vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1459 CtdlFreeMessage(vmsg);
1463 free(aptr->roomname);
1464 free(aptr->collected_addresses);
1470 * When a user sends a message, we may harvest one or more email addresses
1471 * from the recipient list to be added to the user's address book. But we
1472 * want to do this asynchronously so it doesn't keep the user waiting.
1474 void store_harvested_addresses(void) {
1476 struct addresses_to_be_filed *aptr = NULL;
1478 if (atbf == NULL) return;
1480 begin_critical_section(S_ATBF);
1481 while (atbf != NULL) {
1484 end_critical_section(S_ATBF);
1485 store_this_ha(aptr);
1486 begin_critical_section(S_ATBF);
1488 end_critical_section(S_ATBF);
1493 * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
1494 * really this is just so we expose the vCard data to the full text indexer.
1496 void vcard_fixed_output(char *ptr, int len) {
1497 char *serialized_vcard;
1502 serialized_vcard = malloc(len + 1);
1503 safestrncpy(serialized_vcard, ptr, len+1);
1504 v = vcard_load(serialized_vcard);
1505 free(serialized_vcard);
1508 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1509 value = vcard_get_prop(v, "", 0, i++, 0);
1510 cprintf("%s\n", value);
1517 const char *CitadelServiceDICT_TCP="DICT_TCP";
1519 CTDL_MODULE_INIT(vcard)
1525 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1526 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1527 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1528 CtdlRegisterDeleteHook(vcard_delete_remove);
1529 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1530 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1531 CtdlRegisterProtoHook(cmd_igab, "IGAB",
1532 "Initialize Global Address Book");
1533 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1534 CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1535 CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1536 CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1537 CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1538 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1539 CtdlRegisterNetprocHook(vcard_extract_from_network);
1540 CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1541 CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1542 CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1544 /* Create the Global ADdress Book room if necessary */
1545 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1547 /* Set expiration policy to manual; otherwise objects will be lost! */
1548 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1549 qr.QRep.expire_mode = EXPIRE_MANUAL;
1550 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1554 * Also make sure it has a netconfig file, so the networker runs
1555 * on this room even if we don't share it with any other nodes.
1556 * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1559 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1560 fp = fopen(filename, "a");
1561 if (fp != NULL) fclose(fp);
1562 chown(filename, CTDLUID, (-1));
1565 /* for postfix tcpdict */
1566 CtdlRegisterServiceHook(config.c_pftcpdict_port, /* Postfix */
1571 CitadelServiceDICT_TCP);
1573 /* return our Subversion id for the Log */