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