2 * Copyright (c) 1996-2012 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 #include "webserver.h"
20 {HKEY("n")}, /* N is name, but only if there's no FN already there */
21 {HKEY("fn")}, /* FN (full name) is a true 'display name' field */
22 {HKEY("title")}, /* title */
23 {HKEY("org")}, /* organization */
55 HashList *VCToEnum = NULL;
58 * Record compare function for sorting address book indices
60 int abcmp(const void *ab1, const void *ab2) {
62 (((const addrbookent *)ab1)->ab_name),
63 (((const addrbookent *)ab2)->ab_name)
69 * Helper function for do_addrbook_view()
70 * Converts a name into a three-letter tab label
72 void nametab(char *tabbuf, long len, char *name) {
73 stresc(tabbuf, len, name, 0, 0);
74 tabbuf[0] = toupper(tabbuf[0]);
75 tabbuf[1] = tolower(tabbuf[1]);
76 tabbuf[2] = tolower(tabbuf[2]);
82 * If it's an old "Firstname Lastname" style record, try to convert it.
84 void lastfirst_firstlast(char *namebuf) {
89 if (namebuf == NULL) return;
90 if (strchr(namebuf, ';') != NULL) return;
92 i = num_tokens(namebuf, ' ');
95 extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
96 remove_token(namebuf, i-1, ' ');
97 strcpy(firstname, namebuf);
98 sprintf(namebuf, "%s; %s", lastname, firstname);
103 wc_mime_attachment *load_vcard(message_summary *Msg)
106 StrBuf *FoundCharset = NewStrBuf();
111 wc_mime_attachment *Mime;
112 wc_mime_attachment *VCMime = NULL;
114 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
115 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
116 Msg->MsgBody->msgnum = Msg->msgnum;
118 load_message(Msg, FoundCharset, &Error);
120 FreeStrBuf(&FoundCharset);
121 /* look up the vcard... */
122 it = GetNewHashPos(Msg->AllAttach, 0);
123 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
126 Mime = (wc_mime_attachment*) vMime;
127 if ((strcmp(ChrPtr(Mime->ContentType),
128 "text/x-vcard") == 0) ||
129 (strcmp(ChrPtr(Mime->ContentType),
140 MimeLoadData(VCMime);
145 * fetch the display name off a vCard
147 void fetch_ab_name(message_summary *Msg, char **namebuf) {
150 wc_mime_attachment *VCMime = NULL;
152 if (namebuf == NULL) return;
154 VCMime = load_vcard(Msg);
158 /* Grab the name off the card */
159 display_vcard(WC->WBuf, VCMime, 0, 0, namebuf, Msg->msgnum);
161 if (*namebuf != NULL) {
162 lastfirst_firstlast(*namebuf);
164 len = strlen(*namebuf);
165 for (i=0; i<len; ++i) {
166 if ((*namebuf)[i] != ';') return;
169 (*namebuf) = strdup(_("(no name)"));
172 (*namebuf) = strdup(_("(no name)"));
179 * Turn a vCard "n" (name) field into something displayable.
181 void vcard_n_prettyize(char *name)
186 original_name = strdup(name);
187 len = strlen(original_name);
188 for (i=0; i<5; ++i) {
190 if (original_name[len-1] == ' ') {
191 original_name[--len] = 0;
193 if (original_name[len-1] == ';') {
194 original_name[--len] = 0;
200 for (i=0; i<len; ++i) {
201 if (original_name[i] == ';') {
206 name[j++] = original_name[i];
217 * preparse a vcard name
218 * display_vcard() calls this after parsing the textual vCard into
219 * our 'struct vCard' data object.
220 * This gets called instead of display_parsed_vcard() if we are only looking
221 * to extract the person's name instead of displaying the card.
223 void fetchname_parsed_vcard(struct vCard *v, char **storename) {
233 name = vcard_get_prop(v, "n", 1, 0, 0);
236 prop = vcard_get_prop(v, "n", 1, 0, 1);
237 n = num_tokens(prop, ';');
239 for (j=0; j<n; ++j) {
240 extract_token(buf, prop, j, ';', sizeof buf);
241 if (!strcasecmp(buf, "encoding=quoted-printable")) {
244 if (!strcasecmp(buf, "encoding=base64")) {
249 /* %ff can become 6 bytes in utf8 */
250 *storename = malloc(len * 2 + 3);
251 j = CtdlDecodeQuotedPrintable(
257 /* ff will become one byte.. */
258 *storename = malloc(len + 50);
268 *storename = malloc(len + 3); /* \0 + eventualy missing ', '*/
269 memcpy(*storename, name, len + 1);
271 /* vcard_n_prettyize(storename); */
280 * display_vcard() calls this after parsing the textual vCard into
281 * our 'struct vCard' data object.
283 * Set 'full' to nonzero to display the full card, otherwise it will only
284 * show a summary line.
286 * This code is a bit ugly, so perhaps an explanation is due: we do this
287 * in two passes through the vCard fields. On the first pass, we process
288 * fields we understand, and then render them in a pretty fashion at the
289 * end. Then we make a second pass, outputting all the fields we don't
290 * understand in a simple two-column name/value format.
291 * v the vCard to display
292 * full display all items of the vcard?
293 * msgnum Citadel message pointer
295 void display_parsed_vcard(StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
302 char *thisname, *thisvalue;
303 char firsttoken[SIZ];
312 strcpy(fullname, "");
319 StrBufAppendPrintf(Target, "<td>");
320 name = vcard_get_prop(v, "fn", 1, 0, 0);
322 StrEscAppend(Target, NULL, name, 0, 0);
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);
330 StrBufAppendPrintf(Target, " ");
332 StrBufAppendPrintf(Target, "</td>");
336 StrBufAppendPrintf(Target, "<div align=\"center\">"
337 "<table bgcolor=\"#aaaaaa\" width=\"50%%\">");
338 for (pass=1; pass<=2; ++pass) {
340 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
342 thisname = strdup(v->prop[i].name);
343 extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
345 for (j=0; j<num_tokens(thisname, ';'); ++j) {
346 extract_token(buf, thisname, j, ';', sizeof buf);
347 if (!strcasecmp(buf, "encoding=quoted-printable")) {
349 remove_token(thisname, j, ';');
351 if (!strcasecmp(buf, "encoding=base64")) {
353 remove_token(thisname, j, ';');
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);
363 // %ff can become 6 bytes in utf8
364 thisvalue = malloc(len * 2 + 3);
365 j = CtdlDecodeQuotedPrintable(
366 thisvalue, v->prop[i].value,
371 // ff will become one byte..
372 thisvalue = malloc(len + 50);
374 thisvalue, v->prop[i].value,
375 strlen(v->prop[i].value) );
378 thisvalue = strdup(v->prop[i].value);
381 /* Various fields we may encounter ***/
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);
391 /* FN (full name) is a true 'display name' field */
392 else if (!strcasecmp(firsttoken, "fn")) {
393 strcpy(fullname, thisvalue);
397 else if (!strcasecmp(firsttoken, "title")) {
398 strcpy(title, thisvalue);
402 else if (!strcasecmp(firsttoken, "org")) {
403 strcpy(org, thisvalue);
406 else if (!strcasecmp(firsttoken, "email")) {
408 if (!IsEmptyStr(mailto)) strcat(mailto, "<br>");
410 "<a href=\"display_enter"
411 "?force_room=_MAIL_?recp=");
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, ">");
424 strcat(mailto, "\">");
425 len = strlen(mailto);
426 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
427 strcat(mailto, "</A>");
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"))
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)"));
449 else if (!strcasecmp(firsttoken, "adr")) {
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, " ");
462 StrBufAppendPrintf(Target, "</td></tr>\n");
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");
473 else if (!strcasecmp(firsttoken, "version")) {
476 else if (!strcasecmp(firsttoken, "rev")) {
479 else if (!strcasecmp(firsttoken, "label")) {
484 /*** Don't show extra fields. They're ugly.
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");
500 StrBufAppendPrintf(Target, "<tr bgcolor=\"#aaaaaa\">"
501 "<td colspan=2 bgcolor=\"#ffffff\">"
502 "<img align=\"center\" src=\"static/webcit_icons/essen/32x32/contact.png\">"
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>");
511 if (!IsEmptyStr(org)) {
512 StrBufAppendPrintf(Target, "<div align=\"right\">");
513 StrEscAppend(Target, NULL, org, 0, 0);
514 StrBufAppendPrintf(Target, "</div>");
516 StrBufAppendPrintf(Target, "</td></tr>\n");
518 if (!IsEmptyStr(phone)) {
519 StrBufAppendPrintf(Target, "<tr><td>");
520 StrBufAppendPrintf(Target, _("Telephone:"));
521 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", phone);
523 if (!IsEmptyStr(mailto)) {
524 StrBufAppendPrintf(Target, "<tr><td>");
525 StrBufAppendPrintf(Target, _("E-mail:"));
526 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", mailto);
532 StrBufAppendPrintf(Target, "</table></div>\n");
537 * display_vcard() calls this after parsing the textual vCard into
538 * our 'struct vCard' data object.
540 * Set 'full' to nonzero to display the full card, otherwise it will only
541 * show a summary line.
543 * This code is a bit ugly, so perhaps an explanation is due: we do this
544 * in two passes through the vCard fields. On the first pass, we process
545 * fields we understand, and then render them in a pretty fashion at the
546 * end. Then we make a second pass, outputting all the fields we don't
547 * understand in a simple two-column name/value format.
548 * v the vCard to display
549 * full display all items of the vcard?
550 * msgnum Citadel message pointer
552 void parse_vcard(StrBuf *Target, struct vCard *v, HashList *VC, int full, wc_mime_attachment *Mime)
560 StrBuf *thisname = NULL;
561 char firsttoken[SIZ];
565 thisname = NewStrBuf();
566 for (i=0; i<(v->numprops); ++i) {
569 StrBufPlain(thisname, v->prop[i].name, -1);
570 StrBufLowerCase(thisname);
572 /*len = */extract_token(firsttoken, ChrPtr(thisname), 0, ';', sizeof firsttoken);
574 for (j=0; j<num_tokens(ChrPtr(thisname), ';'); ++j) {
575 extract_token(buf, ChrPtr(thisname), j, ';', sizeof buf);
576 if (!strcasecmp(buf, "encoding=quoted-printable")) {
578 /* remove_token(thisname, j, ';');*/
580 if (!strcasecmp(buf, "encoding=base64")) {
582 /* remove_token(thisname, j, ';');*/
586 /* copy over the payload into a StrBuf */
587 Val = NewStrBufPlain(v->prop[i].value, -1);
589 /* if we have some untagged QP, detect it here. */
590 if (is_qp || (strstr(v->prop[i].value, "=?")!=NULL)){
592 StrBuf_RFC822_to_Utf8(Swap, Val, NULL, NULL); /* default charset, current charset */
599 StrBufDecodeBase64(Val);
602 syslog(1, "%s [%s][%s]",
606 if (GetHash(VCToEnum, firsttoken, strlen(firsttoken), &V))
609 Put(VC, IKEY(evc), Val, HFreeStrBuf);
610 syslog(1, "[%ul]\n", evc);
616 TODO: check for layer II
619 long max = num_tokens(thisname, ';');
620 firsttoken[len] = '_';
622 for (j = 0; j < max; j++) {
625 extract_token(buf, thisname, j, ';', sizeof (buf));
626 if (!strcasecmp(buf, "tel"))
628 else if (!strcasecmp(buf, "work"))
629 strcat(phone, _(" (work)"));
630 else if (!strcasecmp(buf, "home"))
631 strcat(phone, _(" (home)"));
632 else if (!strcasecmp(buf, "cell"))
633 strcat(phone, _(" (cell)"));
652 void tmplput_VCARD_ITEM(StrBuf *Target, WCTemplputParams *TP)
658 evc = GetTemplateTokenNumber(Target, TP, 0, -1);
661 if (GetHash(VC, IKEY(evc), &vStr))
663 StrBufAppendTemplate(Target, TP,
671 void new_vcard (StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
674 WCTemplputParams SubTP;
676 memset(&SubTP, 0, sizeof(WCTemplputParams));
679 VC = NewHash(0, Flathash);
680 parse_vcard(Target, v, VC, full, Mime);
682 SubTP.Filter.ContextType = CTX_VCARD;
685 DoTemplate(HKEY("test_vcard"), Target, &SubTP);
692 * Display a textual vCard
693 * (Converts to a vCard object and then calls the actual display function)
694 * Set 'full' to nonzero to display the whole card instead of a one-liner.
695 * Or, if "storename" is non-NULL, just store the person's name in that
696 * buffer instead of displaying the card at all.
698 * vcard_source the buffer containing the vcard text
699 * alpha Display only if name begins with this letter of the alphabet
700 * full Display the full vCard (otherwise just the display name)
701 * storename If not NULL, also store the display name here
702 * msgnum Citadel message pointer
704 void display_vcard(StrBuf *Target,
705 wc_mime_attachment *Mime,
717 v = VCardLoad(Mime->Data);
719 if (v == NULL) return;
721 name = vcard_get_prop(v, "n", 1, 0, 0);
723 Buf = NewStrBufPlain(name, -1);
724 Buf2 = NewStrBufPlain(NULL, StrLength(Buf));
725 StrBuf_RFC822_to_Utf8(Buf2, Buf, WC->DefaultCharset, NULL);
726 this_alpha = ChrPtr(Buf)[0];
731 if (storename != NULL) {
732 fetchname_parsed_vcard(v, storename);
734 else if ((alpha == 0) ||
735 ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha))) ||
736 ((!isalpha(alpha)) && (!isalpha(this_alpha)))
740 new_vcard (Target, v, full, Mime);
742 display_parsed_vcard(Target, v, full, Mime);
752 * Render the address book using info we gathered during the scan
754 * addrbook the addressbook to render
755 * num_ab the number of the addressbook
757 void do_addrbook_view(addrbookent *addrbook, int num_ab) {
761 static int NAMESPERPAGE = 60;
764 char tabfirst_label[64];
766 char tablast_label[64];
767 char this_tablabel[64];
772 wc_printf("<br><br><br><div align=\"center\"><i>");
773 wc_printf(_("This address book is empty."));
774 wc_printf("</i></div>\n");
779 qsort(addrbook, num_ab, sizeof(addrbookent), abcmp);
782 num_pages = (num_ab / NAMESPERPAGE) + 1;
784 tablabels = malloc(num_pages * sizeof (char *));
785 if (tablabels == NULL) {
786 wc_printf("<br><br><br><div align=\"center\"><i>");
787 wc_printf(_("An internal error has occurred."));
788 wc_printf("</i></div>\n");
792 for (i=0; i<num_pages; ++i) {
793 tabfirst = i * NAMESPERPAGE;
794 tablast = tabfirst + NAMESPERPAGE - 1;
795 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
796 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
797 nametab(tablast_label, 64, addrbook[tablast].ab_name);
798 sprintf(this_tablabel, "%s - %s", tabfirst_label, tablast_label);
799 tablabels[i] = strdup(this_tablabel);
802 tabbed_dialog(num_pages, tablabels);
805 for (i=0; i<num_ab; ++i) {
807 if ((i / NAMESPERPAGE) != page) { /* New tab */
808 page = (i / NAMESPERPAGE);
810 wc_printf("</tr></table>\n");
811 end_tab(page-1, num_pages);
813 begin_tab(page, num_pages);
814 wc_printf("<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%%\">\n");
818 if ((displayed % 4) == 0) {
820 wc_printf("</tr>\n");
823 wc_printf("<tr bgcolor=\"#%s\">",
824 (bg ? "dddddd" : "ffffff")
830 wc_printf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
831 addrbook[i].ab_msgnum);
832 wc_printf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
833 vcard_n_prettyize(addrbook[i].ab_name);
834 escputs(addrbook[i].ab_name);
835 wc_printf("</a></td>\n");
839 /* Placeholders for empty columns at end */
840 if ((num_ab % 4) != 0) {
841 for (i=0; i<(4-(num_ab % 4)); ++i) {
842 wc_printf("<td> </td>");
846 wc_printf("</tr></table>\n");
847 end_tab((num_pages-1), num_pages);
849 begin_tab(num_pages, num_pages);
850 /* FIXME there ought to be something here */
851 end_tab(num_pages, num_pages);
853 for (i=0; i<num_pages; ++i) {
863 * Edit the vCard component of a MIME message.
864 * Supply the message number
865 * and MIME part number to fetch. Or, specify -1 for the message number
866 * to start with a blank card.
868 void do_edit_vcard(long msgnum, char *partnum,
869 message_summary *VCMsg,
870 wc_mime_attachment *VCAtt,
871 const char *return_to,
872 const char *force_room) {
873 message_summary *Msg = NULL;
874 wc_mime_attachment *VCMime = NULL;
882 char middlename[256];
896 char primary_inetemail[256];
897 char other_inetemail[SIZ];
898 char extrafields[SIZ];
919 primary_inetemail[0] = 0;
920 other_inetemail[0] = 0;
926 safestrncpy(whatuser, "", sizeof whatuser);
929 ((VCMsg != NULL) && (VCAtt != NULL)))
931 if ((VCMsg == NULL) && (VCAtt == NULL)) {
933 Msg = (message_summary *) malloc(sizeof(message_summary));
934 memset(Msg, 0, sizeof(message_summary));
935 Msg->msgnum = msgnum;
936 VCMime = load_vcard(Msg);
937 if (VCMime == NULL) {
938 convenience_page("770000", _("Error"), "");///TODO: important message
939 DestroyMessageSummary(Msg);
943 v = VCardLoad(VCMime->Data);
946 v = VCardLoad(VCAtt->Data);
949 /* Populate the variables for our form */
951 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
952 char prp[256]; /* property name */
953 char prm[256]; /* parameters */
955 value = vcard_get_prop(v, "", 0, i++, 0);
958 extract_token(prp, key, 0, ';', sizeof prp);
959 safestrncpy(prm, key, sizeof prm);
960 remove_token(prm, 0, ';');
962 if (!strcasecmp(prp, "n")) {
963 extract_token(lastname, value, 0, ';', sizeof lastname);
964 extract_token(firstname, value, 1, ';', sizeof firstname);
965 extract_token(middlename, value, 2, ';', sizeof middlename);
966 extract_token(prefix, value, 3, ';', sizeof prefix);
967 extract_token(suffix, value, 4, ';', sizeof suffix);
970 else if (!strcasecmp(prp, "fn")) {
971 safestrncpy(fullname, value, sizeof fullname);
974 else if (!strcasecmp(prp, "title")) {
975 safestrncpy(title, value, sizeof title);
978 else if (!strcasecmp(prp, "org")) {
979 safestrncpy(org, value, sizeof org);
982 else if (!strcasecmp(prp, "adr")) {
983 extract_token(pobox, value, 0, ';', sizeof pobox);
984 extract_token(extadr, value, 1, ';', sizeof extadr);
985 extract_token(street, value, 2, ';', sizeof street);
986 extract_token(city, value, 3, ';', sizeof city);
987 extract_token(state, value, 4, ';', sizeof state);
988 extract_token(zipcode, value, 5, ';', sizeof zipcode);
989 extract_token(country, value, 6, ';', sizeof country);
992 else if (!strcasecmp(prp, "tel")) {
994 if (bmstrcasestr(prm, "home")) {
995 extract_token(hometel, value, 0, ';', sizeof hometel);
997 else if (bmstrcasestr(prm, "work")) {
998 extract_token(worktel, value, 0, ';', sizeof worktel);
1000 else if (bmstrcasestr(prm, "fax")) {
1001 extract_token(faxtel, value, 0, ';', sizeof faxtel);
1003 else if (bmstrcasestr(prm, "cell")) {
1004 extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
1006 else { /* Missing or unknown type; put it in the home phone */
1007 extract_token(hometel, value, 0, ';', sizeof hometel);
1011 else if ( (!strcasecmp(prp, "email")) && (bmstrcasestr(prm, "internet")) ) {
1012 if (primary_inetemail[0] == 0) {
1013 safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
1016 if (other_inetemail[0] != 0) {
1017 strcat(other_inetemail, "\n");
1019 strcat(other_inetemail, value);
1023 /* Unrecognized properties are preserved here so we don't discard them
1024 * just because the vCard was edited with WebCit.
1027 strcat(extrafields, key);
1028 strcat(extrafields, ":");
1029 strcat(extrafields, value);
1030 strcat(extrafields, "\n");
1038 /* Display the form */
1039 output_headers(1, 1, 1, 0, 0, 0);
1041 do_template("box_begin_1");
1042 StrBufAppendBufPlain(WC->WBuf, _("Edit contact information"), -1, 0);
1043 do_template("box_begin_2");
1045 wc_printf("<form method=\"POST\" action=\"submit_vcard\">\n");
1046 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1048 if (force_room != NULL) {
1049 wc_printf("<input type=\"hidden\" name=\"force_room\" value=\"");
1050 escputs(force_room);
1054 wc_printf("<table class=\"vcard_edit_background\"><tr><td>\n");
1056 wc_printf("<table border=\"0\"><tr>"
1061 "<td>%s</td></tr>\n",
1062 _("Prefix"), _("First Name"), _("Middle Name"), _("Last Name"), _("Suffix")
1064 wc_printf("<tr><td><input type=\"text\" name=\"prefix\" "
1065 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
1067 wc_printf("<td><input type=\"text\" name=\"firstname\" "
1068 "value=\"%s\" maxlength=\"29\"></td>",
1070 wc_printf("<td><input type=\"text\" name=\"middlename\" "
1071 "value=\"%s\" maxlength=\"29\"></td>",
1073 wc_printf("<td><input type=\"text\" name=\"lastname\" "
1074 "value=\"%s\" maxlength=\"29\"></td>",
1076 wc_printf("<td><input type=\"text\" name=\"suffix\" "
1077 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
1080 wc_printf("<table class=\"vcard_edit_background_alt\">");
1081 wc_printf("<tr><td>");
1083 wc_printf(_("Display name:"));
1085 "<input type=\"text\" name=\"fullname\" "
1086 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1090 wc_printf(_("Title:"));
1092 "<input type=\"text\" name=\"title\" "
1093 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1097 wc_printf(_("Organization:"));
1099 "<input type=\"text\" name=\"org\" "
1100 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1104 wc_printf("</td><td>");
1106 wc_printf("<table border=\"0\">");
1107 wc_printf("<tr><td>");
1108 wc_printf(_("PO box:"));
1109 wc_printf("</td><td>"
1110 "<input type=\"text\" name=\"pobox\" "
1111 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1113 wc_printf("<tr><td>");
1114 wc_printf(_("Address:"));
1115 wc_printf("</td><td>"
1116 "<input type=\"text\" name=\"extadr\" "
1117 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1119 wc_printf("<tr><td> </td><td>"
1120 "<input type=\"text\" name=\"street\" "
1121 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1123 wc_printf("<tr><td>");
1124 wc_printf(_("City:"));
1125 wc_printf("</td><td>"
1126 "<input type=\"text\" name=\"city\" "
1127 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1129 wc_printf("<tr><td>");
1130 wc_printf(_("State:"));
1131 wc_printf("</td><td>"
1132 "<input type=\"text\" name=\"state\" "
1133 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1135 wc_printf("<tr><td>");
1136 wc_printf(_("ZIP code:"));
1137 wc_printf("</td><td>"
1138 "<input type=\"text\" name=\"zipcode\" "
1139 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
1141 wc_printf("<tr><td>");
1142 wc_printf(_("Country:"));
1143 wc_printf("</td><td>"
1144 "<input type=\"text\" name=\"country\" "
1145 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
1147 wc_printf("</table>\n");
1149 wc_printf("</table>\n");
1151 wc_printf("<table border=0><tr><td>");
1152 wc_printf(_("Home telephone:"));
1154 "<td><input type=\"text\" name=\"hometel\" "
1155 "value=\"%s\" maxlength=\"29\"></td>\n",
1158 wc_printf(_("Work telephone:"));
1160 "<td><input type=\"text\" name=\"worktel\" "
1161 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1163 wc_printf("<tr><td>");
1164 wc_printf(_("Mobile telephone:"));
1166 "<td><input type=\"text\" name=\"mobiletel\" "
1167 "value=\"%s\" maxlength=\"29\"></td>\n",
1170 wc_printf(_("Fax number:"));
1172 "<td><input type=\"text\" name=\"faxtel\" "
1173 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
1176 wc_printf("<table class=\"vcard_edit_background_alt\">");
1177 wc_printf("<tr><td>");
1179 wc_printf("<table border=0><TR>"
1181 wc_printf(_("Primary Internet e-mail address"));
1183 "<input type=\"text\" name=\"primary_inetemail\" "
1184 "size=40 maxlength=60 value=\"");
1185 escputs(primary_inetemail);
1187 "</td><td valign=top>");
1188 wc_printf(_("Internet e-mail aliases"));
1190 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
1191 escputs(other_inetemail);
1192 wc_printf("</textarea></td></tr></table>\n");
1194 wc_printf("</td></tr></table>\n");
1196 wc_printf("<input type=\"hidden\" name=\"extrafields\" value=\"");
1197 escputs(extrafields);
1200 wc_printf("<input type=\"hidden\" name=\"return_to\" value=\"");
1204 wc_printf("<div class=\"buttons\">\n"
1205 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
1207 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
1213 wc_printf("</td></tr></table>\n");
1214 do_template("box_end");
1217 DestroyMessageSummary(Msg);
1223 * commit the edits to the citadel server
1225 void edit_vcard(void) {
1229 msgnum = lbstr("msgnum");
1230 partnum = bstr("partnum");
1231 do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
1237 * parse edited vcard from the browser
1239 void submit_vcard(void) {
1241 char *serialized_vcard;
1244 const StrBuf *ForceRoom;
1247 if (!havebstr("ok_button")) {
1248 readloop(readnew, eUseDefault);
1252 if (havebstr("force_room")) {
1253 ForceRoom = sbstr("force_room");
1254 if (gotoroom(ForceRoom) != 200) {
1255 AppendImportantMessage(_("Unable to enter the room to save your message"), -1);
1256 AppendImportantMessage(HKEY(": "));
1257 AppendImportantMessage(SKEY(ForceRoom));
1258 AppendImportantMessage(HKEY("; "));
1259 AppendImportantMessage(_("Aborting."), -1);
1261 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1262 select_user_to_edit(NULL);
1264 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1267 else if (!IsEmptyStr(bstr("return_to"))) {
1268 http_redirect(bstr("return_to"));
1271 readloop(readnew, eUseDefault);
1278 serv_write(HKEY("ENT0 1|||4\n"));
1279 if (!StrBuf_ServGetln(Buf) && (GetServerStatus(Buf, NULL) != 4))
1285 /* Make a vCard structure out of the data supplied in the form */
1286 StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1289 v = VCardLoad(Buf); /* Start with the extra fields */
1291 AppendImportantMessage(_("An error has occurred."), -1);
1297 snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
1303 vcard_add_prop(v, "n", buf);
1305 vcard_add_prop(v, "title", bstr("title"));
1306 vcard_add_prop(v, "fn", bstr("fullname"));
1307 vcard_add_prop(v, "org", bstr("org"));
1309 snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
1317 vcard_add_prop(v, "adr", buf);
1319 vcard_add_prop(v, "tel;home", bstr("hometel"));
1320 vcard_add_prop(v, "tel;work", bstr("worktel"));
1321 vcard_add_prop(v, "tel;fax", bstr("faxtel"));
1322 vcard_add_prop(v, "tel;cell", bstr("mobiletel"));
1323 vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
1325 for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
1326 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
1327 if (!IsEmptyStr(buf)) {
1328 vcard_add_prop(v, "email;internet", buf);
1332 serialized_vcard = vcard_serialize(v);
1334 if (serialized_vcard == NULL) {
1335 AppendImportantMessage(_("An error has occurred."), -1);
1341 serv_write(HKEY("Content-type: text/x-vcard; charset=UTF-8\n"));
1342 serv_write(HKEY("\n"));
1343 serv_printf("%s\r\n", serialized_vcard);
1344 serv_write(HKEY("000\n"));
1345 free(serialized_vcard);
1347 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1348 select_user_to_edit(NULL);
1350 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1353 else if (!IsEmptyStr(bstr("return_to"))) {
1354 http_redirect(bstr("return_to"));
1357 readloop(readnew, eUseDefault);
1365 * Extract an embedded photo from a vCard for display on the client
1367 void display_vcard_photo_img(void)
1373 const char *contentType;
1374 wcsession *WCC = WC;
1376 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1378 vcard = load_mimepart(msgnum,"1");
1379 v = VCardLoad(vcard);
1381 photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
1382 FlushStrBuf(WCC->WBuf);
1383 StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
1384 if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
1385 FlushStrBuf(WCC->WBuf);
1387 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
1388 output_headers(0, 0, 0, 0, 0, 0);
1389 hprintf("Content-Type: text/plain\r\n");
1391 wc_printf(_("Could Not decode vcard photo\n"));
1395 contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1396 http_transmit_thing(contentType, 0);
1401 typedef struct _vcardview_struct {
1403 addrbookent *addrbook;
1408 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat,
1409 void **ViewSpecific,
1416 vcardview_struct *VS;
1418 VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
1419 memset(VS, 0, sizeof(vcardview_struct));
1420 *ViewSpecific = (void*)VS;
1422 VS->is_singlecard = ibstr("is_singlecard");
1423 if (VS->is_singlecard != 1) {
1424 if (oper == do_search) {
1425 snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
1428 strcpy(cmd, "MSGS ALL");
1430 Stat->maxmsgs = 9999999;
1435 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat,
1436 void **ViewSpecific,
1437 message_summary* Msg,
1441 vcardview_struct *VS;
1444 VS = (vcardview_struct*) *ViewSpecific;
1447 fetch_ab_name(Msg, &ab_name);
1448 if (ab_name == NULL)
1451 VS->addrbook = realloc(VS->addrbook,
1452 (sizeof(addrbookent) * VS->num_ab) );
1453 safestrncpy(VS->addrbook[VS->num_ab-1].ab_name, ab_name,
1454 sizeof(VS->addrbook[VS->num_ab-1].ab_name));
1455 VS->addrbook[VS->num_ab-1].ab_msgnum = Msg->msgnum;
1461 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1464 vcardview_struct *VS;
1466 VS = (vcardview_struct*) *ViewSpecific;
1467 if (VS->is_singlecard)
1468 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1470 do_addrbook_view(VS->addrbook, VS->num_ab); /* Render the address book */
1474 int vcard_Cleanup(void **ViewSpecific)
1476 vcardview_struct *VS;
1478 VS = (vcardview_struct*) *ViewSpecific;
1481 (VS->addrbook != NULL))
1489 ServerStartModule_VCARD
1492 VCToEnum = NewHash(0, NULL);
1497 ServerShutdownModule_VCARD
1500 DeleteHash(&VCToEnum);
1507 RegisterReadLoopHandlerset(
1509 vcard_GetParamsGetServerCall,
1513 vcard_LoadMsgFromServer,
1514 vcard_RenderView_or_Tail,
1516 WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1517 WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1518 WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1520 Put(VCToEnum, HKEY("n"), (void*)VC_n, reference_free_handler);
1521 Put(VCToEnum, HKEY("fn"), (void*)VC_fn, reference_free_handler);
1522 Put(VCToEnum, HKEY("title"), (void*)VC_title, reference_free_handler);
1523 Put(VCToEnum, HKEY("org"), (void*)VC_org, reference_free_handler);
1524 Put(VCToEnum, HKEY("email"), (void*)VC_email, reference_free_handler);
1525 Put(VCToEnum, HKEY("tel"), (void*)VC_tel, reference_free_handler);
1526 Put(VCToEnum, HKEY("tel_tel"), (void*)VC_tel_tel, reference_free_handler);
1527 Put(VCToEnum, HKEY("tel_work"), (void*)VC_tel_work, reference_free_handler);
1528 Put(VCToEnum, HKEY("tel_home"), (void*)VC_tel_home, reference_free_handler);
1529 Put(VCToEnum, HKEY("tel_cell"), (void*)VC_tel_cell, reference_free_handler);
1530 Put(VCToEnum, HKEY("adr"), (void*)VC_adr, reference_free_handler);
1531 Put(VCToEnum, HKEY("photo"), (void*)VC_photo, reference_free_handler);
1532 Put(VCToEnum, HKEY("version"), (void*)VC_version, reference_free_handler);
1533 Put(VCToEnum, HKEY("rev"), (void*)VC_rev, reference_free_handler);
1534 Put(VCToEnum, HKEY("label"), (void*)VC_label, reference_free_handler);
1537 RegisterNamespace("VC", 1, 2, tmplput_VCARD_ITEM, NULL, CTX_VCARD);
1539 REGISTERTokenParamDefine(VC_n);
1540 REGISTERTokenParamDefine(VC_fn);
1541 REGISTERTokenParamDefine(VC_title);
1542 REGISTERTokenParamDefine(VC_org);
1543 REGISTERTokenParamDefine(VC_email);
1544 REGISTERTokenParamDefine(VC_tel);
1545 REGISTERTokenParamDefine(VC_tel_tel);
1546 REGISTERTokenParamDefine(VC_tel_work);
1547 REGISTERTokenParamDefine(VC_tel_home);
1548 REGISTERTokenParamDefine(VC_tel_cell);
1549 REGISTERTokenParamDefine(VC_adr);
1550 REGISTERTokenParamDefine(VC_photo);
1551 REGISTERTokenParamDefine(VC_version);
1552 REGISTERTokenParamDefine(VC_rev);
1553 REGISTERTokenParamDefine(VC_label);