]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_vcard.c
* Buffered output needs to be flushed in several places. Added calls to
[citadel.git] / citadel / serv_vcard.c
index c1c30c670572f9d49cfd26e4dfb150fd499e47fa..af22b3fda2f44a4997dd47c76d42bb37cfaa54d6 100644 (file)
@@ -113,14 +113,14 @@ void vcard_extract_internet_addresses(struct CtdlMessage *msg,
        do {
                s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
                if (s != NULL) {
-                       addr = strdoop(s);
+                       addr = strdup(s);
                        striplt(addr);
                        if (strlen(addr) > 0) {
                                if (callback != NULL) {
                                        callback(addr, citadel_address);
                                }
                        }
-                       phree(addr);
+                       free(addr);
                        found_something = 1;
                }
                else {
@@ -146,18 +146,19 @@ void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
         * probably just the networker or something.
         */
        if (CC->logged_in) {
-               lprintf(9, "Checking for <%s>...\n", internet_addr);
+               lprintf(CTDL_DEBUG, "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");
+                               lprintf(CTDL_DEBUG, "DOOP!\n");
                                return;
                        }
                }
        }
-       lprintf(9, "ADDING!\n");
+       lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
+                       citadel_addr, internet_addr);
        CtdlDirectoryAddUser(internet_addr, citadel_addr);
 }
 
