+
+
+/*
+ * Back end function for store_harvested_addresses()
+ */
+void store_this_ha(struct addresses_to_be_filed *aptr) {
+ struct CtdlMessage *vmsg = NULL;
+ long vmsgnum = (-1L);
+ char *ser = NULL;
+ struct vCard *v = NULL;
+ char recipient[256];
+ int i;
+
+ /* First remove any addresses we already have in the address book */
+ usergoto(aptr->roomname, 0, 0, NULL, NULL);
+ CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/x-vcard", NULL,
+ strip_addresses_already_have, aptr->collected_addresses);
+
+ if (strlen(aptr->collected_addresses) > 0)
+ for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
+
+ /* Make a vCard out of each address */
+ extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
+ striplt(recipient);
+ v = vcard_new_from_rfc822_addr(recipient);
+ if (v != NULL) {
+ vmsg = malloc(sizeof(struct CtdlMessage));
+ memset(vmsg, 0, sizeof(struct CtdlMessage));
+ vmsg->cm_magic = CTDLMESSAGE_MAGIC;
+ vmsg->cm_anon_type = MES_NORMAL;
+ vmsg->cm_format_type = FMT_RFC822;
+ vmsg->cm_fields['A'] = strdup("Citadel");
+ vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
+ ser = vcard_serialize(v);
+ if (ser != NULL) {
+ vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
+ sprintf(vmsg->cm_fields['M'],
+ "Content-type: text/x-vcard"
+ "\r\n\r\n%s\r\n", ser);
+ free(ser);
+ }
+ vcard_free(v);
+
+ lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
+ vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
+ CtdlFreeMessage(vmsg);
+ }
+ }
+
+ free(aptr->roomname);
+ free(aptr->collected_addresses);
+ free(aptr);
+}
+
+
+/*
+ * When a user sends a message, we may harvest one or more email addresses
+ * from the recipient list to be added to the user's address book. But we
+ * want to do this asynchronously so it doesn't keep the user waiting.
+ */
+void store_harvested_addresses(void) {
+
+ struct addresses_to_be_filed *aptr = NULL;
+
+ if (atbf == NULL) return;
+
+ begin_critical_section(S_ATBF);
+ while (atbf != NULL) {
+ aptr = atbf;
+ atbf = atbf->next;
+ end_critical_section(S_ATBF);
+ store_this_ha(aptr);
+ begin_critical_section(S_ATBF);
+ }
+ end_critical_section(S_ATBF);
+}
+
+
+/*
+ * Function to output vCard data as plain text. Nobody uses MSG0 anymore, so
+ * really this is just so we expose the vCard data to the full text indexer.
+ */
+void vcard_fixed_output(char *ptr, int len) {
+ char *serialized_vcard;
+ struct vCard *v;
+ char *key, *value;
+ int i = 0;
+
+ serialized_vcard = malloc(len + 1);
+ safestrncpy(serialized_vcard, ptr, len+1);
+ v = vcard_load(serialized_vcard);
+ free(serialized_vcard);
+
+ i = 0;
+ while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
+ value = vcard_get_prop(v, "", 0, i++, 0);
+ cprintf("%s\n", value);
+ }
+
+ vcard_free(v);