HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / modules / vcard / serv_vcard.c
index 066fc72534780a57216584f45b17e52faf695238..0e3d264f82b5b7d3454102a504f2f6366c156cfb 100644 (file)
@@ -47,6 +47,7 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
@@ -59,8 +60,6 @@
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
-#include "tools.h"
-#include "mime_parser.h"
 #include "vcard.h"
 #include "serv_vcard.h"
 
@@ -141,8 +140,6 @@ void vcard_extract_internet_addresses(struct CtdlMessage *msg,
 void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
        struct vCard *v = NULL;
        int i;
-       int num_emails = 0;
-       int num_phones = 0;
        int have_addr = 0;
        int have_cn = 0;
        
@@ -156,6 +153,7 @@ void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
        char state[3];
        char zipcode[10];
        char calFBURL[256];
+       char ldap_dn[SIZ];
 
        if (msg == NULL) return;
        if (msg->cm_fields['M'] == NULL) return;
@@ -172,9 +170,11 @@ void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
                msg->cm_fields['N']
        );
 
+       sprintf(ldap_dn, "euid=%s,ou=%s", msg->cm_fields['E'], msg->cm_fields['N']);
+       
        /* Are we just deleting?  If so, it's simple... */
        if (op == V2L_DELETE) {
-               (void) CtdlDoDirectoryServiceFunc (msg->cm_fields['A'], msg->cm_fields['N'], NULL, "ldap", DIRECTORY_USER_DEL);
+               (void) CtdlDoDirectoryServiceFunc (ldap_dn, NULL, NULL, "ldap", DIRECTORY_USER_DEL);
                return;
        }
 
@@ -235,6 +235,12 @@ void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
                                extract_token(state, v->prop[i].value, 4, ';', sizeof state);
                                extract_token(zipcode, v->prop[i].value, 5, ';', sizeof zipcode);
 
+                               // ldap requires these fields to be something
+                               if (IsEmptyStr(street)) strcpy(street, "_");
+                               if (IsEmptyStr(zipcode)) strcpy(zipcode, "_");
+                               if (IsEmptyStr(city)) strcpy(city, "_");
+                               if (IsEmptyStr(state)) strcpy(state, "_");
+
                                (void) CtdlDoDirectoryServiceFunc("street", street, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
                                (void) CtdlDoDirectoryServiceFunc("l", city, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
                                (void) CtdlDoDirectoryServiceFunc("st", state, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
@@ -242,34 +248,24 @@ void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
                        }
                }
 
+               if (!strcasecmp(v->prop[i].name, "tel;home"))
+                       (void) CtdlDoDirectoryServiceFunc("homePhone", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
+               else
+               if (!strcasecmp(v->prop[i].name, "tel;fax"))
+                       (void) CtdlDoDirectoryServiceFunc("facsimileTelephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
+               else
+               if (!strcasecmp(v->prop[i].name, "tel;cell"))
+                       (void) CtdlDoDirectoryServiceFunc("mobile", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
+               else
                if ( (!strcasecmp(v->prop[i].name, "tel"))
                   ||(!strncasecmp(v->prop[i].name, "tel;", 4)) ) {
-                       ++num_phones;
-                       /* The first 'tel' property creates the 'telephoneNumber' attribute */
-                       if (num_phones == 1) {
-                               (void) CtdlDoDirectoryServiceFunc("telephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
-                       }
-                       /* Subsequent 'tel' properties *add to* the 'telephoneNumber' attribute */
-                       else {
-                               (void) CtdlDoDirectoryServiceFunc("telephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
-                       }
+                       (void) CtdlDoDirectoryServiceFunc("telephoneNumber", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
                }
 
 
                if ( (!strcasecmp(v->prop[i].name, "email"))
                   ||(!strcasecmp(v->prop[i].name, "email;internet")) ) {
-       
-                       ++num_emails;
-                       lprintf(CTDL_DEBUG, "email addr %d\n", num_emails);
-
-                       /* The first email address creates the 'mail' attribute */
-                       if (num_emails == 1) {
-                               (void) CtdlDoDirectoryServiceFunc("mail", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
-                       }
-                       /* The second and subsequent email address creates the 'alias' attribute */
-                       else if (num_emails >= 2) {
-                               (void) CtdlDoDirectoryServiceFunc("alias", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
-                       }
+                       (void) CtdlDoDirectoryServiceFunc("mail", v->prop[i].value, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
                }
 
                /* Calendar free/busy URL (take the first one we find, but if a subsequent
@@ -300,19 +296,22 @@ void ctdl_vcard_to_directory(struct CtdlMessage *msg, int op) {
        /* Add a "cn" (Common Name) attribute based on the user's screen name,
         * but only there was no 'fn' (full name) property in the vCard 
         */
-       if (!have_cn) {
+       if (!have_cn)
                (void) CtdlDoDirectoryServiceFunc("cn", msg->cm_fields['A'], &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
-       }
 
        /* Add a "calFBURL" attribute if a calendar free/busy URL exists */
        if (!IsEmptyStr(calFBURL)) {
                (void) CtdlDoDirectoryServiceFunc("calFBURL", calFBURL, &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
        }
        
-       (void) CtdlDoDirectoryServiceFunc(msg->cm_fields['A'], msg->cm_fields['N'], &objectlist, "ldap", DIRECTORY_SAVE_OBJECT);
-
+       // Add this messages EUID as the primary key for this entry.
+       (void) CtdlDoDirectoryServiceFunc("euid", msg->cm_fields['E'], &objectlist, "ldap", DIRECTORY_ATTRIB_ADD);
+       
+       
+       (void) CtdlDoDirectoryServiceFunc(ldap_dn, NULL, &objectlist, "ldap", DIRECTORY_SAVE_OBJECT);
+       
        (void) CtdlDoDirectoryServiceFunc(NULL, NULL, &objectlist, "ldap", DIRECTORY_FREE_OBJECT);
-       lprintf(CTDL_DEBUG, "LDAP write operation complete.\n");
+       lprintf(CTDL_DEBUG, "Directory Services write operation complete.\n");
 }