* fix memleak in preferences
[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         StrBuf *Buf;
507         StrBuf *Buf2;
508         char this_alpha = 0;
509
510         v = vcard_load((char*)vcard_source); ///TODO
511
512         if (v == NULL) return;
513
514         name = vcard_get_prop(v, "n", 1, 0, 0);
515         if (name != NULL) {
516                 Buf = NewStrBufPlain(name, -1);
517                 Buf2 = NewStrBufPlain(NULL, StrLength(Buf));
518                 StrBuf_RFC822_to_Utf8(Buf2, Buf, WC->DefaultCharset, NULL);
519                 this_alpha = ChrPtr(Buf)[0];
520                 FreeStrBuf(&Buf);
521                 FreeStrBuf(&Buf2);
522         }
523
524         if (storename != NULL) {
525                 fetchname_parsed_vcard(v, storename);
526         }
527         else if (       (alpha == 0)
528                         || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
529                         || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
530                 ) {
531                 display_parsed_vcard(Target, v, full,msgnum);
532         }
533
534         vcard_free(v);
535 }
536
537
538
539 /**
540  * \brief Render the address book using info we gathered during the scan
541  * \param addrbook the addressbook to render
542  * \param num_ab the number of the addressbook
543  */
544 void do_addrbook_view(addrbookent *addrbook, int num_ab) {
545         int i = 0;
546         int displayed = 0;
547         int bg = 0;
548         static int NAMESPERPAGE = 60;
549         int num_pages = 0;
550         int tabfirst = 0;
551         char tabfirst_label[64];
552         int tablast = 0;
553         char tablast_label[64];
554         char this_tablabel[64];
555         int page = 0;
556         char **tablabels;
557
558         if (num_ab == 0) {
559                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
560                 wprintf(_("This address book is empty."));
561                 wprintf("</i></div>\n");
562                 return;
563         }
564
565         if (num_ab > 1) {
566                 qsort(addrbook, num_ab, sizeof(addrbookent), abcmp);
567         }
568
569         num_pages = (num_ab / NAMESPERPAGE) + 1;
570
571         tablabels = malloc(num_pages * sizeof (char *));
572         if (tablabels == NULL) {
573                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
574                 wprintf(_("An internal error has occurred."));
575                 wprintf("</i></div>\n");
576                 return;
577         }
578
579         for (i=0; i<num_pages; ++i) {
580                 tabfirst = i * NAMESPERPAGE;
581                 tablast = tabfirst + NAMESPERPAGE - 1;
582                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
583                 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
584                 nametab(tablast_label, 64, addrbook[tablast].ab_name);
585                 sprintf(this_tablabel, "%s&nbsp;-&nbsp;%s", tabfirst_label, tablast_label);
586                 tablabels[i] = strdup(this_tablabel);
587         }
588
589         tabbed_dialog(num_pages, tablabels);
590         page = (-1);
591
592         for (i=0; i<num_ab; ++i) {
593
594                 if ((i / NAMESPERPAGE) != page) {       /* New tab */
595                         page = (i / NAMESPERPAGE);
596                         if (page > 0) {
597                                 wprintf("</tr></table>\n");
598                                 end_tab(page-1, num_pages);
599                         }
600                         begin_tab(page, num_pages);
601                         wprintf("<table border=0 cellspacing=0 cellpadding=3 width=100%%>\n");
602                         displayed = 0;
603                 }
604
605                 if ((displayed % 4) == 0) {
606                         if (displayed > 0) {
607                                 wprintf("</tr>\n");
608                         }
609                         bg = 1 - bg;
610                         wprintf("<tr bgcolor=\"#%s\">",
611                                 (bg ? "DDDDDD" : "FFFFFF")
612                         );
613                 }
614         
615                 wprintf("<td>");
616
617                 wprintf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
618                         addrbook[i].ab_msgnum);
619                 wprintf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
620                 vcard_n_prettyize(addrbook[i].ab_name);
621                 escputs(addrbook[i].ab_name);
622                 wprintf("</a></td>\n");
623                 ++displayed;
624         }
625
626         /* Placeholders for empty columns at end */
627         if ((num_ab % 4) != 0) {
628                 for (i=0; i<(4-(num_ab % 4)); ++i) {
629                         wprintf("<td>&nbsp;</td>");
630                 }
631         }
632
633         wprintf("</tr></table>\n");
634         end_tab((num_pages-1), num_pages);
635
636         begin_tab(num_pages, num_pages);
637         /* FIXME there ought to be something here */
638         end_tab(num_pages, num_pages);
639
640         for (i=0; i<num_pages; ++i) {
641                 free(tablabels[i]);
642         }
643         free(tablabels);
644 }
645
646
647
648
649 /*
650  * Edit the vCard component of a MIME message.  
651  * Supply the message number
652  * and MIME part number to fetch.  Or, specify -1 for the message number
653  * to start with a blank card.
654  */
655 void do_edit_vcard(long msgnum, char *partnum, char *return_to, const char *force_room) {
656         char buf[SIZ];
657         char *serialized_vcard = NULL;
658         size_t total_len = 0;
659         struct vCard *v;
660         int i;
661         char *key, *value;
662         char whatuser[256];
663
664         char lastname[256];
665         char firstname[256];
666         char middlename[256];
667         char prefix[256];
668         char suffix[256];
669         char pobox[256];
670         char extadr[256];
671         char street[256];
672         char city[256];
673         char state[256];
674         char zipcode[256];
675         char country[256];
676         char hometel[256];
677         char worktel[256];
678         char faxtel[256];
679         char mobiletel[256];
680         char primary_inetemail[256];
681         char other_inetemail[SIZ];
682         char extrafields[SIZ];
683         char fullname[256];
684         char title[256];
685         char org[256];
686
687         lastname[0] = 0;
688         firstname[0] = 0;
689         middlename[0] = 0;
690         prefix[0] = 0;
691         suffix[0] = 0;
692         pobox[0] = 0;
693         extadr[0] = 0;
694         street[0] = 0;
695         city[0] = 0;
696         state[0] = 0;
697         zipcode[0] = 0;
698         country[0] = 0;
699         hometel[0] = 0;
700         worktel[0] = 0;
701         faxtel[0] = 0;
702         mobiletel[0] = 0;
703         primary_inetemail[0] = 0;
704         other_inetemail[0] = 0;
705         title[0] = 0;
706         org[0] = 0;
707         extrafields[0] = 0;
708         fullname[0] = 0;
709
710         safestrncpy(whatuser, "", sizeof whatuser);
711
712         if (msgnum >= 0) {
713                 sprintf(buf, "MSG0 %ld|1", msgnum);
714                 serv_puts(buf);
715                 serv_getln(buf, sizeof buf);
716                 if (buf[0] != '1') {
717                         convenience_page("770000", _("Error"), &buf[4]);
718                         return;
719                 }
720                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
721                         if (!strncasecmp(buf, "from=", 5)) {
722                                 safestrncpy(whatuser, &buf[5], sizeof whatuser);
723                         }
724                         else if (!strncasecmp(buf, "node=", 5)) {
725                                 strcat(whatuser, " @ ");
726                                 strcat(whatuser, &buf[5]);
727                         }
728                 }
729         
730                 sprintf(buf, "DLAT %ld|%s", msgnum, partnum);
731                 serv_puts(buf);
732                 serv_getln(buf, sizeof buf);
733                 if (buf[0] != '6') {
734                         convenience_page("770000", "Error", &buf[4]);
735                         return;
736                 }
737         
738                 total_len = atoi(&buf[4]);
739                 serialized_vcard = malloc(total_len + 2);
740
741                 serv_read(serialized_vcard, total_len);
742                 serialized_vcard[total_len] = 0;
743         
744                 v = vcard_load(serialized_vcard);
745                 free(serialized_vcard);
746         
747                 /* Populate the variables for our form */
748                 i = 0;
749                 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
750                         value = vcard_get_prop(v, "", 0, i++, 0);
751         
752                         if (!strcasecmp(key, "n")) {
753                                 extract_token(lastname, value, 0, ';', sizeof lastname);
754                                 extract_token(firstname, value, 1, ';', sizeof firstname);
755                                 extract_token(middlename, value, 2, ';', sizeof middlename);
756                                 extract_token(prefix, value, 3, ';', sizeof prefix);
757                                 extract_token(suffix, value, 4, ';', sizeof suffix);
758                         }
759
760                         else if (!strcasecmp(key, "fn")) {
761                                 safestrncpy(fullname, value, sizeof fullname);
762                         }
763
764                         else if (!strcasecmp(key, "title")) {
765                                 safestrncpy(title, value, sizeof title);
766                         }
767         
768                         else if (!strcasecmp(key, "org")) {
769                                 safestrncpy(org, value, sizeof org);
770                         }
771         
772                         else if ( (!strcasecmp(key, "adr")) || (!strncasecmp(key, "adr;", 4)) ) {
773                                 extract_token(pobox, value, 0, ';', sizeof pobox);
774                                 extract_token(extadr, value, 1, ';', sizeof extadr);
775                                 extract_token(street, value, 2, ';', sizeof street);
776                                 extract_token(city, value, 3, ';', sizeof city);
777                                 extract_token(state, value, 4, ';', sizeof state);
778                                 extract_token(zipcode, value, 5, ';', sizeof zipcode);
779                                 extract_token(country, value, 6, ';', sizeof country);
780                         }
781         
782                         else if ( (!strcasecmp(key, "tel;home")) || (!strcasecmp(key, "tel;type=home")) ) {
783                                 extract_token(hometel, value, 0, ';', sizeof hometel);
784                         }
785         
786                         else if ( (!strcasecmp(key, "tel;work")) || (!strcasecmp(key, "tel;type=work")) ) {
787                                 extract_token(worktel, value, 0, ';', sizeof worktel);
788                         }
789         
790                         else if ( (!strcasecmp(key, "tel;fax")) || (!strcasecmp(key, "tel;type=fax")) ) {
791                                 extract_token(faxtel, value, 0, ';', sizeof faxtel);
792                         }
793         
794                         else if ( (!strcasecmp(key, "tel;cell")) || (!strcasecmp(key, "tel;type=cell")) ) {
795                                 extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
796                         }
797         
798                         else if ( (!strcasecmp(key, "email;internet"))
799                              || (!strcasecmp(key, "email;type=internet")) ) {
800                                 if (primary_inetemail[0] == 0) {
801                                         safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
802                                 }
803                                 else {
804                                         if (other_inetemail[0] != 0) {
805                                                 strcat(other_inetemail, "\n");
806                                         }
807                                         strcat(other_inetemail, value);
808                                 }
809                         }
810         
811                         else {
812                                 strcat(extrafields, key);
813                                 strcat(extrafields, ":");
814                                 strcat(extrafields, value);
815                                 strcat(extrafields, "\n");
816                         }
817         
818                 }
819         
820                 vcard_free(v);
821         }
822
823         /** Display the form */
824         output_headers(1, 1, 1, 0, 0, 0);
825
826         svput("BOXTITLE", WCS_STRING, _("Edit contact information"));
827         do_template("beginboxx", NULL);
828
829         wprintf("<form method=\"POST\" action=\"submit_vcard\">\n");
830         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
831
832         if (force_room != NULL) {
833                 wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
834                 escputs(force_room);
835                 wprintf("\">\n");
836         }
837
838         wprintf("<div class=\"fix_scrollbar_bug\">"
839                 "<table class=\"vcard_edit_background\"><tr><td>\n");
840
841         wprintf("<table border=0><tr>"
842                 "<td>%s</td>"
843                 "<td>%s</td>"
844                 "<td>%s</td>"
845                 "<td>%s</td>"
846                 "<td>%s</td></tr>\n",
847                 _("Prefix"), _("First"), _("Middle"), _("Last"), _("Suffix")
848         );
849         wprintf("<tr><td><input type=\"text\" name=\"prefix\" "
850                 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
851                 prefix);
852         wprintf("<td><input type=\"text\" name=\"firstname\" "
853                 "value=\"%s\" maxlength=\"29\"></td>",
854                 firstname);
855         wprintf("<td><input type=\"text\" name=\"middlename\" "
856                 "value=\"%s\" maxlength=\"29\"></td>",
857                 middlename);
858         wprintf("<td><input type=\"text\" name=\"lastname\" "
859                 "value=\"%s\" maxlength=\"29\"></td>",
860                 lastname);
861         wprintf("<td><input type=\"text\" name=\"suffix\" "
862                 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
863                 suffix);
864
865         wprintf("<table  class=\"vcard_edit_background_alt\">");
866         wprintf("<tr><td>");
867
868         wprintf(_("Display name:"));
869         wprintf("<br>"
870                 "<input type=\"text\" name=\"fullname\" "
871                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
872                 fullname
873         );
874
875         wprintf(_("Title:"));
876         wprintf("<br>"
877                 "<input type=\"text\" name=\"title\" "
878                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
879                 title
880         );
881
882         wprintf(_("Organization:"));
883         wprintf("<br>"
884                 "<input type=\"text\" name=\"org\" "
885                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
886                 org
887         );
888
889         wprintf("</td><td>");
890
891         wprintf("<table border=0>");
892         wprintf("<tr><td>");
893         wprintf(_("PO box:"));
894         wprintf("</td><td>"
895                 "<input type=\"text\" name=\"pobox\" "
896                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
897                 pobox);
898         wprintf("<tr><td>");
899         wprintf(_("Address:"));
900         wprintf("</td><td>"
901                 "<input type=\"text\" name=\"extadr\" "
902                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
903                 extadr);
904         wprintf("<tr><td> </td><td>"
905                 "<input type=\"text\" name=\"street\" "
906                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
907                 street);
908         wprintf("<tr><td>");
909         wprintf(_("City:"));
910         wprintf("</td><td>"
911                 "<input type=\"text\" name=\"city\" "
912                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
913                 city);
914         wprintf("<tr><td>");
915         wprintf(_("State:"));
916         wprintf("</td><td>"
917                 "<input type=\"text\" name=\"state\" "
918                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
919                 state);
920         wprintf("<tr><td>");
921         wprintf(_("ZIP code:"));
922         wprintf("</td><td>"
923                 "<input type=\"text\" name=\"zipcode\" "
924                 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
925                 zipcode);
926         wprintf("<tr><td>");
927         wprintf(_("Country:"));
928         wprintf("</td><td>"
929                 "<input type=\"text\" name=\"country\" "
930                 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
931                 country);
932         wprintf("</table>\n");
933
934         wprintf("</table>\n");
935
936         wprintf("<table border=0><tr><td>");
937         wprintf(_("Home telephone:"));
938         wprintf("</td>"
939                 "<td><input type=\"text\" name=\"hometel\" "
940                 "value=\"%s\" maxlength=\"29\"></td>\n",
941                 hometel);
942         wprintf("<td>");
943         wprintf(_("Work telephone:"));
944         wprintf("</td>"
945                 "<td><input type=\"text\" name=\"worktel\" "
946                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
947                 worktel);
948         wprintf("<tr><td>");
949         wprintf(_("Mobile telephone:"));
950         wprintf("</td>"
951                 "<td><input type=\"text\" name=\"mobiletel\" "
952                 "value=\"%s\" maxlength=\"29\"></td>\n",
953                 mobiletel);
954         wprintf("<td>");
955         wprintf(_("Fax number:"));
956         wprintf("</td>"
957                 "<td><input type=\"text\" name=\"faxtel\" "
958                 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
959                 faxtel);
960
961         wprintf("<table class=\"vcard_edit_background_alt\">");
962         wprintf("<tr><td>");
963
964         wprintf("<table border=0><TR>"
965                 "<td valign=top>");
966         wprintf(_("Primary Internet e-mail address"));
967         wprintf("<br />"
968                 "<input type=\"text\" name=\"primary_inetemail\" "
969                 "size=40 maxlength=60 value=\"");
970         escputs(primary_inetemail);
971         wprintf("\"><br />"
972                 "</td><td valign=top>");
973         wprintf(_("Internet e-mail aliases"));
974         wprintf("<br />"
975                 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
976         escputs(other_inetemail);
977         wprintf("</textarea></td></tr></table>\n");
978
979         wprintf("</td></tr></table>\n");
980
981         wprintf("<input type=\"hidden\" name=\"extrafields\" value=\"");
982         escputs(extrafields);
983         wprintf("\">\n");
984
985         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"");
986         urlescputs(return_to);
987         wprintf("\">\n");
988
989         wprintf("<div class=\"buttons\">\n"
990                 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
991                 "&nbsp;"
992                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
993                 "</div></form>\n",
994                 _("Save changes"),
995                 _("Cancel")
996         );
997         
998         wprintf("</td></tr></table>\n");
999         do_template("endbox", NULL);
1000         wDumpContent(1);
1001 }
1002
1003
1004 /**
1005  *  commit the edits to the citadel server
1006  */
1007 void edit_vcard(void) {
1008         long msgnum;
1009         char *partnum;
1010
1011         msgnum = lbstr("msgnum");
1012         partnum = bstr("partnum");
1013         do_edit_vcard(msgnum, partnum, "", NULL);
1014 }
1015
1016
1017
1018 /**
1019  *  parse edited vcard from the browser
1020  */
1021 void submit_vcard(void) {
1022         struct vCard *v;
1023         char *serialized_vcard;
1024         char buf[SIZ];
1025         int i;
1026
1027         if (!havebstr("ok_button")) { 
1028                 readloop(readnew);
1029                 return;
1030         }
1031
1032         if (havebstr("force_room")) {
1033                 gotoroom(sbstr("force_room"));
1034         }
1035
1036         sprintf(buf, "ENT0 1|||4||");
1037         serv_puts(buf);
1038         serv_getln(buf, sizeof buf);
1039         if (buf[0] != '4') {
1040                 edit_vcard();
1041                 return;
1042         }
1043
1044         /** Make a vCard structure out of the data supplied in the form */
1045
1046         snprintf(buf, sizeof buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1047                 bstr("extrafields")
1048         );
1049         v = vcard_load(buf);    /** Start with the extra fields */
1050         if (v == NULL) {
1051                 safestrncpy(WC->ImportantMessage,
1052                         _("An error has occurred."),
1053                         sizeof WC->ImportantMessage
1054                 );
1055                 edit_vcard();
1056                 return;
1057         }
1058
1059         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
1060                 bstr("lastname"),
1061                 bstr("firstname"),
1062                 bstr("middlename"),
1063                 bstr("prefix"),
1064                 bstr("suffix") );
1065         vcard_add_prop(v, "n", buf);
1066         
1067         vcard_add_prop(v, "title", bstr("title"));
1068         vcard_add_prop(v, "fn", bstr("fullname"));
1069         vcard_add_prop(v, "org", bstr("org"));
1070
1071         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
1072                 bstr("pobox"),
1073                 bstr("extadr"),
1074                 bstr("street"),
1075                 bstr("city"),
1076                 bstr("state"),
1077                 bstr("zipcode"),
1078                 bstr("country") );
1079         vcard_add_prop(v, "adr", buf);
1080
1081         vcard_add_prop(v, "tel;home", bstr("hometel"));
1082         vcard_add_prop(v, "tel;work", bstr("worktel"));
1083         vcard_add_prop(v, "tel;fax", bstr("faxtel"));
1084         vcard_add_prop(v, "tel;cell", bstr("mobiletel"));
1085         vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
1086
1087         for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
1088                 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
1089                 if (!IsEmptyStr(buf)) {
1090                         vcard_add_prop(v, "email;internet", buf);
1091                 }
1092         }
1093
1094         serialized_vcard = vcard_serialize(v);
1095         vcard_free(v);
1096         if (serialized_vcard == NULL) {
1097                 safestrncpy(WC->ImportantMessage,
1098                         _("An error has occurred."),
1099                         sizeof WC->ImportantMessage
1100                 );
1101                 edit_vcard();
1102                 return;
1103         }
1104
1105         serv_puts("Content-type: text/x-vcard; charset=UTF-8");
1106         serv_puts("");
1107         serv_printf("%s\r\n", serialized_vcard);
1108         serv_puts("000");
1109         free(serialized_vcard);
1110
1111         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1112                 select_user_to_edit(NULL, NULL);
1113         }
1114         else if (!strcmp(bstr("return_to"), "do_welcome")) {
1115                 do_welcome();
1116         }
1117         else {
1118                 readloop(readnew);
1119         }
1120 }
1121
1122
1123
1124 /*
1125  * Extract an embedded photo from a vCard for display on the client
1126  */
1127 void display_vcard_photo_img(void)
1128 {
1129         long msgnum = 0L;
1130         char *vcard;
1131         struct vCard *v;
1132         char *photosrc;
1133         const char *contentType;
1134         wcsession *WCC = WC;
1135
1136         msgnum = StrTol(WCC->UrlFragment2);
1137         
1138         vcard = load_mimepart(msgnum,"1");
1139         v = vcard_load(vcard);
1140         
1141         photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
1142         FlushStrBuf(WCC->WBuf);
1143         StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
1144         if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
1145                 FlushStrBuf(WCC->WBuf);
1146                 
1147                 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
1148                 output_headers(0, 0, 0, 0, 0, 0);
1149                 hprintf("Content-Type: text/plain\r\n");
1150                 wprintf(_("Could Not decode vcard photo\n"));
1151                 end_burst();
1152                 return;
1153         }
1154         contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1155         http_transmit_thing(contentType, 0);
1156         free(v);
1157         free(photosrc);
1158 }
1159
1160
1161
1162 void 
1163 InitModule_VCARD
1164 (void)
1165 {
1166         WebcitAddUrlHandler(HKEY("edit_vcard"), edit_vcard, 0);
1167         WebcitAddUrlHandler(HKEY("submit_vcard"), submit_vcard, 0);
1168         WebcitAddUrlHandler(HKEY("vcardphoto"), display_vcard_photo_img, NEED_URL);
1169 }
1170