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