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