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