Back to index

libcitadel  8.12
vcard.c
Go to the documentation of this file.
00001 /*
00002  * vCard implementation for Citadel
00003  *
00004  * Copyright (C) 1999-2008 by the citadel.org development team.
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  */
00020 
00021 
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <stdio.h>
00025 #include <fcntl.h>
00026 #include <signal.h>
00027 
00028 #if TIME_WITH_SYS_TIME
00029 # include <sys/time.h>
00030 # include <time.h>
00031 #else
00032 # if HAVE_SYS_TIME_H
00033 #  include <sys/time.h>
00034 # else
00035 #  include <time.h>
00036 # endif
00037 #endif
00038 
00039 #include <ctype.h>
00040 #include <string.h>
00041 #include <errno.h>
00042 #include <limits.h>
00043 #include <string.h>
00044 #include <libcitadel.h>
00045 
00046 
00047 /* 
00048  * Constructor (empty vCard)
00049  * Returns an empty vcard
00050  */
00051 struct vCard *vcard_new() {
00052        struct vCard *v;
00053 
00054        v = (struct vCard *) malloc(sizeof(struct vCard));
00055        if (v == NULL) return v;
00056 
00057        v->magic = CTDL_VCARD_MAGIC;
00058        v->numprops = 0;
00059        v->prop = NULL;
00060 
00061        return v;
00062 }
00063 
00064 /*
00065  * Remove the "charset=" attribute from a vCard property name
00066  *
00067  */
00068 void remove_charset_attribute(char *strbuf)
00069 {
00070        int i, t;
00071        char compare[256];
00072 
00073        t = num_tokens(strbuf, ';');
00074        for (i=0; i<t; ++i) {
00075               extract_token(compare, strbuf, i, ';', sizeof compare);
00076               striplt(compare);
00077               if (!strncasecmp(compare, "charset=", 8)) {
00078                      remove_token(strbuf, i, ';');
00079               }
00080        }
00081        if (!IsEmptyStr(strbuf)) {
00082               if (strbuf[strlen(strbuf)-1] == ';') {
00083                      strbuf[strlen(strbuf)-1] = 0;
00084               }
00085        }
00086 }
00087 
00088 
00089 /*
00090  * Add a property to a vCard
00091  *
00092  * v          vCard structure to which we are adding
00093  * propname   name of new property
00094  * propvalue  value of new property
00095  */
00096 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
00097        ++v->numprops;
00098        v->prop = realloc(v->prop,
00099               (v->numprops * sizeof(struct vCardProp)) );
00100        v->prop[v->numprops-1].name = strdup(propname);
00101        v->prop[v->numprops-1].value = strdup(propvalue);
00102 }
00103 
00104 /*
00105  * Constructor - returns a new struct vcard given a serialized vcard
00106  */
00107 struct vCard *VCardLoad(StrBuf *vbtext) {
00108        return vcard_load((char*)ChrPtr(vbtext));
00109 }
00110 
00111 /*
00112  * Constructor - returns a new struct vcard given a serialized vcard
00113  */
00114 struct vCard *vcard_load(char *vtext) {
00115        struct vCard *v;
00116        int valid = 0;
00117        char *mycopy, *ptr;
00118        char *namebuf, *valuebuf;
00119        int i;
00120        int colonpos, nlpos;
00121 
00122        if (vtext == NULL) return vcard_new();
00123        mycopy = strdup(vtext);
00124        if (mycopy == NULL) return NULL;
00125 
00126        /*
00127         * First, fix this big pile o' vCard to make it more parseable.
00128         * To make it easier to parse, we convert CRLF to LF, and unfold any
00129         * multi-line fields into single lines.
00130         */
00131        for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
00132               if (!strncmp(&mycopy[i], "\r\n", 2)) {
00133                      strcpy(&mycopy[i], &mycopy[i+1]);
00134               }
00135               if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
00136                      strcpy(&mycopy[i], &mycopy[i+1]);
00137               }
00138        }
00139 
00140        v = vcard_new();
00141        if (v == NULL) return v;
00142 
00143        ptr = mycopy;
00144        while (!IsEmptyStr(ptr)) {
00145               colonpos = pattern2(ptr, ":");
00146               nlpos = pattern2(ptr, "\n");
00147 
00148               if ((nlpos > colonpos) && (colonpos > 0)) {
00149                      namebuf = malloc(colonpos + 1);
00150                      valuebuf = malloc(nlpos - colonpos + 1);
00151                      memcpy(namebuf, ptr, colonpos);
00152                      namebuf[colonpos] = '\0';
00153                      memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
00154                      valuebuf[nlpos-colonpos-1] = '\0';
00155 
00156                      if (!strcasecmp(namebuf, "end")) {
00157                             valid = 0;
00158                      }
00159                      if (   (!strcasecmp(namebuf, "begin"))
00160                             && (!strcasecmp(valuebuf, "vcard"))
00161                      ) {
00162                             valid = 1;
00163                      }
00164 
00165                      if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
00166                             remove_charset_attribute(namebuf);
00167                             ++v->numprops;
00168                             v->prop = realloc(v->prop,
00169                                    (v->numprops * sizeof(struct vCardProp))
00170                             );
00171                             v->prop[v->numprops-1].name = namebuf;
00172                             v->prop[v->numprops-1].value = valuebuf;
00173                      } 
00174                      else {
00175                             free(namebuf);
00176                             free(valuebuf);
00177                      }
00178 
00179               }
00180 
00181               while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
00182                      ++ptr;
00183               }
00184               if (*ptr == '\n') ++ptr;
00185        }
00186 
00187        free(mycopy);
00188        return v;
00189 }
00190 
00191 
00192 /*
00193  * Fetch the value of a particular key.
00194  * If is_partial is set to 1, a partial match is ok (for example,
00195  * a key of "tel;home" will satisfy a search for "tel").
00196  * Set "instance" to a value higher than 0 to return subsequent instances
00197  * of the same key.
00198  *
00199  * Set "get_propname" to nonzero to fetch the property name instead of value.
00200  * v          vCard to get keyvalue from
00201  * propname   key to retrieve
00202  * is_partial
00203  * instance   if nonzero return a later token of the value
00204  * get_propname      if nonzero get the real property name???
00205  *
00206  * returns the requested value / token / propertyname
00207  */
00208 char *vcard_get_prop(struct vCard *v, char *propname,
00209                      int is_partial, int instance, int get_propname) {
00210        int i;
00211        int found_instance = 0;
00212 
00213        if (v->numprops) for (i=0; i<(v->numprops); ++i) {
00214               if ( (!strcasecmp(v->prop[i].name, propname))
00215                  || (propname[0] == 0)
00216                  || (  (!strncasecmp(v->prop[i].name,
00217                                    propname, strlen(propname)))
00218                       && (v->prop[i].name[strlen(propname)] == ';')
00219                       && (is_partial) ) ) {
00220                      if (instance == found_instance++) {
00221                             if (get_propname) {
00222                                    return(v->prop[i].name);
00223                             }
00224                             else {
00225                                    return(v->prop[i].value);
00226                             }
00227                      }
00228               }
00229        }
00230 
00231        return NULL;
00232 }
00233 
00234 
00235 
00236 
00237 /*
00238  * Destructor
00239  */
00240 void vcard_free(struct vCard *v) {
00241        int i;
00242        
00243        if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
00244        
00245        if (v->numprops) for (i=0; i<(v->numprops); ++i) {
00246               free(v->prop[i].name);
00247               free(v->prop[i].value);
00248        }
00249 
00250        if (v->prop != NULL) free(v->prop);
00251        
00252        memset(v, 0, sizeof(struct vCard));
00253        free(v);
00254 }
00255 
00256 
00257 
00258 
00259 /*
00260  * Set a name/value pair in the card
00261  * v          vCard to manipulate
00262  * name              key to set
00263  * value      the value to assign to key
00264  * append     if nonzero, append rather than replace if this key already exists.
00265  */
00266 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
00267        int i;
00268 
00269        if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
00270 
00271        /* If this key is already present, replace it */
00272        if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
00273               if (!strcasecmp(v->prop[i].name, name)) {
00274                      free(v->prop[i].name);
00275                      free(v->prop[i].value);
00276                      v->prop[i].name = strdup(name);
00277                      v->prop[i].value = strdup(value);
00278                      return;
00279               }
00280        }
00281 
00282        /* Otherwise, append it */
00283        ++v->numprops;
00284        v->prop = realloc(v->prop,
00285               (v->numprops * sizeof(struct vCardProp)) );
00286        v->prop[v->numprops-1].name = strdup(name);
00287        v->prop[v->numprops-1].value = strdup(value);
00288 }
00289 
00290 
00291 
00292 
00293 /*
00294  * Serialize a 'struct vcard' into an actual vcard.
00295  */
00296 char *vcard_serialize(struct vCard *v)
00297 {
00298        char *ser;
00299        int i, j;
00300        size_t len;
00301        int is_utf8 = 0;
00302 
00303        if (v == NULL) return NULL;               /* self check */
00304        if (v->magic != CTDL_VCARD_MAGIC) return NULL;   /* self check */
00305 
00306        /* Set the vCard version number to 2.1 at this time. */
00307        vcard_set_prop(v, "VERSION", "2.1", 0);
00308 
00309        /* Figure out how big a buffer we need to allocate */
00310        len = 64;     /* for begin, end, and a little padding for safety */
00311        if (v->numprops) for (i=0; i<(v->numprops); ++i) {
00312               len = len +
00313                      strlen(v->prop[i].name) +
00314                      strlen(v->prop[i].value) + 16;
00315        }
00316 
00317        ser = malloc(len);
00318        if (ser == NULL) return NULL;
00319 
00320        safestrncpy(ser, "begin:vcard\r\n", len);
00321        if (v->numprops) for (i=0; i<(v->numprops); ++i) {
00322               if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
00323                      is_utf8 = 0;
00324                      for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
00325                             if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
00326                                    is_utf8 = 1;
00327                             }
00328                      }
00329                      strcat(ser, v->prop[i].name);
00330                      if (is_utf8) {
00331                             strcat(ser, ";charset=UTF-8");
00332                      }
00333                      strcat(ser, ":");
00334                      strcat(ser, v->prop[i].value);
00335                      strcat(ser, "\r\n");
00336               }
00337        }
00338        strcat(ser, "end:vcard\r\n");
00339 
00340        return ser;
00341 }
00342 
00343 
00344 
00345 /*
00346  * Convert FN (Friendly Name) into N (Name)
00347  *
00348  * vname      Supplied friendly-name
00349  * n          Target buffer to store Name
00350  * vname_size Size of buffer
00351  */
00352 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
00353        char lastname[256];
00354        char firstname[256];
00355        char middlename[256];
00356        char honorific_prefixes[256];
00357        char honorific_suffixes[256];
00358        char buf[256];
00359 
00360        safestrncpy(buf, n, sizeof buf);
00361 
00362        /* Try to intelligently convert the screen name to a
00363         * fully expanded vCard name based on the number of
00364         * words in the name
00365         */
00366        safestrncpy(lastname, "", sizeof lastname);
00367        safestrncpy(firstname, "", sizeof firstname);
00368        safestrncpy(middlename, "", sizeof middlename);
00369        safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
00370        safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
00371 
00372        /* Honorific suffixes */
00373        if (num_tokens(buf, ',') > 1) {
00374               extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
00375                      sizeof honorific_suffixes);
00376               remove_token(buf, (num_tokens(buf, ',') - 1), ',');
00377        }
00378 
00379        /* Find a last name */
00380        extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
00381        remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
00382 
00383        /* Find honorific prefixes */
00384        if (num_tokens(buf, ' ') > 2) {
00385               extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
00386               remove_token(buf, 0, ' ');
00387        }
00388 
00389        /* Find a middle name */
00390        if (num_tokens(buf, ' ') > 1) {
00391               extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
00392               remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
00393        }
00394 
00395        /* Anything left is probably the first name */
00396        safestrncpy(firstname, buf, sizeof firstname);
00397        striplt(firstname);
00398 
00399        /* Compose the structured name */
00400        snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
00401               honorific_prefixes, honorific_suffixes);
00402 }
00403 
00404 
00405 
00406