2 * Copyright (c) 1996-2011 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 as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "webserver.h"
26 {HKEY("n")}, /* N is name, but only if there's no FN already there */
27 {HKEY("fn")}, /* FN (full name) is a true 'display name' field */
28 {HKEY("title")}, /* title */
29 {HKEY("org")}, /* organization */
61 HashList *VCToEnum = NULL;
64 * Record compare function for sorting address book indices
66 int abcmp(const void *ab1, const void *ab2) {
68 (((const addrbookent *)ab1)->ab_name),
69 (((const addrbookent *)ab2)->ab_name)
75 * Helper function for do_addrbook_view()
76 * Converts a name into a three-letter tab label
78 void nametab(char *tabbuf, long len, char *name) {
79 stresc(tabbuf, len, name, 0, 0);
80 tabbuf[0] = toupper(tabbuf[0]);
81 tabbuf[1] = tolower(tabbuf[1]);
82 tabbuf[2] = tolower(tabbuf[2]);
88 * If it's an old "Firstname Lastname" style record, try to convert it.
90 void lastfirst_firstlast(char *namebuf) {
95 if (namebuf == NULL) return;
96 if (strchr(namebuf, ';') != NULL) return;
98 i = num_tokens(namebuf, ' ');
101 extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
102 remove_token(namebuf, i-1, ' ');
103 strcpy(firstname, namebuf);
104 sprintf(namebuf, "%s; %s", lastname, firstname);
109 wc_mime_attachment *load_vcard(message_summary *Msg)
112 StrBuf *FoundCharset = NewStrBuf();
117 wc_mime_attachment *Mime;
118 wc_mime_attachment *VCMime = NULL;
120 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
121 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
122 Msg->MsgBody->msgnum = Msg->msgnum;
124 load_message(Msg, FoundCharset, &Error);
126 FreeStrBuf(&FoundCharset);
127 /* look up the vcard... */
128 it = GetNewHashPos(Msg->AllAttach, 0);
129 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
132 Mime = (wc_mime_attachment*) vMime;
133 if ((strcmp(ChrPtr(Mime->ContentType),
134 "text/x-vcard") == 0) ||
135 (strcmp(ChrPtr(Mime->ContentType),
146 MimeLoadData(VCMime);
151 * fetch the display name off a vCard
153 void fetch_ab_name(message_summary *Msg, char **namebuf) {
156 wc_mime_attachment *VCMime = NULL;
158 if (namebuf == NULL) return;
160 VCMime = load_vcard(Msg);
164 /* Grab the name off the card */
165 display_vcard(WC->WBuf, VCMime, 0, 0, namebuf, Msg->msgnum);
167 if (*namebuf != NULL) {
168 lastfirst_firstlast(*namebuf);
170 len = strlen(*namebuf);
171 for (i=0; i<len; ++i) {
172 if ((*namebuf)[i] != ';') return;
175 (*namebuf) = strdup(_("(no name)"));
178 (*namebuf) = strdup(_("(no name)"));
185 * Turn a vCard "n" (name) field into something displayable.
187 void vcard_n_prettyize(char *name)
192 original_name = strdup(name);
193 len = strlen(original_name);
194 for (i=0; i<5; ++i) {
196 if (original_name[len-1] == ' ') {
197 original_name[--len] = 0;
199 if (original_name[len-1] == ';') {
200 original_name[--len] = 0;
206 for (i=0; i<len; ++i) {
207 if (original_name[i] == ';') {
212 name[j++] = original_name[i];
223 * preparse a vcard name
224 * display_vcard() calls this after parsing the textual vCard into
225 * our 'struct vCard' data object.
226 * This gets called instead of display_parsed_vcard() if we are only looking
227 * to extract the person's name instead of displaying the card.
229 void fetchname_parsed_vcard(struct vCard *v, char **storename) {
239 name = vcard_get_prop(v, "n", 1, 0, 0);
242 prop = vcard_get_prop(v, "n", 1, 0, 1);
243 n = num_tokens(prop, ';');
245 for (j=0; j<n; ++j) {
246 extract_token(buf, prop, j, ';', sizeof buf);
247 if (!strcasecmp(buf, "encoding=quoted-printable")) {
250 if (!strcasecmp(buf, "encoding=base64")) {
255 // %ff can become 6 bytes in utf8
256 *storename = malloc(len * 2 + 3);
257 j = CtdlDecodeQuotedPrintable(
263 // ff will become one byte..
264 *storename = malloc(len + 50);
274 *storename = malloc(len + 3); /* \0 + eventualy missing ', '*/
275 memcpy(*storename, name, len + 1);
277 /* vcard_n_prettyize(storename); */
286 * display_vcard() calls this after parsing the textual vCard into
287 * our 'struct vCard' data object.
289 * Set 'full' to nonzero to display the full card, otherwise it will only
290 * show a summary line.
292 * This code is a bit ugly, so perhaps an explanation is due: we do this
293 * in two passes through the vCard fields. On the first pass, we process
294 * fields we understand, and then render them in a pretty fashion at the
295 * end. Then we make a second pass, outputting all the fields we don't
296 * understand in a simple two-column name/value format.
297 * v the vCard to display
298 * full display all items of the vcard?
299 * msgnum Citadel message pointer
301 void display_parsed_vcard(StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
308 char *thisname, *thisvalue;
309 char firsttoken[SIZ];
318 strcpy(fullname, "");
325 StrBufAppendPrintf(Target, "<td>");
326 name = vcard_get_prop(v, "fn", 1, 0, 0);
328 StrEscAppend(Target, NULL, name, 0, 0);
330 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
331 strcpy(fullname, name);
332 vcard_n_prettyize(fullname);
333 StrEscAppend(Target, NULL, fullname, 0, 0);
336 StrBufAppendPrintf(Target, " ");
338 StrBufAppendPrintf(Target, "</td>");
342 StrBufAppendPrintf(Target, "<div align=\"center\">"
343 "<table bgcolor=\"#aaaaaa\" width=\"50%%\">");
344 for (pass=1; pass<=2; ++pass) {
346 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
348 thisname = strdup(v->prop[i].name);
349 extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
351 for (j=0; j<num_tokens(thisname, ';'); ++j) {
352 extract_token(buf, thisname, j, ';', sizeof buf);
353 if (!strcasecmp(buf, "encoding=quoted-printable")) {
355 remove_token(thisname, j, ';');
357 if (!strcasecmp(buf, "encoding=base64")) {
359 remove_token(thisname, j, ';');
363 len = strlen(v->prop[i].value);
364 /* if we have some untagged QP, detect it here. */
365 if (!is_qp && (strstr(v->prop[i].value, "=?")!=NULL))
366 utf8ify_rfc822_string(&v->prop[i].value);
369 // %ff can become 6 bytes in utf8
370 thisvalue = malloc(len * 2 + 3);
371 j = CtdlDecodeQuotedPrintable(
372 thisvalue, v->prop[i].value,
377 // ff will become one byte..
378 thisvalue = malloc(len + 50);
380 thisvalue, v->prop[i].value,
381 strlen(v->prop[i].value) );
384 thisvalue = strdup(v->prop[i].value);
387 /* Various fields we may encounter ***/
389 /* N is name, but only if there's no FN already there */
390 if (!strcasecmp(firsttoken, "n")) {
391 if (IsEmptyStr(fullname)) {
392 strcpy(fullname, thisvalue);
393 vcard_n_prettyize(fullname);
397 /* FN (full name) is a true 'display name' field */
398 else if (!strcasecmp(firsttoken, "fn")) {
399 strcpy(fullname, thisvalue);
403 else if (!strcasecmp(firsttoken, "title")) {
404 strcpy(title, thisvalue);
408 else if (!strcasecmp(firsttoken, "org")) {
409 strcpy(org, thisvalue);
412 else if (!strcasecmp(firsttoken, "email")) {
414 if (!IsEmptyStr(mailto)) strcat(mailto, "<br>");
416 "<a href=\"display_enter"
417 "?force_room=_MAIL_?recp=");
419 len = strlen(mailto);
420 urlesc(&mailto[len], SIZ - len, "\"");
421 len = strlen(mailto);
422 urlesc(&mailto[len], SIZ - len, fullname);
423 len = strlen(mailto);
424 urlesc(&mailto[len], SIZ - len, "\" <");
425 len = strlen(mailto);
426 urlesc(&mailto[len], SIZ - len, thisvalue);
427 len = strlen(mailto);
428 urlesc(&mailto[len], SIZ - len, ">");
430 strcat(mailto, "\">");
431 len = strlen(mailto);
432 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
433 strcat(mailto, "</A>");
435 else if (!strcasecmp(firsttoken, "tel")) {
436 if (!IsEmptyStr(phone)) strcat(phone, "<br>");
437 strcat(phone, thisvalue);
438 for (j=0; j<num_tokens(thisname, ';'); ++j) {
439 extract_token(buf, thisname, j, ';', sizeof buf);
440 if (!strcasecmp(buf, "tel"))
442 else if (!strcasecmp(buf, "work"))
443 strcat(phone, _(" (work)"));
444 else if (!strcasecmp(buf, "home"))
445 strcat(phone, _(" (home)"));
446 else if (!strcasecmp(buf, "cell"))
447 strcat(phone, _(" (cell)"));
455 else if (!strcasecmp(firsttoken, "adr")) {
457 StrBufAppendPrintf(Target, "<tr><td>");
458 StrBufAppendPrintf(Target, _("Address:"));
459 StrBufAppendPrintf(Target, "</td><td>");
460 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
461 extract_token(buf, thisvalue, j, ';', sizeof buf);
462 if (!IsEmptyStr(buf)) {
463 StrEscAppend(Target, NULL, buf, 0, 0);
464 if (j<3) StrBufAppendPrintf(Target, "<br>");
465 else StrBufAppendPrintf(Target, " ");
468 StrBufAppendPrintf(Target, "</td></tr>\n");
471 /* else if (!strcasecmp(firsttoken, "photo") && full && pass == 2) {
472 // Only output on second pass
473 StrBufAppendPrintf(Target, "<tr><td>");
474 StrBufAppendPrintf(Target, _("Photo:"));
475 StrBufAppendPrintf(Target, "</td><td>");
476 StrBufAppendPrintf(Target, "<img src=\"/vcardphoto/%ld/\" alt=\"Contact photo\"/>",msgnum);
477 StrBufAppendPrintf(Target, "</td></tr>\n");
479 else if (!strcasecmp(firsttoken, "version")) {
482 else if (!strcasecmp(firsttoken, "rev")) {
485 else if (!strcasecmp(firsttoken, "label")) {
490 /*** Don't show extra fields. They're ugly.
492 StrBufAppendPrintf(Target, "<TR><TD>");
493 StrEscAppend(Target, NULL, thisname, 0, 0);
494 StrBufAppendPrintf(Target, "</TD><TD>");
495 StrEscAppend(Target, NULL, thisvalue, 0, 0);
496 StrBufAppendPrintf(Target, "</TD></TR>\n");
506 StrBufAppendPrintf(Target, "<tr bgcolor=\"#aaaaaa\">"
507 "<td colspan=2 bgcolor=\"#ffffff\">"
508 "<img align=\"center\" src=\"static/icons/essen/32x32/contact.png\">"
509 "<font size=\"+1\"><b>");
510 StrEscAppend(Target, NULL, fullname, 0, 0);
511 StrBufAppendPrintf(Target, "</b></font>");
512 if (!IsEmptyStr(title)) {
513 StrBufAppendPrintf(Target, "<div align=\"right>\"");
514 StrEscAppend(Target, NULL, title, 0, 0);
515 StrBufAppendPrintf(Target, "</div>");
517 if (!IsEmptyStr(org)) {
518 StrBufAppendPrintf(Target, "<div align=\"right\">");
519 StrEscAppend(Target, NULL, org, 0, 0);
520 StrBufAppendPrintf(Target, "</div>");
522 StrBufAppendPrintf(Target, "</td></tr>\n");
524 if (!IsEmptyStr(phone)) {
525 StrBufAppendPrintf(Target, "<tr><td>");
526 StrBufAppendPrintf(Target, _("Telephone:"));
527 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", phone);
529 if (!IsEmptyStr(mailto)) {
530 StrBufAppendPrintf(Target, "<tr><td>");
531 StrBufAppendPrintf(Target, _("E-mail:"));
532 StrBufAppendPrintf(Target, "</td><td>%s</td></tr>\n", mailto);
538 StrBufAppendPrintf(Target, "</table></div>\n");
543 * display_vcard() calls this after parsing the textual vCard into
544 * our 'struct vCard' data object.
546 * Set 'full' to nonzero to display the full card, otherwise it will only
547 * show a summary line.
549 * This code is a bit ugly, so perhaps an explanation is due: we do this
550 * in two passes through the vCard fields. On the first pass, we process
551 * fields we understand, and then render them in a pretty fashion at the
552 * end. Then we make a second pass, outputting all the fields we don't
553 * understand in a simple two-column name/value format.
554 * v the vCard to display
555 * full display all items of the vcard?
556 * msgnum Citadel message pointer
558 void parse_vcard(StrBuf *Target, struct vCard *v, HashList *VC, int full, wc_mime_attachment *Mime)
567 StrBuf *thisname = NULL;
568 char *thisvalue = NULL;
569 char firsttoken[SIZ];
575 thisname = NewStrBuf();
576 for (i=0; i<(v->numprops); ++i) {
580 StrBufPlain(thisname, v->prop[i].name, -1);
581 StrBufLowerCase(thisname);
583 len = extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
585 for (j=0; j<num_tokens(thisname, ';'); ++j) {
586 extract_token(buf, thisname, j, ';', sizeof buf);
587 if (!strcasecmp(buf, "encoding=quoted-printable")) {
589 remove_token(thisname, j, ';');
591 if (!strcasecmp(buf, "encoding=base64")) {
593 remove_token(thisname, j, ';');
597 /* copy over the payload into a StrBuf */
598 Val = NewStrBufPlain(v->prop[i].value, -1);
600 /* if we have some untagged QP, detect it here. */
601 if (is_qp || (strstr(v->prop[i].value, "=?")!=NULL)){
603 StrBuf_RFC822_to_Utf8(Swap, Val, NULL, NULL); /* default charset, current charset */
610 StrBufDecodeBase64(Val);
613 syslog(1, "%s [%s][%s]",
617 if (GetHash(VCToEnum, firsttoken, strlen(firsttoken), &V))
620 Put(VC, IKEY(evc), Val, HFreeStrBuf);
621 syslog(1, "[%ld]\n", evc);
627 TODO: check for layer II
630 long max = num_tokens(thisname, ';');
631 firsttoken[len] = '_';
633 for (j = 0; j < max; j++) {
636 extract_token(buf, thisname, j, ';', sizeof (buf));
637 if (!strcasecmp(buf, "tel"))
639 else if (!strcasecmp(buf, "work"))
640 strcat(phone, _(" (work)"));
641 else if (!strcasecmp(buf, "home"))
642 strcat(phone, _(" (home)"));
643 else if (!strcasecmp(buf, "cell"))
644 strcat(phone, _(" (cell)"));
663 void tmplput_VCARD_ITEM(StrBuf *Target, WCTemplputParams *TP)
669 evc = GetTemplateTokenNumber(Target, TP, 0, -1);
672 if (GetHash(VC, IKEY(evc), &vStr))
674 StrBufAppendTemplate(Target, TP,
682 void new_vcard (StrBuf *Target, struct vCard *v, int full, wc_mime_attachment *Mime)
685 WCTemplputParams SubTP;
687 memset(&SubTP, 0, sizeof(WCTemplputParams));
690 VC = NewHash(0, Flathash);
691 parse_vcard(Target, v, VC, full, Mime);
693 SubTP.Filter.ContextType = CTX_VCARD;
696 DoTemplate(HKEY("test_vcard"), Target, &SubTP);
703 * Display a textual vCard
704 * (Converts to a vCard object and then calls the actual display function)
705 * Set 'full' to nonzero to display the whole card instead of a one-liner.
706 * Or, if "storename" is non-NULL, just store the person's name in that
707 * buffer instead of displaying the card at all.
709 * vcard_source the buffer containing the vcard text
710 * alpha Display only if name begins with this letter of the alphabet
711 * full Display the full vCard (otherwise just the display name)
712 * storename If not NULL, also store the display name here
713 * msgnum Citadel message pointer
715 void display_vcard(StrBuf *Target,
716 wc_mime_attachment *Mime,
728 v = VCardLoad(Mime->Data);
730 if (v == NULL) return;
732 name = vcard_get_prop(v, "n", 1, 0, 0);
734 Buf = NewStrBufPlain(name, -1);
735 Buf2 = NewStrBufPlain(NULL, StrLength(Buf));
736 StrBuf_RFC822_to_Utf8(Buf2, Buf, WC->DefaultCharset, NULL);
737 this_alpha = ChrPtr(Buf)[0];
742 if (storename != NULL) {
743 fetchname_parsed_vcard(v, storename);
745 else if ((alpha == 0) ||
746 ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha))) ||
747 ((!isalpha(alpha)) && (!isalpha(this_alpha)))
751 new_vcard (Target, v, full, Mime);
753 display_parsed_vcard(Target, v, full, Mime);
763 * Render the address book using info we gathered during the scan
765 * addrbook the addressbook to render
766 * num_ab the number of the addressbook
768 void do_addrbook_view(addrbookent *addrbook, int num_ab) {
772 static int NAMESPERPAGE = 60;
775 char tabfirst_label[64];
777 char tablast_label[64];
778 char this_tablabel[64];
783 wc_printf("<br><br><br><div align=\"center\"><i>");
784 wc_printf(_("This address book is empty."));
785 wc_printf("</i></div>\n");
790 qsort(addrbook, num_ab, sizeof(addrbookent), abcmp);
793 num_pages = (num_ab / NAMESPERPAGE) + 1;
795 tablabels = malloc(num_pages * sizeof (char *));
796 if (tablabels == NULL) {
797 wc_printf("<br><br><br><div align=\"center\"><i>");
798 wc_printf(_("An internal error has occurred."));
799 wc_printf("</i></div>\n");
803 for (i=0; i<num_pages; ++i) {
804 tabfirst = i * NAMESPERPAGE;
805 tablast = tabfirst + NAMESPERPAGE - 1;
806 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
807 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
808 nametab(tablast_label, 64, addrbook[tablast].ab_name);
809 sprintf(this_tablabel, "%s - %s", tabfirst_label, tablast_label);
810 tablabels[i] = strdup(this_tablabel);
813 tabbed_dialog(num_pages, tablabels);
816 for (i=0; i<num_ab; ++i) {
818 if ((i / NAMESPERPAGE) != page) { /* New tab */
819 page = (i / NAMESPERPAGE);
821 wc_printf("</tr></table>\n");
822 end_tab(page-1, num_pages);
824 begin_tab(page, num_pages);
825 wc_printf("<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%%\">\n");
829 if ((displayed % 4) == 0) {
831 wc_printf("</tr>\n");
834 wc_printf("<tr bgcolor=\"#%s\">",
835 (bg ? "dddddd" : "ffffff")
841 wc_printf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
842 addrbook[i].ab_msgnum);
843 wc_printf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
844 vcard_n_prettyize(addrbook[i].ab_name);
845 escputs(addrbook[i].ab_name);
846 wc_printf("</a></td>\n");
850 /* Placeholders for empty columns at end */
851 if ((num_ab % 4) != 0) {
852 for (i=0; i<(4-(num_ab % 4)); ++i) {
853 wc_printf("<td> </td>");
857 wc_printf("</tr></table>\n");
858 end_tab((num_pages-1), num_pages);
860 begin_tab(num_pages, num_pages);
861 /* FIXME there ought to be something here */
862 end_tab(num_pages, num_pages);
864 for (i=0; i<num_pages; ++i) {
874 * Edit the vCard component of a MIME message.
875 * Supply the message number
876 * and MIME part number to fetch. Or, specify -1 for the message number
877 * to start with a blank card.
879 void do_edit_vcard(long msgnum, char *partnum,
880 message_summary *VCMsg,
881 wc_mime_attachment *VCAtt,
882 const char *return_to,
883 const char *force_room) {
884 message_summary *Msg = NULL;
885 wc_mime_attachment *VCMime = NULL;
893 char middlename[256];
907 char primary_inetemail[256];
908 char other_inetemail[SIZ];
909 char extrafields[SIZ];
930 primary_inetemail[0] = 0;
931 other_inetemail[0] = 0;
937 safestrncpy(whatuser, "", sizeof whatuser);
940 ((VCMsg != NULL) && (VCAtt != NULL)))
942 if ((VCMsg == NULL) && (VCAtt == NULL)) {
944 Msg = (message_summary *) malloc(sizeof(message_summary));
945 memset(Msg, 0, sizeof(message_summary));
946 Msg->msgnum = msgnum;
947 VCMime = load_vcard(Msg);
948 if (VCMime == NULL) {
949 convenience_page("770000", _("Error"), "");///TODO: important message
950 DestroyMessageSummary(Msg);
954 v = VCardLoad(VCMime->Data);
957 v = VCardLoad(VCAtt->Data);
960 /* Populate the variables for our form */
962 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
963 char prp[256]; /* property name */
964 char prm[256]; /* parameters */
966 value = vcard_get_prop(v, "", 0, i++, 0);
969 extract_token(prp, key, 0, ';', sizeof prp);
970 safestrncpy(prm, key, sizeof prm);
971 remove_token(prm, 0, ';');
973 if (!strcasecmp(prp, "n")) {
974 extract_token(lastname, value, 0, ';', sizeof lastname);
975 extract_token(firstname, value, 1, ';', sizeof firstname);
976 extract_token(middlename, value, 2, ';', sizeof middlename);
977 extract_token(prefix, value, 3, ';', sizeof prefix);
978 extract_token(suffix, value, 4, ';', sizeof suffix);
981 else if (!strcasecmp(prp, "fn")) {
982 safestrncpy(fullname, value, sizeof fullname);
985 else if (!strcasecmp(prp, "title")) {
986 safestrncpy(title, value, sizeof title);
989 else if (!strcasecmp(prp, "org")) {
990 safestrncpy(org, value, sizeof org);
993 else if (!strcasecmp(prp, "adr")) {
994 extract_token(pobox, value, 0, ';', sizeof pobox);
995 extract_token(extadr, value, 1, ';', sizeof extadr);
996 extract_token(street, value, 2, ';', sizeof street);
997 extract_token(city, value, 3, ';', sizeof city);
998 extract_token(state, value, 4, ';', sizeof state);
999 extract_token(zipcode, value, 5, ';', sizeof zipcode);
1000 extract_token(country, value, 6, ';', sizeof country);
1003 else if (!strcasecmp(prp, "tel")) {
1005 if (bmstrcasestr(prm, "home")) {
1006 extract_token(hometel, value, 0, ';', sizeof hometel);
1008 else if (bmstrcasestr(prm, "work")) {
1009 extract_token(worktel, value, 0, ';', sizeof worktel);
1011 else if (bmstrcasestr(prm, "fax")) {
1012 extract_token(faxtel, value, 0, ';', sizeof faxtel);
1014 else if (bmstrcasestr(prm, "cell")) {
1015 extract_token(mobiletel, value, 0, ';', sizeof mobiletel);
1017 else { /* Missing or unknown type; put it in the home phone */
1018 extract_token(hometel, value, 0, ';', sizeof hometel);
1022 else if ( (!strcasecmp(prp, "email")) && (bmstrcasestr(prm, "internet")) ) {
1023 if (primary_inetemail[0] == 0) {
1024 safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
1027 if (other_inetemail[0] != 0) {
1028 strcat(other_inetemail, "\n");
1030 strcat(other_inetemail, value);
1034 /* Unrecognized properties are preserved here so we don't discard them
1035 * just because the vCard was edited with WebCit.
1038 strcat(extrafields, key);
1039 strcat(extrafields, ":");
1040 strcat(extrafields, value);
1041 strcat(extrafields, "\n");
1049 /* Display the form */
1050 output_headers(1, 1, 1, 0, 0, 0);
1052 do_template("beginbox_1");
1053 StrBufAppendBufPlain(WC->WBuf, _("Edit contact information"), -1, 0);
1054 do_template("beginbox_2");
1056 wc_printf("<form method=\"POST\" action=\"submit_vcard\">\n");
1057 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1059 if (force_room != NULL) {
1060 wc_printf("<input type=\"hidden\" name=\"force_room\" value=\"");
1061 escputs(force_room);
1065 wc_printf("<table class=\"vcard_edit_background\"><tr><td>\n");
1067 wc_printf("<table border=\"0\"><tr>"
1072 "<td>%s</td></tr>\n",
1073 _("Prefix"), _("First Name"), _("Middle Name"), _("Last Name"), _("Suffix")
1075 wc_printf("<tr><td><input type=\"text\" name=\"prefix\" "
1076 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
1078 wc_printf("<td><input type=\"text\" name=\"firstname\" "
1079 "value=\"%s\" maxlength=\"29\"></td>",
1081 wc_printf("<td><input type=\"text\" name=\"middlename\" "
1082 "value=\"%s\" maxlength=\"29\"></td>",
1084 wc_printf("<td><input type=\"text\" name=\"lastname\" "
1085 "value=\"%s\" maxlength=\"29\"></td>",
1087 wc_printf("<td><input type=\"text\" name=\"suffix\" "
1088 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
1091 wc_printf("<table class=\"vcard_edit_background_alt\">");
1092 wc_printf("<tr><td>");
1094 wc_printf(_("Display name:"));
1096 "<input type=\"text\" name=\"fullname\" "
1097 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1101 wc_printf(_("Title:"));
1103 "<input type=\"text\" name=\"title\" "
1104 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1108 wc_printf(_("Organization:"));
1110 "<input type=\"text\" name=\"org\" "
1111 "value=\"%s\" maxlength=\"40\"><br><br>\n",
1115 wc_printf("</td><td>");
1117 wc_printf("<table border=\"0\">");
1118 wc_printf("<tr><td>");
1119 wc_printf(_("PO box:"));
1120 wc_printf("</td><td>"
1121 "<input type=\"text\" name=\"pobox\" "
1122 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1124 wc_printf("<tr><td>");
1125 wc_printf(_("Address:"));
1126 wc_printf("</td><td>"
1127 "<input type=\"text\" name=\"extadr\" "
1128 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1130 wc_printf("<tr><td> </td><td>"
1131 "<input type=\"text\" name=\"street\" "
1132 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1134 wc_printf("<tr><td>");
1135 wc_printf(_("City:"));
1136 wc_printf("</td><td>"
1137 "<input type=\"text\" name=\"city\" "
1138 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1140 wc_printf("<tr><td>");
1141 wc_printf(_("State:"));
1142 wc_printf("</td><td>"
1143 "<input type=\"text\" name=\"state\" "
1144 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1146 wc_printf("<tr><td>");
1147 wc_printf(_("ZIP code:"));
1148 wc_printf("</td><td>"
1149 "<input type=\"text\" name=\"zipcode\" "
1150 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
1152 wc_printf("<tr><td>");
1153 wc_printf(_("Country:"));
1154 wc_printf("</td><td>"
1155 "<input type=\"text\" name=\"country\" "
1156 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
1158 wc_printf("</table>\n");
1160 wc_printf("</table>\n");
1162 wc_printf("<table border=0><tr><td>");
1163 wc_printf(_("Home telephone:"));
1165 "<td><input type=\"text\" name=\"hometel\" "
1166 "value=\"%s\" maxlength=\"29\"></td>\n",
1169 wc_printf(_("Work telephone:"));
1171 "<td><input type=\"text\" name=\"worktel\" "
1172 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
1174 wc_printf("<tr><td>");
1175 wc_printf(_("Mobile telephone:"));
1177 "<td><input type=\"text\" name=\"mobiletel\" "
1178 "value=\"%s\" maxlength=\"29\"></td>\n",
1181 wc_printf(_("Fax number:"));
1183 "<td><input type=\"text\" name=\"faxtel\" "
1184 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
1187 wc_printf("<table class=\"vcard_edit_background_alt\">");
1188 wc_printf("<tr><td>");
1190 wc_printf("<table border=0><TR>"
1192 wc_printf(_("Primary Internet e-mail address"));
1194 "<input type=\"text\" name=\"primary_inetemail\" "
1195 "size=40 maxlength=60 value=\"");
1196 escputs(primary_inetemail);
1198 "</td><td valign=top>");
1199 wc_printf(_("Internet e-mail aliases"));
1201 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
1202 escputs(other_inetemail);
1203 wc_printf("</textarea></td></tr></table>\n");
1205 wc_printf("</td></tr></table>\n");
1207 wc_printf("<input type=\"hidden\" name=\"extrafields\" value=\"");
1208 escputs(extrafields);
1211 wc_printf("<input type=\"hidden\" name=\"return_to\" value=\"");
1215 wc_printf("<div class=\"buttons\">\n"
1216 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
1218 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
1224 wc_printf("</td></tr></table>\n");
1225 do_template("endbox");
1228 DestroyMessageSummary(Msg);
1234 * commit the edits to the citadel server
1236 void edit_vcard(void) {
1240 msgnum = lbstr("msgnum");
1241 partnum = bstr("partnum");
1242 do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
1248 * parse edited vcard from the browser
1250 void submit_vcard(void) {
1251 wcsession *WCC = WC;
1253 char *serialized_vcard;
1258 if (!havebstr("ok_button")) {
1259 readloop(readnew, eUseDefault);
1263 if (havebstr("force_room")) {
1264 if (gotoroom(sbstr("force_room")) != 200) {
1265 StrBufAppendBufPlain(WCC->ImportantMsg,
1266 _("Unable to enter the room to save your message"),
1268 StrBufAppendBufPlain(WCC->ImportantMsg,
1270 StrBufAppendBuf(WCC->ImportantMsg, sbstr("force_room"), 0);
1271 StrBufAppendBufPlain(WCC->ImportantMsg,
1274 StrBufAppendBufPlain(WCC->ImportantMsg,
1278 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1279 select_user_to_edit(NULL);
1281 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1284 else if (!IsEmptyStr(bstr("return_to"))) {
1285 http_redirect(bstr("return_to"));
1288 readloop(readnew, eUseDefault);
1294 sprintf(buf, "ENT0 1|||4||");
1296 serv_getln(buf, sizeof buf);
1297 if (buf[0] != '4') {
1302 /* Make a vCard structure out of the data supplied in the form */
1304 StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
1307 v = VCardLoad(Buf); /* Start with the extra fields */
1310 safestrncpy(WCC->ImportantMessage,
1311 _("An error has occurred."),
1312 sizeof WCC->ImportantMessage
1318 snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
1324 vcard_add_prop(v, "n", buf);
1326 vcard_add_prop(v, "title", bstr("title"));
1327 vcard_add_prop(v, "fn", bstr("fullname"));
1328 vcard_add_prop(v, "org", bstr("org"));
1330 snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
1338 vcard_add_prop(v, "adr", buf);
1340 vcard_add_prop(v, "tel;home", bstr("hometel"));
1341 vcard_add_prop(v, "tel;work", bstr("worktel"));
1342 vcard_add_prop(v, "tel;fax", bstr("faxtel"));
1343 vcard_add_prop(v, "tel;cell", bstr("mobiletel"));
1344 vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
1346 for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
1347 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
1348 if (!IsEmptyStr(buf)) {
1349 vcard_add_prop(v, "email;internet", buf);
1353 serialized_vcard = vcard_serialize(v);
1355 if (serialized_vcard == NULL) {
1356 safestrncpy(WCC->ImportantMessage,
1357 _("An error has occurred."),
1358 sizeof WCC->ImportantMessage
1364 serv_puts("Content-type: text/x-vcard; charset=UTF-8");
1366 serv_printf("%s\r\n", serialized_vcard);
1368 free(serialized_vcard);
1370 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
1371 select_user_to_edit(NULL);
1373 else if (!strcmp(bstr("return_to"), "do_welcome")) {
1376 else if (!IsEmptyStr(bstr("return_to"))) {
1377 http_redirect(bstr("return_to"));
1380 readloop(readnew, eUseDefault);
1387 * Extract an embedded photo from a vCard for display on the client
1389 void display_vcard_photo_img(void)
1395 const char *contentType;
1396 wcsession *WCC = WC;
1398 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1400 vcard = load_mimepart(msgnum,"1");
1401 v = VCardLoad(vcard);
1403 photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
1404 FlushStrBuf(WCC->WBuf);
1405 StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
1406 if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
1407 FlushStrBuf(WCC->WBuf);
1409 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
1410 output_headers(0, 0, 0, 0, 0, 0);
1411 hprintf("Content-Type: text/plain\r\n");
1413 wc_printf(_("Could Not decode vcard photo\n"));
1417 contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
1418 http_transmit_thing(contentType, 0);
1423 typedef struct _vcardview_struct {
1425 addrbookent *addrbook;
1430 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat,
1431 void **ViewSpecific,
1436 vcardview_struct *VS;
1438 VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
1439 memset(VS, 0, sizeof(vcardview_struct));
1440 *ViewSpecific = (void*)VS;
1442 VS->is_singlecard = ibstr("is_singlecard");
1443 if (VS->is_singlecard != 1) {
1444 if (oper == do_search) {
1445 snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
1448 strcpy(cmd, "MSGS ALL");
1450 Stat->maxmsgs = 9999999;
1455 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat,
1456 void **ViewSpecific,
1457 message_summary* Msg,
1461 vcardview_struct *VS;
1464 VS = (vcardview_struct*) *ViewSpecific;
1467 fetch_ab_name(Msg, &ab_name);
1468 if (ab_name == NULL)
1471 VS->addrbook = realloc(VS->addrbook,
1472 (sizeof(addrbookent) * VS->num_ab) );
1473 safestrncpy(VS->addrbook[VS->num_ab-1].ab_name, ab_name,
1474 sizeof(VS->addrbook[VS->num_ab-1].ab_name));
1475 VS->addrbook[VS->num_ab-1].ab_msgnum = Msg->msgnum;
1481 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1484 vcardview_struct *VS;
1486 VS = (vcardview_struct*) *ViewSpecific;
1487 if (VS->is_singlecard)
1488 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1490 do_addrbook_view(VS->addrbook, VS->num_ab); /* Render the address book */
1494 int vcard_Cleanup(void **ViewSpecific)
1496 vcardview_struct *VS;
1498 VS = (vcardview_struct*) *ViewSpecific;
1501 (VS->addrbook != NULL))
1509 ServerStartModule_VCARD
1512 VCToEnum = NewHash(0, NULL);
1517 ServerShutdownModule_VCARD
1520 DeleteHash(&VCToEnum);
1527 RegisterReadLoopHandlerset(
1529 vcard_GetParamsGetServerCall,
1532 vcard_LoadMsgFromServer,
1533 vcard_RenderView_or_Tail,
1535 WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1536 WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1537 WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1539 Put(VCToEnum, HKEY("n"), (void*)VC_n, reference_free_handler);
1540 Put(VCToEnum, HKEY("fn"), (void*)VC_fn, reference_free_handler);
1541 Put(VCToEnum, HKEY("title"), (void*)VC_title, reference_free_handler);
1542 Put(VCToEnum, HKEY("org"), (void*)VC_org, reference_free_handler);
1543 Put(VCToEnum, HKEY("email"), (void*)VC_email, reference_free_handler);
1544 Put(VCToEnum, HKEY("tel"), (void*)VC_tel, reference_free_handler);
1545 Put(VCToEnum, HKEY("tel_tel"), (void*)VC_tel_tel, reference_free_handler);
1546 Put(VCToEnum, HKEY("tel_work"), (void*)VC_tel_work, reference_free_handler);
1547 Put(VCToEnum, HKEY("tel_home"), (void*)VC_tel_home, reference_free_handler);
1548 Put(VCToEnum, HKEY("tel_cell"), (void*)VC_tel_cell, reference_free_handler);
1549 Put(VCToEnum, HKEY("adr"), (void*)VC_adr, reference_free_handler);
1550 Put(VCToEnum, HKEY("photo"), (void*)VC_photo, reference_free_handler);
1551 Put(VCToEnum, HKEY("version"), (void*)VC_version, reference_free_handler);
1552 Put(VCToEnum, HKEY("rev"), (void*)VC_rev, reference_free_handler);
1553 Put(VCToEnum, HKEY("label"), (void*)VC_label, reference_free_handler);
1556 RegisterNamespace("VC", 1, 2, tmplput_VCARD_ITEM, NULL, CTX_VCARD);
1558 REGISTERTokenParamDefine(VC_n);
1559 REGISTERTokenParamDefine(VC_fn);
1560 REGISTERTokenParamDefine(VC_title);
1561 REGISTERTokenParamDefine(VC_org);
1562 REGISTERTokenParamDefine(VC_email);
1563 REGISTERTokenParamDefine(VC_tel);
1564 REGISTERTokenParamDefine(VC_tel_tel);
1565 REGISTERTokenParamDefine(VC_tel_work);
1566 REGISTERTokenParamDefine(VC_tel_home);
1567 REGISTERTokenParamDefine(VC_tel_cell);
1568 REGISTERTokenParamDefine(VC_adr);
1569 REGISTERTokenParamDefine(VC_photo);
1570 REGISTERTokenParamDefine(VC_version);
1571 REGISTERTokenParamDefine(VC_rev);
1572 REGISTERTokenParamDefine(VC_label);