* Set all version numbers to 7.41 for upcoming beta.
[citadel.git] / webcit / vcard_edit.c
blob97ef83fa988c572ba30bf0f002b20c9be5ba3333
1 /*
2 * $Id$
3 */
5 #include "webcit.h"
8 /**
9 * \brief Record compare function for sorting address book indices
10 * \param ab1 adressbook one
11 * \param ab2 adressbook two
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 ));
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
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;
36 /**
37 * \brief display the adressbook overview
38 * \param msgnum the citadel message number
39 * \param alpha what????
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
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) {
60 /** Display the summary line */
61 display_vcard(WC->WBuf, vcard_source, alpha, 0, NULL,msgnum);
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"));
74 free(vcard_source);
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
86 void lastfirst_firstlast(char *namebuf) {
87 char firstname[SIZ];
88 char lastname[SIZ];
89 int i;
91 if (namebuf == NULL) return;
92 if (strchr(namebuf, ';') != NULL) return;
94 i = num_tokens(namebuf, ' ');
95 if (i < 2) return;
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);
104 * \brief fetch what??? name
105 * \param msgnum the citadel message number
106 * \param namebuf where to put the name in???
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
120 if (namebuf == NULL) return;
121 strcpy(namebuf, "");
123 memset(&summ, 0, sizeof(summ));
124 //////safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
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;
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);
139 if ( (!strcasecmp(mime_content_type, "text/x-vcard"))
140 || (!strcasecmp(mime_content_type, "text/vcard")) ) {
141 strcpy(vcard_partnum, mime_partnum);
147 if (!IsEmptyStr(vcard_partnum)) {
148 vcard_source = load_mimepart(Msg->msgnum, vcard_partnum);
149 if (vcard_source != NULL) {
151 /* Grab the name off the card */
152 display_vcard(WC->WBuf, vcard_source, 0, 0, namebuf, Msg->msgnum);
154 free(vcard_source);
158 lastfirst_firstlast(namebuf);
159 striplt(namebuf);
160 len = strlen(namebuf);
161 for (i=0; i<len; ++i) {
162 if (namebuf[i] != ';') return;
164 strcpy(namebuf, _("(no name)"));
170 * \brief Turn a vCard "n" (name) field into something displayable.
171 * \param name the name field to convert
173 void vcard_n_prettyize(char *name)
175 char *original_name;
176 int i, j, len;
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;
185 if (original_name[len-1] == ';') {
186 original_name[--len] = 0;
190 strcpy(name, "");
191 j=0;
192 for (i=0; i<len; ++i) {
193 if (original_name[i] == ';') {
194 name[j++] = ',';
195 name[j++] = ' ';
197 else {
198 name[j++] = original_name[i];
201 name[j] = '\0';
202 free(original_name);
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
217 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
218 char *name;
220 strcpy(storename, "");
222 name = vcard_get_prop(v, "n", 1, 0, 0);
223 if (name != NULL) {
224 strcpy(storename, name);
225 /* vcard_n_prettyize(storename); */
233 * \brief html print a vcard
234 * display_vcard() calls this after parsing the textual vCard into
235 * our 'struct vCard' data object.
237 * Set 'full' to nonzero to display the full card, otherwise it will only
238 * show a summary line.
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
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;
259 char fullname[SIZ];
260 char title[SIZ];
261 char org[SIZ];
262 char phone[SIZ];
263 char mailto[SIZ];
265 strcpy(fullname, "");
266 strcpy(phone, "");
267 strcpy(mailto, "");
268 strcpy(title, "");
269 strcpy(org, "");
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);
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);
282 else {
283 StrBufAppendPrintf(Target, "&nbsp;");
285 StrBufAppendPrintf(Target, "</TD>");
286 return;
289 StrBufAppendPrintf(Target, "<div align=center>"
290 "<table bgcolor=#aaaaaa width=50%%>");
291 for (pass=1; pass<=2; ++pass) {
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);
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, ';');
304 if (!strcasecmp(buf, "encoding=base64")) {
305 is_b64 = 1;
306 remove_token(thisname, j, ';');
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);
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;
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) );
330 else {
331 thisvalue = strdup(v->prop[i].value);
334 /** Various fields we may encounter ***/
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);
344 /** FN (full name) is a true 'display name' field */
345 else if (!strcasecmp(firsttoken, "fn")) {
346 strcpy(fullname, thisvalue);
349 /** title */
350 else if (!strcasecmp(firsttoken, "title")) {
351 strcpy(title, thisvalue);
354 /** organization */
355 else if (!strcasecmp(firsttoken, "org")) {
356 strcpy(org, thisvalue);
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=");
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, ">");
377 strcat(mailto, "\">");
378 len = strlen(mailto);
379 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
380 strcat(mailto, "</A>");
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, ")");
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, " ");
415 StrBufAppendPrintf(Target, "</TD></TR>\n");
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 */
429 else if (!strcasecmp(firsttoken, "rev")) {
430 /* ignore */
432 else if (!strcasecmp(firsttoken, "label")) {
433 /* ignore */
435 else {
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");
445 ***/
448 free(thisname);
449 free(thisvalue);
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>");
464 if (!IsEmptyStr(org)) {
465 StrBufAppendPrintf(Target, "<div align=right>");
466 StrEscAppend(Target, NULL, org, 0, 0);
467 StrBufAppendPrintf(Target, "</div>");
469 StrBufAppendPrintf(Target, "</TD></TR>\n");
471 if (!IsEmptyStr(phone)) {
472 StrBufAppendPrintf(Target, "<tr><td>");
473 StrBufAppendPrintf(Target, _("Telephone:"));
474 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", phone);
476 if (!IsEmptyStr(mailto)) {
477 StrBufAppendPrintf(Target, "<tr><td>");
478 StrBufAppendPrintf(Target, _("E-mail:"));
479 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", mailto);
485 StrBufAppendPrintf(Target, "</table></div>\n");
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
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;
510 v = vcard_load((char*)vcard_source); ///TODO
512 if (v == NULL) return;
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);
524 if (storename != NULL) {
525 fetchname_parsed_vcard(v, storename);
527 else if ( (alpha == 0)
528 || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
529 || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
531 display_parsed_vcard(Target, v, full,msgnum);
534 vcard_free(v);
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
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;
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;
565 if (num_ab > 1) {
566 qsort(addrbook, num_ab, sizeof(addrbookent), abcmp);
569 num_pages = (num_ab / NAMESPERPAGE) + 1;
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;
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);
589 tabbed_dialog(num_pages, tablabels);
590 page = (-1);
592 for (i=0; i<num_ab; ++i) {
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);
600 begin_tab(page, num_pages);
601 wprintf("<table border=0 cellspacing=0 cellpadding=3 width=100%%>\n");
602 displayed = 0;
605 if ((displayed % 4) == 0) {
606 if (displayed > 0) {
607 wprintf("</tr>\n");
609 bg = 1 - bg;
610 wprintf("<tr bgcolor=\"#%s\">",
611 (bg ? "DDDDDD" : "FFFFFF")
615 wprintf("<td>");
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;
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>");
633 wprintf("</tr></table>\n");
634 end_tab((num_pages-1), num_pages);
636 begin_tab(num_pages, num_pages);
637 /* FIXME there ought to be something here */
638 end_tab(num_pages, num_pages);
640 for (i=0; i<num_pages; ++i) {
641 free(tablabels[i]);
643 free(tablabels);
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.
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];
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];
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;
710 safestrncpy(whatuser, "", sizeof whatuser);
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;
720 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
721 if (!strncasecmp(buf, "from=", 5)) {
722 safestrncpy(whatuser, &buf[5], sizeof whatuser);
724 else if (!strncasecmp(buf, "node=", 5)) {
725 strcat(whatuser, " @ ");
726 strcat(whatuser, &buf[5]);
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;
738 total_len = atoi(&buf[4]);
739 serialized_vcard = malloc(total_len + 2);
741 serv_read(serialized_vcard, total_len);
742 serialized_vcard[total_len] = 0;
744 v = vcard_load(serialized_vcard);
745 free(serialized_vcard);
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);
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);
760 else if (!strcasecmp(key, "fn")) {
761 safestrncpy(fullname, value, sizeof fullname);
764 else if (!strcasecmp(key, "title")) {
765 safestrncpy(title, value, sizeof title);
768 else if (!strcasecmp(key, "org")) {
769 safestrncpy(org, value, sizeof org);
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);
782 else if ( (!strcasecmp(key, "tel;home")) || (!strcasecmp(key, "tel;type=home")) ) {
783 extract_token(hometel, value, 0, ';', sizeof hometel);
786 else if ( (!strcasecmp(key, "tel;work")) || (!strcasecmp(key, "tel;type=work")) ) {
787 extract_token(worktel, value, 0, ';', sizeof worktel);
790 else if ( (!strcasecmp(key, "tel;fax")) || (!strcasecmp(key, "tel;type=fax")) ) {
791 extract_token(faxtel, value, 0, ';', sizeof faxtel);
794 else if ( (!strcasecmp(key, "tel;cell")) || (!strcasecmp(key, "tel;type=cell")) ) {
795 extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
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);
803 else {
804 if (other_inetemail[0] != 0) {
805 strcat(other_inetemail, "\n");
807 strcat(other_inetemail, value);
811 else {
812 strcat(extrafields, key);
813 strcat(extrafields, ":");
814 strcat(extrafields, value);
815 strcat(extrafields, "\n");
820 vcard_free(v);
823 /** Display the form */
824 output_headers(1, 1, 1, 0, 0, 0);
826 svput("BOXTITLE", WCS_STRING, _("Edit contact information"));
827 do_template("beginboxx", NULL);
829 wprintf("<form method=\"POST\" action=\"submit_vcard\">\n");
830 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
832 if (force_room != NULL) {
833 wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
834 escputs(force_room);
835 wprintf("\">\n");
838 wprintf("<div class=\"fix_scrollbar_bug\">"
839 "<table class=\"vcard_edit_background\"><tr><td>\n");
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")
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);
865 wprintf("<table class=\"vcard_edit_background_alt\">");
866 wprintf("<tr><td>");
868 wprintf(_("Display name:"));
869 wprintf("<br>"
870 "<input type=\"text\" name=\"fullname\" "
871 "value=\"%s\" maxlength=\"40\"><br><br>\n",
872 fullname
875 wprintf(_("Title:"));
876 wprintf("<br>"
877 "<input type=\"text\" name=\"title\" "
878 "value=\"%s\" maxlength=\"40\"><br><br>\n",
879 title
882 wprintf(_("Organization:"));
883 wprintf("<br>"
884 "<input type=\"text\" name=\"org\" "
885 "value=\"%s\" maxlength=\"40\"><br><br>\n",
889 wprintf("</td><td>");
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");
934 wprintf("</table>\n");
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);
961 wprintf("<table class=\"vcard_edit_background_alt\">");
962 wprintf("<tr><td>");
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");
979 wprintf("</td></tr></table>\n");
981 wprintf("<input type=\"hidden\" name=\"extrafields\" value=\"");
982 escputs(extrafields);
983 wprintf("\">\n");
985 wprintf("<input type=\"hidden\" name=\"return_to\" value=\"");
986 urlescputs(return_to);
987 wprintf("\">\n");
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")
998 wprintf("</td></tr></table>\n");
999 do_template("endbox", NULL);
1000 wDumpContent(1);
1005 * commit the edits to the citadel server
1007 void edit_vcard(void) {
1008 long msgnum;
1009 char *partnum;
1011 msgnum = lbstr("msgnum");
1012 partnum = bstr("partnum");
1013 do_edit_vcard(msgnum, partnum, "", NULL);
1019 * parse edited vcard from the browser
1021 void submit_vcard(void) {
1022 struct vCard *v;
1023 char *serialized_vcard;
1024 char buf[SIZ];
1025 int i;
1027 if (!havebstr("ok_button")) {
1028 readloop(readnew);
1029 return;
1032 if (havebstr("force_room")) {
1033 gotoroom(sbstr("force_room"));
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;
1044 /** Make a vCard structure out of the data supplied in the form */
1046 snprintf(buf, sizeof buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1047 bstr("extrafields")
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
1055 edit_vcard();
1056 return;
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);
1067 vcard_add_prop(v, "title", bstr("title"));
1068 vcard_add_prop(v, "fn", bstr("fullname"));
1069 vcard_add_prop(v, "org", bstr("org"));
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);
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"));
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);
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
1101 edit_vcard();
1102 return;
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);
1111 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1112 select_user_to_edit(NULL, NULL);
1114 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1115 do_welcome();
1117 else {
1118 readloop(readnew);
1125 * Extract an embedded photo from a vCard for display on the client
1127 void display_vcard_photo_img(void)
1129 long msgnum = 0L;
1130 char *vcard;
1131 struct vCard *v;
1132 char *photosrc;
1133 const char *contentType;
1134 wcsession *WCC = WC;
1136 msgnum = StrTol(WCC->UrlFragment2);
1138 vcard = load_mimepart(msgnum,"1");
1139 v = vcard_load(vcard);
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);
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;
1154 contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1155 http_transmit_thing(contentType, 0);
1156 free(v);
1157 free(photosrc);
1162 void
1163 InitModule_VCARD
1164 (void)
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);