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