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_ldap.h"
66 #include "serv_vcard.h"
69 #include "ctdl_module.h"
74 * set global flag calling for an aide to validate new users
76 void set_mm_valid(void) {
77 begin_critical_section(S_CONTROL);
79 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
81 end_critical_section(S_CONTROL);
87 * Extract Internet e-mail addresses from a message containing a vCard, and
88 * perform a callback for any found.
90 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
91 void (*callback)(char *, char *) ) {
95 char citadel_address[SIZ];
97 int found_something = 0;
99 if (msg->cm_fields['A'] == NULL) return;
100 if (msg->cm_fields['N'] == NULL) return;
101 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
102 msg->cm_fields['A'], msg->cm_fields['N']);
104 v = vcard_load(msg->cm_fields['M']);
105 if (v == NULL) return;
107 /* Go through the vCard searching for *all* instances of
108 * the "email;internet" key
111 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
115 if (strlen(addr) > 0) {
116 if (callback != NULL) {
117 callback(addr, citadel_address);
126 } while(found_something);
134 * Callback for vcard_add_to_directory()
135 * (Lotsa ugly nested callbacks. Oh well.)
137 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
140 /* We have to validate that we're not stepping on someone else's
141 * email address ... but only if we're logged in. Otherwise it's
142 * probably just the networker or something.
145 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
146 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
147 if (strcasecmp(buf, citadel_addr)) {
148 /* This address belongs to someone else.
149 * Bail out silently without saving.
151 lprintf(CTDL_DEBUG, "DOOP!\n");
156 lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
157 citadel_addr, internet_addr);
158 CtdlDirectoryAddUser(internet_addr, citadel_addr);
163 * Back end function for cmd_igab()
165 void vcard_add_to_directory(long msgnum, void *data) {
166 struct CtdlMessage *msg;
168 msg = CtdlFetchMessage(msgnum, 1);
170 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
174 ctdl_vcard_to_ldap(msg, V2L_WRITE);
177 CtdlFreeMessage(msg);
182 * Initialize Global Adress Book
184 void cmd_igab(char *argbuf) {
185 char hold_rm[ROOMNAMELEN];
187 if (CtdlAccessCheck(ac_aide)) return;
189 strcpy(hold_rm, CC->room.QRname); /* save current room */
191 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
192 getroom(&CC->room, hold_rm);
193 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
197 /* Empty the existing database first.
201 /* We want *all* vCards in this room */
202 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
203 NULL, vcard_add_to_directory, NULL);
205 getroom(&CC->room, hold_rm); /* return to saved room */
206 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
213 * See if there is a valid Internet address in a vCard to use for outbound
214 * Internet messages. If there is, stick it in the buffer.
216 void extract_inet_email_addrs(char *emailaddrbuf, size_t emailaddrbuf_len,
217 char *secemailaddrbuf, size_t secemailaddrbuf_len,
218 struct vCard *v, int local_addrs_only) {
221 int saved_instance = 0;
223 /* Go through the vCard searching for *all* instances of
224 * the "email;internet" key
226 while (s = vcard_get_prop(v, "email;internet", 0, instance++, 0), s != NULL) {
229 if (strlen(addr) > 0) {
230 if ( (IsDirectory(addr, 1)) ||
231 (!local_addrs_only) ) {
233 if ((saved_instance == 1) && (emailaddrbuf != NULL)) {
234 safestrncpy(emailaddrbuf, addr, emailaddrbuf_len);
236 else if ((saved_instance == 2) && (secemailaddrbuf != NULL)) {
237 safestrncpy(secemailaddrbuf, addr, secemailaddrbuf_len);
239 else if ((saved_instance > 2) && (secemailaddrbuf != NULL)) {
240 if ( (strlen(addr) + strlen(secemailaddrbuf) + 2)
241 < secemailaddrbuf_len ) {
242 strcat(secemailaddrbuf, "|");
243 strcat(secemailaddrbuf, addr);
255 * See if there is a name / screen name / friendly name in a vCard to use for outbound
256 * Internet messages. If there is, stick it in the buffer.
258 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
262 s = vcard_get_prop(v, "fn", 0, 0, 0);
264 s = vcard_get_prop(v, "n", 0, 0, 0);
268 safestrncpy(namebuf, s, namebuf_len);
274 * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
276 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
277 void *content, char *cbtype, char *cbcharset, size_t length,
278 char *encoding, void *cbuserdata)
280 struct vCard **v = (struct vCard **) cbuserdata;
282 if ( (!strcasecmp(cbtype, "text/x-vcard"))
283 || (!strcasecmp(cbtype, "text/vcard")) ) {
285 lprintf(CTDL_DEBUG, "Part %s contains a vCard! Loading...\n", partnum);
289 *v = vcard_load(content);
295 * This handler detects whether the user is attempting to save a new
296 * vCard as part of his/her personal configuration, and handles the replace
297 * function accordingly (delete the user's existing vCard in the config room
298 * and in the global address book).
300 int vcard_upload_beforesave(struct CtdlMessage *msg) {
304 struct ctdluser usbuf;
306 struct vCard *v = NULL;
309 int yes_my_citadel_config = 0;
310 int yes_any_vcard_room = 0;
312 if (!CC->logged_in) return(0); /* Only do this if logged in. */
314 /* Is this some user's "My Citadel Config" room? */
315 if ( (CC->room.QRflags && QR_MAILBOX)
316 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
317 /* Yes, we want to do this */
318 yes_my_citadel_config = 1;
320 #ifdef VCARD_SAVES_BY_AIDES_ONLY
321 /* Prevent non-aides from performing registration changes */
322 if (CC->user.axlevel < 6) {
329 /* Is this a room with an address book in it? */
330 if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
331 yes_any_vcard_room = 1;
334 /* If neither condition exists, don't run this hook. */
335 if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
339 /* If this isn't a MIME message, don't bother. */
340 if (msg->cm_format_type != 4) return(0);
342 /* Ok, if we got this far, look into the situation further... */
344 ptr = msg->cm_fields['M'];
345 if (ptr == NULL) return(0);
347 mime_parser(msg->cm_fields['M'],
349 *vcard_extract_vcard,
351 &v, /* user data ptr - put the vcard here */
355 if (v == NULL) return(0); /* no vCards were found in this message */
357 /* If users cannot create their own accounts, they cannot re-register either. */
358 if ( (yes_my_citadel_config) && (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
362 s = vcard_get_prop(v, "FN", 0, 0, 0);
363 if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
365 if (yes_my_citadel_config) {
366 /* Bingo! The user is uploading a new vCard, so
367 * delete the old one. First, figure out which user
368 * is being re-registered...
370 what_user = atol(CC->room.QRname);
372 if (what_user == CC->user.usernum) {
373 /* It's the logged in user. That was easy. */
374 memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
377 else if (getuserbynumber(&usbuf, what_user) == 0) {
378 /* We fetched a valid user record */
382 /* somebody set up us the bomb! */
383 yes_my_citadel_config = 0;
387 if (yes_my_citadel_config) {
388 /* Delete the user's old vCard. This would probably
389 * get taken care of by the replication check, but we
390 * want to make sure there is absolutely only one
391 * vCard in the user's config room at all times.
394 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$");
396 /* Make the author of the message the name of the user. */
397 if (msg->cm_fields['A'] != NULL) {
398 free(msg->cm_fields['A']);
400 msg->cm_fields['A'] = strdup(usbuf.fullname);
403 /* Insert or replace RFC2739-compliant free/busy URL */
404 if (yes_my_citadel_config) {
405 sprintf(buf, "http://%s/%s.vfb",
408 for (i=0; i<strlen(buf); ++i) {
409 if (buf[i] == ' ') buf[i] = '_';
411 vcard_set_prop(v, "FBURL;PREF", buf, 0);
414 /* If the vCard has no UID, then give it one. */
415 s = vcard_get_prop(v, "UID", 0, 0, 0);
418 vcard_set_prop(v, "UID", buf, 0);
421 /* Enforce local UID policy if applicable */
422 if (yes_my_citadel_config) {
423 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
424 vcard_set_prop(v, "UID", buf, 0);
428 * Set the EUID of the message to the UID of the vCard.
430 if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
431 s = vcard_get_prop(v, "UID", 0, 0, 0);
433 msg->cm_fields['E'] = strdup(s);
434 if (msg->cm_fields['U'] == NULL) {
435 msg->cm_fields['U'] = strdup(s);
440 * Set the Subject to the name in the vCard.
442 s = vcard_get_prop(v, "FN", 0, 0, 0);
444 s = vcard_get_prop(v, "N", 0, 0, 0);
447 if (msg->cm_fields['U'] != NULL) {
448 free(msg->cm_fields['U']);
450 msg->cm_fields['U'] = strdup(s);
453 /* Re-serialize it back into the msg body */
454 ser = vcard_serialize(v);
456 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
457 sprintf(msg->cm_fields['M'],
458 "Content-type: " VCARD_MIME_TYPE
459 "\r\n\r\n%s\r\n", ser);
463 /* Now allow the save to complete. */
471 * This handler detects whether the user is attempting to save a new
472 * vCard as part of his/her personal configuration, and handles the replace
473 * function accordingly (copy the vCard from the config room to the global
476 int vcard_upload_aftersave(struct CtdlMessage *msg) {
482 if (!CC->logged_in) return(0); /* Only do this if logged in. */
484 /* If this isn't the configuration room, or if this isn't a MIME
485 * message, don't bother.
487 if (msg->cm_fields['O'] == NULL) return(0);
488 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
489 if (msg->cm_format_type != 4) return(0);
491 ptr = msg->cm_fields['M'];
492 if (ptr == NULL) return(0);
493 while (ptr != NULL) {
495 linelen = strcspn(ptr, "\n");
496 if (linelen == 0) return(0); /* end of headers */
498 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
499 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
501 * Bingo! The user is uploading a new vCard, so
502 * copy it to the Global Address Book room.
505 I = atol(msg->cm_fields['I']);
506 if (I < 0L) return(0);
508 /* Store our Internet return address in memory */
509 v = vcard_load(msg->cm_fields['M']);
510 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
511 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
513 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
516 /* Put it in the Global Address Book room... */
517 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
519 /* ...and also in the directory database. */
520 vcard_add_to_directory(I, NULL);
522 /* Some sites want an Aide to be notified when a
523 * user registers or re-registers...
527 /* ...which also means we need to flag the user */
528 lgetuser(&CC->user, CC->curr_user);
529 CC->user.flags |= (US_REGIS|US_NEEDVALID);
535 ptr = strchr((char *)ptr, '\n');
536 if (ptr != NULL) ++ptr;
545 * back end function used for callbacks
547 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
550 msgnum = (long *) userdata;
551 *msgnum = supplied_msgnum;
556 * If this user has a vcard on disk, read it into memory, otherwise allocate
557 * and return an empty vCard.
559 struct vCard *vcard_get_user(struct ctdluser *u) {
560 char hold_rm[ROOMNAMELEN];
561 char config_rm[ROOMNAMELEN];
562 struct CtdlMessage *msg = NULL;
566 strcpy(hold_rm, CC->room.QRname); /* save current room */
567 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
569 if (getroom(&CC->room, config_rm) != 0) {
570 getroom(&CC->room, hold_rm);
574 /* We want the last (and probably only) vcard in this room */
576 CtdlForEachMessage(MSGS_LAST, 1, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
577 NULL, vcard_gu_backend, (void *)&VCmsgnum );
578 getroom(&CC->room, hold_rm); /* return to saved room */
580 if (VCmsgnum < 0L) return vcard_new();
582 msg = CtdlFetchMessage(VCmsgnum, 1);
583 if (msg == NULL) return vcard_new();
585 v = vcard_load(msg->cm_fields['M']);
586 CtdlFreeMessage(msg);
592 * Store this user's vCard in the appropriate place
595 * Write our config to disk
597 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
602 CtdlMakeTempFileName(temp, sizeof temp);
603 ser = vcard_serialize(v);
605 fp = fopen(temp, "w");
606 if (fp == NULL) return;
608 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
610 fwrite(ser, strlen(ser), 1, fp);
615 /* This handy API function does all the work for us.
616 * NOTE: normally we would want to set that last argument to 1, to
617 * force the system to delete the user's old vCard. But it doesn't
618 * have to, because the vcard_upload_beforesave() hook above
619 * is going to notice what we're trying to do, and delete the old vCard.
621 CtdlWriteObject(USERCONFIGROOM, /* which room */
622 VCARD_MIME_TYPE,/* MIME type */
623 temp, /* temp file */
626 0, /* don't delete others of this type */
635 * Old style "enter registration info" command. This function simply honors
636 * the REGI protocol command, translates the entered parameters into a vCard,
637 * and enters the vCard into the user's configuration.
639 void cmd_regi(char *argbuf) {
642 struct vCard *my_vcard;
648 char tmpaddress[SIZ];
649 char tmpcountry[SIZ];
653 if (!(CC->logged_in)) {
654 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
658 /* If users cannot create their own accounts, they cannot re-register either. */
659 if ( (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
660 cprintf("%d Self-service registration is not allowed here.\n",
661 ERROR + HIGHER_ACCESS_REQUIRED);
664 my_vcard = vcard_get_user(&CC->user);
667 strcpy(tmpstate, "");
669 strcpy(tmpcountry, "USA");
671 cprintf("%d Send registration...\n", SEND_LISTING);
673 while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
674 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
675 if (a==1) strcpy(tmpaddr, buf);
676 if (a==2) strcpy(tmpcity, buf);
677 if (a==3) strcpy(tmpstate, buf);
679 for (c=0; c<strlen(buf); ++c) {
680 if ((buf[c]>='0') && (buf[c]<='9')) {
687 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
688 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
689 if (a==7) strcpy(tmpcountry, buf);
693 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
694 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
695 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
696 vcard_write_user(&CC->user, my_vcard);
697 vcard_free(my_vcard);
702 * Protocol command to fetch registration info for a user
704 void cmd_greg(char *argbuf)
706 struct ctdluser usbuf;
709 char who[USERNAME_SIZE];
713 extract_token(who, argbuf, 0, '|', sizeof who);
715 if (!(CC->logged_in)) {
716 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
720 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
722 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
723 cprintf("%d Higher access required.\n",
724 ERROR + HIGHER_ACCESS_REQUIRED);
728 if (getuser(&usbuf, who) != 0) {
729 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
733 v = vcard_get_user(&usbuf);
735 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
736 cprintf("%ld\n", usbuf.usernum);
737 cprintf("%s\n", usbuf.password);
738 s = vcard_get_prop(v, "n", 0, 0, 0);
739 cprintf("%s\n", s ? s : " "); /* name */
741 s = vcard_get_prop(v, "adr", 0, 0, 0);
742 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
744 extract_token(buf, adr, 2, ';', sizeof buf);
745 cprintf("%s\n", buf); /* street */
746 extract_token(buf, adr, 3, ';', sizeof buf);
747 cprintf("%s\n", buf); /* city */
748 extract_token(buf, adr, 4, ';', sizeof buf);
749 cprintf("%s\n", buf); /* state */
750 extract_token(buf, adr, 5, ';', sizeof buf);
751 cprintf("%s\n", buf); /* zip */
753 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
754 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
762 cprintf("%d\n", usbuf.axlevel);
764 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
765 cprintf("%s\n", s ? s : " ");
766 s = vcard_get_prop(v, "adr", 0, 0, 0);
767 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
769 extract_token(buf, adr, 6, ';', sizeof buf);
770 cprintf("%s\n", buf); /* country */
778 * When a user is being created, create his/her vCard.
780 void vcard_newuser(struct ctdluser *usbuf) {
786 vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
787 lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
789 /* Create and save the vCard */
791 if (v == NULL) return;
792 sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
793 for (i=0; i<strlen(buf); ++i) {
794 if (buf[i] == ' ') buf[i] = '_';
796 vcard_add_prop(v, "fn", usbuf->fullname);
797 vcard_add_prop(v, "n", vname);
798 vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
799 vcard_add_prop(v, "email;internet", buf);
800 vcard_write_user(usbuf, v);
806 * When a user is being deleted, we have to remove his/her vCard.
807 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
808 * field, and the same Exclusive ID as the existing card.
810 void vcard_purge(struct ctdluser *usbuf) {
811 struct CtdlMessage *msg;
814 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
815 if (msg == NULL) return;
816 memset(msg, 0, sizeof(struct CtdlMessage));
818 msg->cm_magic = CTDLMESSAGE_MAGIC;
819 msg->cm_anon_type = MES_NORMAL;
820 msg->cm_format_type = 0;
821 msg->cm_fields['A'] = strdup(usbuf->fullname);
822 msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
823 msg->cm_fields['N'] = strdup(NODENAME);
824 msg->cm_fields['M'] = strdup("Purge this vCard\n");
826 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
827 msg->cm_fields['A'], NODENAME);
828 msg->cm_fields['E'] = strdup(buf);
830 msg->cm_fields['S'] = strdup("CANCEL");
832 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
833 CtdlFreeMessage(msg);
838 * Grab vCard directory stuff out of incoming network messages
840 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
844 if (msg == NULL) return(0);
846 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
850 if (msg->cm_format_type != 4) return(0);
852 ptr = msg->cm_fields['M'];
853 if (ptr == NULL) return(0);
854 while (ptr != NULL) {
856 linelen = strcspn(ptr, "\n");
857 if (linelen == 0) return(0); /* end of headers */
859 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
860 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
861 /* It's a vCard. Add it to the directory. */
862 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
866 ptr = strchr((char *)ptr, '\n');
867 if (ptr != NULL) ++ptr;
876 * When a vCard is being removed from the Global Address Book room, remove it
877 * from the directory as well.
879 void vcard_delete_remove(char *room, long msgnum) {
880 struct CtdlMessage *msg;
884 if (msgnum <= 0L) return;
886 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
890 msg = CtdlFetchMessage(msgnum, 1);
891 if (msg == NULL) return;
893 ptr = msg->cm_fields['M'];
894 if (ptr == NULL) goto EOH;
895 while (ptr != NULL) {
896 linelen = strcspn(ptr, "\n");
897 if (linelen == 0) goto EOH;
899 if ( (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
900 || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
901 /* Bingo! A vCard is being deleted. */
902 vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
904 ctdl_vcard_to_ldap(msg, V2L_DELETE);
907 ptr = strchr((char *)ptr, '\n');
908 if (ptr != NULL) ++ptr;
911 EOH: CtdlFreeMessage(msg);
917 * Get Valid Screen Names
919 void cmd_gvsn(char *argbuf)
921 if (CtdlAccessCheck(ac_logged_in)) return;
923 cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
924 cprintf("%s\n", CC->user.fullname);
925 if ( (strlen(CC->cs_inet_fn) > 0) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
926 cprintf("%s\n", CC->cs_inet_fn);
933 * Get Valid Email Addresses
935 void cmd_gvea(char *argbuf)
937 int num_secondary_emails = 0;
941 if (CtdlAccessCheck(ac_logged_in)) return;
943 cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
944 if (strlen(CC->cs_inet_email) > 0) {
945 cprintf("%s\n", CC->cs_inet_email);
947 if (strlen(CC->cs_inet_other_emails) > 0) {
948 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
949 for (i=0; i<num_secondary_emails; ++i) {
950 extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
951 cprintf("%s\n", buf);
961 * Callback function for cmd_dvca() that hunts for vCard content types
962 * and outputs any email addresses found within.
964 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
965 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
969 char displayname[256];
975 if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
979 v = vcard_load(content);
980 if (v == NULL) return;
982 extract_friendly_name(displayname, sizeof displayname, v);
983 extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
985 displayname_len = strlen(displayname);
986 for (i=0; i<displayname_len; ++i) {
987 if (displayname[i] == '\"') displayname[i] = ' ';
988 if (displayname[i] == ';') displayname[i] = ',';
989 if (displayname[i] == ',') has_commas = 1;
991 striplt(displayname);
993 cprintf("%s%s%s <%s>\n",
994 (has_commas ? "\"" : ""),
996 (has_commas ? "\"" : ""),
1005 * Back end callback function for cmd_dvca()
1007 * It's basically just passed a list of message numbers, which we're going
1008 * to fetch off the disk and then pass along to the MIME parser via another
1009 * layer of callback...
1011 void dvca_callback(long msgnum, void *userdata) {
1012 struct CtdlMessage *msg = NULL;
1014 msg = CtdlFetchMessage(msgnum, 1);
1015 if (msg == NULL) return;
1016 mime_parser(msg->cm_fields['M'],
1018 *dvca_mime_callback, /* callback function */
1020 NULL, /* user data */
1023 CtdlFreeMessage(msg);
1028 * Dump VCard Addresses
1030 void cmd_dvca(char *argbuf)
1032 if (CtdlAccessCheck(ac_logged_in)) return;
1034 cprintf("%d addresses:\n", LISTING_FOLLOWS);
1035 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1043 void cmd_qdir(char *argbuf) {
1044 char citadel_addr[256];
1045 char internet_addr[256];
1047 if (CtdlAccessCheck(ac_logged_in)) return;
1049 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1051 if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1052 cprintf("%d %s was not found.\n",
1053 ERROR + NO_SUCH_USER, internet_addr);
1057 cprintf("%d %s\n", CIT_OK, citadel_addr);
1061 * Query Directory, in fact an alias to match postfix tcp auth.
1063 void check_get(void) {
1064 char internet_addr[256];
1069 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1070 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1071 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1075 lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1076 while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1078 if (strcasecmp(cmdbuf, "GET "));
1080 struct recptypes *rcpt;
1081 char *argbuf = &cmdbuf[4];
1083 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1084 rcpt = validate_recipients(internet_addr);
1085 if ((rcpt != NULL)&&
1087 (*rcpt->recp_local != '\0')||
1088 (*rcpt->recp_room != '\0')||
1089 (*rcpt->recp_ignet != '\0')))
1092 cprintf("200 OK %s\n", internet_addr);
1093 lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1097 cprintf("500 REJECT noone here by that name.\n");
1099 lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1101 if (rcpt != NULL) free_recipients(rcpt);
1105 void check_get_greeting(void) {
1106 /* dummy function, we have no greeting in this verry simple protocol. */
1111 * We don't know if the Contacts room exists so we just create it at login
1113 void vcard_create_room(void)
1118 /* Create the calendar room if it doesn't already exist */
1119 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1121 /* Set expiration policy to manual; otherwise objects will be lost! */
1122 if (lgetroom(&qr, USERCONTACTSROOM)) {
1123 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1126 qr.QRep.expire_mode = EXPIRE_MANUAL;
1127 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1130 /* Set the view to a calendar view */
1131 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1132 vbuf.v_view = 2; /* 2 = address book view */
1133 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1142 * When a user logs in...
1144 void vcard_session_login_hook(void) {
1145 struct vCard *v = NULL;
1147 v = vcard_get_user(&CC->user);
1148 extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1149 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1151 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1154 vcard_create_room();
1159 * Turn an arbitrary RFC822 address into a struct vCard for possible
1160 * inclusion into an address book.
1162 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1164 char user[256], node[256], name[256], email[256], n[256], uid[256];
1168 if (v == NULL) return(NULL);
1170 process_rfc822_addr(addr, user, node, name);
1171 vcard_set_prop(v, "fn", name, 0);
1173 vcard_fn_to_n(n, name, sizeof n);
1174 vcard_set_prop(v, "n", n, 0);
1176 snprintf(email, sizeof email, "%s@%s", user, node);
1177 vcard_set_prop(v, "email;internet", email, 0);
1179 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1180 for (i=0; i<strlen(uid); ++i) {
1181 if (isspace(uid[i])) uid[i] = '_';
1182 uid[i] = tolower(uid[i]);
1184 vcard_set_prop(v, "UID", uid, 0);
1192 * This is called by store_harvested_addresses() to remove from the
1193 * list any addresses we already have in our address book.
1195 void strip_addresses_already_have(long msgnum, void *userdata) {
1196 char *collected_addresses;
1197 struct CtdlMessage *msg = NULL;
1201 char addr[256], user[256], node[256], name[256];
1203 collected_addresses = (char *)userdata;
1205 msg = CtdlFetchMessage(msgnum, 1);
1206 if (msg == NULL) return;
1207 v = vcard_load(msg->cm_fields['M']);
1208 CtdlFreeMessage(msg);
1211 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1213 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1214 extract_token(addr, collected_addresses, j, ',', sizeof addr);
1216 /* Remove the address if we already have it! */
1217 process_rfc822_addr(addr, user, node, name);
1218 snprintf(addr, sizeof addr, "%s@%s", user, node);
1219 if (!strcasecmp(value, addr)) {
1220 remove_token(collected_addresses, j, ',');
1232 * Back end function for store_harvested_addresses()
1234 void store_this_ha(struct addresses_to_be_filed *aptr) {
1235 struct CtdlMessage *vmsg = NULL;
1236 long vmsgnum = (-1L);
1238 struct vCard *v = NULL;
1239 char recipient[256];
1242 /* First remove any addresses we already have in the address book */
1243 usergoto(aptr->roomname, 0, 0, NULL, NULL);
1244 CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1245 strip_addresses_already_have, aptr->collected_addresses);
1247 if (strlen(aptr->collected_addresses) > 0)
1248 for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1250 /* Make a vCard out of each address */
1251 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1253 v = vcard_new_from_rfc822_addr(recipient);
1255 vmsg = malloc(sizeof(struct CtdlMessage));
1256 memset(vmsg, 0, sizeof(struct CtdlMessage));
1257 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1258 vmsg->cm_anon_type = MES_NORMAL;
1259 vmsg->cm_format_type = FMT_RFC822;
1260 vmsg->cm_fields['A'] = strdup("Citadel");
1261 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1262 ser = vcard_serialize(v);
1264 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1265 sprintf(vmsg->cm_fields['M'],
1266 "Content-type: " VCARD_MIME_TYPE
1267 "\r\n\r\n%s\r\n", ser);
1272 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1273 vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1274 CtdlFreeMessage(vmsg);
1278 free(aptr->roomname);
1279 free(aptr->collected_addresses);
1285 * When a user sends a message, we may harvest one or more email addresses
1286 * from the recipient list to be added to the user's address book. But we
1287 * want to do this asynchronously so it doesn't keep the user waiting.
1289 void store_harvested_addresses(void) {
1291 struct addresses_to_be_filed *aptr = NULL;
1293 if (atbf == NULL) return;
1295 begin_critical_section(S_ATBF);
1296 while (atbf != NULL) {
1299 end_critical_section(S_ATBF);
1300 store_this_ha(aptr);
1301 begin_critical_section(S_ATBF);
1303 end_critical_section(S_ATBF);
1308 * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
1309 * really this is just so we expose the vCard data to the full text indexer.
1311 void vcard_fixed_output(char *ptr, int len) {
1312 char *serialized_vcard;
1317 serialized_vcard = malloc(len + 1);
1318 safestrncpy(serialized_vcard, ptr, len+1);
1319 v = vcard_load(serialized_vcard);
1320 free(serialized_vcard);
1323 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1324 value = vcard_get_prop(v, "", 0, i++, 0);
1325 cprintf("%s\n", value);
1334 CTDL_MODULE_INIT(vcard)
1340 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1341 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1342 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1343 CtdlRegisterDeleteHook(vcard_delete_remove);
1344 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1345 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1346 CtdlRegisterProtoHook(cmd_igab, "IGAB",
1347 "Initialize Global Address Book");
1348 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1349 CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1350 CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1351 CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1352 CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1353 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1354 CtdlRegisterNetprocHook(vcard_extract_from_network);
1355 CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1356 CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1357 CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1359 /* Create the Global ADdress Book room if necessary */
1360 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1362 /* Set expiration policy to manual; otherwise objects will be lost! */
1363 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1364 qr.QRep.expire_mode = EXPIRE_MANUAL;
1365 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1369 * Also make sure it has a netconfig file, so the networker runs
1370 * on this room even if we don't share it with any other nodes.
1371 * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1374 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1375 fp = fopen(filename, "a");
1376 if (fp != NULL) fclose(fp);
1377 chown(filename, CTDLUID, (-1));
1380 /* for postfix tcpdict */
1381 CtdlRegisterServiceHook(config.c_pftcpdict_port, /* Postfix */
1387 /* return our Subversion id for the Log */