* Silently refuse to add directory entries for Internet addresses already
[citadel.git] / citadel / serv_vcard.c
index 36f4b2316cb2ad9a63af636df1b3fa86913bc25e..f9853303e5dd461041c6dfd0e3bd0cf89a570401 100644 (file)
@@ -4,7 +4,7 @@
  * A server-side module for Citadel which supports address book information
  * using the standard vCard format.
  * 
- * Copyright (c) 1999-2001 / released under the GNU General Public License
+ * Copyright (c) 1999-2002 / released under the GNU General Public License
  */
 
 /*
@@ -83,6 +83,8 @@ void vcard_extract_internet_addresses(struct CtdlMessage *msg,
        char *s;
        char *addr;
        char citadel_address[SIZ];
+       int instance = 0;
+       int found_something = 0;
 
        if (msg->cm_fields['A'] == NULL) return;
        if (msg->cm_fields['N'] == NULL) return;
@@ -92,31 +94,70 @@ void vcard_extract_internet_addresses(struct CtdlMessage *msg,
        v = vcard_load(msg->cm_fields['M']);
        if (v == NULL) return;
 
-       s = vcard_get_prop(v, "email;internet", 0); /* FIXME handle multiples */
-       if (s != NULL) {
-               addr = strdoop(s);
-               striplt(addr);
-               if (strlen(addr) > 0) {
-                       if (callback != NULL) {
-                               callback(addr, citadel_address);
+       /* Go through the vCard searching for *all* instances of
+        * the "email;internet" key
+        */
+       do {
+               s = vcard_get_prop(v, "email;internet", 0, instance++);
+               if (s != NULL) {
+                       addr = strdoop(s);
+                       striplt(addr);
+                       if (strlen(addr) > 0) {
+                               if (callback != NULL) {
+                                       callback(addr, citadel_address);
+                               }
                        }
+                       phree(addr);
+                       found_something = 1;
                }
-               phree(addr);
-       }
+               else {
+                       found_something = 0;
+               }
+       } while(found_something);
 
        vcard_free(v);
 }
 
+
+
+/*
+ * Callback for vcard_add_to_directory()
+ * (Lotsa ugly nested callbacks.  Oh well.)
+ * This little shim function makes sure we're not 
+ */
+void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
+       char buf[SIZ];
+
+       /* We have to validate that we're not stepping on someone else's
+        * email address ... but only if we're logged in.  Otherwise it's
+        * probably just the networker or something.
+        */
+       if (CC->logged_in) {
+               lprintf(9, "Checking for <%s>...\n", internet_addr);
+               if (CtdlDirectoryLookup(buf, internet_addr) == 0) {
+                       if (strcasecmp(buf, citadel_addr)) {
+                               /* This address belongs to someone else.
+                                * Bail out silently without saving.
+                                */
+                               lprintf(9, "DOOP!\n");
+                               return;
+                       }
+               }
+       }
+       lprintf(9, "ADDING!\n");
+       CtdlDirectoryAddUser(internet_addr, citadel_addr);
+}
+
+
 /*
  * Back end function for cmd_igab()
- * FIXME use a callback that actually writes to the database, dumbass...
  */
 void vcard_add_to_directory(long msgnum, void *data) {
        struct CtdlMessage *msg;
 
        msg = CtdlFetchMessage(msgnum);
        if (msg != NULL) {
-               vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
+               vcard_extract_internet_addresses(msg, vcard_directory_add_user);
        }
 
        CtdlFreeMessage(msg);
@@ -139,19 +180,59 @@ void cmd_igab(char *argbuf) {
                return;
         }
 
-       /* FIXME empty the existing database first.  And don't be a
-        * freakin' momo and dump addresses to the client.  We want to write
-        * the harvested addresses into the database and send an OK to the
-        * client when finished.
+       /* Empty the existing database first.
         */
-       
-       cprintf("%d Directory will be rebuilt\n", OK);
+       CtdlDirectoryInit();
 
         /* We want the last (and probably only) vcard in this room */
         CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
                NULL, vcard_add_to_directory, NULL);
 
         getroom(&CC->quickroom, hold_rm);      /* return to saved room */
