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