Removed some old TECH_PREVIEW sections.
[citadel.git] / webcit / vcard_edit.c
1
2 #include "webcit.h"
3 #include "webserver.h"
4 #include "calendar.h"
5
6
7
8 ConstStr VCStr [] = {
9         {HKEY("n")}, /* N is name, but only if there's no FN already there */
10         {HKEY("fn")}, /* FN (full name) is a true 'display name' field */
11         {HKEY("title")},   /* title */
12         {HKEY("org")},    /* organization */
13         {HKEY("email")},
14         {HKEY("tel")},
15         {HKEY("tel_tel")},
16         {HKEY("tel_work")},
17         {HKEY("tel_home")},
18         {HKEY("tel_cell")},
19         {HKEY("adr")},
20         {HKEY("photo")},
21         {HKEY("version")},
22         {HKEY("rev")},
23         {HKEY("label")}
24 };
25
26 typedef enum _eVC{
27         VC_n,
28         VC_fn,
29         VC_title,
30         VC_org,
31         VC_email,
32         VC_tel,
33         VC_tel_tel,
34         VC_tel_work,
35         VC_tel_home,
36         VC_tel_cell,
37         VC_adr,
38         VC_photo,
39         VC_version,
40         VC_rev,
41         VC_label
42 } eVC;
43
44 HashList *VCToEnum = NULL;
45
46 /*
47  * Record compare function for sorting address book indices
48  */
49 int abcmp(const void *ab1, const void *ab2) {
50         return(strcasecmp(
51                 (((const addrbookent *)ab1)->ab_name),
52                 (((const addrbookent *)ab2)->ab_name)
53         ));
54 }
55
56
57 /*
58  * Helper function for do_addrbook_view()
59  * Converts a name into a three-letter tab label
60  */
61 void nametab(char *tabbuf, long len, char *name) {
62         stresc(tabbuf, len, name, 0, 0);
63         tabbuf[0] = toupper(tabbuf[0]);
64         tabbuf[1] = tolower(tabbuf[1]);
65         tabbuf[2] = tolower(tabbuf[2]);
66         tabbuf[3] = 0;
67 }
68
69
70 /*
71  * If it's an old "Firstname Lastname" style record, try to convert it.
72  */
73 void lastfirst_firstlast(char *namebuf) {
74         char firstname[SIZ];
75         char lastname[SIZ];
76         int i;
77
78         if (namebuf == NULL) return;
79         if (strchr(namebuf, ';') != NULL) return;
80
81         i = num_tokens(namebuf, ' ');
82         if (i < 2) return;
83
84         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
85         remove_token(namebuf, i-1, ' ');
86         strcpy(firstname, namebuf);
87         sprintf(namebuf, "%s; %s", lastname, firstname);
88 }
89
90
91
92 wc_mime_attachment *load_vcard(message_summary *Msg) 
93 {
94         HashPos  *it;
95         StrBuf *FoundCharset = NewStrBuf();
96         StrBuf *Error;
97         void *vMime;
98         const char *Key;
99         long len;
100         wc_mime_attachment *Mime;
101         wc_mime_attachment *VCMime = NULL;
102
103         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
104         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
105         Msg->MsgBody->msgnum = Msg->msgnum;
106
107         load_message(Msg, FoundCharset, &Error);
108
109         FreeStrBuf(&FoundCharset);
110         /* look up the vcard... */
111         it = GetNewHashPos(Msg->AllAttach, 0);
112         while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) && 
113                (vMime != NULL)) 
114         {
115                 Mime = (wc_mime_attachment*) vMime;
116                 if ((strcmp(ChrPtr(Mime->ContentType),
117                            "text/x-vcard") == 0) ||
118                     (strcmp(ChrPtr(Mime->ContentType),
119                             "text/vcard") == 0))
120                 {
121                         VCMime = Mime;
122                         break;
123                 }
124         }
125         DeleteHashPos(&it);
126         if (VCMime == NULL)
127                 return NULL;
128
129         MimeLoadData(VCMime);
130         return VCMime;
131 }
132
133 /*
134  * fetch the display name off a vCard
135  */
136 void fetch_ab_name(message_summary *Msg, char **namebuf) {
137         long len;
138         int i;
139         wc_mime_attachment *VCMime = NULL;
140
141         if (namebuf == NULL) return;
142
143         VCMime = load_vcard(Msg);
144         if (VCMime == NULL)
145                 return;
146
147         /* Grab the name off the card */
148         display_vcard(WC->WBuf, VCMime, 0, 0, namebuf, Msg->msgnum);
149
150         if (*namebuf != NULL) {
151                 lastfirst_firstlast(*namebuf);
152                 striplt(*namebuf);
153                 len = strlen(*namebuf);
154                 for (i=0; i<len; ++i) {
155                         if ((*namebuf)[i] != ';') return;
156                 }
157                 free (*namebuf);
158                 (*namebuf) = strdup(_("(no name)"));
159         }
160         else {
161                 (*namebuf) = strdup(_("(no name)"));
162         }
163 }
164
165
166
167 /*
168  * Turn a vCard "n" (name) field into something displayable.
169  */
170 void vcard_n_prettyize(char *name)
171 {
172         char *original_name;
173         int i, j, len;
174
175         original_name = strdup(name);
176         len = strlen(original_name);
177         for (i=0; i<5; ++i) {
178                 if (len > 0) {
179                         if (original_name[len-1] == ' ') {
180                                 original_name[--len] = 0;
181                         }
182                         if (original_name[len-1] == ';') {
183                                 original_name[--len] = 0;
184                         }
185                 }
186         }
187         strcpy(name, "");
188         j=0;
189         for (i=0; i<len; ++i) {
190                 if (original_name[i] == ';') {
191                         name[j++] = ',';
192                         name[j++] = ' ';                        
193                 }
194                 else {
195                         name[j++] = original_name[i];
196                 }
197         }
198         name[j] = '\0';
199         free(original_name);
200 }
201
202
203
204
205 /*
206  * preparse a vcard name
207  * display_vcard() calls this after parsing the textual vCard into
208  * our 'struct vCard' data object.
209  * This gets called instead of display_parsed_vcard() if we are only looking
210  * to extract the person's name instead of displaying the card.
211  */
212 void fetchname_parsed_vcard(struct vCard *v, char **storename) {
213         char *name;
214         char *prop;
215         char buf[SIZ];
216         int j, n, len;
217         int is_qp = 0;
218         int is_b64 = 0;
219
220         *storename = NULL;
221
222         name = vcard_get_prop(v, "n", 1, 0, 0);
223         if (name != NULL) {
224                 len = strlen(name);
225                 prop = vcard_get_prop(v, "n", 1, 0, 1);
226                 n = num_tokens(prop, ';');
227
228                 for (j=0; j<n; ++j) {
229                         extract_token(buf, prop, j, ';', sizeof buf);
230                         if (!strcasecmp(buf, "encoding=quoted-printable")) {
231                                 is_qp = 1;
232                         }
233                         if (!strcasecmp(buf, "encoding=base64")) {
234                                 is_b64 = 1;
235                         }
236                 }
237                 if (is_qp) {
238                         // %ff can become 6 bytes in utf8 
239                         *storename = malloc(len * 2 + 3); 
240                         j = CtdlDecodeQuotedPrintable(
241                                 *storename, name,
242                                 len);
243                         (*storename)[j] = 0;
244                 }
245                 else if (is_b64) {
246                         // ff will become one byte..
247                         *storename = malloc(len + 50);
248                         CtdlDecodeBase64(
249                                 *storename, name,
250                                 len);
251                 }
252                 else {
253                         size_t len;
254
255                         len = strlen (name);
256                         
257                         *storename = malloc(len + 3); /* \0 + eventualy missing ', '*/
258                         memcpy(*storename, name, len + 1);
259                 }
260                 /* vcard_n_prettyize(storename); */
261         }
262
263 }
264
265
266
267 /*
268  * html print a vcard
269  * display_vcard() calls this after parsing the textual vCard into
270  * our 'struct vCard' data object.
271  *
272  * Set 'full' to nonzero to display the full card, otherwise it will only
273  * show a summary line.
274  *
275  * This code is a bit ugly, so perhaps an explanation is due: we do this
276  * in two passes through the vCard fields.  On the first pass, we process
277  * fields we understand, and then render them in a pretty fashion at the
278  * end.  Then we make a second pass, outputting all the fields we don't
279  * understand in a simple two-column name/value format.
280  * v            the vCard to display
281  * full         display all items of the vcard?
282  * msgnum       Citadel message pointer
283  */
284 void display_parsed_vcard(StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
285 {
286         int i, j;
287         char buf[SIZ];
288         char *name;
289         int is_qp = 0;
290         int is_b64 = 0;
291         char *thisname, *thisvalue;
292         char firsttoken[SIZ];
293         int pass;
294
295         char fullname[SIZ];
296         char title[SIZ];
297         char org[SIZ];
298         char phone[SIZ];
299         char mailto[SIZ];
300
301         strcpy(fullname, "");
302         strcpy(phone, "");
303         strcpy(mailto, "");
304         strcpy(title, "");
305         strcpy(org, "");
306
307         if (!full) {
308                 StrBufAppendPrintf(Target, "<td>");
309                 name = vcard_get_prop(v, "fn", 1, 0, 0);
310                 if (name != NULL) {
311                         StrEscAppend(Target, NULL, name, 0, 0);
312                 }
313                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
314                         strcpy(fullname, name);
315                         vcard_n_prettyize(fullname);
316                         StrEscAppend(Target, NULL, fullname, 0, 0);
317                 }
318                 else {
319                         StrBufAppendPrintf(Target, "&nbsp;");
320                 }
321                 StrBufAppendPrintf(Target, "</td>");
322                 return;
323         }
324
325         StrBufAppendPrintf(Target, "<div align=\"center\">"
326                 "<table bgcolor=\"#aaaaaa\" width=\"50%%\">");
327         for (pass=1; pass<=2; ++pass) {
328
329                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
330                         int len;
331                         thisname = strdup(v->prop[i].name);
332                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
333         
334                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
335                                 extract_token(buf, thisname, j, ';', sizeof buf);
336                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
337                                         is_qp = 1;
338                                         remove_token(thisname, j, ';');
339                                 }
340                                 if (!strcasecmp(buf, "encoding=base64")) {
341                                         is_b64 = 1;
342                                         remove_token(thisname, j, ';');
343                                 }
344                         }
345                         
346                         len = strlen(v->prop[i].value);
347                         /* if we have some untagged QP, detect it here. */
348                         if (!is_qp && (strstr(v->prop[i].value, "=?")!=NULL))
349                                 utf8ify_rfc822_string(&v->prop[i].value);
350
351                         if (is_qp) {
352                                 // %ff can become 6 bytes in utf8 
353                                 thisvalue = malloc(len * 2 + 3); 
354                                 j = CtdlDecodeQuotedPrintable(
355                                         thisvalue, v->prop[i].value,
356                                         len);
357                                 thisvalue[j] = 0;
358                         }
359                         else if (is_b64) {
360                                 // ff will become one byte..
361                                 thisvalue = malloc(len + 50);
362                                 CtdlDecodeBase64(
363                                         thisvalue, v->prop[i].value,
364                                         strlen(v->prop[i].value) );
365                         }
366                         else {
367                                 thisvalue = strdup(v->prop[i].value);
368                         }
369         
370                         /* Various fields we may encounter ***/
371         
372                         /* N is name, but only if there's no FN already there */
373                         if (!strcasecmp(firsttoken, "n")) {
374                                 if (IsEmptyStr(fullname)) {
375                                         strcpy(fullname, thisvalue);
376                                         vcard_n_prettyize(fullname);
377                                 }
378                         }
379         
380                         /* FN (full name) is a true 'display name' field */
381                         else if (!strcasecmp(firsttoken, "fn")) {
382                                 strcpy(fullname, thisvalue);
383                         }
384
385                         /* title */
386                         else if (!strcasecmp(firsttoken, "title")) {
387                                 strcpy(title, thisvalue);
388                         }
389         
390                         /* organization */
391                         else if (!strcasecmp(firsttoken, "org")) {
392                                 strcpy(org, thisvalue);
393                         }
394         
395                         else if (!strcasecmp(firsttoken, "email")) {
396                                 size_t len;
397                                 if (!IsEmptyStr(mailto)) strcat(mailto, "<br>");
398                                 strcat(mailto,
399                                         "<a href=\"display_enter"
400                                         "?force_room=_MAIL_?recp=");
401
402                                 len = strlen(mailto);
403                                 urlesc(&mailto[len], SIZ - len, "\"");
404                                 len = strlen(mailto);
405                                 urlesc(&mailto[len], SIZ - len,  fullname);
406                                 len = strlen(mailto);
407                                 urlesc(&mailto[len], SIZ - len, "\" <");
408                                 len = strlen(mailto);
409                                 urlesc(&mailto[len], SIZ - len, thisvalue);
410                                 len = strlen(mailto);
411                                 urlesc(&mailto[len], SIZ - len, ">");
412
413                                 strcat(mailto, "\">");
414                                 len = strlen(mailto);
415                                 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
416                                 strcat(mailto, "</A>");
417                         }
418                         else if (!strcasecmp(firsttoken, "tel")) {
419                                 if (!IsEmptyStr(phone)) strcat(phone, "<br>");
420                                 strcat(phone, thisvalue);
421                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
422                                         extract_token(buf, thisname, j, ';', sizeof buf);
423                                         if (!strcasecmp(buf, "tel"))
424                                                 strcat(phone, "");
425                                         else if (!strcasecmp(buf, "work"))
426                                                 strcat(phone, _(" (work)"));
427                                         else if (!strcasecmp(buf, "home"))
428                                                 strcat(phone, _(" (home)"));
429                                         else if (!strcasecmp(buf, "cell"))
430                                                 strcat(phone, _(" (cell)"));
431                                         else {
432                                                 strcat(phone, " (");
433                                                 strcat(phone, buf);
434                                                 strcat(phone, ")");
435                                         }
436                                 }
437                         }
438                         else if (!strcasecmp(firsttoken, "adr")) {
439                                 if (pass == 2) {
440                                         StrBufAppendPrintf(Target, "<tr><td>");
441                                         StrBufAppendPrintf(Target, _("Address:"));
442                                         StrBufAppendPrintf(Target, "</td><td>");
443                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
444                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
445                                                 if (!IsEmptyStr(buf)) {
446                                                         StrEscAppend(Target, NULL, buf, 0, 0);
447                                                         if (j<3) StrBufAppendPrintf(Target, "<br>");
448                                                         else StrBufAppendPrintf(Target, " ");
449                                                 }
450                                         }
451                                         StrBufAppendPrintf(Target, "</td></tr>\n");
452                                 }
453                         }
454                         /* else if (!strcasecmp(firsttoken, "photo") && full && pass == 2) { 
455                                 // Only output on second pass
456                                 StrBufAppendPrintf(Target, "<tr><td>");
457                                 StrBufAppendPrintf(Target, _("Photo:"));
458                                 StrBufAppendPrintf(Target, "</td><td>");
459                                 StrBufAppendPrintf(Target, "<img src=\"/vcardphoto/%ld/\" alt=\"Contact photo\"/>",msgnum);
460                                 StrBufAppendPrintf(Target, "</td></tr>\n");
461                         } */
462                         else if (!strcasecmp(firsttoken, "version")) {
463                                 /* ignore */
464                         }
465                         else if (!strcasecmp(firsttoken, "rev")) {
466                                 /* ignore */
467                         }
468                         else if (!strcasecmp(firsttoken, "label")) {
469                                 /* ignore */
470                         }
471                         else {
472
473                                 /*** Don't show extra fields.  They're ugly.
474                                 if (pass == 2) {
475                                         StrBufAppendPrintf(Target, "<TR><TD>");
476                                         StrEscAppend(Target, NULL, thisname, 0, 0);
477                                         StrBufAppendPrintf(Target, "</TD><TD>");
478                                         StrEscAppend(Target, NULL, thisvalue, 0, 0);
479                                         StrBufAppendPrintf(Target, "</TD></TR>\n");
480                                 }
481                                 ***/
482                         }
483         
484                         free(thisname);
485                         free(thisvalue);
486                 }
487         
488                 if (pass == 1) {
489                         StrBufAppendPrintf(Target, "<tr bgcolor=\"#aaaaaa\">"
490       "<td colspan=2 bgcolor=\"#ffffff\">"
491       "<img align=\"center\" src=\"static/viewcontacts_48x.gif\">"
492       "<font size=\"+1\"><b>");
493                         StrEscAppend(Target, NULL, fullname, 0, 0);
494                         StrBufAppendPrintf(Target, "</b></font>");
495                         if (!IsEmptyStr(title)) {
496                                 StrBufAppendPrintf(Target, "<div align=\"right>\"");
497                                 StrEscAppend(Target, NULL, title, 0, 0);
498                                 StrBufAppendPrintf(Target, "</div>");
499                         }
500                         if (!IsEmptyStr(org)) {
501                                 StrBufAppendPrintf(Target, "<div align=\"right\">");
502                                 StrEscAppend(Target, NULL, org, 0, 0);
503                                 StrBufAppendPrintf(Target, "</div>");
504                         }
505                         StrBufAppendPrintf(Target, "</td></tr>\n");
506                 
507                         if (!IsEmptyStr(phone)) {
508                                 StrBufAppendPrintf(Target, "<tr><td>");
509                                 StrBufAppendPrintf(Target, _("Telephone:"));
510                                 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", phone);
511                         }
512                         if (!IsEmptyStr(mailto)) {
513                                 StrBufAppendPrintf(Target, "<tr><td>");
514                                 StrBufAppendPrintf(Target, _("E-mail:"));
515                                 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", mailto);
516                         }
517                 }
518
519         }
520
521         StrBufAppendPrintf(Target, "</table></div>\n");
522 }
523
524 /*
525  * html print a vcard
526  * display_vcard() calls this after parsing the textual vCard into
527  * our 'struct vCard' data object.
528  *
529  * Set 'full' to nonzero to display the full card, otherwise it will only
530  * show a summary line.
531  *
532  * This code is a bit ugly, so perhaps an explanation is due: we do this
533  * in two passes through the vCard fields.  On the first pass, we process
534  * fields we understand, and then render them in a pretty fashion at the
535  * end.  Then we make a second pass, outputting all the fields we don't
536  * understand in a simple two-column name/value format.
537  * v            the vCard to display
538  * full         display all items of the vcard?
539  * msgnum       Citadel message pointer
540  */
541 void parse_vcard(StrBuf *Target, struct vCard *v, HashList *VC, int full, wc_mime_attachment *Mime)
542 {
543         StrBuf *Val = NULL;
544         StrBuf *Swap = NULL;
545         int i, j;
546         char buf[SIZ];
547         char *name;
548         int is_qp = 0;
549         int is_b64 = 0;
550         StrBuf *thisname = NULL;
551         char *thisvalue = NULL;
552         char firsttoken[SIZ];
553         int pass;
554         long len;
555         void *V;
556
557         Swap = NewStrBuf ();
558         thisname = NewStrBuf();
559         for (i=0; i<(v->numprops); ++i) {
560                 int len;
561                 is_qp = 0;
562                 is_b64 = 0;
563                 StrBufPlain(thisname, v->prop[i].name, -1);
564                 StrBufLowerCase(thisname);
565                 
566                 len = extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
567                 
568                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
569                         extract_token(buf, thisname, j, ';', sizeof buf);
570                         if (!strcasecmp(buf, "encoding=quoted-printable")) {
571                                 is_qp = 1;
572                                 remove_token(thisname, j, ';');
573                         }
574                         if (!strcasecmp(buf, "encoding=base64")) {
575                                 is_b64 = 1;
576                                 remove_token(thisname, j, ';');
577                         }
578                 }
579                 
580                 /* copy over the payload into a StrBuf */
581                 Val = NewStrBufPlain(v->prop[i].value, -1);
582                         
583                 /* if we have some untagged QP, detect it here. */
584                 if (is_qp || (strstr(v->prop[i].value, "=?")!=NULL)){
585                         StrBuf *b;
586                         StrBuf_RFC822_to_Utf8(Swap, Val, NULL, NULL); /* default charset, current charset */
587                         b = Val;
588                         Val = Swap; 
589                         Swap = b;
590                         FlushStrBuf(Swap);
591                 }
592                 else if (is_b64) {
593                         StrBufDecodeBase64(Val);
594
595                 }
596                 lprintf(1, "%s [%s][%s]",
597                         firsttoken,
598                         ChrPtr(Val),
599                         v->prop[i].value);
600                 if (GetHash(VCToEnum, firsttoken, strlen(firsttoken), &V))
601                 {
602                         eVC evc = (eVC) V;
603                         Put(VC, IKEY(evc), Val, HFreeStrBuf);
604                         lprintf(1, "[%ld]\n", evc);
605                         Val = NULL;
606                 }
607                 else
608                         lprintf(1, "[]\n");
609 /*
610 TODO: check for layer II
611                 else 
612                 {
613                         long max = num_tokens(thisname, ';');
614                         firsttoken[len] = '_';
615
616                         for (j = 0; j < max; j++) {
617 //                      firsttoken[len]
618
619                                 extract_token(buf, thisname, j, ';', sizeof (buf));
620                                         if (!strcasecmp(buf, "tel"))
621                                                 strcat(phone, "");
622                                         else if (!strcasecmp(buf, "work"))
623                                                 strcat(phone, _(" (work)"));
624                                         else if (!strcasecmp(buf, "home"))
625                                                 strcat(phone, _(" (home)"));
626                                         else if (!strcasecmp(buf, "cell"))
627                                                 strcat(phone, _(" (cell)"));
628                                         else {
629                                                 strcat(phone, " (");
630                                                 strcat(phone, buf);
631                                                 strcat(phone, ")");
632                                         }
633                                 }
634                         }
635
636                 }
637 */
638         
639                 FreeStrBuf(&Val);
640                 free(thisname);
641                 thisname = NULL;
642         }
643
644 }
645
646 void tmplput_VCARD_ITEM(StrBuf *Target, WCTemplputParams *TP)
647 {
648         HashList *VC = CTX;
649         eVC evc;
650         void *vStr;
651
652         evc = GetTemplateTokenNumber(Target, TP, 0, -1);
653         if (evc != -1)
654         {
655                 if (GetHash(VC, IKEY(evc), &vStr))
656                 {
657                         StrBufAppendTemplate(Target, TP,
658                                              (StrBuf*) vStr,
659                                              1);
660                 }
661         }
662         
663 }
664
665 void new_vcard (StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
666 {
667         HashList *VC;
668         WCTemplputParams SubTP;
669
670         memset(&SubTP, 0, sizeof(WCTemplputParams));    
671
672
673         VC = NewHash(0, Flathash);
674         parse_vcard(Target, v, VC, full, Mime);
675
676         SubTP.Filter.ContextType = CTX_VCARD;
677         SubTP.Context = VC;
678
679         DoTemplate(HKEY("test_vcard"), Target, &SubTP);
680         DeleteHash(&VC);
681 }
682
683
684
685 /*
686  * Display a textual vCard
687  * (Converts to a vCard object and then calls the actual display function)
688  * Set 'full' to nonzero to display the whole card instead of a one-liner.
689  * Or, if "storename" is non-NULL, just store the person's name in that
690  * buffer instead of displaying the card at all.
691  *
692  * vcard_source the buffer containing the vcard text
693  * alpha        Display only if name begins with this letter of the alphabet
694  * full         Display the full vCard (otherwise just the display name)
695  * storename    If not NULL, also store the display name here
696  * msgnum       Citadel message pointer
697  */
698 void display_vcard(StrBuf *Target, 
699                    wc_mime_attachment *Mime, 
700                    char alpha, 
701                    int full, 
702                    char **storename, 
703                    long msgnum) 
704 {
705         struct vCard *v;
706         char *name;
707         StrBuf *Buf;
708         StrBuf *Buf2;
709         char this_alpha = 0;
710
711         v = VCardLoad(Mime->Data);
712
713         if (v == NULL) return;
714
715         name = vcard_get_prop(v, "n", 1, 0, 0);
716         if (name != NULL) {
717                 Buf = NewStrBufPlain(name, -1);
718                 Buf2 = NewStrBufPlain(NULL, StrLength(Buf));
719                 StrBuf_RFC822_to_Utf8(Buf2, Buf, WC->DefaultCharset, NULL);
720                 this_alpha = ChrPtr(Buf)[0];
721                 FreeStrBuf(&Buf);
722                 FreeStrBuf(&Buf2);
723         }
724
725         if (storename != NULL) {
726                 fetchname_parsed_vcard(v, storename);
727         }
728         else if ((alpha == 0) || 
729                  ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha))) || 
730                  ((!isalpha(alpha)) && (!isalpha(this_alpha)))
731                 ) 
732         {
733 #ifdef XXX_XXX
734                 new_vcard (Target, v, full, Mime);
735 #else
736                 display_parsed_vcard(Target, v, full, Mime);
737 #endif          
738         }
739
740         vcard_free(v);
741 }
742
743
744
745 /*
746  * Render the address book using info we gathered during the scan
747  *
748  * addrbook     the addressbook to render
749  * num_ab       the number of the addressbook
750  */
751 void do_addrbook_view(addrbookent *addrbook, int num_ab) {
752         int i = 0;
753         int displayed = 0;
754         int bg = 0;
755         static int NAMESPERPAGE = 60;
756         int num_pages = 0;
757         int tabfirst = 0;
758         char tabfirst_label[64];
759         int tablast = 0;
760         char tablast_label[64];
761         char this_tablabel[64];
762         int page = 0;
763         char **tablabels;
764
765         if (num_ab == 0) {
766                 wc_printf("<br><br><br><div align=\"center\"><i>");
767                 wc_printf(_("This address book is empty."));
768                 wc_printf("</i></div>\n");
769                 return;
770         }
771
772         if (num_ab > 1) {
773                 qsort(addrbook, num_ab, sizeof(addrbookent), abcmp);
774         }
775
776         num_pages = (num_ab / NAMESPERPAGE) + 1;
777
778         tablabels = malloc(num_pages * sizeof (char *));
779         if (tablabels == NULL) {
780                 wc_printf("<br><br><br><div align=\"center\"><i>");
781                 wc_printf(_("An internal error has occurred."));
782                 wc_printf("</i></div>\n");
783                 return;
784         }
785
786         for (i=0; i<num_pages; ++i) {
787                 tabfirst = i * NAMESPERPAGE;
788                 tablast = tabfirst + NAMESPERPAGE - 1;
789                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
790                 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
791                 nametab(tablast_label, 64, addrbook[tablast].ab_name);
792                 sprintf(this_tablabel, "%s&nbsp;-&nbsp;%s", tabfirst_label, tablast_label);
793                 tablabels[i] = strdup(this_tablabel);
794         }
795
796         tabbed_dialog(num_pages, tablabels);
797         page = (-1);
798
799         for (i=0; i<num_ab; ++i) {
800
801                 if ((i / NAMESPERPAGE) != page) {       /* New tab */
802                         page = (i / NAMESPERPAGE);
803                         if (page > 0) {
804                                 wc_printf("</tr></table>\n");
805                                 end_tab(page-1, num_pages);
806                         }
807                         begin_tab(page, num_pages);
808                         wc_printf("<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%%\">\n");
809                         displayed = 0;
810                 }
811
812                 if ((displayed % 4) == 0) {
813                         if (displayed > 0) {
814                                 wc_printf("</tr>\n");
815                         }
816                         bg = 1 - bg;
817                         wc_printf("<tr bgcolor=\"#%s\">",
818                                 (bg ? "dddddd" : "ffffff")
819                         );
820                 }
821         
822                 wc_printf("<td>");
823
824                 wc_printf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
825                         addrbook[i].ab_msgnum);
826                 wc_printf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
827                 vcard_n_prettyize(addrbook[i].ab_name);
828                 escputs(addrbook[i].ab_name);
829                 wc_printf("</a></td>\n");
830                 ++displayed;
831         }
832
833         /* Placeholders for empty columns at end */
834         if ((num_ab % 4) != 0) {
835                 for (i=0; i<(4-(num_ab % 4)); ++i) {
836                         wc_printf("<td>&nbsp;</td>");
837                 }
838         }
839
840         wc_printf("</tr></table>\n");
841         end_tab((num_pages-1), num_pages);
842
843         begin_tab(num_pages, num_pages);
844         /* FIXME there ought to be something here */
845         end_tab(num_pages, num_pages);
846
847         for (i=0; i<num_pages; ++i) {
848                 free(tablabels[i]);
849         }
850         free(tablabels);
851 }
852
853
854
855
856 /*
857  * Edit the vCard component of a MIME message.  
858  * Supply the message number
859  * and MIME part number to fetch.  Or, specify -1 for the message number
860  * to start with a blank card.
861  */
862 void do_edit_vcard(long msgnum, char *partnum, 
863                    message_summary *VCMsg,
864                    wc_mime_attachment *VCAtt,
865                    const char *return_to, 
866                    const char *force_room) {
867         message_summary *Msg = NULL;
868         wc_mime_attachment *VCMime = NULL;
869         struct vCard *v;
870         int i;
871         char *key, *value;
872         char whatuser[256];
873
874         char lastname[256];
875         char firstname[256];
876         char middlename[256];
877         char prefix[256];
878         char suffix[256];
879         char pobox[256];
880         char extadr[256];
881         char street[256];
882         char city[256];
883         char state[256];
884         char zipcode[256];
885         char country[256];
886         char hometel[256];
887         char worktel[256];
888         char faxtel[256];
889         char mobiletel[256];
890         char primary_inetemail[256];
891         char other_inetemail[SIZ];
892         char extrafields[SIZ];
893         char fullname[256];
894         char title[256];
895         char org[256];
896
897         lastname[0] = 0;
898         firstname[0] = 0;
899         middlename[0] = 0;
900         prefix[0] = 0;
901         suffix[0] = 0;
902         pobox[0] = 0;
903         extadr[0] = 0;
904         street[0] = 0;
905         city[0] = 0;
906         state[0] = 0;
907         zipcode[0] = 0;
908         country[0] = 0;
909         hometel[0] = 0;
910         worktel[0] = 0;
911         faxtel[0] = 0;
912         mobiletel[0] = 0;
913         primary_inetemail[0] = 0;
914         other_inetemail[0] = 0;
915         title[0] = 0;
916         org[0] = 0;
917         extrafields[0] = 0;
918         fullname[0] = 0;
919
920         safestrncpy(whatuser, "", sizeof whatuser);
921
922         if ((msgnum >= 0) || 
923             ((VCMsg != NULL) && (VCAtt != NULL)))
924         {
925                 if ((VCMsg == NULL) && (VCAtt == NULL)) {
926
927                         Msg = (message_summary *) malloc(sizeof(message_summary));
928                         memset(Msg, 0, sizeof(message_summary));
929                         Msg->msgnum = msgnum;
930                         VCMime = load_vcard(Msg);
931                         if (VCMime == NULL) {
932                                 convenience_page("770000", _("Error"), "");///TODO: important message
933                                 DestroyMessageSummary(Msg);
934                                 return;
935                         }
936                 
937                         v = VCardLoad(VCMime->Data);
938                 }
939                 else {
940                         v = VCardLoad(VCAtt->Data);
941                 }
942         
943                 /* Populate the variables for our form */
944                 i = 0;
945                 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
946                         char prp[256];  /* property name */
947                         char prm[256];  /* parameters */
948
949                         value = vcard_get_prop(v, "", 0, i++, 0);
950
951
952                         extract_token(prp, key, 0, ';', sizeof prp);
953                         safestrncpy(prm, key, sizeof prm);
954                         remove_token(prm, 0, ';');
955
956                         if (!strcasecmp(prp, "n")) {
957                                 extract_token(lastname, value, 0, ';', sizeof lastname);
958                                 extract_token(firstname, value, 1, ';', sizeof firstname);
959                                 extract_token(middlename, value, 2, ';', sizeof middlename);
960                                 extract_token(prefix, value, 3, ';', sizeof prefix);
961                                 extract_token(suffix, value, 4, ';', sizeof suffix);
962                         }
963
964                         else if (!strcasecmp(prp, "fn")) {
965                                 safestrncpy(fullname, value, sizeof fullname);
966                         }
967
968                         else if (!strcasecmp(prp, "title")) {
969                                 safestrncpy(title, value, sizeof title);
970                         }
971         
972                         else if (!strcasecmp(prp, "org")) {
973                                 safestrncpy(org, value, sizeof org);
974                         }
975         
976                         else if (!strcasecmp(prp, "adr")) {
977                                 extract_token(pobox, value, 0, ';', sizeof pobox);
978                                 extract_token(extadr, value, 1, ';', sizeof extadr);
979                                 extract_token(street, value, 2, ';', sizeof street);
980                                 extract_token(city, value, 3, ';', sizeof city);
981                                 extract_token(state, value, 4, ';', sizeof state);
982                                 extract_token(zipcode, value, 5, ';', sizeof zipcode);
983                                 extract_token(country, value, 6, ';', sizeof country);
984                         }
985
986                         else if (!strcasecmp(prp, "tel")) {
987
988                                 if (bmstrcasestr(prm, "home")) {
989                                         extract_token(hometel, value, 0, ';', sizeof hometel);
990                                 }
991                                 else if (bmstrcasestr(prm, "work")) {
992                                         extract_token(worktel, value, 0, ';', sizeof worktel);
993                                 }
994                                 else if (bmstrcasestr(prm, "fax")) {
995                                         extract_token(faxtel, value, 0, ';', sizeof faxtel);
996                                 }
997                                 else if (bmstrcasestr(prm, "cell")) {
998                                         extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
999                                 }
1000                                 else {  /* Missing or unknown type; put it in the home phone */
1001                                         extract_token(hometel, value, 0, ';', sizeof hometel);
1002                                 }
1003                         }
1004         
1005                         else if ( (!strcasecmp(prp, "email")) && (bmstrcasestr(prm, "internet")) ) {
1006                                 if (primary_inetemail[0] == 0) {
1007                                         safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
1008                                 }
1009                                 else {
1010                                         if (other_inetemail[0] != 0) {
1011                                                 strcat(other_inetemail, "\n");
1012                                         }
1013                                         strcat(other_inetemail, value);
1014                                 }
1015                         }
1016
1017                         /* Unrecognized properties are preserved here so we don't discard them
1018                          * just because the vCard was edited with WebCit.
1019                          */
1020                         else {
1021                                 strcat(extrafields, key);
1022                                 strcat(extrafields, ":");
1023                                 strcat(extrafields, value);
1024                                 strcat(extrafields, "\n");
1025                         }
1026         
1027                 }
1028         
1029                 vcard_free(v);
1030         }
1031
1032         /* Display the form */
1033         output_headers(1, 1, 1, 0, 0, 0);
1034
1035         do_template("beginbox_1", NULL);
1036         StrBufAppendBufPlain(WC->WBuf, _("Edit contact information"), -1, 0);
1037         do_template("beginbox_2", NULL);
1038
1039         wc_printf("<form method=\"POST\" action=\"submit_vcard\">\n");
1040         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1041
1042         if (force_room != NULL) {
1043                 wc_printf("<input type=\"hidden\" name=\"force_room\" value=\"");
1044                 escputs(force_room);
1045                 wc_printf("\">\n");
1046         }
1047
1048         wc_printf("<table class=\"vcard_edit_background\"><tr><td>\n");
1049
1050         wc_printf("<table border=\"0\"><tr>"
1051                 "<td>%s</td>"
1052                 "<td>%s</td>"
1053                 "<td>%s</td>"
1054                 "<td>%s</td>"
1055                 "<td>%s</td></tr>\n",
1056                 _("Prefix"), _("First Name"), _("Middle Name"), _("Last Name"), _("Suffix")
1057         );
1058         wc_printf("<tr><td><input type=\"text\" name=\"prefix\" "
1059                 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
1060                 prefix);
1061         wc_printf("<td><input type=\"text\" name=\"firstname\" "
1062                 "value=\"%s\" maxlength=\"29\"></td>",
1063                 firstname);
1064         wc_printf("<td><input type=\"text\" name=\"middlename\" "
1065                 "value=\"%s\" maxlength=\"29\"></td>",
1066                 middlename);
1067         wc_printf("<td><input type=\"text\" name=\"lastname\" "
1068                 "value=\"%s\" maxlength=\"29\"></td>",
1069                 lastname);
1070         wc_printf("<td><input type=\"text\" name=\"suffix\" "
1071                 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
1072                 suffix);
1073
1074         wc_printf("<table  class=\"vcard_edit_background_alt\">");
1075         wc_printf("<tr><td>");
1076
1077         wc_printf(_("Display name:"));
1078         wc_printf("<br>"
1079                 "<input type=\"text\" name=\"fullname\" "
1080                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1081                 fullname
1082         );
1083
1084         wc_printf(_("Title:"));
1085         wc_printf("<br>"
1086                 "<input type=\"text\" name=\"title\" "
1087                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1088                 title
1089         );
1090
1091         wc_printf(_("Organization:"));
1092         wc_printf("<br>"
1093                 "<input type=\"text\" name=\"org\" "
1094                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1095                 org
1096         );
1097
1098         wc_printf("</td><td>");
1099
1100         wc_printf("<table border=\"0\">");
1101         wc_printf("<tr><td>");
1102         wc_printf(_("PO box:"));
1103         wc_printf("</td><td>"
1104                 "<input type=\"text\" name=\"pobox\" "
1105                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1106                 pobox);
1107         wc_printf("<tr><td>");
1108         wc_printf(_("Address:"));
1109         wc_printf("</td><td>"
1110                 "<input type=\"text\" name=\"extadr\" "
1111                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1112                 extadr);
1113         wc_printf("<tr><td> </td><td>"
1114                 "<input type=\"text\" name=\"street\" "
1115                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1116                 street);
1117         wc_printf("<tr><td>");
1118         wc_printf(_("City:"));
1119         wc_printf("</td><td>"
1120                 "<input type=\"text\" name=\"city\" "
1121                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1122                 city);
1123         wc_printf("<tr><td>");
1124         wc_printf(_("State:"));
1125         wc_printf("</td><td>"
1126                 "<input type=\"text\" name=\"state\" "
1127                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1128                 state);
1129         wc_printf("<tr><td>");
1130         wc_printf(_("ZIP code:"));
1131         wc_printf("</td><td>"
1132                 "<input type=\"text\" name=\"zipcode\" "
1133                 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
1134                 zipcode);
1135         wc_printf("<tr><td>");
1136         wc_printf(_("Country:"));
1137         wc_printf("</td><td>"
1138                 "<input type=\"text\" name=\"country\" "
1139                 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
1140                 country);
1141         wc_printf("</table>\n");
1142
1143         wc_printf("</table>\n");
1144
1145         wc_printf("<table border=0><tr><td>");
1146         wc_printf(_("Home telephone:"));
1147         wc_printf("</td>"
1148                 "<td><input type=\"text\" name=\"hometel\" "
1149                 "value=\"%s\" maxlength=\"29\"></td>\n",
1150                 hometel);
1151         wc_printf("<td>");
1152         wc_printf(_("Work telephone:"));
1153         wc_printf("</td>"
1154                 "<td><input type=\"text\" name=\"worktel\" "
1155                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1156                 worktel);
1157         wc_printf("<tr><td>");
1158         wc_printf(_("Mobile telephone:"));
1159         wc_printf("</td>"
1160                 "<td><input type=\"text\" name=\"mobiletel\" "
1161                 "value=\"%s\" maxlength=\"29\"></td>\n",
1162                 mobiletel);
1163         wc_printf("<td>");
1164         wc_printf(_("Fax number:"));
1165         wc_printf("</td>"
1166                 "<td><input type=\"text\" name=\"faxtel\" "
1167                 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
1168                 faxtel);
1169
1170         wc_printf("<table class=\"vcard_edit_background_alt\">");
1171         wc_printf("<tr><td>");
1172
1173         wc_printf("<table border=0><TR>"
1174                 "<td valign=top>");
1175         wc_printf(_("Primary Internet e-mail address"));
1176         wc_printf("<br>"
1177                 "<input type=\"text\" name=\"primary_inetemail\" "
1178                 "size=40 maxlength=60 value=\"");
1179         escputs(primary_inetemail);
1180         wc_printf("\"><br>"
1181                 "</td><td valign=top>");
1182         wc_printf(_("Internet e-mail aliases"));
1183         wc_printf("<br>"
1184                 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
1185         escputs(other_inetemail);
1186         wc_printf("</textarea></td></tr></table>\n");
1187
1188         wc_printf("</td></tr></table>\n");
1189
1190         wc_printf("<input type=\"hidden\" name=\"extrafields\" value=\"");
1191         escputs(extrafields);
1192         wc_printf("\">\n");
1193
1194         wc_printf("<input type=\"hidden\" name=\"return_to\" value=\"");
1195         escputs(return_to);
1196         wc_printf("\">\n");
1197
1198         wc_printf("<div class=\"buttons\">\n"
1199                 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
1200                 "&nbsp;"
1201                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
1202                 "</div></form>\n",
1203                 _("Save changes"),
1204                 _("Cancel")
1205         );
1206         
1207         wc_printf("</td></tr></table>\n");
1208         do_template("endbox", NULL);
1209         wDumpContent(1);
1210         if (Msg != NULL) {
1211                 DestroyMessageSummary(Msg);
1212         }
1213 }
1214
1215
1216 /*
1217  *  commit the edits to the citadel server
1218  */
1219 void edit_vcard(void) {
1220         long msgnum;
1221         char *partnum;
1222
1223         msgnum = lbstr("msgnum");
1224         partnum = bstr("partnum");
1225         do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
1226 }
1227
1228
1229
1230 /*
1231  *  parse edited vcard from the browser
1232  */
1233 void submit_vcard(void) {
1234         wcsession *WCC = WC;
1235         struct vCard *v;
1236         char *serialized_vcard;
1237         char buf[SIZ];
1238         StrBuf *Buf;
1239         int i;
1240
1241         if (!havebstr("ok_button")) { 
1242                 readloop(readnew, eUseDefault);
1243                 return;
1244         }
1245
1246         if (havebstr("force_room")) {
1247                 if (gotoroom(sbstr("force_room")) != 200) {
1248                         StrBufAppendBufPlain(WCC->ImportantMsg,
1249                                              _("Unable to enter the room to save your message"),
1250                                              -1, 0);
1251                         StrBufAppendBufPlain(WCC->ImportantMsg,
1252                                              HKEY(": "), 0);
1253                         StrBufAppendBuf(WCC->ImportantMsg, sbstr("force_room"), 0);
1254                         StrBufAppendBufPlain(WCC->ImportantMsg,
1255                                              HKEY("; "), 0);
1256                                                
1257                         StrBufAppendBufPlain(WCC->ImportantMsg,
1258                                              _("Aborting."),
1259                                              -1, 0);
1260
1261                         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1262                                 select_user_to_edit(NULL);
1263                         }
1264                         else if (!strcmp(bstr("return_to"), "do_welcome")) {
1265                                 do_welcome();
1266                         }
1267                         else if (!IsEmptyStr(bstr("return_to"))) {
1268                                 http_redirect(bstr("return_to"));
1269                         }
1270                         else {
1271                                 readloop(readnew, eUseDefault);
1272                         }
1273                         return;
1274                 }
1275         }
1276
1277         sprintf(buf, "ENT0 1|||4||");
1278         serv_puts(buf);
1279         serv_getln(buf, sizeof buf);
1280         if (buf[0] != '4') {
1281                 edit_vcard();
1282                 return;
1283         }
1284
1285         /* Make a vCard structure out of the data supplied in the form */
1286         Buf = NewStrBuf();
1287         StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1288                      bstr("extrafields")
1289         );
1290         v = VCardLoad(Buf);     /* Start with the extra fields */
1291         FreeStrBuf(&Buf);
1292         if (v == NULL) {
1293                 safestrncpy(WCC->ImportantMessage,
1294                         _("An error has occurred."),
1295                         sizeof WCC->ImportantMessage
1296                 );
1297                 edit_vcard();
1298                 return;
1299         }
1300
1301         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
1302                 bstr("lastname"),
1303                 bstr("firstname"),
1304                 bstr("middlename"),
1305                 bstr("prefix"),
1306                 bstr("suffix") );
1307         vcard_add_prop(v, "n", buf);
1308         
1309         vcard_add_prop(v, "title", bstr("title"));
1310         vcard_add_prop(v, "fn", bstr("fullname"));
1311         vcard_add_prop(v, "org", bstr("org"));
1312
1313         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
1314                 bstr("pobox"),
1315                 bstr("extadr"),
1316                 bstr("street"),
1317                 bstr("city"),
1318                 bstr("state"),
1319                 bstr("zipcode"),
1320                 bstr("country") );
1321         vcard_add_prop(v, "adr", buf);
1322
1323         vcard_add_prop(v, "tel;home", bstr("hometel"));
1324         vcard_add_prop(v, "tel;work", bstr("worktel"));
1325         vcard_add_prop(v, "tel;fax", bstr("faxtel"));
1326         vcard_add_prop(v, "tel;cell", bstr("mobiletel"));
1327         vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
1328
1329         for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
1330                 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
1331                 if (!IsEmptyStr(buf)) {
1332                         vcard_add_prop(v, "email;internet", buf);
1333                 }
1334         }
1335
1336         serialized_vcard = vcard_serialize(v);
1337         vcard_free(v);
1338         if (serialized_vcard == NULL) {
1339                 safestrncpy(WCC->ImportantMessage,
1340                         _("An error has occurred."),
1341                         sizeof WCC->ImportantMessage
1342                 );
1343                 edit_vcard();
1344                 return;
1345         }
1346
1347         serv_puts("Content-type: text/x-vcard; charset=UTF-8");
1348         serv_puts("");
1349         serv_printf("%s\r\n", serialized_vcard);
1350         serv_puts("000");
1351         free(serialized_vcard);
1352
1353         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1354                 select_user_to_edit(NULL);
1355         }
1356         else if (!strcmp(bstr("return_to"), "do_welcome")) {
1357                 do_welcome();
1358         }
1359         else if (!IsEmptyStr(bstr("return_to"))) {
1360                 http_redirect(bstr("return_to"));
1361         }
1362         else {
1363                 readloop(readnew, eUseDefault);
1364         }
1365 }
1366
1367
1368
1369 /*
1370  * Extract an embedded photo from a vCard for display on the client
1371  */
1372 void display_vcard_photo_img(void)
1373 {
1374         long msgnum = 0L;
1375         StrBuf *vcard;
1376         struct vCard *v;
1377         char *photosrc;
1378         const char *contentType;
1379         wcsession *WCC = WC;
1380
1381         msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1382         
1383         vcard = load_mimepart(msgnum,"1");
1384         v = VCardLoad(vcard);
1385         
1386         photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
1387         FlushStrBuf(WCC->WBuf);
1388         StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
1389         if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
1390                 FlushStrBuf(WCC->WBuf);
1391                 
1392                 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
1393                 output_headers(0, 0, 0, 0, 0, 0);
1394                 hprintf("Content-Type: text/plain\r\n");
1395                 begin_burst();
1396                 wc_printf(_("Could Not decode vcard photo\n"));
1397                 end_burst();
1398                 return;
1399         }
1400         contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1401         http_transmit_thing(contentType, 0);
1402         free(v);
1403         free(photosrc);
1404 }
1405
1406 typedef struct _vcardview_struct {
1407         long is_singlecard;
1408         addrbookent *addrbook;
1409         long num_ab;
1410
1411 } vcardview_struct;
1412
1413 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1414                                  void **ViewSpecific, 
1415                                  long oper, 
1416                                  char *cmd, 
1417                                  long len)
1418 {
1419         vcardview_struct *VS;
1420
1421         VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
1422         memset(VS, 0, sizeof(vcardview_struct));
1423         *ViewSpecific = (void*)VS;
1424
1425         VS->is_singlecard = ibstr("is_singlecard");
1426         if (VS->is_singlecard != 1) {
1427                 if (oper == do_search) {
1428                         snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
1429                 }
1430                 else {
1431                         strcpy(cmd, "MSGS ALL");
1432                 }
1433                 Stat->maxmsgs = 9999999;
1434         }
1435         return 200;
1436 }
1437
1438 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat, 
1439                             void **ViewSpecific, 
1440                             message_summary* Msg, 
1441                             int is_new, 
1442                             int i)
1443 {
1444         vcardview_struct *VS;
1445         char *ab_name;
1446
1447         VS = (vcardview_struct*) *ViewSpecific;
1448
1449         ab_name = NULL;
1450         fetch_ab_name(Msg, &ab_name);
1451         if (ab_name == NULL) 
1452                 return 0;
1453         ++VS->num_ab;
1454         VS->addrbook = realloc(VS->addrbook,
1455                                (sizeof(addrbookent) * VS->num_ab) );
1456         safestrncpy(VS->addrbook[VS->num_ab-1].ab_name, ab_name,
1457                     sizeof(VS->addrbook[VS->num_ab-1].ab_name));
1458         VS->addrbook[VS->num_ab-1].ab_msgnum = Msg->msgnum;
1459         free(ab_name);
1460         return 0;
1461 }
1462
1463
1464 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1465 {
1466         const StrBuf *Mime;
1467         vcardview_struct *VS;
1468
1469         VS = (vcardview_struct*) *ViewSpecific;
1470         if (VS->is_singlecard)
1471                 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1472         else
1473                 do_addrbook_view(VS->addrbook, VS->num_ab);     /* Render the address book */
1474         return 0;
1475 }
1476
1477 int vcard_Cleanup(void **ViewSpecific)
1478 {
1479         vcardview_struct *VS;
1480
1481         VS = (vcardview_struct*) *ViewSpecific;
1482         wDumpContent(1);
1483         if ((VS != NULL) && 
1484             (VS->addrbook != NULL))
1485                 free(VS->addrbook);
1486         if (VS != NULL) 
1487                 free(VS);
1488         return 0;
1489 }
1490
1491 void 
1492 ServerStartModule_VCARD
1493 (void)
1494 {
1495         VCToEnum = NewHash(0, NULL);
1496
1497 }
1498
1499 void 
1500 ServerShutdownModule_VCARD
1501 (void)
1502 {
1503         DeleteHash(&VCToEnum);
1504 }
1505
1506 void 
1507 InitModule_VCARD
1508 (void)
1509 {
1510         RegisterReadLoopHandlerset(
1511                 VIEW_ADDRESSBOOK,
1512                 vcard_GetParamsGetServerCall,
1513                 NULL,
1514                 NULL, 
1515                 vcard_LoadMsgFromServer,
1516                 vcard_RenderView_or_Tail,
1517                 vcard_Cleanup);
1518         WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1519         WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1520         WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1521
1522         Put(VCToEnum, HKEY("n"), (void*)VC_n, reference_free_handler);
1523         Put(VCToEnum, HKEY("fn"), (void*)VC_fn, reference_free_handler);
1524         Put(VCToEnum, HKEY("title"), (void*)VC_title, reference_free_handler);
1525         Put(VCToEnum, HKEY("org"), (void*)VC_org, reference_free_handler);
1526         Put(VCToEnum, HKEY("email"), (void*)VC_email, reference_free_handler);
1527         Put(VCToEnum, HKEY("tel"), (void*)VC_tel, reference_free_handler);
1528         Put(VCToEnum, HKEY("tel_tel"), (void*)VC_tel_tel, reference_free_handler);
1529         Put(VCToEnum, HKEY("tel_work"), (void*)VC_tel_work, reference_free_handler);
1530         Put(VCToEnum, HKEY("tel_home"), (void*)VC_tel_home, reference_free_handler);
1531         Put(VCToEnum, HKEY("tel_cell"), (void*)VC_tel_cell, reference_free_handler);
1532         Put(VCToEnum, HKEY("adr"), (void*)VC_adr, reference_free_handler);
1533         Put(VCToEnum, HKEY("photo"), (void*)VC_photo, reference_free_handler);
1534         Put(VCToEnum, HKEY("version"), (void*)VC_version, reference_free_handler);
1535         Put(VCToEnum, HKEY("rev"), (void*)VC_rev, reference_free_handler);
1536         Put(VCToEnum, HKEY("label"), (void*)VC_label, reference_free_handler);
1537
1538
1539         RegisterNamespace("VC", 1, 2, tmplput_VCARD_ITEM, NULL, CTX_VCARD);
1540
1541         REGISTERTokenParamDefine(VC_n);
1542         REGISTERTokenParamDefine(VC_fn);
1543         REGISTERTokenParamDefine(VC_title);
1544         REGISTERTokenParamDefine(VC_org);
1545         REGISTERTokenParamDefine(VC_email);
1546         REGISTERTokenParamDefine(VC_tel);
1547         REGISTERTokenParamDefine(VC_tel_tel);
1548         REGISTERTokenParamDefine(VC_tel_work);
1549         REGISTERTokenParamDefine(VC_tel_home);
1550         REGISTERTokenParamDefine(VC_tel_cell);
1551         REGISTERTokenParamDefine(VC_adr);
1552         REGISTERTokenParamDefine(VC_photo);
1553         REGISTERTokenParamDefine(VC_version);
1554         REGISTERTokenParamDefine(VC_rev);
1555         REGISTERTokenParamDefine(VC_label);
1556
1557 }
1558