+       cprintf("%d Directory has been rebuilt.\n", OK);
+}
+
+
+
+
+/*
+ * See if there is a valid Internet address in a vCard to use for outbound
+ * Interet messages.  If there is, stick it in CC->cs_inet_email.
+ */
+void vcard_populate_cs_inet_email(struct vCard *v) {
+       char *s, *addr;
+       int continue_searching = 1;
+       int instance = 0;
+
+       /* 
+        * Clear whatever was in there previously.
+        */
+       if (CC->cs_inet_email != NULL) {
+               phree(CC->cs_inet_email);
+               CC->cs_inet_email = NULL;
+       }
+
+       /* Go through the vCard searching for *all* instances of
+        * the "email;internet" key
+        */
+       do {
+               s = vcard_get_prop(v, "email;internet", 0, instance++);
+               if (s != NULL) {
+                       continue_searching = 1;
+                       addr = strdoop(s);
+                       striplt(addr);
+                       if (strlen(addr) > 0) {
+                               if (IsDirectory(addr)) {
+                                       continue_searching = 0;
+                                       CC->cs_inet_email = strdoop(addr);
+                               }
+                       }
+                       phree(addr);
+               }
+               else {
+                       continue_searching = 0;
+               }
+       } while(continue_searching);
 }
 
 
@@ -235,6 +316,7 @@ int vcard_upload_aftersave(struct CtdlMessage *msg) {
        char *ptr;
        int linelen;
        long I;
+       struct vCard *v;
 
        if (!CC->logged_in) return(0);  /* Only do this if logged in. */
 
@@ -267,6 +349,11 @@ int vcard_upload_aftersave(struct CtdlMessage *msg) {
                        /* ...and also in the directory database. */
                        vcard_add_to_directory(I, NULL);
 
+                       /* Store our Internet return address in memory */
+                       v = vcard_load(msg->cm_fields['M']);
+                       vcard_populate_cs_inet_email(v);
+                       vcard_free(v);
+
                        return(0);
                }
 
@@ -397,7 +484,7 @@ void cmd_regi(char *argbuf) {
        cprintf("%d Send registration...\n", SEND_LISTING);
        a=0;
        while (client_gets(buf), strcmp(buf,"000")) {
-               if (a==0) vcard_set_prop(my_vcard, "n", buf);
+               if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
                if (a==1) strcpy(tmpaddr, buf);
                if (a==2) strcpy(tmpcity, buf);
                if (a==3) strcpy(tmpstate, buf);
@@ -410,15 +497,15 @@ void cmd_regi(char *argbuf) {
                                }
                        }
                }
-               if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
-               if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
+               if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
+               if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
                if (a==7) strcpy(tmpcountry, buf);
                ++a;
        }
 
        sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
                tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
-       vcard_set_prop(my_vcard, "adr", tmpaddress);
+       vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
        vcard_write_user(&CC->usersupp, my_vcard);
        vcard_free(my_vcard);
 
@@ -473,10 +560,10 @@ void cmd_greg(char *argbuf)
        cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
        cprintf("%ld\n", usbuf.usernum);
        cprintf("%s\n", usbuf.password);
-       s = vcard_get_prop(v, "n", 0);
+       s = vcard_get_prop(v, "n", 0, 0);
        cprintf("%s\n", s ? s : " ");   /* name */
 
-       s = vcard_get_prop(v, "adr", 0);
+       s = vcard_get_prop(v, "adr", 0, 0);
        sprintf(adr, "%s", s ? s : " ");/* address... */
 
        extract_token(buf, adr, 2, ';');
@@ -488,8 +575,8 @@ void cmd_greg(char *argbuf)
        extract_token(buf, adr, 5, ';');
        cprintf("%s\n", buf);                           /* zip */
 
