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"
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
51 #include "serv_extensions.h"
57 #include "internet_addressing.h"
59 #include "mime_parser.h"
61 #include "serv_ldap.h"
62 #include "serv_vcard.h"
65 * set global flag calling for an aide to validate new users
67 void set_mm_valid(void) {
68 begin_critical_section(S_CONTROL);
70 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
72 end_critical_section(S_CONTROL);
78 * Extract Internet e-mail addresses from a message containing a vCard, and
79 * perform a callback for any found.
81 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
82 void (*callback)(char *, char *) ) {
86 char citadel_address[SIZ];
88 int found_something = 0;
90 if (msg->cm_fields['A'] == NULL) return;
91 if (msg->cm_fields['N'] == NULL) return;
92 snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
93 msg->cm_fields['A'], msg->cm_fields['N']);
95 v = vcard_load(msg->cm_fields['M']);
96 if (v == NULL) return;
98 /* Go through the vCard searching for *all* instances of
99 * the "email;internet" key
102 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
106 if (strlen(addr) > 0) {
107 if (callback != NULL) {
108 callback(addr, citadel_address);
117 } while(found_something);
125 * Callback for vcard_add_to_directory()
126 * (Lotsa ugly nested callbacks. Oh well.)
128 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
131 /* We have to validate that we're not stepping on someone else's
132 * email address ... but only if we're logged in. Otherwise it's
133 * probably just the networker or something.
136 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
137 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
138 if (strcasecmp(buf, citadel_addr)) {
139 /* This address belongs to someone else.
140 * Bail out silently without saving.
142 lprintf(CTDL_DEBUG, "DOOP!\n");
147 lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
148 citadel_addr, internet_addr);
149 CtdlDirectoryAddUser(internet_addr, citadel_addr);
154 * Back end function for cmd_igab()
156 void vcard_add_to_directory(long msgnum, void *data) {
157 struct CtdlMessage *msg;
159 msg = CtdlFetchMessage(msgnum, 1);
161 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
165 ctdl_vcard_to_ldap(msg, V2L_WRITE);
168 CtdlFreeMessage(msg);
173 * Initialize Global Adress Book
175 void cmd_igab(char *argbuf) {
176 char hold_rm[ROOMNAMELEN];
178 if (CtdlAccessCheck(ac_aide)) return;
180 strcpy(hold_rm, CC->room.QRname); /* save current room */
182 if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
183 getroom(&CC->room, hold_rm);
184 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
188 /* Empty the existing database first.
192 /* We want *all* vCards in this room */
193 CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/x-vcard",
194 NULL, vcard_add_to_directory, NULL);
196 getroom(&CC->room, hold_rm); /* return to saved room */
197 cprintf("%d Directory has been rebuilt.\n", CIT_OK);
204 * See if there is a valid Internet address in a vCard to use for outbound
205 * Internet messages. If there is, stick it in the buffer.
207 void extract_primary_inet_email(char *emailaddrbuf, size_t emailaddrbuf_len, struct vCard *v) {
209 int continue_searching = 1;
212 /* Go through the vCard searching for *all* instances of
213 * the "email;internet" key
216 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
218 continue_searching = 1;
221 if (strlen(addr) > 0) {
222 if (IsDirectory(addr)) {
223 continue_searching = 0;
224 safestrncpy(emailaddrbuf, addr,
231 continue_searching = 0;
233 } while(continue_searching);
239 * See if there is a name / screen name / friendly name in a vCard to use for outbound
240 * Internet messages. If there is, stick it in the buffer.
242 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
246 s = vcard_get_prop(v, "fn", 0, 0, 0);
248 s = vcard_get_prop(v, "n", 0, 0, 0);
252 safestrncpy(namebuf, s, namebuf_len);
258 * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
260 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
261 void *content, char *cbtype, char *cbcharset, size_t length,
262 char *encoding, void *cbuserdata)
264 struct vCard **v = (struct vCard **) cbuserdata;
266 if ( (!strcasecmp(cbtype, "text/x-vcard"))
267 || (!strcasecmp(cbtype, "text/vcard")) ) {
269 lprintf(CTDL_DEBUG, "Part %s contains a vCard! Loading...\n", partnum);
273 *v = vcard_load(content);
279 * This handler detects whether the user is attempting to save a new
280 * vCard as part of his/her personal configuration, and handles the replace
281 * function accordingly (delete the user's existing vCard in the config room
282 * and in the global address book).
284 int vcard_upload_beforesave(struct CtdlMessage *msg) {
288 struct ctdluser usbuf;
290 struct vCard *v = NULL;
293 int yes_my_citadel_config = 0;
294 int yes_any_vcard_room = 0;
296 if (!CC->logged_in) return(0); /* Only do this if logged in. */
298 /* Is this some user's "My Citadel Config" room? */
299 if ( (CC->room.QRflags && QR_MAILBOX)
300 && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
301 /* Yes, we want to do this */
302 yes_my_citadel_config = 1;
304 #ifdef VCARD_SAVES_BY_AIDES_ONLY
305 /* Prevent non-aides from performing registration changes */
306 if (CC->user.axlevel < 6) {
313 /* Is this a room with an address book in it? */
314 if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
315 yes_any_vcard_room = 1;
318 /* If neither condition exists, don't run this hook. */
319 if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
323 /* If this isn't a MIME message, don't bother. */
324 if (msg->cm_format_type != 4) return(0);
326 /* Ok, if we got this far, look into the situation further... */
328 ptr = msg->cm_fields['M'];
329 if (ptr == NULL) return(0);
331 mime_parser(msg->cm_fields['M'],
333 *vcard_extract_vcard,
335 &v, /* user data ptr - put the vcard here */
339 if (v == NULL) return(0); /* no vCards were found in this message */
341 s = vcard_get_prop(v, "FN", 0, 0, 0);
342 if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
344 if (yes_my_citadel_config) {
345 /* Bingo! The user is uploading a new vCard, so
346 * delete the old one. First, figure out which user
347 * is being re-registered...
349 what_user = atol(CC->room.QRname);
351 if (what_user == CC->user.usernum) {
352 /* It's the logged in user. That was easy. */
353 memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
356 else if (getuserbynumber(&usbuf, what_user) == 0) {
357 /* We fetched a valid user record */
361 /* somebody set up us the bomb! */
362 yes_my_citadel_config = 0;
366 if (yes_my_citadel_config) {
367 /* Delete the user's old vCard. This would probably
368 * get taken care of by the replication check, but we
369 * want to make sure there is absolutely only one
370 * vCard in the user's config room at all times.
372 * FIXME also handle text/vcard by implementing regexp
374 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "text/x-vcard");
376 /* Make the author of the message the name of the user. */
377 if (msg->cm_fields['A'] != NULL) {
378 free(msg->cm_fields['A']);
380 msg->cm_fields['A'] = strdup(usbuf.fullname);
383 /* Insert or replace RFC2739-compliant free/busy URL */
384 if (yes_my_citadel_config) {
385 sprintf(buf, "http://%s/%s.vfb",
388 for (i=0; i<strlen(buf); ++i) {
389 if (buf[i] == ' ') buf[i] = '_';
391 vcard_set_prop(v, "FBURL;PREF", buf, 0);
394 /* If this is an address book room, and the vCard has
395 * no UID, then give it one.
397 if (yes_any_vcard_room) {
398 s = vcard_get_prop(v, "UID", 0, 0, 0);
401 vcard_set_prop(v, "UID", buf, 0);
405 /* Enforce local UID policy if applicable */
406 if (yes_my_citadel_config) {
407 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
408 vcard_set_prop(v, "UID", buf, 0);
411 if (yes_any_vcard_room) {
413 * Set the EUID of the message to the UID of the vCard.
415 if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
416 s = vcard_get_prop(v, "UID", 0, 0, 0);
418 msg->cm_fields['E'] = strdup(s);
419 if (msg->cm_fields['U'] == NULL) {
420 msg->cm_fields['U'] = strdup(s);
425 * Set the Subject to the name in the vCard.
427 s = vcard_get_prop(v, "FN", 0, 0, 0);
429 s = vcard_get_prop(v, "N", 0, 0, 0);
432 if (msg->cm_fields['U'] != NULL) {
433 free(msg->cm_fields['U']);
435 msg->cm_fields['U'] = strdup(s);
438 /* Re-serialize it back into the msg body */
439 ser = vcard_serialize(v);
441 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
442 sprintf(msg->cm_fields['M'],
443 "Content-type: text/x-vcard"
444 "\r\n\r\n%s\r\n", ser);
449 /* Now allow the save to complete. */
457 * This handler detects whether the user is attempting to save a new
458 * vCard as part of his/her personal configuration, and handles the replace
459 * function accordingly (copy the vCard from the config room to the global
462 int vcard_upload_aftersave(struct CtdlMessage *msg) {
468 if (!CC->logged_in) return(0); /* Only do this if logged in. */
470 /* If this isn't the configuration room, or if this isn't a MIME
471 * message, don't bother.
473 if (msg->cm_fields['O'] == NULL) return(0);
474 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
475 if (msg->cm_format_type != 4) return(0);
477 ptr = msg->cm_fields['M'];
478 if (ptr == NULL) return(0);
479 while (ptr != NULL) {
481 linelen = strcspn(ptr, "\n");
482 if (linelen == 0) return(0); /* end of headers */
484 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
485 /* Bingo! The user is uploading a new vCard, so
486 * copy it to the Global Address Book room.
489 I = atol(msg->cm_fields['I']);
490 if (I < 0L) return(0);
492 /* Store our Internet return address in memory */
493 v = vcard_load(msg->cm_fields['M']);
494 extract_primary_inet_email(CC->cs_inet_email, sizeof CC->cs_inet_email, v);
495 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
498 /* Put it in the Global Address Book room... */
499 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
501 /* ...and also in the directory database. */
502 vcard_add_to_directory(I, NULL);
504 /* Some sites want an Aide to be notified when a
505 * user registers or re-registers...
509 /* ...which also means we need to flag the user */
510 lgetuser(&CC->user, CC->curr_user);
511 CC->user.flags |= (US_REGIS|US_NEEDVALID);
517 ptr = strchr((char *)ptr, '\n');
518 if (ptr != NULL) ++ptr;
527 * back end function used for callbacks
529 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
532 msgnum = (long *) userdata;
533 *msgnum = supplied_msgnum;
538 * If this user has a vcard on disk, read it into memory, otherwise allocate
539 * and return an empty vCard.
541 struct vCard *vcard_get_user(struct ctdluser *u) {
542 char hold_rm[ROOMNAMELEN];
543 char config_rm[ROOMNAMELEN];
544 struct CtdlMessage *msg = NULL;
548 strcpy(hold_rm, CC->room.QRname); /* save current room */
549 MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
551 if (getroom(&CC->room, config_rm) != 0) {
552 getroom(&CC->room, hold_rm);
556 /* We want the last (and probably only) vcard in this room */
558 CtdlForEachMessage(MSGS_LAST, 1, NULL, "text/x-vcard",
559 NULL, vcard_gu_backend, (void *)&VCmsgnum );
560 getroom(&CC->room, hold_rm); /* return to saved room */
562 if (VCmsgnum < 0L) return vcard_new();
564 msg = CtdlFetchMessage(VCmsgnum, 1);
565 if (msg == NULL) return vcard_new();
567 v = vcard_load(msg->cm_fields['M']);
568 CtdlFreeMessage(msg);
574 * Store this user's vCard in the appropriate place
577 * Write our config to disk
579 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
584 CtdlMakeTempFileName(temp, sizeof temp);
585 ser = vcard_serialize(v);
587 fp = fopen(temp, "w");
588 if (fp == NULL) return;
590 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
592 fwrite(ser, strlen(ser), 1, fp);
597 /* This handy API function does all the work for us.
598 * NOTE: normally we would want to set that last argument to 1, to
599 * force the system to delete the user's old vCard. But it doesn't
600 * have to, because the vcard_upload_beforesave() hook above
601 * is going to notice what we're trying to do, and delete the old vCard.
603 CtdlWriteObject(USERCONFIGROOM, /* which room */
604 "text/x-vcard", /* MIME type */
605 temp, /* temp file */
608 0, /* don't delete others of this type */
617 * Old style "enter registration info" command. This function simply honors
618 * the REGI protocol command, translates the entered parameters into a vCard,
619 * and enters the vCard into the user's configuration.
621 void cmd_regi(char *argbuf) {
624 struct vCard *my_vcard;
630 char tmpaddress[SIZ];
631 char tmpcountry[SIZ];
635 if (!(CC->logged_in)) {
636 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
640 my_vcard = vcard_get_user(&CC->user);
643 strcpy(tmpstate, "");
645 strcpy(tmpcountry, "USA");
647 cprintf("%d Send registration...\n", SEND_LISTING);
649 while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
650 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
651 if (a==1) strcpy(tmpaddr, buf);
652 if (a==2) strcpy(tmpcity, buf);
653 if (a==3) strcpy(tmpstate, buf);
655 for (c=0; c<strlen(buf); ++c) {
656 if ((buf[c]>='0') && (buf[c]<='9')) {
663 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
664 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
665 if (a==7) strcpy(tmpcountry, buf);
669 snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
670 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
671 vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
672 vcard_write_user(&CC->user, my_vcard);
673 vcard_free(my_vcard);
678 * Protocol command to fetch registration info for a user
680 void cmd_greg(char *argbuf)
682 struct ctdluser usbuf;
685 char who[USERNAME_SIZE];
689 extract_token(who, argbuf, 0, '|', sizeof who);
691 if (!(CC->logged_in)) {
692 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
696 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
698 if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
699 cprintf("%d Higher access required.\n",
700 ERROR + HIGHER_ACCESS_REQUIRED);
704 if (getuser(&usbuf, who) != 0) {
705 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
709 v = vcard_get_user(&usbuf);
711 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
712 cprintf("%ld\n", usbuf.usernum);
713 cprintf("%s\n", usbuf.password);
714 s = vcard_get_prop(v, "n", 0, 0, 0);
715 cprintf("%s\n", s ? s : " "); /* name */
717 s = vcard_get_prop(v, "adr", 0, 0, 0);
718 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
720 extract_token(buf, adr, 2, ';', sizeof buf);
721 cprintf("%s\n", buf); /* street */
722 extract_token(buf, adr, 3, ';', sizeof buf);
723 cprintf("%s\n", buf); /* city */
724 extract_token(buf, adr, 4, ';', sizeof buf);
725 cprintf("%s\n", buf); /* state */
726 extract_token(buf, adr, 5, ';', sizeof buf);
727 cprintf("%s\n", buf); /* zip */
729 s = vcard_get_prop(v, "tel;home", 0, 0, 0);
730 if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
738 cprintf("%d\n", usbuf.axlevel);
740 s = vcard_get_prop(v, "email;internet", 0, 0, 0);
741 cprintf("%s\n", s ? s : " ");
742 s = vcard_get_prop(v, "adr", 0, 0, 0);
743 snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
745 extract_token(buf, adr, 6, ';', sizeof buf);
746 cprintf("%s\n", buf); /* country */
753 * When a user is being created, create his/her vCard.
755 void vcard_newuser(struct ctdluser *usbuf) {
761 vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
762 lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
764 /* Create and save the vCard */
766 if (v == NULL) return;
767 sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
768 for (i=0; i<strlen(buf); ++i) {
769 if (buf[i] == ' ') buf[i] = '_';
771 vcard_add_prop(v, "fn", usbuf->fullname);
772 vcard_add_prop(v, "n", vname);
773 vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
774 vcard_add_prop(v, "email;internet", buf);
775 vcard_write_user(usbuf, v);
781 * When a user is being deleted, we have to remove his/her vCard.
782 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
783 * field, and the same Exclusive ID as the existing card.
785 void vcard_purge(struct ctdluser *usbuf) {
786 struct CtdlMessage *msg;
789 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
790 if (msg == NULL) return;
791 memset(msg, 0, sizeof(struct CtdlMessage));
793 msg->cm_magic = CTDLMESSAGE_MAGIC;
794 msg->cm_anon_type = MES_NORMAL;
795 msg->cm_format_type = 0;
796 msg->cm_fields['A'] = strdup(usbuf->fullname);
797 msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
798 msg->cm_fields['N'] = strdup(NODENAME);
799 msg->cm_fields['M'] = strdup("Purge this vCard\n");
801 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
802 msg->cm_fields['A'], NODENAME);
803 msg->cm_fields['E'] = strdup(buf);
805 msg->cm_fields['S'] = strdup("CANCEL");
807 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
808 CtdlFreeMessage(msg);
813 * Grab vCard directory stuff out of incoming network messages
815 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
819 if (msg == NULL) return(0);
821 if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
825 if (msg->cm_format_type != 4) return(0);
827 ptr = msg->cm_fields['M'];
828 if (ptr == NULL) return(0);
829 while (ptr != NULL) {
831 linelen = strcspn(ptr, "\n");
832 if (linelen == 0) return(0); /* end of headers */
834 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
835 /* It's a vCard. Add it to the directory. */
836 vcard_extract_internet_addresses(msg,
837 CtdlDirectoryAddUser);
841 ptr = strchr((char *)ptr, '\n');
842 if (ptr != NULL) ++ptr;
851 * When a vCard is being removed from the Global Address Book room, remove it
852 * from the directory as well.
854 void vcard_delete_remove(char *room, long msgnum) {
855 struct CtdlMessage *msg;
859 if (msgnum <= 0L) return;
861 if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
865 msg = CtdlFetchMessage(msgnum, 1);
866 if (msg == NULL) return;
868 ptr = msg->cm_fields['M'];
869 if (ptr == NULL) goto EOH;
870 while (ptr != NULL) {
871 linelen = strcspn(ptr, "\n");
872 if (linelen == 0) goto EOH;
874 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
875 /* Bingo! A vCard is being deleted.
877 vcard_extract_internet_addresses(msg,
878 CtdlDirectoryDelUser);
880 ctdl_vcard_to_ldap(msg, V2L_DELETE);
883 ptr = strchr((char *)ptr, '\n');
884 if (ptr != NULL) ++ptr;
887 EOH: CtdlFreeMessage(msg);
893 * Get Valid Screen Names
895 void cmd_gvsn(char *argbuf)
897 if (CtdlAccessCheck(ac_logged_in)) return;
899 cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
900 cprintf("%s\n", CC->user.fullname);
901 if ( (strlen(CC->cs_inet_fn) > 0) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
902 cprintf("%s\n", CC->cs_inet_fn);
911 void cmd_qdir(char *argbuf) {
912 char citadel_addr[256];
913 char internet_addr[256];
915 if (CtdlAccessCheck(ac_logged_in)) return;
917 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
919 if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
920 cprintf("%d %s was not found.\n",
921 ERROR + NO_SUCH_USER, internet_addr);
925 cprintf("%d %s\n", CIT_OK, citadel_addr);
929 * Query Directory, in fact an alias to match postfix tcp auth.
931 void check_get(void) {
932 char internet_addr[256];
937 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
938 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
939 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
943 lprintf(CTDL_INFO, ": %s\n", cmdbuf);
944 while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
946 if (strcasecmp(cmdbuf, "GET "));
948 struct recptypes *rcpt;
949 char *argbuf = &cmdbuf[4];
951 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
952 rcpt = validate_recipients(internet_addr);
955 (*rcpt->recp_local != '\0')||
956 (*rcpt->recp_room != '\0')||
957 (*rcpt->recp_ignet != '\0')))
960 cprintf("200 OK %s\n", internet_addr);
961 lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
965 cprintf("500 REJECT noone here by that name.\n");
967 lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
969 if (rcpt != NULL) free (rcpt);
974 void check_get_greeting(void) {
975 /* dummy function, we have no greeting in this verry simple protocol. */
980 * We don't know if the Contacts room exists so we just create it at login
982 void vcard_create_room(void)
987 /* Create the calendar room if it doesn't already exist */
988 create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
990 /* Set expiration policy to manual; otherwise objects will be lost! */
991 if (lgetroom(&qr, USERCONTACTSROOM)) {
992 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
995 qr.QRep.expire_mode = EXPIRE_MANUAL;
996 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
999 /* Set the view to a calendar view */
1000 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1001 vbuf.v_view = 2; /* 2 = address book view */
1002 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1011 * When a user logs in...
1013 void vcard_session_login_hook(void) {
1014 struct vCard *v = NULL;
1016 v = vcard_get_user(&CC->user);
1017 extract_primary_inet_email(CC->cs_inet_email, sizeof CC->cs_inet_email, v);
1018 extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1021 vcard_create_room();
1026 * Turn an arbitrary RFC822 address into a struct vCard for possible
1027 * inclusion into an address book.
1029 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1031 char user[256], node[256], name[256], email[256], n[256], uid[256];
1035 if (v == NULL) return(NULL);
1037 process_rfc822_addr(addr, user, node, name);
1038 vcard_set_prop(v, "fn", name, 0);
1040 vcard_fn_to_n(n, name, sizeof n);
1041 vcard_set_prop(v, "n", n, 0);
1043 snprintf(email, sizeof email, "%s@%s", user, node);
1044 vcard_set_prop(v, "email;internet", email, 0);
1046 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1047 for (i=0; i<strlen(uid); ++i) {
1048 if (isspace(uid[i])) uid[i] = '_';
1049 uid[i] = tolower(uid[i]);
1051 vcard_set_prop(v, "UID", uid, 0);
1059 * This is called by store_harvested_addresses() to remove from the
1060 * list any addresses we already have in our address book.
1062 void strip_addresses_already_have(long msgnum, void *userdata) {
1063 char *collected_addresses;
1064 struct CtdlMessage *msg = NULL;
1068 char addr[256], user[256], node[256], name[256];
1070 collected_addresses = (char *)userdata;
1072 msg = CtdlFetchMessage(msgnum, 1);
1073 if (msg == NULL) return;
1074 v = vcard_load(msg->cm_fields['M']);
1075 CtdlFreeMessage(msg);
1078 while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1080 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1081 extract_token(addr, collected_addresses, j, ',', sizeof addr);
1083 /* Remove the address if we already have it! */
1084 process_rfc822_addr(addr, user, node, name);
1085 snprintf(addr, sizeof addr, "%s@%s", user, node);
1086 if (!strcasecmp(value, addr)) {
1087 remove_token(collected_addresses, j, ',');
1099 * Back end function for store_harvested_addresses()
1101 void store_this_ha(struct addresses_to_be_filed *aptr) {
1102 struct CtdlMessage *vmsg = NULL;
1103 long vmsgnum = (-1L);
1105 struct vCard *v = NULL;
1106 char recipient[256];
1109 /* First remove any addresses we already have in the address book */
1110 usergoto(aptr->roomname, 0, 0, NULL, NULL);
1111 CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/x-vcard", NULL,
1112 strip_addresses_already_have, aptr->collected_addresses);
1114 if (strlen(aptr->collected_addresses) > 0)
1115 for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1117 /* Make a vCard out of each address */
1118 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1120 v = vcard_new_from_rfc822_addr(recipient);
1122 vmsg = malloc(sizeof(struct CtdlMessage));
1123 memset(vmsg, 0, sizeof(struct CtdlMessage));
1124 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1125 vmsg->cm_anon_type = MES_NORMAL;
1126 vmsg->cm_format_type = FMT_RFC822;
1127 vmsg->cm_fields['A'] = strdup("Citadel");
1128 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1129 ser = vcard_serialize(v);
1131 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1132 sprintf(vmsg->cm_fields['M'],
1133 "Content-type: text/x-vcard"
1134 "\r\n\r\n%s\r\n", ser);
1139 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1140 vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1141 CtdlFreeMessage(vmsg);
1145 free(aptr->roomname);
1146 free(aptr->collected_addresses);
1152 * When a user sends a message, we may harvest one or more email addresses
1153 * from the recipient list to be added to the user's address book. But we
1154 * want to do this asynchronously so it doesn't keep the user waiting.
1156 void store_harvested_addresses(void) {
1158 struct addresses_to_be_filed *aptr = NULL;
1160 if (atbf == NULL) return;
1162 begin_critical_section(S_ATBF);
1163 while (atbf != NULL) {
1166 end_critical_section(S_ATBF);
1167 store_this_ha(aptr);
1168 begin_critical_section(S_ATBF);
1170 end_critical_section(S_ATBF);
1175 * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
1176 * really this is just so we expose the vCard data to the full text indexer.
1178 void vcard_fixed_output(char *ptr, int len) {
1179 char *serialized_vcard;
1184 serialized_vcard = malloc(len + 1);
1185 safestrncpy(serialized_vcard, ptr, len+1);
1186 v = vcard_load(serialized_vcard);
1187 free(serialized_vcard);
1190 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1191 value = vcard_get_prop(v, "", 0, i++, 0);
1192 cprintf("%s\n", value);
1199 char *serv_postfix_tcpdict(void)
1201 CtdlRegisterServiceHook(config.c_pftcpdict_port, /* Postfix */
1211 char *serv_vcard_init(void)
1217 CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1218 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1219 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1220 CtdlRegisterDeleteHook(vcard_delete_remove);
1221 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1222 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1223 CtdlRegisterProtoHook(cmd_igab, "IGAB",
1224 "Initialize Global Address Book");
1225 CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1226 CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1227 CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1228 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1229 CtdlRegisterNetprocHook(vcard_extract_from_network);
1230 CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1231 CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1233 /* Create the Global ADdress Book room if necessary */
1234 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1236 /* Set expiration policy to manual; otherwise objects will be lost! */
1237 if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1238 qr.QRep.expire_mode = EXPIRE_MANUAL;
1239 qr.QRdefaultview = VIEW_ADDRESSBOOK; /* 2 = address book view */
1243 * Also make sure it has a netconfig file, so the networker runs
1244 * on this room even if we don't share it with any other nodes.
1245 * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1248 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1249 fp = fopen(filename, "a");
1250 if (fp != NULL) fclose(fp);
1251 chown(filename, CTDLUID, (-1));