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"
17 CtxType CTX_VCARD = CTX_NONE;
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 if (VCMime->Data == NULL)
141 MimeLoadData(VCMime);
146 * fetch the display name off a vCard
148 void fetch_ab_name(message_summary *Msg, char **namebuf) {
151 wc_mime_attachment *VCMime = NULL;
153 if (namebuf == NULL) return;
155 VCMime = load_vcard(Msg);
159 /* Grab the name off the card */
160 display_vcard(WC->WBuf, VCMime, 0, 0, namebuf, Msg->msgnum);
162 if (*namebuf != NULL) {
163 lastfirst_firstlast(*namebuf);
165 len = strlen(*namebuf);
166 for (i=0; i<len; ++i) {
167 if ((*namebuf)[i] != ';') return;
170 (*namebuf) = strdup(_("(no name)"));
173 (*namebuf) = strdup(_("(no name)"));
180 * Turn a vCard "n" (name) field into something displayable.
182 void vcard_n_prettyize(char *name)
187 original_name = strdup(name);
188 len = strlen(original_name);
189 for (i=0; i<5; ++i) {
191 if (original_name[len-1] == ' ') {
192 original_name[--len] = 0;
194 if (original_name[len-1] == ';') {
195 original_name[--len] = 0;
201 for (i=0; i<len; ++i) {
202 if (original_name[i] == ';') {
207 name[j++] = original_name[i];
218 * preparse a vcard name
219 * display_vcard() calls this after parsing the textual vCard into
220 * our 'struct vCard' data object.
221 * This gets called instead of display_parsed_vcard() if we are only looking
222 * to extract the person's name instead of displaying the card.
224 void fetchname_parsed_vcard(struct vCard *v, char **storename) {
234 name = vcard_get_prop(v, "n", 1, 0, 0);
237 prop = vcard_get_prop(v, "n", 1, 0, 1);
238 n = num_tokens(prop, ';');
240 for (j=0; j<n; ++j) {
241 extract_token(buf, prop, j, ';', sizeof buf);
242 if (!strcasecmp(buf, "encoding=quoted-printable")) {
245 if (!strcasecmp(buf, "encoding=base64")) {
250 /* %ff can become 6 bytes in utf8 */
251 *storename = malloc(len * 2 + 3);
252 j = CtdlDecodeQuotedPrintable(
258 /* ff will become one byte.. */
259 *storename = malloc(len + 50);
269 *storename = malloc(len + 3); /* \0 + eventualy missing ', '*/
270 memcpy(*storename, name, len + 1);
272 /* vcard_n_prettyize(storename); */
281 * display_vcard() calls this after parsing the textual vCard into
282 * our 'struct vCard' data object.
284 * Set 'full' to nonzero to display the full card, otherwise it will only
285 * show a summary line.
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 * v the vCard to display
293 * full display all items of the vcard?
294 * msgnum Citadel message pointer
296 void display_parsed_vcard(StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
303 char *thisname, *thisvalue;
304 char firsttoken[SIZ];
313 strcpy(fullname, "");
320 StrBufAppendPrintf(Target, "<td>");
321 name = vcard_get_prop(v, "fn", 1, 0, 0);
323 StrEscAppend(Target, NULL, name, 0, 0);
325 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
326 strcpy(fullname, name);
327 vcard_n_prettyize(fullname);
328 StrEscAppend(Target, NULL, fullname, 0, 0);
331 StrBufAppendPrintf(Target, " ");
333 StrBufAppendPrintf(Target, "</td>");
337 StrBufAppendPrintf(Target, "<div align=\"center\">"
338 "<table bgcolor=\"#aaaaaa\" width=\"50%%\">");
339 for (pass=1; pass<=2; ++pass) {
341 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
343 thisname = strdup(v->prop[i].name);
344 extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
346 for (j=0; j<num_tokens(thisname, ';'); ++j) {
347 extract_token(buf, thisname, j, ';', sizeof buf);
348 if (!strcasecmp(buf, "encoding=quoted-printable")) {
350 remove_token(thisname, j, ';');
352 if (!strcasecmp(buf, "encoding=base64")) {
354 remove_token(thisname, j, ';');
358 len = strlen(v->prop[i].value);
359 /* if we have some untagged QP, detect it here. */
360 if (!is_qp && (strstr(v->prop[i].value, "=?")!=NULL))
361 utf8ify_rfc822_string(&v->prop[i].value);
364 // %ff can become 6 bytes in utf8
365 thisvalue = malloc(len * 2 + 3);
366 j = CtdlDecodeQuotedPrintable(
367 thisvalue, v->prop[i].value,
372 // ff will become one byte..
373 thisvalue = malloc(len + 50);
375 thisvalue, v->prop[i].value,
376 strlen(v->prop[i].value) );
379 thisvalue = strdup(v->prop[i].value);
382 /* Various fields we may encounter ***/
384 /* N is name, but only if there's no FN already there */
385 if (!strcasecmp(firsttoken, "n")) {
386 if (IsEmptyStr(fullname)) {
387 strcpy(fullname, thisvalue);
388 vcard_n_prettyize(fullname);
392 /* FN (full name) is a true 'display name' field */
393 else if (!strcasecmp(firsttoken, "fn")) {
394 strcpy(fullname, thisvalue);
398 else if (!strcasecmp(firsttoken, "title")) {
399 strcpy(title, thisvalue);
403 else if (!strcasecmp(firsttoken, "org")) {
404 strcpy(org, thisvalue);
407 else if (!strcasecmp(firsttoken, "email")) {
409 if (!IsEmptyStr(mailto)) strcat(mailto, "<br>");
411 "<a href=\"display_enter"
412 "?force_room=_MAIL_?recp=");
414 len = strlen(mailto);
415 urlesc(&mailto[len], SIZ - len, "\"");
416 len = strlen(mailto);
417 urlesc(&mailto[len], SIZ - len, fullname);
418 len = strlen(mailto);
419 urlesc(&mailto[len], SIZ - len, "\" <");
420 len = strlen(mailto);
421 urlesc(&mailto[len], SIZ - len, thisvalue);
422 len = strlen(mailto);
423 urlesc(&mailto[len], SIZ - len, ">");
425 strcat(mailto, "\">");
426 len = strlen(mailto);
427 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
428 strcat(mailto, "</A>");
430 else if (!strcasecmp(firsttoken, "tel")) {
431 if (!IsEmptyStr(phone)) strcat(phone, "<br>");
432 strcat(phone, thisvalue);
433 for (j=0; j<num_tokens(thisname, ';'); ++j) {
434 extract_token(buf, thisname, j, ';', sizeof buf);
435 if (!strcasecmp(buf, "tel"))
437 else if (!strcasecmp(buf, "work"))
438 strcat(phone, _(" (work)"));
439 else if (!strcasecmp(buf, "home"))
440 strcat(phone, _(" (home)"));
441 else if (!strcasecmp(buf, "cell"))
442 strcat(phone, _(" (cell)"));
450 else if (!strcasecmp(firsttoken, "adr")) {
452 StrBufAppendPrintf(Target, "<tr><td>");
453 StrBufAppendPrintf(Target, _("Address:"));
454 StrBufAppendPrintf(Target, "</td><td>");
455 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
456 extract_token(buf, thisvalue, j, ';', sizeof buf);
457 if (!IsEmptyStr(buf)) {
458 StrEscAppend(Target, NULL, buf, 0, 0);
459 if (j<3) StrBufAppendPrintf(Target, "<br>");
460 else StrBufAppendPrintf(Target, " ");
463 StrBufAppendPrintf(Target, "</td></tr>\n");
466 /* else if (!strcasecmp(firsttoken, "photo") && full && pass == 2) {
467 // Only output on second pass
468 StrBufAppendPrintf(Target, "<tr><td>");
469 StrBufAppendPrintf(Target, _("Photo:"));
470 StrBufAppendPrintf(Target, "</td><td>");
471 StrBufAppendPrintf(Target, "<img src=\"/vcardphoto/%ld/\" alt=\"Contact photo\"/>",msgnum);
472 StrBufAppendPrintf(Target, "</td></tr>\n");
474 else if (!strcasecmp(firsttoken, "version")) {
477 else if (!strcasecmp(firsttoken, "rev")) {
480 else if (!strcasecmp(firsttoken, "label")) {
485 /*** Don't show extra fields. They're ugly.
487 StrBufAppendPrintf(Target, "<TR><TD>");
488 StrEscAppend(Target, NULL, thisname, 0, 0);
489 StrBufAppendPrintf(Target, "</TD><TD>");
490 StrEscAppend(Target, NULL, thisvalue, 0, 0);
491 StrBufAppendPrintf(Target, "</TD></TR>\n");
501 StrBufAppendPrintf(Target, "<tr bgcolor=\"#aaaaaa\">"
502 "<td colspan=2 bgcolor=\"#ffffff\">"
503 "<img align=\"center\" src=\"static/webcit_icons/essen/32x32/contact.png\">"
504 "<font size=\"+1\"><b>");
505 StrEscAppend(Target, NULL, fullname, 0, 0);
506 StrBufAppendPrintf(Target, "</b></font>");
507 if (!IsEmptyStr(title)) {
508 StrBufAppendPrintf(Target, "<div align=\"right>\"");
509 StrEscAppend(Target, NULL, title, 0, 0);
510 StrBufAppendPrintf(Target, "</div>");
512 if (!IsEmptyStr(org)) {
513 StrBufAppendPrintf(Target, "<div align=\"right\">");
514 StrEscAppend(Target, NULL, org, 0, 0);
515 StrBufAppendPrintf(Target, "</div>");
517 StrBufAppendPrintf(Target, "</td></tr>\n");
519 if (!IsEmptyStr(phone)) {
520 StrBufAppendPrintf(Target, "<tr><td>");
521 StrBufAppendPrintf(Target, _("Telephone:"));
522 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", phone);
524 if (!IsEmptyStr(mailto)) {
525 StrBufAppendPrintf(Target, "<tr><td>");
526 StrBufAppendPrintf(Target, _("E-mail:"));
527 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", mailto);
533 StrBufAppendPrintf(Target, "</table></div>\n");
538 * display_vcard() calls this after parsing the textual vCard into
539 * our 'struct vCard' data object.
541 * Set 'full' to nonzero to display the full card, otherwise it will only
542 * show a summary line.
544 * This code is a bit ugly, so perhaps an explanation is due: we do this
545 * in two passes through the vCard fields. On the first pass, we process
546 * fields we understand, and then render them in a pretty fashion at the
547 * end. Then we make a second pass, outputting all the fields we don't
548 * understand in a simple two-column name/value format.
549 * v the vCard to display
550 * full display all items of the vcard?
551 * msgnum Citadel message pointer
553 void parse_vcard(StrBuf *Target, struct vCard *v, HashList *VC, int full, wc_mime_attachment *Mime)
561 StrBuf *thisname = NULL;
562 char firsttoken[SIZ];
566 thisname = NewStrBuf();
567 for (i=0; i<(v->numprops); ++i) {
570 StrBufPlain(thisname, v->prop[i].name, -1);
571 StrBufLowerCase(thisname);
573 /*len = */extract_token(firsttoken, ChrPtr(thisname), 0, ';', sizeof firsttoken);
575 for (j=0; j<num_tokens(ChrPtr(thisname), ';'); ++j) {
576 extract_token(buf, ChrPtr(thisname), j, ';', sizeof buf);
577 if (!strcasecmp(buf, "encoding=quoted-printable")) {
579 /* remove_token(thisname, j, ';');*/
581 if (!strcasecmp(buf, "encoding=base64")) {
583 /* remove_token(thisname, j, ';');*/
587 /* copy over the payload into a StrBuf */
588 Val = NewStrBufPlain(v->prop[i].value, -1);
590 /* if we have some untagged QP, detect it here. */
591 if (is_qp || (strstr(v->prop[i].value, "=?")!=NULL)){
593 StrBuf_RFC822_to_Utf8(Swap, Val, NULL, NULL); /* default charset, current charset */
600 StrBufDecodeBase64(Val);
603 syslog(LOG_DEBUG, "%s [%s][%s]",
607 if (GetHash(VCToEnum, firsttoken, strlen(firsttoken), &V))
610 Put(VC, IKEY(evc), Val, HFreeStrBuf);
611 syslog(LOG_DEBUG, "[%ul]\n", evc);
615 syslog(LOG_DEBUG, "[]\n");
617 TODO: check for layer II
620 long max = num_tokens(thisname, ';');
621 firsttoken[len] = '_';
623 for (j = 0; j < max; j++) {
626 extract_token(buf, thisname, j, ';', sizeof (buf));
627 if (!strcasecmp(buf, "tel"))
629 else if (!strcasecmp(buf, "work"))
630 strcat(phone, _(" (work)"));
631 else if (!strcasecmp(buf, "home"))
632 strcat(phone, _(" (home)"));
633 else if (!strcasecmp(buf, "cell"))
634 strcat(phone, _(" (cell)"));
653 void tmplput_VCARD_ITEM(StrBuf *Target, WCTemplputParams *TP)
655 HashList *VC = CTX(CTX_VCARD);
659 evc = GetTemplateTokenNumber(Target, TP, 0, -1);
662 if (GetHash(VC, IKEY(evc), &vStr))
664 StrBufAppendTemplate(Target, TP,
672 void new_vcard (StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
675 WCTemplputParams SubTP;
677 memset(&SubTP, 0, sizeof(WCTemplputParams));
680 VC = NewHash(0, Flathash);
681 parse_vcard(Target, v, VC, full, Mime);
683 SubTP.Filter.ContextType = CTX_VCARD;
686 DoTemplate(HKEY("test_vcard"), Target, &SubTP);
693 * Display a textual vCard
694 * (Converts to a vCard object and then calls the actual display function)
695 * Set 'full' to nonzero to display the whole card instead of a one-liner.
696 * Or, if "storename" is non-NULL, just store the person's name in that
697 * buffer instead of displaying the card at all.
699 * vcard_source the buffer containing the vcard text
700 * alpha Display only if name begins with this letter of the alphabet
701 * full Display the full vCard (otherwise just the display name)
702 * storename If not NULL, also store the display name here
703 * msgnum Citadel message pointer
705 void display_vcard(StrBuf *Target,
706 wc_mime_attachment *Mime,
718 v = VCardLoad(Mime->Data);
720 if (v == NULL) return;
722 name = vcard_get_prop(v, "n", 1, 0, 0);
724 Buf = NewStrBufPlain(name, -1);
725 Buf2 = NewStrBufPlain(NULL, StrLength(Buf));
726 StrBuf_RFC822_to_Utf8(Buf2, Buf, WC->DefaultCharset, NULL);
727 this_alpha = ChrPtr(Buf)[0];
732 if (storename != NULL) {
733 fetchname_parsed_vcard(v, storename);
735 else if ((alpha == 0) ||
736 ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha))) ||
737 ((!isalpha(alpha)) && (!isalpha(this_alpha)))
741 new_vcard (Target, v, full, Mime);
743 display_parsed_vcard(Target, v, full, Mime);
753 * Render the address book using info we gathered during the scan
755 * addrbook the addressbook to render
756 * num_ab the number of the addressbook
758 void do_addrbook_view(addrbookent *addrbook, int num_ab) {
762 static int NAMESPERPAGE = 60;
765 char tabfirst_label[64];
767 char tablast_label[64];
768 char this_tablabel[64];
773 wc_printf("<br><br><br><div align=\"center\"><i>");
774 wc_printf(_("This address book is empty."));
775 wc_printf("</i></div>\n");
780 qsort(addrbook, num_ab, sizeof(addrbookent), abcmp);
783 num_pages = (num_ab / NAMESPERPAGE) + 1;
785 tablabels = malloc(num_pages * sizeof (char *));
786 if (tablabels == NULL) {
787 wc_printf("<br><br><br><div align=\"center\"><i>");
788 wc_printf(_("An internal error has occurred."));
789 wc_printf("</i></div>\n");
793 for (i=0; i<num_pages; ++i) {
794 tabfirst = i * NAMESPERPAGE;
795 tablast = tabfirst + NAMESPERPAGE - 1;
796 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
797 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
798 nametab(tablast_label, 64, addrbook[tablast].ab_name);
799 sprintf(this_tablabel, "%s - %s", tabfirst_label, tablast_label);
800 tablabels[i] = strdup(this_tablabel);
803 tabbed_dialog(num_pages, tablabels);
806 for (i=0; i<num_ab; ++i) {
808 if ((i / NAMESPERPAGE) != page) { /* New tab */
809 page = (i / NAMESPERPAGE);
811 wc_printf("</tr></table>\n");
812 end_tab(page-1, num_pages);
814 begin_tab(page, num_pages);
815 wc_printf("<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%%\">\n");
819 if ((displayed % 4) == 0) {
821 wc_printf("</tr>\n");
824 wc_printf("<tr bgcolor=\"#%s\">",
825 (bg ? "dddddd" : "ffffff")
831 wc_printf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
832 addrbook[i].ab_msgnum);
833 wc_printf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
834 vcard_n_prettyize(addrbook[i].ab_name);
835 escputs(addrbook[i].ab_name);
836 wc_printf("</a></td>\n");
840 /* Placeholders for empty columns at end */
841 if ((num_ab % 4) != 0) {
842 for (i=0; i<(4-(num_ab % 4)); ++i) {
843 wc_printf("<td> </td>");
847 wc_printf("</tr></table>\n");
848 end_tab((num_pages-1), num_pages);
850 begin_tab(num_pages, num_pages);
851 /* FIXME there ought to be something here */
852 end_tab(num_pages, num_pages);
854 for (i=0; i<num_pages; ++i) {
864 * Edit the vCard component of a MIME message.
865 * Supply the message number
866 * and MIME part number to fetch. Or, specify -1 for the message number
867 * to start with a blank card.
869 void do_edit_vcard(long msgnum, char *partnum,
870 message_summary *VCMsg,
871 wc_mime_attachment *VCAtt,
872 const char *return_to,
873 const char *force_room) {
875 message_summary *Msg = NULL;
876 wc_mime_attachment *VCMime = NULL;
884 char middlename[256];
898 char primary_inetemail[256];
899 char other_inetemail[SIZ];
900 char extrafields[SIZ];
921 primary_inetemail[0] = 0;
922 other_inetemail[0] = 0;
928 safestrncpy(whatuser, "", sizeof whatuser);
931 ((VCMsg != NULL) && (VCAtt != NULL)))
933 if ((VCMsg == NULL) && (VCAtt == NULL)) {
935 Msg = (message_summary *) malloc(sizeof(message_summary));
936 memset(Msg, 0, sizeof(message_summary));
937 Msg->msgnum = msgnum;
938 VCMime = load_vcard(Msg);
939 if (VCMime == NULL) {
940 convenience_page("770000", _("Error"), "");///TODO: important message
941 DestroyMessageSummary(Msg);
945 v = VCardLoad(VCMime->Data);
948 v = VCardLoad(VCAtt->Data);
951 /* Populate the variables for our form */
953 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
954 char prp[256]; /* property name */
955 char prm[256]; /* parameters */
957 value = vcard_get_prop(v, "", 0, i++, 0);
960 extract_token(prp, key, 0, ';', sizeof prp);
961 safestrncpy(prm, key, sizeof prm);
962 remove_token(prm, 0, ';');
964 if (!strcasecmp(prp, "n")) {
965 extract_token(lastname, value, 0, ';', sizeof lastname);
966 extract_token(firstname, value, 1, ';', sizeof firstname);
967 extract_token(middlename, value, 2, ';', sizeof middlename);
968 extract_token(prefix, value, 3, ';', sizeof prefix);
969 extract_token(suffix, value, 4, ';', sizeof suffix);
972 else if (!strcasecmp(prp, "fn")) {
973 safestrncpy(fullname, value, sizeof fullname);
976 else if (!strcasecmp(prp, "title")) {
977 safestrncpy(title, value, sizeof title);
980 else if (!strcasecmp(prp, "org")) {
981 safestrncpy(org, value, sizeof org);
984 else if (!strcasecmp(prp, "adr")) {
985 extract_token(pobox, value, 0, ';', sizeof pobox);
986 extract_token(extadr, value, 1, ';', sizeof extadr);
987 extract_token(street, value, 2, ';', sizeof street);
988 extract_token(city, value, 3, ';', sizeof city);
989 extract_token(state, value, 4, ';', sizeof state);
990 extract_token(zipcode, value, 5, ';', sizeof zipcode);
991 extract_token(country, value, 6, ';', sizeof country);
994 else if (!strcasecmp(prp, "tel")) {
996 if (bmstrcasestr(prm, "home")) {
997 extract_token(hometel, value, 0, ';', sizeof hometel);
999 else if (bmstrcasestr(prm, "work")) {
1000 extract_token(worktel, value, 0, ';', sizeof worktel);
1002 else if (bmstrcasestr(prm, "fax")) {
1003 extract_token(faxtel, value, 0, ';', sizeof faxtel);
1005 else if (bmstrcasestr(prm, "cell")) {
1006 extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
1008 else { /* Missing or unknown type; put it in the home phone */
1009 extract_token(hometel, value, 0, ';', sizeof hometel);
1013 else if ( (!strcasecmp(prp, "email")) && (bmstrcasestr(prm, "internet")) ) {
1014 if (primary_inetemail[0] == 0) {
1015 safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
1018 if (other_inetemail[0] != 0) {
1019 strcat(other_inetemail, "\n");
1021 strcat(other_inetemail, value);
1025 /* Unrecognized properties are preserved here so we don't discard them
1026 * just because the vCard was edited with WebCit.
1029 strcat(extrafields, key);
1030 strcat(extrafields, ":");
1031 strcat(extrafields, value);
1032 strcat(extrafields, "\n");
1040 /* Display the form */
1041 output_headers(1, 1, 1, 0, 0, 0);
1043 do_template("box_begin_1");
1044 StrBufAppendBufPlain(WC->WBuf, _("Edit contact information"), -1, 0);
1045 do_template("box_begin_2");
1047 wc_printf("<form method=\"POST\" action=\"submit_vcard\">\n");
1048 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1050 if (force_room != NULL) {
1051 wc_printf("<input type=\"hidden\" name=\"force_room\" value=\"");
1052 escputs(force_room);
1057 wc_printf("<input type=\"hidden\" name=\"go\" value=\"");
1058 StrEscAppend(WCC->WBuf, WCC->CurRoom.name, NULL, 0, 0);
1062 wc_printf("<table class=\"vcard_edit_background\"><tr><td>\n");
1064 wc_printf("<table border=\"0\"><tr>"
1069 "<td>%s</td></tr>\n",
1070 _("Prefix"), _("First Name"), _("Middle Name"), _("Last Name"), _("Suffix")
1072 wc_printf("<tr><td><input type=\"text\" name=\"prefix\" "
1073 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
1075 wc_printf("<td><input type=\"text\" name=\"firstname\" "
1076 "value=\"%s\" maxlength=\"29\"></td>",
1078 wc_printf("<td><input type=\"text\" name=\"middlename\" "
1079 "value=\"%s\" maxlength=\"29\"></td>",
1081 wc_printf("<td><input type=\"text\" name=\"lastname\" "
1082 "value=\"%s\" maxlength=\"29\"></td>",
1084 wc_printf("<td><input type=\"text\" name=\"suffix\" "
1085 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
1088 wc_printf("<table class=\"vcard_edit_background_alt\">");
1089 wc_printf("<tr><td>");
1091 wc_printf(_("Display name:"));
1093 "<input type=\"text\" name=\"fullname\" "
1094 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1098 wc_printf(_("Title:"));
1100 "<input type=\"text\" name=\"title\" "
1101 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1105 wc_printf(_("Organization:"));
1107 "<input type=\"text\" name=\"org\" "
1108 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1112 wc_printf("</td><td>");
1114 wc_printf("<table border=\"0\">");
1115 wc_printf("<tr><td>");
1116 wc_printf(_("PO box:"));
1117 wc_printf("</td><td>"
1118 "<input type=\"text\" name=\"pobox\" "
1119 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1121 wc_printf("<tr><td>");
1122 wc_printf(_("Address:"));
1123 wc_printf("</td><td>"
1124 "<input type=\"text\" name=\"extadr\" "
1125 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1127 wc_printf("<tr><td> </td><td>"
1128 "<input type=\"text\" name=\"street\" "
1129 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1131 wc_printf("<tr><td>");
1132 wc_printf(_("City:"));
1133 wc_printf("</td><td>"
1134 "<input type=\"text\" name=\"city\" "
1135 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1137 wc_printf("<tr><td>");
1138 wc_printf(_("State:"));
1139 wc_printf("</td><td>"
1140 "<input type=\"text\" name=\"state\" "
1141 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1143 wc_printf("<tr><td>");
1144 wc_printf(_("ZIP code:"));
1145 wc_printf("</td><td>"
1146 "<input type=\"text\" name=\"zipcode\" "
1147 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
1149 wc_printf("<tr><td>");
1150 wc_printf(_("Country:"));
1151 wc_printf("</td><td>"
1152 "<input type=\"text\" name=\"country\" "
1153 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
1155 wc_printf("</table>\n");
1157 wc_printf("</table>\n");
1159 wc_printf("<table border=0><tr><td>");
1160 wc_printf(_("Home telephone:"));
1162 "<td><input type=\"text\" name=\"hometel\" "
1163 "value=\"%s\" maxlength=\"29\"></td>\n",
1166 wc_printf(_("Work telephone:"));
1168 "<td><input type=\"text\" name=\"worktel\" "
1169 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1171 wc_printf("<tr><td>");
1172 wc_printf(_("Mobile telephone:"));
1174 "<td><input type=\"text\" name=\"mobiletel\" "
1175 "value=\"%s\" maxlength=\"29\"></td>\n",
1178 wc_printf(_("Fax number:"));
1180 "<td><input type=\"text\" name=\"faxtel\" "
1181 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
1184 wc_printf("<table class=\"vcard_edit_background_alt\">");
1185 wc_printf("<tr><td>");
1187 wc_printf("<table border=0><TR>"
1189 wc_printf(_("Primary Internet e-mail address"));
1191 "<input type=\"text\" name=\"primary_inetemail\" "
1192 "size=40 maxlength=60 value=\"");
1193 escputs(primary_inetemail);
1195 "</td><td valign=top>");
1196 wc_printf(_("Internet e-mail aliases"));
1198 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
1199 escputs(other_inetemail);
1200 wc_printf("</textarea></td></tr></table>\n");
1202 wc_printf("</td></tr></table>\n");
1204 wc_printf("<input type=\"hidden\" name=\"extrafields\" value=\"");
1205 escputs(extrafields);
1208 wc_printf("<input type=\"hidden\" name=\"return_to\" value=\"");
1212 wc_printf("<div class=\"buttons\">\n"
1213 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
1215 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
1221 wc_printf("</td></tr></table>\n");
1222 do_template("box_end");
1225 DestroyMessageSummary(Msg);
1231 * commit the edits to the citadel server
1233 void edit_vcard(void) {
1237 msgnum = lbstr("msgnum");
1238 partnum = bstr("partnum");
1239 do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
1245 * parse edited vcard from the browser
1247 void submit_vcard(void) {
1249 char *serialized_vcard;
1252 const StrBuf *ForceRoom;
1255 if (!havebstr("ok_button")) {
1256 readloop(readnew, eUseDefault);
1260 if (havebstr("force_room")) {
1261 ForceRoom = sbstr("force_room");
1262 if (gotoroom(ForceRoom) != 200) {
1263 AppendImportantMessage(_("Unable to enter the room to save your message"), -1);
1264 AppendImportantMessage(HKEY(": "));
1265 AppendImportantMessage(SKEY(ForceRoom));
1266 AppendImportantMessage(HKEY("; "));
1267 AppendImportantMessage(_("Aborting."), -1);
1269 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1270 select_user_to_edit(NULL);
1272 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1275 else if (!IsEmptyStr(bstr("return_to"))) {
1276 http_redirect(bstr("return_to"));
1279 readloop(readnew, eUseDefault);
1286 serv_write(HKEY("ENT0 1|||4\n"));
1287 if (!StrBuf_ServGetln(Buf) && (GetServerStatus(Buf, NULL) != 4))
1293 /* Make a vCard structure out of the data supplied in the form */
1294 StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1297 v = VCardLoad(Buf); /* Start with the extra fields */
1299 AppendImportantMessage(_("An error has occurred."), -1);
1305 snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
1311 vcard_add_prop(v, "n", buf);
1313 vcard_add_prop(v, "title", bstr("title"));
1314 vcard_add_prop(v, "fn", bstr("fullname"));
1315 vcard_add_prop(v, "org", bstr("org"));
1317 snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
1325 vcard_add_prop(v, "adr", buf);
1327 vcard_add_prop(v, "tel;home", bstr("hometel"));
1328 vcard_add_prop(v, "tel;work", bstr("worktel"));
1329 vcard_add_prop(v, "tel;fax", bstr("faxtel"));
1330 vcard_add_prop(v, "tel;cell", bstr("mobiletel"));
1331 vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
1333 for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
1334 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
1335 if (!IsEmptyStr(buf)) {
1336 vcard_add_prop(v, "email;internet", buf);
1340 serialized_vcard = vcard_serialize(v);
1342 if (serialized_vcard == NULL) {
1343 AppendImportantMessage(_("An error has occurred."), -1);
1349 serv_write(HKEY("Content-type: text/x-vcard; charset=UTF-8\n"));
1350 serv_write(HKEY("\n"));
1351 serv_printf("%s\r\n", serialized_vcard);
1352 serv_write(HKEY("000\n"));
1353 free(serialized_vcard);
1355 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1356 select_user_to_edit(NULL);
1358 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1361 else if (!IsEmptyStr(bstr("return_to"))) {
1362 http_redirect(bstr("return_to"));
1365 readloop(readnew, eUseDefault);
1373 * Extract an embedded photo from a vCard for display on the client
1375 void display_vcard_photo_img(void)
1381 const char *contentType;
1382 wcsession *WCC = WC;
1384 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1386 vcard = load_mimepart(msgnum,"1");
1387 v = VCardLoad(vcard);
1389 photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
1390 FlushStrBuf(WCC->WBuf);
1391 StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
1392 if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
1393 FlushStrBuf(WCC->WBuf);
1395 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
1396 output_headers(0, 0, 0, 0, 0, 0);
1397 hprintf("Content-Type: text/plain\r\n");
1399 wc_printf(_("Could Not decode vcard photo\n"));
1403 contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1404 http_transmit_thing(contentType, 0);
1409 typedef struct _vcardview_struct {
1411 addrbookent *addrbook;
1416 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat,
1417 void **ViewSpecific,
1424 vcardview_struct *VS;
1426 VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
1427 memset(VS, 0, sizeof(vcardview_struct));
1428 *ViewSpecific = (void*)VS;
1430 VS->is_singlecard = ibstr("is_singlecard");
1431 if (VS->is_singlecard != 1) {
1432 if (oper == do_search) {
1433 snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
1436 strcpy(cmd, "MSGS ALL");
1438 Stat->maxmsgs = 9999999;
1443 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat,
1444 void **ViewSpecific,
1445 message_summary* Msg,
1449 vcardview_struct *VS;
1452 VS = (vcardview_struct*) *ViewSpecific;
1455 fetch_ab_name(Msg, &ab_name);
1456 if (ab_name == NULL)
1459 VS->addrbook = realloc(VS->addrbook,
1460 (sizeof(addrbookent) * VS->num_ab) );
1461 safestrncpy(VS->addrbook[VS->num_ab-1].ab_name, ab_name,
1462 sizeof(VS->addrbook[VS->num_ab-1].ab_name));
1463 VS->addrbook[VS->num_ab-1].ab_msgnum = Msg->msgnum;
1469 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1472 vcardview_struct *VS;
1474 VS = (vcardview_struct*) *ViewSpecific;
1475 if (VS->is_singlecard)
1476 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1478 do_addrbook_view(VS->addrbook, VS->num_ab); /* Render the address book */
1482 int vcard_Cleanup(void **ViewSpecific)
1484 vcardview_struct *VS;
1486 VS = (vcardview_struct*) *ViewSpecific;
1489 (VS->addrbook != NULL))
1497 ServerStartModule_VCARD
1500 VCToEnum = NewHash(0, NULL);
1505 ServerShutdownModule_VCARD
1508 DeleteHash(&VCToEnum);
1515 RegisterCTX(CTX_VCARD);
1516 RegisterReadLoopHandlerset(
1518 vcard_GetParamsGetServerCall,
1522 vcard_LoadMsgFromServer,
1523 vcard_RenderView_or_Tail,
1525 WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1526 WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1527 WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1529 Put(VCToEnum, HKEY("n"), (void*)VC_n, reference_free_handler);
1530 Put(VCToEnum, HKEY("fn"), (void*)VC_fn, reference_free_handler);
1531 Put(VCToEnum, HKEY("title"), (void*)VC_title, reference_free_handler);
1532 Put(VCToEnum, HKEY("org"), (void*)VC_org, reference_free_handler);
1533 Put(VCToEnum, HKEY("email"), (void*)VC_email, reference_free_handler);
1534 Put(VCToEnum, HKEY("tel"), (void*)VC_tel, reference_free_handler);
1535 Put(VCToEnum, HKEY("tel_tel"), (void*)VC_tel_tel, reference_free_handler);
1536 Put(VCToEnum, HKEY("tel_work"), (void*)VC_tel_work, reference_free_handler);
1537 Put(VCToEnum, HKEY("tel_home"), (void*)VC_tel_home, reference_free_handler);
1538 Put(VCToEnum, HKEY("tel_cell"), (void*)VC_tel_cell, reference_free_handler);
1539 Put(VCToEnum, HKEY("adr"), (void*)VC_adr, reference_free_handler);
1540 Put(VCToEnum, HKEY("photo"), (void*)VC_photo, reference_free_handler);
1541 Put(VCToEnum, HKEY("version"), (void*)VC_version, reference_free_handler);
1542 Put(VCToEnum, HKEY("rev"), (void*)VC_rev, reference_free_handler);
1543 Put(VCToEnum, HKEY("label"), (void*)VC_label, reference_free_handler);
1546 RegisterNamespace("VC", 1, 2, tmplput_VCARD_ITEM, NULL, CTX_VCARD);
1548 REGISTERTokenParamDefine(VC_n);
1549 REGISTERTokenParamDefine(VC_fn);
1550 REGISTERTokenParamDefine(VC_title);
1551 REGISTERTokenParamDefine(VC_org);
1552 REGISTERTokenParamDefine(VC_email);
1553 REGISTERTokenParamDefine(VC_tel);
1554 REGISTERTokenParamDefine(VC_tel_tel);
1555 REGISTERTokenParamDefine(VC_tel_work);
1556 REGISTERTokenParamDefine(VC_tel_home);
1557 REGISTERTokenParamDefine(VC_tel_cell);
1558 REGISTERTokenParamDefine(VC_adr);
1559 REGISTERTokenParamDefine(VC_photo);
1560 REGISTERTokenParamDefine(VC_version);
1561 REGISTERTokenParamDefine(VC_rev);
1562 REGISTERTokenParamDefine(VC_label);