@@ -168,7 +169,7 @@ void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
 void vcard_add_to_directory(long msgnum, void *data) {
        struct CtdlMessage *msg;
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        if (msg != NULL) {
                vcard_extract_internet_addresses(msg, vcard_directory_add_user);
        }
@@ -228,7 +229,7 @@ void vcard_populate_cs_inet_email(struct vCard *v) {
                s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
                if (s != NULL) {
                        continue_searching = 1;
-                       addr = strdoop(s);
+                       addr = strdup(s);
                        striplt(addr);
                        if (strlen(addr) > 0) {
                                if (IsDirectory(addr)) {
@@ -239,7 +240,7 @@ void vcard_populate_cs_inet_email(struct vCard *v) {
                                        );
                                }
                        }
-                       phree(addr);
+                       free(addr);
                }
                else {
                        continue_searching = 0;
@@ -261,6 +262,9 @@ int vcard_upload_beforesave(struct CtdlMessage *msg) {
        char buf[SIZ];
        struct ctdluser usbuf;
        long what_user;
+       struct vCard *v = NULL;
+       char *ser = NULL;
+       int i = 0;
 
        if (!CC->logged_in) return(0);  /* Only do this if logged in. */
 
@@ -319,16 +323,44 @@ int vcard_upload_beforesave(struct CtdlMessage *msg) {
                         * replication always works correctly
                         */
                         if (msg->cm_fields['E'] != NULL)
-                                phree(msg->cm_fields['E']);
+                                free(msg->cm_fields['E']);
 
                         if (msg->cm_fields['A'] != NULL)
-                                phree(msg->cm_fields['A']);
+                                free(msg->cm_fields['A']);
 
-                       msg->cm_fields['A'] = strdoop(usbuf.fullname);
+                       msg->cm_fields['A'] = strdup(usbuf.fullname);
 
                         snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
                                 msg->cm_fields['A'], NODENAME);
-                        msg->cm_fields['E'] = strdoop(buf);
+                        msg->cm_fields['E'] = strdup(buf);
+
+                       /* Insert or replace RFC2739-compliant free/busy URL */
+                       v = vcard_load(msg->cm_fields['M']);
+                       if (v != NULL) {
+
+                               /* Manipulate the vCard data structure */
+                               sprintf(buf, "http://%s/%s.vfb",
+                                       config.c_fqdn,
+                                       usbuf.fullname);
+                               for (i=0; i<strlen(buf); ++i) {
+                                       if (buf[i] == ' ') buf[i] = '_';
+                               }
+                               vcard_set_prop(v, "FBURL;PREF", buf, 0);
+
+                               /* Re-serialize it back into the msg body */
+                               ser = vcard_serialize(v);
+                               if (ser != NULL) {
+                                       msg->cm_fields['M'] = realloc(
+                                               msg->cm_fields['M'],
+                                               strlen(ser) + 1024
+                                       );
+                                       sprintf(msg->cm_fields['M'],
+                                               "Content-type: text/x-vcard"
+                                               "\r\n\r\n%s\r\n", ser);
+                                       free(ser);
+                               }
+                               vcard_free(v);
+                       }
 
                        /* Now allow the save to complete. */
                        return(0);
@@ -379,6 +411,11 @@ int vcard_upload_aftersave(struct CtdlMessage *msg) {
                        I = atol(msg->cm_fields['I']);
                        if (I < 0L) return(0);
 
+                       /* Store our Internet return address in memory */
+                       v = vcard_load(msg->cm_fields['M']);
+                       vcard_populate_cs_inet_email(v);
+                       vcard_free(v);
+
                        /* Put it in the Global Address Book room... */
                        CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
                                (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
@@ -386,11 +423,6 @@ 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);
-
                        /* Some sites want an Aide to be notified when a
                         * user registers or re-registers...
                         */
@@ -447,7 +479,7 @@ struct vCard *vcard_get_user(struct ctdluser *u) {
 
        if (VC->msgnum < 0L) return vcard_new();
 
-       msg = CtdlFetchMessage(VC->msgnum);
+       msg = CtdlFetchMessage(VC->msgnum, 1);
        if (msg == NULL) return vcard_new();
 
        v = vcard_load(msg->cm_fields['M']);
@@ -476,7 +508,7 @@ void vcard_write_user(struct ctdluser *u, struct vCard *v) {
                fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
        } else {
                fwrite(ser, strlen(ser), 1, fp);
-               phree(ser);
+               free(ser);
        }
         fclose(fp);
 
@@ -529,6 +561,7 @@ void cmd_regi(char *argbuf) {
        strcpy(tmpcountry, "USA");
 
        cprintf("%d Send registration...\n", SEND_LISTING);
+       flush_output();
        a=0;
        while (client_gets(buf), strcmp(buf,"000")) {
                if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
@@ -632,32 +665,108 @@ void cmd_greg(char *argbuf)
 }
 
 
+/*
+ * When a user is being created, create his/her vCard.
+ */
+void vcard_newuser(struct ctdluser *usbuf) {
+       char buf[SIZ];
+       char vname[SIZ];
+
+       char lastname[SIZ];
+       char firstname[SIZ];
+       char middlename[SIZ];
+       char honorific_prefixes[SIZ];
+       char honorific_suffixes[SIZ];
+
+       struct vCard *v;
+       int i;
+
+       /* Try to intelligently convert the screen name to a
+        * fully expanded vCard name based on the number of
+        * words in the name
+        */
+       strcpy(lastname, "");
+       strcpy(firstname, "");
+       strcpy(middlename, "");
+       strcpy(honorific_prefixes, "");
+       strcpy(honorific_suffixes, "");
+
+       strcpy(buf, usbuf->fullname);
+
+       /* Honorific suffixes */
+       if (num_tokens(buf, ',') > 1) {
+               extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',');
+               remove_token(buf, (num_tokens(buf, ',') - 1), ',');
+       }
+
+       /* Find a last name */
+       extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ');
+       remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
+
+       /* Find honorific prefixes */
+       if (num_tokens(buf, ' ') > 2) {
+               extract_token(honorific_prefixes, buf, 0, ' ');
+               remove_token(buf, 0, ' ');
+       }
+
+       /* Find a middle name */
+       if (num_tokens(buf, ' ') > 1) {
+               extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ');
+               remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
+       }
+
+       /* Anything left is probably the first name */
+       strcpy(firstname, buf);
+       striplt(firstname);
+
+       /* Compose the structured name */
+       sprintf(vname, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
+               honorific_prefixes, honorific_suffixes);
+
+       lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
+
+       /* Create and save the vCard */
+        v = vcard_new();
+       if (v == NULL) return;
+       sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
+       for (i=0; i<strlen(buf); ++i) {
+               if (buf[i] == ' ') buf[i] = '_';
+       }
+       vcard_add_prop(v, "fn", usbuf->fullname);
+       vcard_add_prop(v, "n", vname);
+       vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
+       vcard_add_prop(v, "email;internet", buf);
+       vcard_write_user(usbuf, v);
+       vcard_free(v);
+}
+
+
 /*
  * When a user is being deleted, we have to remove his/her vCard.
  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
  * field, and the same Extended ID as the existing card.
  */
-void vcard_purge(char *username, long usernum) {
+void vcard_purge(struct ctdluser *usbuf) {
        struct CtdlMessage *msg;
        char buf[SIZ];
 
-       msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
+       msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
        if (msg == NULL) return;
        memset(msg, 0, sizeof(struct CtdlMessage));
 
         msg->cm_magic = CTDLMESSAGE_MAGIC;
         msg->cm_anon_type = MES_NORMAL;
         msg->cm_format_type = 0;
-        msg->cm_fields['A'] = strdoop(username);
-        msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
-        msg->cm_fields['N'] = strdoop(NODENAME);
-        msg->cm_fields['M'] = strdoop("Purge this vCard\n");
+        msg->cm_fields['A'] = strdup(usbuf->fullname);
+        msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
+        msg->cm_fields['N'] = strdup(NODENAME);
+        msg->cm_fields['M'] = strdup("Purge this vCard\n");
 
         snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
                        msg->cm_fields['A'], NODENAME);
-        msg->cm_fields['E'] = strdoop(buf);
+        msg->cm_fields['E'] = strdup(buf);
 
-       msg->cm_fields['S'] = strdoop("CANCEL");
+       msg->cm_fields['S'] = strdup("CANCEL");
 
         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
         CtdlFreeMessage(msg);
@@ -717,7 +826,7 @@ void vcard_delete_remove(char *room, long msgnum) {
                return;
        }
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;
 
        ptr = msg->cm_fields['M'];
@@ -778,7 +887,7 @@ void vcard_create_room(void)
 
        /* Set expiration policy to manual; otherwise objects will be lost! */
        if (lgetroom(&qr, USERCONTACTSROOM)) {
-               lprintf(3, "Couldn't get the user CONTACTS room!\n");
+               lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
                return;
        }
        qr.QRep.expire_mode = EXPIRE_MANUAL;
@@ -834,6 +943,7 @@ char *serv_vcard_init(void)
        CtdlRegisterProtoHook(cmd_igab, "IGAB",
                                        "Initialize Global Address Book");
        CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
+       CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
        CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
        CtdlRegisterNetprocHook(vcard_extract_from_network);