950fae6f43671e43fb53a16449eacd4390554b48
[citadel.git] / webcit / vcard_edit.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15 #include "calendar.h"
16
17 CtxType CTX_VCARD = CTX_NONE;
18
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(CTX_VCARD);
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         wcsession *WCC = WC;
874         message_summary *Msg = NULL;
875         wc_mime_attachment *VCMime = NULL;
876         struct vCard *v;
877         int i;
878         char *key, *value;
879         char whatuser[256];
880
881         char lastname[256];
882         char firstname[256];
883         char middlename[256];
884         char prefix[256];
885         char suffix[256];
886         char pobox[256];
887         char extadr[256];
888         char street[256];
889         char city[256];
890         char state[256];
891         char zipcode[256];
892         char country[256];
893         char hometel[256];
894         char worktel[256];
895         char faxtel[256];
896         char mobiletel[256];
897         char primary_inetemail[256];
898         char other_inetemail[SIZ];
899         char extrafields[SIZ];
900         char fullname[256];
901         char title[256];
902         char org[256];
903
904         lastname[0] = 0;
905         firstname[0] = 0;
906         middlename[0] = 0;
907         prefix[0] = 0;
908         suffix[0] = 0;
909         pobox[0] = 0;
910         extadr[0] = 0;
911         street[0] = 0;
912         city[0] = 0;
913         state[0] = 0;
914         zipcode[0] = 0;
915         country[0] = 0;
916         hometel[0] = 0;
917         worktel[0] = 0;
918         faxtel[0] = 0;
919         mobiletel[0] = 0;
920         primary_inetemail[0] = 0;
921         other_inetemail[0] = 0;
922         title[0] = 0;
923         org[0] = 0;
924         extrafields[0] = 0;
925         fullname[0] = 0;
926
927         safestrncpy(whatuser, "", sizeof whatuser);
928
929         if ((msgnum >= 0) || 
930             ((VCMsg != NULL) && (VCAtt != NULL)))
931         {
932                 if ((VCMsg == NULL) && (VCAtt == NULL)) {
933
934                         Msg = (message_summary *) malloc(sizeof(message_summary));
935                         memset(Msg, 0, sizeof(message_summary));
936                         Msg->msgnum = msgnum;
937                         VCMime = load_vcard(Msg);
938                         if (VCMime == NULL) {
939                                 convenience_page("770000", _("Error"), "");///TODO: important message
940                                 DestroyMessageSummary(Msg);
941                                 return;
942                         }
943                 
944                         v = VCardLoad(VCMime->Data);
945                 }
946                 else {
947                         v = VCardLoad(VCAtt->Data);
948                 }
949         
950                 /* Populate the variables for our form */
951                 i = 0;
952                 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
953                         char prp[256];  /* property name */
954                         char prm[256];  /* parameters */
955
956                         value = vcard_get_prop(v, "", 0, i++, 0);
957
958
959                         extract_token(prp, key, 0, ';', sizeof prp);
960                         safestrncpy(prm, key, sizeof prm);
961                         remove_token(prm, 0, ';');
962
963                         if (!strcasecmp(prp, "n")) {
964                                 extract_token(lastname, value, 0, ';', sizeof lastname);
965                                 extract_token(firstname, value, 1, ';', sizeof firstname);
966                                 extract_token(middlename, value, 2, ';', sizeof middlename);
967                                 extract_token(prefix, value, 3, ';', sizeof prefix);
968                                 extract_token(suffix, value, 4, ';', sizeof suffix);
969                         }
970
971                         else if (!strcasecmp(prp, "fn")) {
972                                 safestrncpy(fullname, value, sizeof fullname);
973                         }
974
975                         else if (!strcasecmp(prp, "title")) {
976                                 safestrncpy(title, value, sizeof title);
977                         }
978         
979                         else if (!strcasecmp(prp, "org")) {
980                                 safestrncpy(org, value, sizeof org);
981                         }
982         
983                         else if (!strcasecmp(prp, "adr")) {
984                                 extract_token(pobox, value, 0, ';', sizeof pobox);
985                                 extract_token(extadr, value, 1, ';', sizeof extadr);
986                                 extract_token(street, value, 2, ';', sizeof street);
987                                 extract_token(city, value, 3, ';', sizeof city);
988                                 extract_token(state, value, 4, ';', sizeof state);
989                                 extract_token(zipcode, value, 5, ';', sizeof zipcode);
990                                 extract_token(country, value, 6, ';', sizeof country);
991                         }
992
993                         else if (!strcasecmp(prp, "tel")) {
994
995                                 if (bmstrcasestr(prm, "home")) {
996                                         extract_token(hometel, value, 0, ';', sizeof hometel);
997                                 }
998                                 else if (bmstrcasestr(prm, "work")) {
999                                         extract_token(worktel, value, 0, ';', sizeof worktel);
1000                                 }
1001                                 else if (bmstrcasestr(prm, "fax")) {
1002                                         extract_token(faxtel, value, 0, ';', sizeof faxtel);
1003                                 }
1004                                 else if (bmstrcasestr(prm, "cell")) {
1005                                         extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
1006                                 }
1007                                 else {  /* Missing or unknown type; put it in the home phone */
1008                                         extract_token(hometel, value, 0, ';', sizeof hometel);
1009                                 }
1010                         }
1011         
1012                         else if ( (!strcasecmp(prp, "email")) && (bmstrcasestr(prm, "internet")) ) {
1013                                 if (primary_inetemail[0] == 0) {
1014                                         safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
1015                                 }
1016                                 else {
1017                                         if (other_inetemail[0] != 0) {
1018                                                 strcat(other_inetemail, "\n");
1019                                         }
1020                                         strcat(other_inetemail, value);
1021                                 }
1022                         }
1023
1024                         /* Unrecognized properties are preserved here so we don't discard them
1025                          * just because the vCard was edited with WebCit.
1026                          */
1027                         else {
1028                                 strcat(extrafields, key);
1029                                 strcat(extrafields, ":");
1030                                 strcat(extrafields, value);
1031                                 strcat(extrafields, "\n");
1032                         }
1033         
1034                 }
1035         
1036                 vcard_free(v);
1037         }
1038
1039         /* Display the form */
1040         output_headers(1, 1, 1, 0, 0, 0);
1041
1042         do_template("box_begin_1");
1043         StrBufAppendBufPlain(WC->WBuf, _("Edit contact information"), -1, 0);
1044         do_template("box_begin_2");
1045
1046         wc_printf("<form method=\"POST\" action=\"submit_vcard\">\n");
1047         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1048
1049         if (force_room != NULL) {
1050                 wc_printf("<input type=\"hidden\" name=\"force_room\" value=\"");
1051                 escputs(force_room);
1052                 wc_printf("\">\n");
1053         }
1054         else
1055         {
1056                 wc_printf("<input type=\"hidden\" name=\"go\" value=\"");
1057                 StrEscAppend(WCC->WBuf, WCC->CurRoom.name, NULL, 0, 0);
1058                 wc_printf("\">\n");
1059         }
1060
1061         wc_printf("<table class=\"vcard_edit_background\"><tr><td>\n");
1062
1063         wc_printf("<table border=\"0\"><tr>"
1064                 "<td>%s</td>"
1065                 "<td>%s</td>"
1066                 "<td>%s</td>"
1067                 "<td>%s</td>"
1068                 "<td>%s</td></tr>\n",
1069                 _("Prefix"), _("First Name"), _("Middle Name"), _("Last Name"), _("Suffix")
1070         );
1071         wc_printf("<tr><td><input type=\"text\" name=\"prefix\" "
1072                 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
1073                 prefix);
1074         wc_printf("<td><input type=\"text\" name=\"firstname\" "
1075                 "value=\"%s\" maxlength=\"29\"></td>",
1076                 firstname);
1077         wc_printf("<td><input type=\"text\" name=\"middlename\" "
1078                 "value=\"%s\" maxlength=\"29\"></td>",
1079                 middlename);
1080         wc_printf("<td><input type=\"text\" name=\"lastname\" "
1081                 "value=\"%s\" maxlength=\"29\"></td>",
1082                 lastname);
1083         wc_printf("<td><input type=\"text\" name=\"suffix\" "
1084                 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
1085                 suffix);
1086
1087         wc_printf("<table  class=\"vcard_edit_background_alt\">");
1088         wc_printf("<tr><td>");
1089
1090         wc_printf(_("Display name:"));
1091         wc_printf("<br>"
1092                 "<input type=\"text\" name=\"fullname\" "
1093                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1094                 fullname
1095         );
1096
1097         wc_printf(_("Title:"));
1098         wc_printf("<br>"
1099                 "<input type=\"text\" name=\"title\" "
1100                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1101                 title
1102         );
1103
1104         wc_printf(_("Organization:"));
1105         wc_printf("<br>"
1106                 "<input type=\"text\" name=\"org\" "
1107                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1108                 org
1109         );
1110
1111         wc_printf("</td><td>");
1112
1113         wc_printf("<table border=\"0\">");
1114         wc_printf("<tr><td>");
1115         wc_printf(_("PO box:"));
1116         wc_printf("</td><td>"
1117                 "<input type=\"text\" name=\"pobox\" "
1118                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1119                 pobox);
1120         wc_printf("<tr><td>");
1121         wc_printf(_("Address:"));
1122         wc_printf("</td><td>"
1123                 "<input type=\"text\" name=\"extadr\" "
1124                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1125                 extadr);
1126         wc_printf("<tr><td> </td><td>"
1127                 "<input type=\"text\" name=\"street\" "
1128                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1129                 street);
1130         wc_printf("<tr><td>");
1131         wc_printf(_("City:"));
1132         wc_printf("</td><td>"
1133                 "<input type=\"text\" name=\"city\" "
1134                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1135                 city);
1136         wc_printf("<tr><td>");
1137         wc_printf(_("State:"));
1138         wc_printf("</td><td>"
1139                 "<input type=\"text\" name=\"state\" "
1140                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1141                 state);
1142         wc_printf("<tr><td>");
1143         wc_printf(_("ZIP code:"));
1144         wc_printf("</td><td>"
1145                 "<input type=\"text\" name=\"zipcode\" "
1146                 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
1147                 zipcode);
1148         wc_printf("<tr><td>");
1149         wc_printf(_("Country:"));
1150         wc_printf("</td><td>"
1151                 "<input type=\"text\" name=\"country\" "
1152                 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
1153                 country);
1154         wc_printf("</table>\n");
1155
1156         wc_printf("</table>\n");
1157
1158         wc_printf("<table border=0><tr><td>");
1159         wc_printf(_("Home telephone:"));
1160         wc_printf("</td>"
1161                 "<td><input type=\"text\" name=\"hometel\" "
1162                 "value=\"%s\" maxlength=\"29\"></td>\n",
1163                 hometel);
1164         wc_printf("<td>");
1165         wc_printf(_("Work telephone:"));
1166         wc_printf("</td>"
1167                 "<td><input type=\"text\" name=\"worktel\" "
1168                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1169                 worktel);
1170         wc_printf("<tr><td>");
1171         wc_printf(_("Mobile telephone:"));
1172         wc_printf("</td>"
1173                 "<td><input type=\"text\" name=\"mobiletel\" "
1174                 "value=\"%s\" maxlength=\"29\"></td>\n",
1175                 mobiletel);
1176         wc_printf("<td>");
1177         wc_printf(_("Fax number:"));
1178         wc_printf("</td>"
1179                 "<td><input type=\"text\" name=\"faxtel\" "
1180                 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
1181                 faxtel);
1182
1183         wc_printf("<table class=\"vcard_edit_background_alt\">");
1184         wc_printf("<tr><td>");
1185
1186         wc_printf("<table border=0><TR>"
1187                 "<td valign=top>");
1188         wc_printf(_("Primary Internet e-mail address"));
1189         wc_printf("<br>"
1190                 "<input type=\"text\" name=\"primary_inetemail\" "
1191                 "size=40 maxlength=60 value=\"");
1192         escputs(primary_inetemail);
1193         wc_printf("\"><br>"
1194                 "</td><td valign=top>");
1195         wc_printf(_("Internet e-mail aliases"));
1196         wc_printf("<br>"
1197                 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
1198         escputs(other_inetemail);
1199         wc_printf("</textarea></td></tr></table>\n");
1200
1201         wc_printf("</td></tr></table>\n");
1202
1203         wc_printf("<input type=\"hidden\" name=\"extrafields\" value=\"");
1204         escputs(extrafields);
1205         wc_printf("\">\n");
1206
1207         wc_printf("<input type=\"hidden\" name=\"return_to\" value=\"");
1208         escputs(return_to);
1209         wc_printf("\">\n");
1210
1211         wc_printf("<div class=\"buttons\">\n"
1212                 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
1213                 "&nbsp;"
1214                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
1215                 "</div></form>\n",
1216                 _("Save changes"),
1217                 _("Cancel")
1218         );
1219         
1220         wc_printf("</td></tr></table>\n");
1221         do_template("box_end");
1222         wDumpContent(1);
1223         if (Msg != NULL) {
1224                 DestroyMessageSummary(Msg);
1225         }
1226 }
1227
1228
1229 /*
1230  *  commit the edits to the citadel server
1231  */
1232 void edit_vcard(void) {
1233         long msgnum;
1234         char *partnum;
1235
1236         msgnum = lbstr("msgnum");
1237         partnum = bstr("partnum");
1238         do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
1239 }
1240
1241
1242
1243 /*
1244  *  parse edited vcard from the browser
1245  */
1246 void submit_vcard(void) {
1247         struct vCard *v;
1248         char *serialized_vcard;
1249         char buf[SIZ];
1250         StrBuf *Buf;
1251         const StrBuf *ForceRoom;
1252         int i;
1253
1254         if (!havebstr("ok_button")) { 
1255                 readloop(readnew, eUseDefault);
1256                 return;
1257         }
1258
1259         if (havebstr("force_room")) {
1260                 ForceRoom = sbstr("force_room");
1261                 if (gotoroom(ForceRoom) != 200) {
1262                         AppendImportantMessage(_("Unable to enter the room to save your message"), -1);
1263                         AppendImportantMessage(HKEY(": "));
1264                         AppendImportantMessage(SKEY(ForceRoom));
1265                         AppendImportantMessage(HKEY("; "));
1266                         AppendImportantMessage(_("Aborting."), -1);
1267
1268                         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1269                                 select_user_to_edit(NULL);
1270                         }
1271                         else if (!strcmp(bstr("return_to"), "do_welcome")) {
1272                                 do_welcome();
1273                         }
1274                         else if (!IsEmptyStr(bstr("return_to"))) {
1275                                 http_redirect(bstr("return_to"));
1276                         }
1277                         else {
1278                                 readloop(readnew, eUseDefault);
1279                         }
1280                         return;
1281                 }
1282         }
1283
1284         Buf = NewStrBuf();
1285         serv_write(HKEY("ENT0 1|||4\n"));
1286         if (!StrBuf_ServGetln(Buf) && (GetServerStatus(Buf, NULL) != 4))
1287         {
1288                 edit_vcard();
1289                 return;
1290         }
1291
1292         /* Make a vCard structure out of the data supplied in the form */
1293         StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1294                      bstr("extrafields")
1295         );
1296         v = VCardLoad(Buf);     /* Start with the extra fields */
1297         if (v == NULL) {
1298                 AppendImportantMessage(_("An error has occurred."), -1);
1299                 edit_vcard();
1300                 FreeStrBuf(&Buf);
1301                 return;
1302         }
1303
1304         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
1305                 bstr("lastname"),
1306                 bstr("firstname"),
1307                 bstr("middlename"),
1308                 bstr("prefix"),
1309                 bstr("suffix") );
1310         vcard_add_prop(v, "n", buf);
1311         
1312         vcard_add_prop(v, "title", bstr("title"));
1313         vcard_add_prop(v, "fn", bstr("fullname"));
1314         vcard_add_prop(v, "org", bstr("org"));
1315
1316         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
1317                 bstr("pobox"),
1318                 bstr("extadr"),
1319                 bstr("street"),
1320                 bstr("city"),
1321                 bstr("state"),
1322                 bstr("zipcode"),
1323                 bstr("country") );
1324         vcard_add_prop(v, "adr", buf);
1325
1326         vcard_add_prop(v, "tel;home", bstr("hometel"));
1327         vcard_add_prop(v, "tel;work", bstr("worktel"));
1328         vcard_add_prop(v, "tel;fax", bstr("faxtel"));
1329         vcard_add_prop(v, "tel;cell", bstr("mobiletel"));
1330         vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
1331
1332         for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
1333                 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
1334                 if (!IsEmptyStr(buf)) {
1335                         vcard_add_prop(v, "email;internet", buf);
1336                 }
1337         }
1338
1339         serialized_vcard = vcard_serialize(v);
1340         vcard_free(v);
1341         if (serialized_vcard == NULL) {
1342                 AppendImportantMessage(_("An error has occurred."), -1);
1343                 edit_vcard();
1344                 FreeStrBuf(&Buf);
1345                 return;
1346         }
1347
1348         serv_write(HKEY("Content-type: text/x-vcard; charset=UTF-8\n"));
1349         serv_write(HKEY("\n"));
1350         serv_printf("%s\r\n", serialized_vcard);
1351         serv_write(HKEY("000\n"));
1352         free(serialized_vcard);
1353
1354         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1355                 select_user_to_edit(NULL);
1356         }
1357         else if (!strcmp(bstr("return_to"), "do_welcome")) {
1358                 do_welcome();
1359         }
1360         else if (!IsEmptyStr(bstr("return_to"))) {
1361                 http_redirect(bstr("return_to"));
1362         }
1363         else {
1364                 readloop(readnew, eUseDefault);
1365         }
1366         FreeStrBuf(&Buf);
1367 }
1368
1369
1370
1371 /*
1372  * Extract an embedded photo from a vCard for display on the client
1373  */
1374 void display_vcard_photo_img(void)
1375 {
1376         long msgnum = 0L;
1377         StrBuf *vcard;
1378         struct vCard *v;
1379         char *photosrc;
1380         const char *contentType;
1381         wcsession *WCC = WC;
1382
1383         msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1384         
1385         vcard = load_mimepart(msgnum,"1");
1386         v = VCardLoad(vcard);
1387         
1388         photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
1389         FlushStrBuf(WCC->WBuf);
1390         StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
1391         if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
1392                 FlushStrBuf(WCC->WBuf);
1393                 
1394                 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
1395                 output_headers(0, 0, 0, 0, 0, 0);
1396                 hprintf("Content-Type: text/plain\r\n");
1397                 begin_burst();
1398                 wc_printf(_("Could Not decode vcard photo\n"));
1399                 end_burst();
1400                 return;
1401         }
1402         contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1403         http_transmit_thing(contentType, 0);
1404         free(v);
1405         free(photosrc);
1406 }
1407
1408 typedef struct _vcardview_struct {
1409         long is_singlecard;
1410         addrbookent *addrbook;
1411         long num_ab;
1412
1413 } vcardview_struct;
1414
1415 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1416                                  void **ViewSpecific, 
1417                                  long oper, 
1418                                  char *cmd, 
1419                                  long len,
1420                                  char *filter,
1421                                  long flen)
1422 {
1423         vcardview_struct *VS;
1424
1425         VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
1426         memset(VS, 0, sizeof(vcardview_struct));
1427         *ViewSpecific = (void*)VS;
1428
1429         VS->is_singlecard = ibstr("is_singlecard");
1430         if (VS->is_singlecard != 1) {
1431                 if (oper == do_search) {
1432                         snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
1433                 }
1434                 else {
1435                         strcpy(cmd, "MSGS ALL");
1436                 }
1437                 Stat->maxmsgs = 9999999;
1438         }
1439         return 200;
1440 }
1441
1442 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat, 
1443                             void **ViewSpecific, 
1444                             message_summary* Msg, 
1445                             int is_new, 
1446                             int i)
1447 {
1448         vcardview_struct *VS;
1449         char *ab_name;
1450
1451         VS = (vcardview_struct*) *ViewSpecific;
1452
1453         ab_name = NULL;
1454         fetch_ab_name(Msg, &ab_name);
1455         if (ab_name == NULL) 
1456                 return 0;
1457         ++VS->num_ab;
1458         VS->addrbook = realloc(VS->addrbook,
1459                                (sizeof(addrbookent) * VS->num_ab) );
1460         safestrncpy(VS->addrbook[VS->num_ab-1].ab_name, ab_name,
1461                     sizeof(VS->addrbook[VS->num_ab-1].ab_name));
1462         VS->addrbook[VS->num_ab-1].ab_msgnum = Msg->msgnum;
1463         free(ab_name);
1464         return 0;
1465 }
1466
1467
1468 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1469 {
1470         const StrBuf *Mime;
1471         vcardview_struct *VS;
1472
1473         VS = (vcardview_struct*) *ViewSpecific;
1474         if (VS->is_singlecard)
1475                 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1476         else
1477                 do_addrbook_view(VS->addrbook, VS->num_ab);     /* Render the address book */
1478         return 0;
1479 }
1480
1481 int vcard_Cleanup(void **ViewSpecific)
1482 {
1483         vcardview_struct *VS;
1484
1485         VS = (vcardview_struct*) *ViewSpecific;
1486         wDumpContent(1);
1487         if ((VS != NULL) && 
1488             (VS->addrbook != NULL))
1489                 free(VS->addrbook);
1490         if (VS != NULL) 
1491                 free(VS);
1492         return 0;
1493 }
1494
1495 void 
1496 ServerStartModule_VCARD
1497 (void)
1498 {
1499         VCToEnum = NewHash(0, NULL);
1500
1501 }
1502
1503 void 
1504 ServerShutdownModule_VCARD
1505 (void)
1506 {
1507         DeleteHash(&VCToEnum);
1508 }
1509
1510 void 
1511 InitModule_VCARD
1512 (void)
1513 {
1514         RegisterCTX(CTX_VCARD);
1515         RegisterReadLoopHandlerset(
1516                 VIEW_ADDRESSBOOK,
1517                 vcard_GetParamsGetServerCall,
1518                 NULL,
1519                 NULL,
1520                 NULL, 
1521                 vcard_LoadMsgFromServer,
1522                 vcard_RenderView_or_Tail,
1523                 vcard_Cleanup);
1524         WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1525         WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1526         WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1527
1528         Put(VCToEnum, HKEY("n"), (void*)VC_n, reference_free_handler);
1529         Put(VCToEnum, HKEY("fn"), (void*)VC_fn, reference_free_handler);
1530         Put(VCToEnum, HKEY("title"), (void*)VC_title, reference_free_handler);
1531         Put(VCToEnum, HKEY("org"), (void*)VC_org, reference_free_handler);
1532         Put(VCToEnum, HKEY("email"), (void*)VC_email, reference_free_handler);
1533         Put(VCToEnum, HKEY("tel"), (void*)VC_tel, reference_free_handler);
1534         Put(VCToEnum, HKEY("tel_tel"), (void*)VC_tel_tel, reference_free_handler);
1535         Put(VCToEnum, HKEY("tel_work"), (void*)VC_tel_work, reference_free_handler);
1536         Put(VCToEnum, HKEY("tel_home"), (void*)VC_tel_home, reference_free_handler);
1537         Put(VCToEnum, HKEY("tel_cell"), (void*)VC_tel_cell, reference_free_handler);
1538         Put(VCToEnum, HKEY("adr"), (void*)VC_adr, reference_free_handler);
1539         Put(VCToEnum, HKEY("photo"), (void*)VC_photo, reference_free_handler);
1540         Put(VCToEnum, HKEY("version"), (void*)VC_version, reference_free_handler);
1541         Put(VCToEnum, HKEY("rev"), (void*)VC_rev, reference_free_handler);
1542         Put(VCToEnum, HKEY("label"), (void*)VC_label, reference_free_handler);
1543
1544
1545         RegisterNamespace("VC", 1, 2, tmplput_VCARD_ITEM, NULL, CTX_VCARD);
1546
1547         REGISTERTokenParamDefine(VC_n);
1548         REGISTERTokenParamDefine(VC_fn);
1549         REGISTERTokenParamDefine(VC_title);
1550         REGISTERTokenParamDefine(VC_org);
1551         REGISTERTokenParamDefine(VC_email);
1552         REGISTERTokenParamDefine(VC_tel);
1553         REGISTERTokenParamDefine(VC_tel_tel);
1554         REGISTERTokenParamDefine(VC_tel_work);
1555         REGISTERTokenParamDefine(VC_tel_home);
1556         REGISTERTokenParamDefine(VC_tel_cell);
1557         REGISTERTokenParamDefine(VC_adr);
1558         REGISTERTokenParamDefine(VC_photo);
1559         REGISTERTokenParamDefine(VC_version);
1560         REGISTERTokenParamDefine(VC_rev);
1561         REGISTERTokenParamDefine(VC_label);
1562
1563 }
1564