a34336c417d3b29fca5206f0043912174766def3
[citadel.git] / webcit / vcard_edit.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15 #include "calendar.h"
16
17 CtxType CTX_VCARD = CTX_NONE;
18 CtxType CTX_VCARD_LIST = CTX_NONE;
19 CtxType CTX_VCARD_TYPE = CTX_NONE;
20 long VCEnumCounter = 0;
21
22 typedef enum _VCStrEnum {
23         FlatString,
24         StringCluster,
25         PhoneNumber,
26         EmailAddr,
27         Address,
28         Street,
29         Number,
30         AliasFor,
31         Base64BinaryAttachment,
32         UnKnown,
33         TerminateList
34 }VCStrEnum;
35 typedef struct vcField vcField;
36 struct vcField {
37         ConstStr STR;
38         VCStrEnum Type;
39         vcField *Sub;
40         long cval;
41         long parentCVal;
42         ConstStr Name;
43 };
44
45 vcField VCStr_Ns [] = {
46         {{HKEY("last")},   FlatString,    NULL, 0, 0, {HKEY("Last Name")}},
47         {{HKEY("first")},  FlatString,    NULL, 0, 0, {HKEY("First Name")}},
48         {{HKEY("middle")}, FlatString,    NULL, 0, 0, {HKEY("Middle Name")}},
49         {{HKEY("prefix")}, FlatString,    NULL, 0, 0, {HKEY("Prefix")}},
50         {{HKEY("suffix")}, FlatString,    NULL, 0, 0, {HKEY("Suffix")}},
51         {{HKEY("")},       TerminateList, NULL, 0, 0, {HKEY("")}}
52 };
53
54 vcField VCStr_Addrs [] = {
55         {{HKEY("POBox")},    Address,       NULL, 0, 0, {HKEY("PO box")}},
56         {{HKEY("extadr")},   Address,       NULL, 0, 0, {HKEY("Address")}},
57         {{HKEY("street")},   Address,       NULL, 0, 0, {HKEY("")}},
58         {{HKEY("city")},     Address,       NULL, 0, 0, {HKEY("City")}},
59         {{HKEY("state")},    Address,       NULL, 0, 0, {HKEY("State")}},
60         {{HKEY("zip")},      Address,       NULL, 0, 0, {HKEY("ZIP code")}},
61         {{HKEY("country")},  Address,       NULL, 0, 0, {HKEY("Country")}},
62         {{HKEY("")},         TerminateList, NULL, 0, 0, {HKEY("")}}
63 };
64
65 vcField VCStrE [] = {
66         {{HKEY("version")},         Number,                 NULL,        0, 0, {HKEY("")}},
67         {{HKEY("rev")},             Number,                 NULL,        0, 0, {HKEY("")}},
68         {{HKEY("label")},           FlatString,             NULL,        0, 0, {HKEY("")}},
69         {{HKEY("uid")},             FlatString,             NULL,        0, 0, {HKEY("")}},
70         {{HKEY("n")},               StringCluster,          VCStr_Ns,    0, 0, {HKEY("")}}, /* N is name, but only if there's no FN already there */
71         {{HKEY("fn")},              FlatString,             NULL,        0, 0, {HKEY("")}}, /* FN (full name) is a true 'display name' field */
72         {{HKEY("title")},           FlatString,             NULL,        0, 0, {HKEY("Title:")}},
73         {{HKEY("org")},             FlatString,             NULL,        0, 0, {HKEY("Organization:")}},/* organization */
74         {{HKEY("email")},           EmailAddr,              NULL,        0, 0, {HKEY("E-mail:")}},
75         {{HKEY("tel")},             PhoneNumber,            NULL,        0, 0, {HKEY("Telephone:")}},
76         {{HKEY("adr")},             StringCluster,          VCStr_Addrs, 0, 0, {HKEY("Address:")}},
77         {{HKEY("photo")},           Base64BinaryAttachment, NULL,        0, 0, {HKEY("Photo:")}},
78         {{HKEY("tel;home")},        PhoneNumber,            NULL,        0, 0, {HKEY(" (home)")}},
79         {{HKEY("tel;work")},        PhoneNumber,            NULL,        0, 0, {HKEY(" (work)")}},
80         {{HKEY("tel;fax")},         PhoneNumber,            NULL,        0, 0, {HKEY(" (fax)")}},
81         {{HKEY("tel;cell")},        PhoneNumber,            NULL,        0, 0, {HKEY(" (cell)")}},
82         {{HKEY("email;internet")},  EmailAddr,              NULL,        0, 0, {HKEY("E-mail:")}},
83         {{HKEY("UNKNOWN")},         UnKnown,                NULL,        0, 0, {HKEY("")}},
84         {{HKEY("")},                TerminateList,          NULL,        0, 0, {HKEY("")}}
85 };
86
87 ConstStr VCStr [] = {
88         {HKEY("")},
89         {HKEY("n")}, /* N is name, but only if there's no FN already there */
90         {HKEY("fn")}, /* FN (full name) is a true 'display name' field */
91         {HKEY("title")},   /* title */
92         {HKEY("org")},    /* organization */
93         {HKEY("email")},
94         {HKEY("tel")},
95         {HKEY("work")},
96         {HKEY("home")},
97         {HKEY("cell")},
98         {HKEY("adr")},
99         {HKEY("photo")},
100         {HKEY("version")},
101         {HKEY("rev")},
102         {HKEY("label")},
103         {HKEY("uid")}
104 };
105
106 /*
107  * Address book entry (keep it short and sweet, it's just a quickie lookup
108  * which we can use to get to the real meat and bones later)
109  */
110 typedef struct _addrbookent {
111         StrBuf *name;
112         HashList *VC;
113         long ab_msgnum;         /* message number of address book entry */
114 } addrbookent;
115
116 void deleteAbEnt(void *v) {
117         addrbookent *vc = (addrbookent*)v;
118         DeleteHash(&vc->VC);
119         FreeStrBuf(&vc->name);
120 }
121
122 HashList *DefineToToken = NULL;
123 HashList *VCTokenToDefine = NULL;
124 HashList *vcNames = NULL; /* todo: fill with the name strings */
125 vcField* vcfUnknown = NULL;
126
127 void RegisterVCardToken(vcField* vf, StrBuf *name, int inTokenCount)
128 {
129         if (vf->Type == UnKnown) {
130                 vcfUnknown = vf;
131         }
132         RegisterTokenParamDefine(SKEY(name), vf->cval);
133         Put(DefineToToken, LKEY(vf->cval), vf, reference_free_handler);
134         Put(vcNames, LKEY(vf->cval), NewStrBufPlain(CKEY(vf->Name)), HFreeStrBuf);
135
136         syslog(LOG_DEBUG, "Token: %s -> %ld, %d", 
137                ChrPtr(name),
138                vf->cval, 
139                inTokenCount);
140
141 }
142
143 void autoRegisterTokens(long *enumCounter, vcField* vf, StrBuf *BaseStr, int layer, long parentCVal)
144 {
145         int i = 0;
146         StrBuf *subStr = NewStrBuf();
147         while (vf[i].STR.len > 0) {
148                 FlushStrBuf(subStr);
149                 vf[i].cval = (*enumCounter) ++;
150                 vf[i].parentCVal = parentCVal;
151                 StrBufAppendBuf(subStr, BaseStr, 0);
152                 if (StrLength(subStr) > 0) {
153                         StrBufAppendBufPlain(subStr, HKEY("."), 0);
154                 }
155                 StrBufAppendBufPlain(subStr, CKEY(vf[i].STR), 0);
156                 if (layer == 0) {
157                         Put(VCTokenToDefine, CKEY(vf[i].STR), &vf[i], reference_free_handler);
158                 }
159                 switch (vf[i].Type) {
160                 case FlatString:
161                         break;
162                 case StringCluster:
163                 {
164                         autoRegisterTokens(enumCounter, vf[i].Sub, subStr, 1, vf[i].cval);
165                 }
166                 break;
167                 case PhoneNumber:
168                         break;
169                 case EmailAddr:
170                         break;
171                 case Street:
172                         break;
173                 case Number:
174                         break;
175                 case AliasFor:
176                         break;
177                 case Base64BinaryAttachment:
178                         break;
179                 case TerminateList:
180                         break;
181                 case Address:
182                         break;
183                 case UnKnown:
184                         break;
185                 }
186                 RegisterVCardToken(&vf[i], subStr, i);
187                 i++;
188         }
189         FreeStrBuf(&subStr);
190 }
191
192 int preeval_vcard_item(WCTemplateToken *Token)
193 {
194         WCTemplputParams TPP;
195         WCTemplputParams *TP;
196         int searchFieldNo;
197         StrBuf *Target = NULL;
198
199         memset(&TPP, 0, sizeof(WCTemplputParams));
200         TP = &TPP;
201         TP->Tokens = Token;
202         searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
203         if (searchFieldNo >= VCEnumCounter) {
204                 LogTemplateError(NULL, "VCardItem", ERR_PARM1, TP,
205                                  "Invalid define");
206                 return 0;
207         }
208         return 1;
209 }
210
211 void tmpl_vcard_item(StrBuf *Target, WCTemplputParams *TP)
212 {
213         void *vItem;
214         long searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
215         addrbookent *ab = (addrbookent*) CTX(CTX_VCARD);
216         if (GetHash(ab->VC, LKEY(searchFieldNo), &vItem) && (vItem != NULL)) {
217                 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 1);
218         }
219 }
220
221 void tmpl_vcard_context_item(StrBuf *Target, WCTemplputParams *TP)
222 {
223         void *vItem;
224         vcField *t = (vcField*) CTX(CTX_VCARD_TYPE);
225         addrbookent *ab = (addrbookent*) CTX(CTX_VCARD);
226
227         if (t == NULL) {
228                 LogTemplateError(NULL, "VCard item", ERR_NAME, TP,
229                                  "Missing context");
230                 return;
231         }
232
233         if (GetHash(ab->VC, LKEY(t->cval), &vItem) && (vItem != NULL)) {
234                 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 0);
235         }
236         else {
237                 LogTemplateError(NULL, "VCard item", ERR_NAME, TP,
238                                  "Doesn't have that key - did you miss to filter in advance?");
239         }
240 }
241 int preeval_vcard_name_str(WCTemplateToken *Token)
242 {
243         WCTemplputParams TPP;
244         WCTemplputParams *TP;
245         int searchFieldNo;
246         StrBuf *Target = NULL;
247
248         memset(&TPP, 0, sizeof(WCTemplputParams));
249         TP = &TPP;
250         TP->Tokens = Token;
251         searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
252         if (searchFieldNo >= VCEnumCounter) {
253                 LogTemplateError(NULL, "VCardName", ERR_PARM1, TP,
254                                  "Invalid define");
255                 return 0;
256         }
257         return 1;
258 }
259
260 void tmpl_vcard_name_str(StrBuf *Target, WCTemplputParams *TP)
261 {
262         void *vItem;
263         long searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
264         /* todo: get descriptive string for this vcard type */
265         if (GetHash(vcNames, LKEY(searchFieldNo), &vItem) && (vItem != NULL)) {
266                 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 1);
267         }
268         else {
269                 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
270                                  "No i18n string for this.");
271                 return;
272         }
273 }
274
275 void tmpl_vcard_context_name_str(StrBuf *Target, WCTemplputParams *TP)
276 {
277         void *vItem;
278         vcField *t = (vcField*) CTX(CTX_VCARD_TYPE);
279
280         if (t == NULL) {
281                 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
282                                  "Missing context");
283                 return;
284         }
285         
286         if (GetHash(vcNames, LKEY(t->cval), &vItem) && (vItem != NULL)) {
287                 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 1);
288         }
289         else {
290                 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
291                                  "No i18n string for this.");
292                 return;
293         }
294 }
295
296 int filter_VC_ByType(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP)
297 {
298         long searchType;
299         long type = 0;
300         void *v;
301         int rc = 0;
302         vcField *vf = (vcField*) Context;
303
304         memcpy(&type, key, sizeof(long));
305         searchType = GetTemplateTokenNumber(Target, TP, IT_ADDT_PARAM(0), 0);
306         
307         if (vf->Type == searchType) {
308                 addrbookent *ab = (addrbookent*) CTX(CTX_VCARD);
309                 if (GetHash(ab->VC, LKEY(vf->cval), &v) && v != NULL)
310                         return 1;
311         }
312         return rc;
313 }
314
315
316
317
318 HashList *getContextVcard(StrBuf *Target, WCTemplputParams *TP)
319 {
320         vcField *vf = (vcField*) CTX(CTX_VCARD_TYPE);
321         addrbookent *ab = (addrbookent*) CTX(CTX_VCARD);
322
323         if ((vf == NULL) || (ab == NULL)) {
324                 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
325                                  "Need VCard and Vcard type in context");
326                 
327                 return NULL;
328         }
329         return ab->VC;
330 }
331
332 int filter_VC_ByContextType(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP)
333 {
334         long searchType;
335         vcField *vf = (vcField*) CTX(CTX_VCARD_TYPE);
336
337         memcpy(&searchType, key, sizeof(long));
338         
339         if (vf->cval == searchType) {
340                 return 1;
341         }
342         else {
343                 return 0;
344         }
345 }
346
347
348 int conditional_VC_Havetype(StrBuf *Target, WCTemplputParams *TP)
349 {
350         addrbookent *ab = (addrbookent*) CTX(CTX_VCARD);
351         long HaveFieldType = GetTemplateTokenNumber(Target, TP, 2, 0);
352         int rc = 0;     
353         void *vVCitem;
354         const char *Key;
355         long len;
356         HashPos *it = GetNewHashPos(ab->VC, 0);
357         while (GetNextHashPos(ab->VC, it, &len, &Key, &vVCitem) && 
358                (vVCitem != NULL)) 
359         {
360                 void *vvcField;
361                 long type = 0;
362                 memcpy(&type, Key, sizeof(long));
363                 if (GetHash(DefineToToken, LKEY(type), &vvcField) &&
364                     (vvcField != NULL))
365                 {
366                         vcField *t = (vcField*) vvcField;
367                         if (t && t->Type == HaveFieldType) {
368                                 rc = 1;
369                                 break;
370                         }
371                 }
372         }
373         DeleteHashPos(&it);
374         return rc;
375 }
376
377
378
379 /*
380  * Helper function for do_addrbook_view()
381  * Converts a name into a three-letter tab label
382  */
383 void nametab(char *tabbuf, long len, char *name) {
384         stresc(tabbuf, len, name, 0, 0);
385         tabbuf[0] = toupper(tabbuf[0]);
386         tabbuf[1] = tolower(tabbuf[1]);
387         tabbuf[2] = tolower(tabbuf[2]);
388         tabbuf[3] = 0;
389 }
390
391 wc_mime_attachment *load_vcard(message_summary *Msg) 
392 {
393         HashPos  *it;
394         StrBuf *FoundCharset = NewStrBuf();
395         StrBuf *Error;
396         void *vMime;
397         const char *Key;
398         long len;
399         wc_mime_attachment *Mime;
400         wc_mime_attachment *VCMime = NULL;
401
402         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
403         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
404         Msg->MsgBody->msgnum = Msg->msgnum;
405
406         load_message(Msg, FoundCharset, &Error);
407
408         FreeStrBuf(&FoundCharset);
409         /* look up the vcard... */
410         it = GetNewHashPos(Msg->AllAttach, 0);
411         while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) && 
412                (vMime != NULL)) 
413         {
414                 Mime = (wc_mime_attachment*) vMime;
415                 if ((strcmp(ChrPtr(Mime->ContentType),
416                            "text/x-vcard") == 0) ||
417                     (strcmp(ChrPtr(Mime->ContentType),
418                             "text/vcard") == 0))
419                 {
420                         VCMime = Mime;
421                         break;
422                 }
423         }
424         DeleteHashPos(&it);
425         if (VCMime == NULL)
426                 return NULL;
427
428         if (VCMime->Data == NULL)
429                 MimeLoadData(VCMime);
430         return VCMime;
431 }
432
433
434
435 /*
436  * Turn a vCard "n" (name) field into something displayable.
437  */
438 void vcard_n_prettyize(char *name)
439 {
440         char *original_name;
441         int i, j, len;
442
443         original_name = strdup(name);
444         len = strlen(original_name);
445         for (i=0; i<5; ++i) {
446                 if (len > 0) {
447                         if (original_name[len-1] == ' ') {
448                                 original_name[--len] = 0;
449                         }
450                         if (original_name[len-1] == ';') {
451                                 original_name[--len] = 0;
452                         }
453                 }
454         }
455         strcpy(name, "");
456         j=0;
457         for (i=0; i<len; ++i) {
458                 if (original_name[i] == ';') {
459                         name[j++] = ',';
460                         name[j++] = ' ';                        
461                 }
462                 else {
463                         name[j++] = original_name[i];
464                 }
465         }
466         name[j] = '\0';
467         free(original_name);
468 }
469
470
471 void PutVcardItem(HashList *thisVC, vcField *thisField, StrBuf *ThisFieldStr, int is_qp, StrBuf *Swap)
472 {
473         /* if we have some untagged QP, detect it here. */
474         if (is_qp || (strstr(ChrPtr(ThisFieldStr), "=?")!=NULL)){
475                 StrBuf *b;
476                 StrBuf_RFC822_to_Utf8(Swap, ThisFieldStr, NULL, NULL); /* default charset, current charset */
477                 b = ThisFieldStr;
478                 ThisFieldStr = Swap; 
479                 Swap = b;
480                 FlushStrBuf(Swap);
481         }
482         Put(thisVC, LKEY(thisField->cval), ThisFieldStr, HFreeStrBuf);
483 }
484 /*
485  * html print a vcard
486  * display_vcard() calls this after parsing the textual vCard into
487  * our 'struct vCard' data object.
488  *
489  * Set 'full' to nonzero to display the full card, otherwise it will only
490  * show a summary line.
491  *
492  * This code is a bit ugly, so perhaps an explanation is due: we do this
493  * in two passes through the vCard fields.  On the first pass, we process
494  * fields we understand, and then render them in a pretty fashion at the
495  * end.  Then we make a second pass, outputting all the fields we don't
496  * understand in a simple two-column name/value format.
497  * v            the vCard to parse
498  * msgnum       Citadel message pointer
499  */
500 void parse_vcard(StrBuf *Target, struct vCard *v, HashList *VC, wc_mime_attachment *Mime)
501 {
502         StrBuf *Val = NULL;
503         StrBuf *Swap = NULL;
504         int i, j, k;
505         char buf[SIZ];
506         int is_qp = 0;
507         int is_b64 = 0;
508         int ntokens, len;
509         StrBuf *thisname = NULL;
510         char firsttoken[SIZ];
511         StrBuf *thisVCToken;
512         void *vField = NULL;
513
514         Swap = NewStrBuf ();
515         thisname = NewStrBuf();
516         thisVCToken = NewStrBufPlain(NULL, 63);
517         for (i=0; i<(v->numprops); ++i) {
518                 FlushStrBuf(thisVCToken);
519                 is_qp = 0;
520                 is_b64 = 0;
521                 syslog(LOG_DEBUG, "i: %d oneprop: %s - value: %s", i, v->prop[i].name, v->prop[i].value);
522                 StrBufPlain(thisname, v->prop[i].name, -1);
523                 StrBufLowerCase(thisname);
524                 
525                 /*len = */extract_token(firsttoken, ChrPtr(thisname), 0, ';', sizeof firsttoken);
526                 ntokens = num_tokens(ChrPtr(thisname), ';');
527                 for (j=0, k=0; j < ntokens && k < 10; ++j) {
528                         /*int evc[10];*/
529                         
530                         len = extract_token(buf, ChrPtr(thisname), j, ';', sizeof buf);
531                         if (!strcasecmp(buf, "encoding=quoted-printable")) {
532                                 is_qp = 1;
533 /*                              remove_token(thisname, j, ';');*/
534                         }
535                         else if (!strcasecmp(buf, "encoding=base64")) {
536                                 is_b64 = 1;
537 /*                              remove_token(thisname, j, ';');*/
538                         }
539                         else{
540                                 if (StrLength(thisVCToken) > 0) {
541                                         StrBufAppendBufPlain(thisVCToken, HKEY(";"), 0);
542                                 }
543                                 StrBufAppendBufPlain(thisVCToken, buf, len, 0);
544                                 /*
545                                 if (GetHash(VCToEnum, buf, len, &V))
546                                 {
547                                         evc[k] = (int) V;
548
549                                         Put(VC, IKEY(evc), Val, HFreeStrBuf);
550
551                                         syslog(LOG_DEBUG, "[%ul] -> k: %d %s - %s", evc, k, buf, VCStr[evc[k]].Key);
552                                         k++;
553                                 }
554 */
555
556                         }
557                 }
558
559                 vField = NULL;  
560                 if ((StrLength(thisVCToken) > 0) &&
561                     GetHash(VCTokenToDefine, SKEY(thisVCToken), &vField) && 
562                     (vField != NULL)) {
563                         vcField *thisField = (vcField *)vField;
564                         StrBuf *ThisFieldStr = NULL;
565                         syslog(LOG_DEBUG, "got this token: %s, found: %s", ChrPtr(thisVCToken), thisField->STR.Key);
566                         switch (thisField->Type) {
567                         case StringCluster: {
568                                 int j = 0;
569                                 const char *Pos = NULL;
570                                 StrBuf *thisArray = NewStrBufPlain(v->prop[i].value, -1);
571                                 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(thisArray));
572                                 while (thisField->Sub[j].STR.len > 0) {
573                                         StrBufExtract_NextToken(Buf, thisArray, &Pos, ';');
574                                         ThisFieldStr = NewStrBufDup(Buf);
575                                         
576                                         PutVcardItem(VC, &thisField->Sub[j], ThisFieldStr, is_qp, Swap);
577                                         j++;
578                                 }
579                                 FreeStrBuf(&thisArray);
580                                 FreeStrBuf(&Buf);
581                         }
582                                 break;
583                         case Address:
584                         case FlatString:
585                         case PhoneNumber:
586                         case EmailAddr:
587                         case Street:
588                         case Number:
589                         case AliasFor:
590                                 /* copy over the payload into a StrBuf */
591                                 ThisFieldStr = NewStrBufPlain(v->prop[i].value, -1);
592                                 PutVcardItem(VC, thisField, ThisFieldStr, is_qp, Swap);
593
594                                 break;
595                         case Base64BinaryAttachment:
596                         case TerminateList:
597                         case UnKnown:
598                                 break;
599                         }
600
601                 }
602                 else if (StrLength(thisVCToken) > 0) {
603                         /* Add it to the UNKNOWN field... */
604                         void *pv = NULL;
605                         StrBuf *oldVal;
606                         GetHash(VC, IKEY(vcfUnknown->cval), &pv);
607                         oldVal = (StrBuf*) pv;
608                         if (oldVal == NULL) {
609                                 oldVal = NewStrBuf();
610                                 Put(VC, IKEY(vcfUnknown->cval), oldVal, HFreeStrBuf);
611                         }
612                         else {
613                                 StrBufAppendBufPlain(oldVal, HKEY("\n"), 0);
614                         }
615
616                         StrBufAppendBuf(oldVal, thisVCToken, 0);
617                         StrBufAppendBufPlain(oldVal, HKEY(":"), 0);
618                         StrBufAppendBufPlain(oldVal, v->prop[i].value, -1, 0);
619                         continue;
620                 }
621
622                 /* copy over the payload into a StrBuf */
623                 Val = NewStrBufPlain(v->prop[i].value, -1);
624                         
625                 /* if we have some untagged QP, detect it here. */
626                 if (is_qp || (strstr(v->prop[i].value, "=?")!=NULL)){
627                         StrBuf *b;
628                         StrBuf_RFC822_to_Utf8(Swap, Val, NULL, NULL); /* default charset, current charset */
629                         b = Val;
630                         Val = Swap; 
631                         Swap = b;
632                         FlushStrBuf(Swap);
633                 }
634                 else if (is_b64) {
635                         StrBufDecodeBase64(Val);
636                 }
637 #if 0
638                 syslog(LOG_DEBUG, "-> firsttoken: %s thisname: %s Value: [%s][%s]",
639                         firsttoken,
640                        ChrPtr(thisname),
641                         ChrPtr(Val),
642                         v->prop[i].value);
643 #endif  
644                 FreeStrBuf(&Val);
645         }
646         FreeStrBuf(&thisname);
647         FreeStrBuf(&Swap);
648         FreeStrBuf(&thisVCToken);
649 }
650
651 void tmplput_VCARD_ITEM(StrBuf *Target, WCTemplputParams *TP)
652 {
653         addrbookent *ab = CTX(CTX_VCARD);
654         int evc;
655         void *vStr;
656
657         evc = GetTemplateTokenNumber(Target, TP, 0, -1);
658         if (evc != -1)
659         {
660                 if (GetHash(ab->VC, IKEY(evc), &vStr))
661                 {
662                         StrBufAppendTemplate(Target, TP,
663                                              (StrBuf*) vStr,
664                                              1);
665                 }
666         }
667         
668 }
669
670 void display_one_vcard (StrBuf *Target, addrbookent *ab, const char *tp_name, size_t tp_name_len)
671 {
672         WCTemplputParams *TP = NULL;
673         WCTemplputParams SubTP;
674
675         memset(&SubTP, 0, sizeof(WCTemplputParams));    
676         StackContext(TP, &SubTP, ab, CTX_VCARD, 0, NULL);
677
678         DoTemplate(tp_name, tp_name_len, Target, &SubTP);
679         UnStackContext(&SubTP);
680 }
681
682 /*
683  * Extract an embedded photo from a vCard for display on the client
684  */
685 void display_vcard_photo_img(void)
686 {
687         long msgnum = 0L;
688         StrBuf *vcard;
689         struct vCard *v;
690         char *photosrc;
691         const char *contentType;
692         wcsession *WCC = WC;
693
694         msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
695         
696         vcard = load_mimepart(msgnum,"1");
697         v = VCardLoad(vcard);
698         
699         photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
700         FlushStrBuf(WCC->WBuf);
701         StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
702         if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
703                 FlushStrBuf(WCC->WBuf);
704                 
705                 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
706                 output_headers(0, 0, 0, 0, 0, 0);
707                 hprintf("Content-Type: text/plain\r\n");
708                 begin_burst();
709                 wc_printf(_("Could Not decode vcard photo\n"));
710                 end_burst();
711                 return;
712         }
713         contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
714         http_transmit_thing(contentType, 0);
715         free(v);
716         free(photosrc);
717 }
718
719 /*
720  * Edit the vCard component of a MIME message.  
721  * Supply the message number
722  * and MIME part number to fetch.  Or, specify -1 for the message number
723  * to start with a blank card.
724  */
725 void do_edit_vcard(long msgnum, char *partnum, 
726                    message_summary *VCMsg,
727                    wc_mime_attachment *VCAtt,
728                    const char *return_to, 
729                    const char *force_room) {
730         HashList *VC;   WCTemplputParams SubTP;
731         wcsession *WCC = WC;
732         message_summary *Msg = NULL;
733         wc_mime_attachment *VCMime = NULL;
734         struct vCard *v;
735         char whatuser[256];
736         addrbookent ab;
737
738         memset(&ab, 0, sizeof(addrbookent));
739         ab.VC = NewHash(0, lFlathash);
740         /* Display the form */
741         output_headers(1, 1, 1, 0, 0, 0);
742
743         safestrncpy(whatuser, "", sizeof whatuser);
744
745         if ((msgnum >= 0) || 
746             ((VCMsg != NULL) && (VCAtt != NULL)))
747         {
748                 if ((VCMsg == NULL) && (VCAtt == NULL)) {
749
750                         Msg = (message_summary *) malloc(sizeof(message_summary));
751                         memset(Msg, 0, sizeof(message_summary));
752                         Msg->msgnum = msgnum;
753                         VCMime = load_vcard(Msg);
754                         if (VCMime == NULL) {
755                                 convenience_page("770000", _("Error"), "");/*TODO: important message*/
756                                 DestroyMessageSummary(Msg);
757                                 return;
758                                 DeleteHash(&ab.VC);
759                         }
760                 
761                         v = VCardLoad(VCMime->Data);
762                 }
763                 else {
764                         v = VCardLoad(VCAtt->Data);
765                 }
766
767                 parse_vcard(WCC->WBuf, v, VC, NULL);
768         
769         
770                 vcard_free(v);
771         }
772
773         memset(&SubTP, 0, sizeof(WCTemplputParams));    
774         {
775                 WCTemplputParams *TP = NULL;
776                 WCTemplputParams SubTP;
777
778                 StackContext(TP, &SubTP, &ab, CTX_VCARD, 0, NULL);
779
780                 DoTemplate(HKEY("vcard_edit"), WCC->WBuf, &SubTP);
781                 UnStackContext(&SubTP);
782         }
783         DeleteHash(&ab.VC);
784
785
786         wDumpContent(1);
787         if (Msg != NULL) {
788                 DestroyMessageSummary(Msg);
789         }
790 }
791
792
793 /*
794  *  commit the edits to the citadel server
795  */
796 void edit_vcard(void) {
797         long msgnum;
798         char *partnum;
799
800         msgnum = lbstr("msgnum");
801         partnum = bstr("partnum");
802         do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
803 }
804
805
806
807 /*
808  *  parse edited vcard from the browser
809  */
810 void submit_vcard(void) {
811         struct vCard *v;
812         char *serialized_vcard;
813         StrBuf *Buf;
814         const StrBuf *ForceRoom;
815         HashList* postVcard;
816         HashPos *it, *itSub;
817         const char *Key;
818         long len;
819         void *pv;
820         StrBuf *SubStr;
821         const StrBuf *s;
822         const char *Pos = NULL;
823
824         if (!havebstr("ok_button")) { 
825                 readloop(readnew, eUseDefault);
826                 return;
827         }
828
829         if (havebstr("force_room")) {
830                 ForceRoom = sbstr("force_room");
831                 if (gotoroom(ForceRoom) != 200) {
832                         AppendImportantMessage(_("Unable to enter the room to save your message"), -1);
833                         AppendImportantMessage(HKEY(": "));
834                         AppendImportantMessage(SKEY(ForceRoom));
835                         AppendImportantMessage(HKEY("; "));
836                         AppendImportantMessage(_("Aborting."), -1);
837
838                         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
839                                 select_user_to_edit(NULL);
840                         }
841                         else if (!strcmp(bstr("return_to"), "do_welcome")) {
842                                 do_welcome();
843                         }
844                         else if (!IsEmptyStr(bstr("return_to"))) {
845                                 http_redirect(bstr("return_to"));
846                         }
847                         else {
848                                 readloop(readnew, eUseDefault);
849                         }
850                         return;
851                 }
852         }
853
854         postVcard = getSubStruct(HKEY("VC"));
855         if (postVcard == NULL) {
856                 AppendImportantMessage(_("An error has occurred."), -1);
857                 edit_vcard();
858                 return;/*/// more details*/
859         }
860         
861         Buf = NewStrBuf();
862         serv_write(HKEY("ENT0 1|||4\n"));
863         if (!StrBuf_ServGetln(Buf) && (GetServerStatus(Buf, NULL) != 4))
864         {
865                 edit_vcard();
866                 return;
867         }
868         
869         /* Make a vCard structure out of the data supplied in the form */
870         StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
871                      bstr("extrafields")
872         );
873         v = VCardLoad(Buf);     /* Start with the extra fields */
874         if (v == NULL) {
875                 AppendImportantMessage(_("An error has occurred."), -1);
876                 edit_vcard();
877                 FreeStrBuf(&Buf);
878                 return;
879         }
880
881         SubStr = NewStrBuf();
882         it = GetNewHashPos(DefineToToken, 0);
883         while (GetNextHashPos(DefineToToken, it, &len, &Key, &pv) && 
884                (pv != NULL)) 
885         {
886                 char buf[32];
887                 long blen;
888                 vcField *t = (vcField*) pv;
889
890                 if (t->Sub != NULL){
891                         vcField *Sub;
892                         FlushStrBuf(SubStr);
893                         itSub = GetNewHashPos(DefineToToken, 0);
894                         while (GetNextHashPos(DefineToToken, itSub, &len, &Key, &pv) && 
895                                (pv != NULL)) 
896                         {
897                                 Sub = (vcField*) pv;
898                                 if (Sub->parentCVal == t->cval) {
899                                         if (StrLength(SubStr) > 0)
900                                                 StrBufAppendBufPlain(SubStr, HKEY(";"), 0);
901
902
903
904                                         blen = snprintf(buf, sizeof(buf), "%ld", Sub->cval);
905                                         s = SSubBstr(postVcard, buf, blen);
906                         
907                                         if ((s != NULL) && (StrLength(s) > 0)) {
908                                                 /// todo: utf8 qp
909                                                 StrBufAppendBuf(SubStr, s, 0);
910                                         }
911                                 }
912                         }
913                         if (StrLength(SubStr) > 0) {
914                                 vcard_add_prop(v, t->STR.Key, ChrPtr(SubStr));
915                         }
916                         DeleteHashPos(&itSub);
917                 }
918                 else if (t->parentCVal == 0) {
919                         blen = snprintf(buf, sizeof(buf), "%ld", t->cval);
920                         s = SSubBstr(postVcard, buf, blen);
921                         
922                         if ((s != NULL) && (StrLength(s) > 0)) {
923                                 vcard_add_prop(v, t->STR.Key, ChrPtr(s));
924                         }
925                 }
926         }
927         DeleteHashPos(&it);
928
929         s = sbstr("other_inetemail");
930         if (StrLength(s) > 0) {
931                 FlushStrBuf(SubStr);
932                 while (StrBufSipLine(SubStr, s, &Pos), ((Pos!=StrBufNOTNULL) && (Pos!=NULL)) ) {
933                         if (StrLength(SubStr) > 0) {
934                                 vcard_add_prop(v, "email;internet", ChrPtr(SubStr));
935                         }
936                 }
937         }
938
939         FreeStrBuf(&SubStr);
940
941
942         serialized_vcard = vcard_serialize(v);
943         vcard_free(v);
944         if (serialized_vcard == NULL) {
945                 AppendImportantMessage(_("An error has occurred."), -1);
946                 edit_vcard();
947                 FreeStrBuf(&Buf);
948                 return;
949         }
950
951         printf("%s", serialized_vcard);
952         serv_write(HKEY("Content-type: text/x-vcard; charset=UTF-8\n"));
953         serv_write(HKEY("\n"));
954         serv_printf("%s\r\n", serialized_vcard);
955         serv_write(HKEY("000\n"));
956         free(serialized_vcard);
957
958         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
959                 select_user_to_edit(NULL);
960         }
961         else if (!strcmp(bstr("return_to"), "do_welcome")) {
962                 do_welcome();
963         }
964         else if (!IsEmptyStr(bstr("return_to"))) {
965                 http_redirect(bstr("return_to"));
966         }
967         else {
968                 readloop(readnew, eUseDefault);
969         }
970         FreeStrBuf(&Buf);
971 }
972
973
974
975 typedef struct _vcardview_struct {
976         long is_singlecard;
977         HashList *addrbook;
978
979 } vcardview_struct;
980
981 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat, 
982                                  void **ViewSpecific, 
983                                  long oper, 
984                                  char *cmd, 
985                                  long len,
986                                  char *filter,
987                                  long flen)
988 {
989         vcardview_struct *VS;
990
991         VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
992         memset(VS, 0, sizeof(vcardview_struct));
993         *ViewSpecific = (void*)VS;
994
995         VS->is_singlecard = ibstr("is_singlecard");
996         if (VS->is_singlecard != 1) {
997                 VS->addrbook = NewHash(0, NULL);
998                 if (oper == do_search) {
999                         snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
1000                 }
1001                 else {
1002                         strcpy(cmd, "MSGS ALL");
1003                 }
1004                 Stat->maxmsgs = 9999999;
1005         }
1006         return 200;
1007 }
1008
1009 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat, 
1010                             void **ViewSpecific, 
1011                             message_summary* Msg, 
1012                             int is_new, 
1013                             int i)
1014 {
1015         vcardview_struct *VS;
1016         wc_mime_attachment *VCMime = NULL;
1017         struct vCard *v;
1018         addrbookent* abEntry;
1019
1020         VS = (vcardview_struct*) *ViewSpecific;
1021
1022         VCMime = load_vcard(Msg);
1023         if (VCMime == NULL)
1024                 return 0;
1025
1026         v = VCardLoad(VCMime->Data);
1027
1028         if (v == NULL) return 0;
1029
1030         abEntry = (addrbookent*) malloc(sizeof(addrbookent));
1031         memset(abEntry, 0, sizeof(addrbookent));
1032         abEntry->name = NewStrBuf();
1033         abEntry->VC = NewHash(0, lFlathash);
1034         abEntry->ab_msgnum = Msg->msgnum;
1035         parse_vcard(WC->WBuf, v, abEntry->VC, VCMime);
1036
1037         display_one_vcard(abEntry->name, abEntry, HKEY("vcard_list_name"));
1038
1039         if (StrLength(abEntry->name) == 0) {
1040                 StrBufPlain(abEntry->name, _("(no name)"), -1);
1041         }
1042
1043         vcard_free(v);
1044         
1045         Put(VS->addrbook, SKEY(abEntry->name), abEntry, deleteAbEnt);
1046         return 0;
1047 }
1048
1049
1050 /*
1051  * Render the address book using info we gathered during the scan
1052  *
1053  * addrbook     the addressbook to render
1054  * num_ab       the number of the addressbook
1055  */
1056 void do_addrbook_view(vcardview_struct* VS) {
1057         long i = 0;
1058         int displayed = 0;
1059         int bg = 0;
1060         static int NAMESPERPAGE = 60;
1061         int num_pages = 0;
1062         int tabfirst = 0;
1063         int tablast = 0;
1064         int page = 0;
1065         char **tablabels;
1066         int num_ab = GetCount(VS->addrbook);
1067         HashList *headlines;
1068         HashPos *it;
1069         wcsession *WCC = WC;
1070
1071         WCTemplputParams *TP = NULL;
1072         WCTemplputParams SubTP;
1073
1074         memset(&SubTP, 0, sizeof(WCTemplputParams));    
1075         
1076         if (num_ab == 0) {
1077                 do_template("vcard_list_empty");
1078                 return;
1079         }
1080
1081         if (num_ab > 1) {
1082                 ///SortByHashKey(VS->addrbook, 0);
1083         }
1084
1085         num_pages = (GetCount(VS->addrbook) / NAMESPERPAGE) + 1;
1086
1087         tablabels = malloc(num_pages * sizeof (char *));
1088         if (tablabels == NULL) {
1089                 return;
1090         }
1091
1092         headlines = NewHash(0, lFlathash);
1093         for (i=0; i<num_pages; ++i) {
1094                 void *v1 = NULL;
1095                 void *v2 = NULL;
1096                 long hklen1, hklen2;
1097                 const char *c1, *c2;
1098                 StrBuf *headline;
1099                 addrbookent *a1, *a2;
1100
1101                 tabfirst = i * NAMESPERPAGE;
1102                 tablast = tabfirst + NAMESPERPAGE - 1;
1103                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1104
1105                 headline = NewStrBufPlain(NULL, StrLength(v1) + StrLength(v2) + 10);
1106                 if (GetHashAt(VS->addrbook, tabfirst, &hklen1, &c1, &v1)) {
1107                         a1 = (addrbookent*) v1;
1108                         StrBufAppendBuf(headline, a1->name, 0);
1109
1110                         if (GetHashAt(VS->addrbook, tablast, &hklen2, &c2, &v2)) {
1111
1112                                 a2 = (addrbookent*) v2;
1113                                 StrBufAppendBufPlain(headline, HKEY(" - "), 0);
1114                                 StrBufAppendBuf(headline, a2->name, 0);
1115                         }
1116                 }
1117                 Put(headlines, LKEY(i), headline, HFreeStrBuf);
1118         }
1119
1120         tabbed_dialog(num_pages, tablabels);
1121         page = (-1);
1122
1123         it = GetNewHashPos(VS->addrbook, 0);
1124         for (i=0; i<num_ab; ++i) {
1125                 void *v;
1126                 long hklen;
1127                 const char *key;
1128                 addrbookent *abEnt;
1129                 GetNextHashPos(VS->addrbook, it, &hklen, &key, &v);
1130                 if (v == NULL)
1131                         continue;
1132                 abEnt = (addrbookent *) v;
1133                 if ((i / NAMESPERPAGE) != page) {       /* New tab */
1134                         page = (i / NAMESPERPAGE);
1135                         if (page > 0) {
1136                                 do_template("vcard_list_section_end");
1137                                 end_tab(page-1, num_pages);
1138                         }
1139                         begin_tab(page, num_pages);
1140                         do_template("vcard_list_section_start");
1141                         displayed = 0;
1142                 }
1143
1144                 if ((displayed % 4) == 0) {
1145                         if (displayed > 0) {
1146                                 do_template("vcard_list_row_end");
1147                         }
1148                         do_template("vcard_list_row_start");
1149                         bg = 1 - bg;
1150                 }
1151         
1152
1153                 StackContext(TP, &SubTP, abEnt, CTX_VCARD, 0, NULL);
1154
1155                 DoTemplate(HKEY("vcard_list_entry"), WCC->WBuf, &SubTP);
1156                 UnStackContext(&SubTP);
1157
1158                 ++displayed;
1159         }
1160         DeleteHashPos(&it);
1161
1162         /* Placeholders for empty columns at end */
1163         if ((num_ab % 4) != 0) {
1164                 for (i=0; i<(4-(num_ab % 4)); ++i) {
1165                         do_template("vcard_list_cell_end");
1166                 }
1167         }
1168         
1169         do_template("vcard_list_section_end");
1170         end_tab((num_pages-1), num_pages);
1171
1172         begin_tab(num_pages, num_pages);
1173         /* FIXME there ought to be something here */
1174         end_tab(num_pages, num_pages);
1175
1176         DeleteHash(&headlines);
1177         wDumpContent(1);
1178 }
1179
1180
1181
1182 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1183 {
1184         const StrBuf *Mime;
1185         vcardview_struct *VS;
1186
1187         VS = (vcardview_struct*) *ViewSpecific;
1188         if (VS->is_singlecard)
1189                 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1190         else
1191                 do_addrbook_view(VS);   /* Render the address book */
1192         return 0;
1193 }
1194
1195 int vcard_Cleanup(void **ViewSpecific)
1196 {
1197         vcardview_struct *VS;
1198
1199         VS = (vcardview_struct*) *ViewSpecific;
1200         wDumpContent(1);
1201         if ((VS != NULL) && 
1202             (VS->addrbook != NULL))
1203                 DeleteHash(&VS->addrbook);
1204         if (VS != NULL) 
1205                 free(VS);
1206
1207         return 0;
1208 }
1209
1210 void render_MIME_VCard(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1211 {
1212         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1213         wcsession *WCC = WC;
1214         if (StrLength(Mime->Data) == 0)
1215                 MimeLoadData(Mime);
1216         if (StrLength(Mime->Data) > 0) {
1217                 struct vCard *v;
1218                 StrBuf *Buf;
1219
1220                 Buf = NewStrBuf();
1221                 /** If it's my vCard I can edit it */
1222                 if (    (!strcasecmp(ChrPtr(WCC->CurRoom.name), USERCONFIGROOM))
1223                         || ((StrLength(WCC->CurRoom.name) > 11) &&
1224                             (!strcasecmp(&(ChrPtr(WCC->CurRoom.name)[11]), USERCONFIGROOM)))
1225                         || (WCC->CurRoom.view == VIEW_ADDRESSBOOK)
1226                         ) {
1227                         StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld?partnum=%s\">",
1228                                 Mime->msgnum, ChrPtr(Mime->PartNum));
1229                         StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
1230                 }
1231
1232                 /* In all cases, display the full card */
1233
1234                 v = VCardLoad(Mime->Data);
1235
1236                 if (v != NULL) {
1237                         HashList *VC;
1238                         addrbookent ab;
1239                         memset(&ab, 0, sizeof(addrbookent));
1240
1241                         ab.VC = NewHash(0, lFlathash);
1242                         ab.ab_msgnum = Mime->msgnum;
1243
1244                         parse_vcard(Target, v, VC, Mime);
1245                         display_one_vcard (Target, &ab, HKEY("vcard_msg_display"));
1246                         DeleteHash(&VC);
1247
1248                 }
1249                 else {
1250                         StrBufPlain(Buf, _("failed to load vcard"), -1);
1251                 }
1252                 FreeStrBuf(&Mime->Data);
1253                 Mime->Data = Buf;
1254         }
1255
1256 }
1257
1258 void 
1259 ServerStartModule_VCARD
1260 (void)
1261 {
1262         ///VCToEnum = NewHash(0, NULL);
1263
1264 }
1265
1266 void 
1267 ServerShutdownModule_VCARD
1268 (void)
1269 {
1270         DeleteHash(&DefineToToken);
1271         DeleteHash(&vcNames);
1272         DeleteHash(&VCTokenToDefine);
1273         /// DeleteHash(&VCToEnum);
1274 }
1275
1276 void 
1277 InitModule_VCARD
1278 (void)
1279 {
1280         StrBuf *Prefix  = NewStrBufPlain(HKEY("VC:"));
1281         DefineToToken   = NewHash(1, lFlathash);
1282         vcNames         = NewHash(1, lFlathash);
1283         VCTokenToDefine = NewHash(1, NULL);
1284         autoRegisterTokens(&VCEnumCounter, VCStrE, Prefix, 0, 0);
1285         FreeStrBuf(&Prefix);
1286
1287         RegisterCTX(CTX_VCARD);
1288         RegisterCTX(CTX_VCARD_LIST);
1289         RegisterCTX(CTX_VCARD_TYPE);
1290
1291         RegisterReadLoopHandlerset(
1292                 VIEW_ADDRESSBOOK,
1293                 vcard_GetParamsGetServerCall,
1294                 NULL,
1295                 NULL,
1296                 NULL, 
1297                 vcard_LoadMsgFromServer,
1298                 vcard_RenderView_or_Tail,
1299                 vcard_Cleanup);
1300         WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1301         WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1302         WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1303
1304         RegisterNamespace("VC:ITEM", 2, 2, tmpl_vcard_item, preeval_vcard_item, CTX_VCARD);
1305         RegisterNamespace("VC:CTXITEM", 1, 1, tmpl_vcard_context_item, NULL, CTX_VCARD_TYPE);
1306         RegisterNamespace("VC:NAME", 1, 1, tmpl_vcard_name_str, preeval_vcard_name_str, CTX_VCARD);
1307         RegisterNamespace("VC:CTXNAME", 1, 1, tmpl_vcard_context_name_str, NULL, CTX_VCARD_TYPE);
1308         REGISTERTokenParamDefine(FlatString);
1309         REGISTERTokenParamDefine(StringCluster);
1310         REGISTERTokenParamDefine(PhoneNumber);
1311         REGISTERTokenParamDefine(EmailAddr);
1312         REGISTERTokenParamDefine(Street);
1313         REGISTERTokenParamDefine(Number);
1314         REGISTERTokenParamDefine(AliasFor);
1315         REGISTERTokenParamDefine(Base64BinaryAttachment);
1316         REGISTERTokenParamDefine(TerminateList);
1317         REGISTERTokenParamDefine(Address);
1318
1319         RegisterConditional("VC:HAVE:TYPE",      1, conditional_VC_Havetype, CTX_VCARD);
1320         RegisterFilteredIterator("VC:TYPE", 1, DefineToToken, NULL, NULL, NULL, filter_VC_ByType, CTX_VCARD_TYPE, CTX_VCARD, IT_NOFLAG);
1321         RegisterFilteredIterator("VC:TYPE:ITEMS", 0, NULL, getContextVcard, NULL, NULL, filter_VC_ByContextType, CTX_STRBUF, CTX_VCARD_TYPE, IT_NOFLAG);
1322
1323         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard, 1, 201);
1324         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard, 1, 200);
1325 }