-       s = vcard_get_prop(v, "tel;home", 0);
-       if (s == NULL) s = vcard_get_prop(v, "tel", 1);
+       s = vcard_get_prop(v, "tel;home", 0, 0);
+       if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0);
        if (s != NULL) {
                cprintf("%s\n", s);
        }
@@ -499,9 +586,9 @@ void cmd_greg(char *argbuf)
 
        cprintf("%d\n", usbuf.axlevel);
 
-       s = vcard_get_prop(v, "email;internet", 0);
+       s = vcard_get_prop(v, "email;internet", 0, 0);
        cprintf("%s\n", s ? s : " ");
-       s = vcard_get_prop(v, "adr", 0);
+       s = vcard_get_prop(v, "adr", 0, 0);
        sprintf(adr, "%s", s ? s : " ");/* address... */
 
        extract_token(buf, adr, 6, ';');
@@ -539,8 +626,89 @@ void vcard_purge(char *username, long usernum) {
         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
         CtdlFreeMessage(msg);
 }
+
+
+/*
+ * Grab vCard directory stuff out of incoming network messages
+ */
+int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
+       char *ptr;
+       int linelen;
+
+       if (msg == NULL) return(0);
+
+       if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
+               return(0);
+       }
+
+       if (msg->cm_format_type != 4) return(0);
+
+       ptr = msg->cm_fields['M'];
+       if (ptr == NULL) return(0);
+       while (ptr != NULL) {
        
-       
+               linelen = strcspn(ptr, "\n");
+               if (linelen == 0) return(0);    /* end of headers */    
+               
+               if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
+                        /* It's a vCard.  Add it to the directory. */
+                       vcard_extract_internet_addresses(msg,
+                                                       CtdlDirectoryAddUser);
+                       return(0);
+               }
+
+               ptr = strchr((char *)ptr, '\n');
+               if (ptr != NULL) ++ptr;
+       }
+
+       return(0);
+}
+
+
+
+/* 
+ * When a vCard is being removed from the Global Address Book room, remove it
+ * from the directory as well.
+ */
+void vcard_delete_remove(char *room, long msgnum) {
+       struct CtdlMessage *msg;
+
+       if (msgnum <= 0L) return;
+
+       if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
+               return;
+       }
+
+       msg = CtdlFetchMessage(msgnum);
+       if (msg != NULL) {
+               vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
+       }
+
+       CtdlFreeMessage(msg);
+}
+
+
+/*
+ * Query Directory
+ */
+void cmd_qdir(char *argbuf) {
+       char citadel_addr[SIZ];
+       char internet_addr[SIZ];
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+
+       extract(internet_addr, argbuf, 0);
+
+       if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
+               cprintf("%d %s was not found.\n",
+                       ERROR+NO_SUCH_USER, internet_addr);
+               return;
+       }
+
+       cprintf("%d %s\n", OK, citadel_addr);
+}
+
+
 
 
 /*
@@ -551,17 +719,46 @@ void vcard_session_startup_hook(void) {
 }
 
 
+/*
+ * When a user logs in...
+ */
+void vcard_session_login_hook(void) {
+       struct vCard *v;
+
+       v = vcard_get_user(&CC->usersupp);
+       vcard_populate_cs_inet_email(v);
+
+       vcard_free(v);
+}
+
+
+/*
+ * When a user logs out...
+ */
+void vcard_session_logout_hook(void) {
+       if (CC->cs_inet_email != NULL) {
+               phree(CC->cs_inet_email);
+               CC->cs_inet_email = NULL;
+       }
+}
+
+
 char *Dynamic_Module_Init(void)
 {
        SYM_VCARD = CtdlGetDynamicSymbol();
        CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
+       CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
+       CtdlRegisterSessionHook(vcard_session_logout_hook, EVT_LOGOUT);
        CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
        CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
+       CtdlRegisterDeleteHook(vcard_delete_remove);
        CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
        CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
        CtdlRegisterProtoHook(cmd_igab, "IGAB",
                                        "Initialize Global Address Book");
+       CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
        CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
+       CtdlRegisterNetprocHook(vcard_extract_from_network);
        create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);
        return "$Id$";
 }