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