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;
18 CtxType CTX_VCARD_TYPE = CTX_NONE;
19 long VCEnumCounter = 0;
21 typedef enum _VCStrEnum {
30 Base64BinaryAttachment,
34 typedef struct vcField vcField;
44 vcField VCStr_Ns [] = {
45 {{HKEY("last")}, FlatString, NULL, 0, 0, {HKEY("Last Name")}},
46 {{HKEY("first")}, FlatString, NULL, 0, 0, {HKEY("First Name")}},
47 {{HKEY("middle")}, FlatString, NULL, 0, 0, {HKEY("Middle Name")}},
48 {{HKEY("prefix")}, FlatString, NULL, 0, 0, {HKEY("Prefix")}},
49 {{HKEY("suffix")}, FlatString, NULL, 0, 0, {HKEY("Suffix")}},
50 {{HKEY("")}, TerminateList, NULL, 0, 0, {HKEY("")}}
53 vcField VCStr_Addrs [] = {
54 {{HKEY("POBox")}, Address, NULL, 0, 0, {HKEY("PO box")}},
55 {{HKEY("extadr")}, Address, NULL, 0, 0, {HKEY("Address")}},
56 {{HKEY("street")}, Address, NULL, 0, 0, {HKEY("")}},
57 {{HKEY("city")}, Address, NULL, 0, 0, {HKEY("City")}},
58 {{HKEY("state")}, Address, NULL, 0, 0, {HKEY("State")}},
59 {{HKEY("zip")}, Address, NULL, 0, 0, {HKEY("ZIP code")}},
60 {{HKEY("country")}, Address, NULL, 0, 0, {HKEY("Country")}},
61 {{HKEY("")}, TerminateList, NULL, 0, 0, {HKEY("")}}
65 {{HKEY("version")}, Number, NULL, 0, 0, {HKEY("")}},
66 {{HKEY("rev")}, Number, NULL, 0, 0, {HKEY("")}},
67 {{HKEY("label")}, FlatString, NULL, 0, 0, {HKEY("")}},
68 {{HKEY("uid")}, FlatString, NULL, 0, 0, {HKEY("")}},
69 {{HKEY("n")}, StringCluster, VCStr_Ns, 0, 0, {HKEY("")}}, /* N is name, but only if there's no FN already there */
70 {{HKEY("fn")}, FlatString, NULL, 0, 0, {HKEY("")}}, /* FN (full name) is a true 'display name' field */
71 {{HKEY("title")}, FlatString, NULL, 0, 0, {HKEY("Title:")}},
72 {{HKEY("org")}, FlatString, NULL, 0, 0, {HKEY("Organization:")}},/* organization */
73 {{HKEY("email")}, EmailAddr, NULL, 0, 0, {HKEY("E-mail:")}},
74 {{HKEY("tel")}, PhoneNumber, NULL, 0, 0, {HKEY("Telephone:")}},
75 {{HKEY("adr")}, StringCluster, VCStr_Addrs, 0, 0, {HKEY("Address:")}},
76 {{HKEY("photo")}, Base64BinaryAttachment, NULL, 0, 0, {HKEY("Photo:")}},
77 {{HKEY("tel;home")}, PhoneNumber, NULL, 0, 0, {HKEY(" (home)")}},
78 {{HKEY("tel;work")}, PhoneNumber, NULL, 0, 0, {HKEY(" (work)")}},
79 {{HKEY("tel;fax")}, PhoneNumber, NULL, 0, 0, {HKEY(" (fax)")}},
80 {{HKEY("tel;cell")}, PhoneNumber, NULL, 0, 0, {HKEY(" (cell)")}},
81 {{HKEY("email;internet")}, EmailAddr, NULL, 0, 0, {HKEY("E-mail:")}},
82 {{HKEY("UNKNOWN")}, UnKnown, NULL, 0, 0, {HKEY("")}},
83 {{HKEY("")}, TerminateList, NULL, 0, 0, {HKEY("")}}
88 {HKEY("n")}, /* N is name, but only if there's no FN already there */
89 {HKEY("fn")}, /* FN (full name) is a true 'display name' field */
90 {HKEY("title")}, /* title */
91 {HKEY("org")}, /* organization */
106 HashList *DefineToToken = NULL;
107 HashList *VCTokenToDefine = NULL;
108 HashList *vcNames = NULL; /* todo: fill with the name strings */
109 vcField* vcfUnknown = NULL;
111 void RegisterVCardToken(vcField* vf, StrBuf *name, int inTokenCount)
113 if (vf->Type == UnKnown) {
116 RegisterTokenParamDefine(SKEY(name), vf->cval);
117 Put(DefineToToken, LKEY(vf->cval), vf, reference_free_handler);
118 Put(vcNames, LKEY(vf->cval), NewStrBufPlain(CKEY(vf->Name)), HFreeStrBuf);
120 syslog(LOG_DEBUG, "Token: %s -> %ld, %d",
127 void autoRegisterTokens(long *enumCounter, vcField* vf, StrBuf *BaseStr, int layer, long parentCVal)
130 StrBuf *subStr = NewStrBuf();
131 while (vf[i].STR.len > 0) {
133 vf[i].cval = (*enumCounter) ++;
134 vf[i].parentCVal = parentCVal;
135 StrBufAppendBuf(subStr, BaseStr, 0);
136 if (StrLength(subStr) > 0) {
137 StrBufAppendBufPlain(subStr, HKEY("."), 0);
139 StrBufAppendBufPlain(subStr, CKEY(vf[i].STR), 0);
141 Put(VCTokenToDefine, CKEY(vf[i].STR), &vf[i], reference_free_handler);
143 switch (vf[i].Type) {
148 autoRegisterTokens(enumCounter, vf[i].Sub, subStr, 1, vf[i].cval);
161 case Base64BinaryAttachment:
170 RegisterVCardToken(&vf[i], subStr, i);
176 int preeval_vcard_item(WCTemplateToken *Token)
178 WCTemplputParams TPP;
179 WCTemplputParams *TP;
181 StrBuf *Target = NULL;
183 memset(&TPP, 0, sizeof(WCTemplputParams));
186 searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
187 if (searchFieldNo >= VCEnumCounter) {
188 LogTemplateError(NULL, "VCardItem", ERR_PARM1, TP,
195 void tmpl_vcard_item(StrBuf *Target, WCTemplputParams *TP)
198 long searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
199 HashList *vc = (HashList*) CTX(CTX_VCARD);
200 if (GetHash(vc, LKEY(searchFieldNo), &vItem) && (vItem != NULL)) {
201 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 1);
205 void tmpl_vcard_context_item(StrBuf *Target, WCTemplputParams *TP)
208 vcField *t = (vcField*) CTX(CTX_VCARD_TYPE);
209 HashList *vc = (HashList*) CTX(CTX_VCARD);
212 LogTemplateError(NULL, "VCard item", ERR_NAME, TP,
217 if (GetHash(vc, LKEY(t->cval), &vItem) && (vItem != NULL)) {
218 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 0);
221 LogTemplateError(NULL, "VCard item", ERR_NAME, TP,
222 "Doesn't have that key - did you miss to filter in advance?");
225 int preeval_vcard_name_str(WCTemplateToken *Token)
227 WCTemplputParams TPP;
228 WCTemplputParams *TP;
230 StrBuf *Target = NULL;
232 memset(&TPP, 0, sizeof(WCTemplputParams));
235 searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
236 if (searchFieldNo >= VCEnumCounter) {
237 LogTemplateError(NULL, "VCardName", ERR_PARM1, TP,
244 void tmpl_vcard_name_str(StrBuf *Target, WCTemplputParams *TP)
247 long searchFieldNo = GetTemplateTokenNumber(Target, TP, 0, 0);
248 /* todo: get descriptive string for this vcard type */
249 if (GetHash(vcNames, LKEY(searchFieldNo), &vItem) && (vItem != NULL)) {
250 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 1);
253 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
254 "No i18n string for this.");
259 void tmpl_vcard_context_name_str(StrBuf *Target, WCTemplputParams *TP)
262 vcField *t = (vcField*) CTX(CTX_VCARD_TYPE);
265 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
270 if (GetHash(vcNames, LKEY(t->cval), &vItem) && (vItem != NULL)) {
271 StrBufAppendTemplate(Target, TP, (StrBuf*) vItem, 1);
274 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
275 "No i18n string for this.");
280 int filter_VC_ByType(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP)
286 vcField *vf = (vcField*) Context;
288 memcpy(&type, key, sizeof(long));
289 searchType = GetTemplateTokenNumber(Target, TP, IT_ADDT_PARAM(0), 0);
291 if (vf->Type == searchType) {
292 HashList *vc = (HashList*) CTX(CTX_VCARD);
293 if (GetHash(vc, LKEY(vf->cval), &v) && v != NULL)
302 HashList *getContextVcard(StrBuf *Target, WCTemplputParams *TP)
304 vcField *vf = (vcField*) CTX(CTX_VCARD_TYPE);
305 HashList *vc = (HashList*) CTX(CTX_VCARD);
307 if ((vf == NULL) || (vc == NULL)) {
308 LogTemplateError(NULL, "VCard item type", ERR_NAME, TP,
309 "Need VCard and Vcard type in context");
316 int filter_VC_ByContextType(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP)
319 vcField *vf = (vcField*) CTX(CTX_VCARD_TYPE);
321 memcpy(&searchType, key, sizeof(long));
323 if (vf->cval == searchType) {
332 int conditional_VC_Havetype(StrBuf *Target, WCTemplputParams *TP)
334 HashList *vc = (HashList*) CTX(CTX_VCARD);
335 long HaveFieldType = GetTemplateTokenNumber(Target, TP, 2, 0);
340 HashPos *it = GetNewHashPos(vc, 0);
341 while (GetNextHashPos(vc, it, &len, &Key, &vVCitem) &&
346 memcpy(&type, Key, sizeof(long));
347 if (GetHash(DefineToToken, LKEY(type), &vvcField) &&
350 vcField *t = (vcField*) vvcField;
351 if (t && t->Type == HaveFieldType) {
364 * Helper function for do_addrbook_view()
365 * Converts a name into a three-letter tab label
367 void nametab(char *tabbuf, long len, char *name) {
368 stresc(tabbuf, len, name, 0, 0);
369 tabbuf[0] = toupper(tabbuf[0]);
370 tabbuf[1] = tolower(tabbuf[1]);
371 tabbuf[2] = tolower(tabbuf[2]);
375 wc_mime_attachment *load_vcard(message_summary *Msg)
378 StrBuf *FoundCharset = NewStrBuf();
383 wc_mime_attachment *Mime;
384 wc_mime_attachment *VCMime = NULL;
386 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
387 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
388 Msg->MsgBody->msgnum = Msg->msgnum;
390 load_message(Msg, FoundCharset, &Error);
392 FreeStrBuf(&FoundCharset);
393 /* look up the vcard... */
394 it = GetNewHashPos(Msg->AllAttach, 0);
395 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
398 Mime = (wc_mime_attachment*) vMime;
399 if ((strcmp(ChrPtr(Mime->ContentType),
400 "text/x-vcard") == 0) ||
401 (strcmp(ChrPtr(Mime->ContentType),
412 if (VCMime->Data == NULL)
413 MimeLoadData(VCMime);
420 * Turn a vCard "n" (name) field into something displayable.
422 void vcard_n_prettyize(char *name)
427 original_name = strdup(name);
428 len = strlen(original_name);
429 for (i=0; i<5; ++i) {
431 if (original_name[len-1] == ' ') {
432 original_name[--len] = 0;
434 if (original_name[len-1] == ';') {
435 original_name[--len] = 0;
441 for (i=0; i<len; ++i) {
442 if (original_name[i] == ';') {
447 name[j++] = original_name[i];
455 void PutVcardItem(HashList *thisVC, vcField *thisField, StrBuf *ThisFieldStr, int is_qp, StrBuf *Swap)
457 /* if we have some untagged QP, detect it here. */
458 if (is_qp || (strstr(ChrPtr(ThisFieldStr), "=?")!=NULL)){
460 StrBuf_RFC822_to_Utf8(Swap, ThisFieldStr, NULL, NULL); /* default charset, current charset */
466 Put(thisVC, LKEY(thisField->cval), ThisFieldStr, HFreeStrBuf);
470 * display_vcard() calls this after parsing the textual vCard into
471 * our 'struct vCard' data object.
473 * Set 'full' to nonzero to display the full card, otherwise it will only
474 * show a summary line.
476 * This code is a bit ugly, so perhaps an explanation is due: we do this
477 * in two passes through the vCard fields. On the first pass, we process
478 * fields we understand, and then render them in a pretty fashion at the
479 * end. Then we make a second pass, outputting all the fields we don't
480 * understand in a simple two-column name/value format.
481 * v the vCard to parse
482 * msgnum Citadel message pointer
484 void parse_vcard(StrBuf *Target, struct vCard *v, HashList *VC, wc_mime_attachment *Mime)
493 StrBuf *thisname = NULL;
494 char firsttoken[SIZ];
499 thisname = NewStrBuf();
500 thisVCToken = NewStrBufPlain(NULL, 63);
501 for (i=0; i<(v->numprops); ++i) {
502 FlushStrBuf(thisVCToken);
505 syslog(LOG_DEBUG, "i: %d oneprop: %s - value: %s", i, v->prop[i].name, v->prop[i].value);
506 StrBufPlain(thisname, v->prop[i].name, -1);
507 StrBufLowerCase(thisname);
509 /*len = */extract_token(firsttoken, ChrPtr(thisname), 0, ';', sizeof firsttoken);
510 ntokens = num_tokens(ChrPtr(thisname), ';');
511 for (j=0, k=0; j < ntokens && k < 10; ++j) {
514 len = extract_token(buf, ChrPtr(thisname), j, ';', sizeof buf);
515 if (!strcasecmp(buf, "encoding=quoted-printable")) {
517 /* remove_token(thisname, j, ';');*/
519 else if (!strcasecmp(buf, "encoding=base64")) {
521 /* remove_token(thisname, j, ';');*/
524 if (StrLength(thisVCToken) > 0) {
525 StrBufAppendBufPlain(thisVCToken, HKEY(";"), 0);
527 StrBufAppendBufPlain(thisVCToken, buf, len, 0);
529 if (GetHash(VCToEnum, buf, len, &V))
533 Put(VC, IKEY(evc), Val, HFreeStrBuf);
535 syslog(LOG_DEBUG, "[%ul] -> k: %d %s - %s", evc, k, buf, VCStr[evc[k]].Key);
544 if ((StrLength(thisVCToken) > 0) &&
545 GetHash(VCTokenToDefine, SKEY(thisVCToken), &vField) &&
547 vcField *thisField = (vcField *)vField;
548 StrBuf *ThisFieldStr = NULL;
549 syslog(LOG_DEBUG, "got this token: %s, found: %s", ChrPtr(thisVCToken), thisField->STR.Key);
550 switch (thisField->Type) {
551 case StringCluster: {
553 const char *Pos = NULL;
554 StrBuf *thisArray = NewStrBufPlain(v->prop[i].value, -1);
555 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(thisArray));
556 while (thisField->Sub[j].STR.len > 0) {
557 StrBufExtract_NextToken(Buf, thisArray, &Pos, ';');
558 ThisFieldStr = NewStrBufDup(Buf);
560 PutVcardItem(VC, &thisField->Sub[j], ThisFieldStr, is_qp, Swap);
563 FreeStrBuf(&thisArray);
574 /* copy over the payload into a StrBuf */
575 ThisFieldStr = NewStrBufPlain(v->prop[i].value, -1);
576 PutVcardItem(VC, thisField, ThisFieldStr, is_qp, Swap);
579 case Base64BinaryAttachment:
586 else if (StrLength(thisVCToken) > 0) {
587 /* Add it to the UNKNOWN field... */
590 GetHash(VC, IKEY(vcfUnknown->cval), &pv);
591 oldVal = (StrBuf*) pv;
592 if (oldVal == NULL) {
593 oldVal = NewStrBuf();
594 Put(VC, IKEY(vcfUnknown->cval), oldVal, HFreeStrBuf);
597 StrBufAppendBufPlain(oldVal, HKEY("\n"), 0);
600 StrBufAppendBuf(oldVal, thisVCToken, 0);
601 StrBufAppendBufPlain(oldVal, HKEY(":"), 0);
602 StrBufAppendBufPlain(oldVal, v->prop[i].value, -1, 0);
606 /* copy over the payload into a StrBuf */
607 Val = NewStrBufPlain(v->prop[i].value, -1);
609 /* if we have some untagged QP, detect it here. */
610 if (is_qp || (strstr(v->prop[i].value, "=?")!=NULL)){
612 StrBuf_RFC822_to_Utf8(Swap, Val, NULL, NULL); /* default charset, current charset */
619 StrBufDecodeBase64(Val);
622 syslog(LOG_DEBUG, "-> firsttoken: %s thisname: %s Value: [%s][%s]",
630 FreeStrBuf(&thisname);
632 FreeStrBuf(&thisVCToken);
635 void tmplput_VCARD_ITEM(StrBuf *Target, WCTemplputParams *TP)
637 HashList *VC = CTX(CTX_VCARD);
641 evc = GetTemplateTokenNumber(Target, TP, 0, -1);
644 if (GetHash(VC, IKEY(evc), &vStr))
646 StrBufAppendTemplate(Target, TP,
654 void display_one_vcard (StrBuf *Target, HashList *VC, const char *tp_name, size_t tp_name_len)
656 WCTemplputParams *TP = NULL;
657 WCTemplputParams SubTP;
659 memset(&SubTP, 0, sizeof(WCTemplputParams));
660 StackContext(TP, &SubTP, VC, CTX_VCARD, 0, NULL);
662 DoTemplate(tp_name, tp_name_len, Target, &SubTP);
663 UnStackContext(&SubTP);
667 * Extract an embedded photo from a vCard for display on the client
669 void display_vcard_photo_img(void)
675 const char *contentType;
678 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
680 vcard = load_mimepart(msgnum,"1");
681 v = VCardLoad(vcard);
683 photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
684 FlushStrBuf(WCC->WBuf);
685 StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
686 if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
687 FlushStrBuf(WCC->WBuf);
689 hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
690 output_headers(0, 0, 0, 0, 0, 0);
691 hprintf("Content-Type: text/plain\r\n");
693 wc_printf(_("Could Not decode vcard photo\n"));
697 contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
698 http_transmit_thing(contentType, 0);
704 * Edit the vCard component of a MIME message.
705 * Supply the message number
706 * and MIME part number to fetch. Or, specify -1 for the message number
707 * to start with a blank card.
709 void do_edit_vcard(long msgnum, char *partnum,
710 message_summary *VCMsg,
711 wc_mime_attachment *VCAtt,
712 const char *return_to,
713 const char *force_room) {
714 HashList *VC; WCTemplputParams SubTP;
716 message_summary *Msg = NULL;
717 wc_mime_attachment *VCMime = NULL;
720 VC = NewHash(0, lFlathash);
722 /* Display the form */
723 output_headers(1, 1, 1, 0, 0, 0);
725 safestrncpy(whatuser, "", sizeof whatuser);
728 ((VCMsg != NULL) && (VCAtt != NULL)))
730 if ((VCMsg == NULL) && (VCAtt == NULL)) {
732 Msg = (message_summary *) malloc(sizeof(message_summary));
733 memset(Msg, 0, sizeof(message_summary));
734 Msg->msgnum = msgnum;
735 VCMime = load_vcard(Msg);
736 if (VCMime == NULL) {
737 convenience_page("770000", _("Error"), "");/*TODO: important message*/
738 DestroyMessageSummary(Msg);
742 v = VCardLoad(VCMime->Data);
745 v = VCardLoad(VCAtt->Data);
748 parse_vcard(WCC->WBuf, v, VC, NULL);
754 memset(&SubTP, 0, sizeof(WCTemplputParams));
757 WCTemplputParams *TP = NULL;
758 WCTemplputParams SubTP;
759 StackContext(TP, &SubTP, VC, CTX_VCARD, 0, NULL);
761 DoTemplate(HKEY("vcard_edit"), WCC->WBuf, &SubTP);
762 UnStackContext(&SubTP);
769 DestroyMessageSummary(Msg);
775 * commit the edits to the citadel server
777 void edit_vcard(void) {
781 msgnum = lbstr("msgnum");
782 partnum = bstr("partnum");
783 do_edit_vcard(msgnum, partnum, NULL, NULL, "", NULL);
789 * parse edited vcard from the browser
791 void submit_vcard(void) {
793 char *serialized_vcard;
795 const StrBuf *ForceRoom;
803 const char *Pos = NULL;
805 if (!havebstr("ok_button")) {
806 readloop(readnew, eUseDefault);
810 if (havebstr("force_room")) {
811 ForceRoom = sbstr("force_room");
812 if (gotoroom(ForceRoom) != 200) {
813 AppendImportantMessage(_("Unable to enter the room to save your message"), -1);
814 AppendImportantMessage(HKEY(": "));
815 AppendImportantMessage(SKEY(ForceRoom));
816 AppendImportantMessage(HKEY("; "));
817 AppendImportantMessage(_("Aborting."), -1);
819 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
820 select_user_to_edit(NULL);
822 else if (!strcmp(bstr("return_to"), "do_welcome")) {
825 else if (!IsEmptyStr(bstr("return_to"))) {
826 http_redirect(bstr("return_to"));
829 readloop(readnew, eUseDefault);
835 postVcard = getSubStruct(HKEY("VC"));
836 if (postVcard == NULL) {
837 AppendImportantMessage(_("An error has occurred."), -1);
839 return;/*/// more details*/
843 serv_write(HKEY("ENT0 1|||4\n"));
844 if (!StrBuf_ServGetln(Buf) && (GetServerStatus(Buf, NULL) != 4))
850 /* Make a vCard structure out of the data supplied in the form */
851 StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
854 v = VCardLoad(Buf); /* Start with the extra fields */
856 AppendImportantMessage(_("An error has occurred."), -1);
862 SubStr = NewStrBuf();
863 it = GetNewHashPos(DefineToToken, 0);
864 while (GetNextHashPos(DefineToToken, it, &len, &Key, &pv) &&
869 vcField *t = (vcField*) pv;
874 itSub = GetNewHashPos(DefineToToken, 0);
875 while (GetNextHashPos(DefineToToken, itSub, &len, &Key, &pv) &&
879 if (Sub->parentCVal == t->cval) {
880 if (StrLength(SubStr) > 0)
881 StrBufAppendBufPlain(SubStr, HKEY(";"), 0);
885 blen = snprintf(buf, sizeof(buf), "%ld", Sub->cval);
886 s = SSubBstr(postVcard, buf, blen);
888 if ((s != NULL) && (StrLength(s) > 0)) {
890 StrBufAppendBuf(SubStr, s, 0);
894 if (StrLength(SubStr) > 0) {
895 vcard_add_prop(v, t->STR.Key, ChrPtr(SubStr));
897 DeleteHashPos(&itSub);
899 else if (t->parentCVal == 0) {
900 blen = snprintf(buf, sizeof(buf), "%ld", t->cval);
901 s = SSubBstr(postVcard, buf, blen);
903 if ((s != NULL) && (StrLength(s) > 0)) {
904 vcard_add_prop(v, t->STR.Key, ChrPtr(s));
910 s = sbstr("other_inetemail");
911 if (StrLength(s) > 0) {
913 while (StrBufSipLine(SubStr, s, &Pos), ((Pos!=StrBufNOTNULL) && (Pos!=NULL)) ) {
914 if (StrLength(SubStr) > 0) {
915 vcard_add_prop(v, "email;internet", ChrPtr(SubStr));
923 serialized_vcard = vcard_serialize(v);
925 if (serialized_vcard == NULL) {
926 AppendImportantMessage(_("An error has occurred."), -1);
932 printf("%s", serialized_vcard);
933 serv_write(HKEY("Content-type: text/x-vcard; charset=UTF-8\n"));
934 serv_write(HKEY("\n"));
935 serv_printf("%s\r\n", serialized_vcard);
936 serv_write(HKEY("000\n"));
937 free(serialized_vcard);
939 if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
940 select_user_to_edit(NULL);
942 else if (!strcmp(bstr("return_to"), "do_welcome")) {
945 else if (!IsEmptyStr(bstr("return_to"))) {
946 http_redirect(bstr("return_to"));
949 readloop(readnew, eUseDefault);
957 * Address book entry (keep it short and sweet, it's just a quickie lookup
958 * which we can use to get to the real meat and bones later)
960 typedef struct _addrbookent {
963 long ab_msgnum; /* message number of address book entry */
966 void deleteAbEnt(void *v) {
967 addrbookent *vc = (addrbookent*)v;
969 FreeStrBuf(&vc->name);
972 typedef struct _vcardview_struct {
978 int vcard_GetParamsGetServerCall(SharedMessageStatus *Stat,
986 vcardview_struct *VS;
988 VS = (vcardview_struct*) malloc (sizeof(vcardview_struct));
989 memset(VS, 0, sizeof(vcardview_struct));
990 *ViewSpecific = (void*)VS;
992 VS->is_singlecard = ibstr("is_singlecard");
993 if (VS->is_singlecard != 1) {
994 VS->addrbook = NewHash(0, NULL);
995 if (oper == do_search) {
996 snprintf(cmd, len, "MSGS SEARCH|%s", bstr("query"));
999 strcpy(cmd, "MSGS ALL");
1001 Stat->maxmsgs = 9999999;
1006 int vcard_LoadMsgFromServer(SharedMessageStatus *Stat,
1007 void **ViewSpecific,
1008 message_summary* Msg,
1012 vcardview_struct *VS;
1013 wc_mime_attachment *VCMime = NULL;
1015 addrbookent* abEntry;
1017 VS = (vcardview_struct*) *ViewSpecific;
1019 VCMime = load_vcard(Msg);
1023 v = VCardLoad(VCMime->Data);
1025 if (v == NULL) return 0;
1027 abEntry = (addrbookent*) malloc(sizeof(addrbookent));
1028 memset(abEntry, 0, sizeof(addrbookent));
1029 abEntry->name = NewStrBuf();
1030 abEntry->VC = NewHash(0, lFlathash);
1031 abEntry->ab_msgnum = Msg->msgnum;
1032 parse_vcard(WC->WBuf, v, abEntry->VC, VCMime);
1034 display_one_vcard(abEntry->name, abEntry->VC, HKEY("vcard_list_name"));
1036 if (StrLength(abEntry->name) == 0) {
1037 StrBufPlain(abEntry->name, _("(no name)"), -1);
1042 Put(VS->addrbook, SKEY(abEntry->name), abEntry, deleteAbEnt);
1048 * Render the address book using info we gathered during the scan
1050 * addrbook the addressbook to render
1051 * num_ab the number of the addressbook
1053 void do_addrbook_view(vcardview_struct* VS) {
1057 static int NAMESPERPAGE = 60;
1063 int num_ab = GetCount(VS->addrbook);
1064 HashList *headlines;
1066 wcsession *WCC = WC;
1068 WCTemplputParams *TP = NULL;
1069 WCTemplputParams SubTP;
1071 memset(&SubTP, 0, sizeof(WCTemplputParams));
1074 do_template("vcard_list_empty");
1079 ///SortByHashKey(VS->addrbook, 0);
1082 num_pages = (GetCount(VS->addrbook) / NAMESPERPAGE) + 1;
1084 tablabels = malloc(num_pages * sizeof (char *));
1085 if (tablabels == NULL) {
1089 headlines = NewHash(0, lFlathash);
1090 for (i=0; i<num_pages; ++i) {
1093 long hklen1, hklen2;
1094 const char *c1, *c2;
1096 addrbookent *a1, *a2;
1098 tabfirst = i * NAMESPERPAGE;
1099 tablast = tabfirst + NAMESPERPAGE - 1;
1100 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1102 headline = NewStrBufPlain(NULL, StrLength(v1) + StrLength(v2) + 10);
1103 if (GetHashAt(VS->addrbook, tabfirst, &hklen1, &c1, &v1)) {
1104 a1 = (addrbookent*) v1;
1105 StrBufAppendBuf(headline, a1->name, 0);
1107 if (GetHashAt(VS->addrbook, tablast, &hklen2, &c2, &v2)) {
1109 a2 = (addrbookent*) v2;
1110 StrBufAppendBufPlain(headline, HKEY(" - "), 0);
1111 StrBufAppendBuf(headline, a2->name, 0);
1114 Put(headlines, LKEY(i), headline, HFreeStrBuf);
1117 tabbed_dialog(num_pages, tablabels);
1120 it = GetNewHashPos(VS->addrbook, 0);
1121 for (i=0; i<num_ab; ++i) {
1126 GetNextHashPos(VS->addrbook, it, &hklen, &key, &v);
1129 abEnt = (addrbookent *) v;
1130 if ((i / NAMESPERPAGE) != page) { /* New tab */
1131 page = (i / NAMESPERPAGE);
1133 do_template("vcard_list_section_end");
1134 end_tab(page-1, num_pages);
1136 begin_tab(page, num_pages);
1137 do_template("vcard_list_section_start");
1141 if ((displayed % 4) == 0) {
1142 if (displayed > 0) {
1143 do_template("vcard_list_row_end");
1145 do_template("vcard_list_row_start");
1150 StackContext(TP, &SubTP, abEnt->VC, CTX_VCARD, 0, NULL);
1152 DoTemplate(HKEY("vcard_list_entry"), WCC->WBuf, &SubTP);
1153 UnStackContext(&SubTP);
1159 /* Placeholders for empty columns at end */
1160 if ((num_ab % 4) != 0) {
1161 for (i=0; i<(4-(num_ab % 4)); ++i) {
1162 do_template("vcard_list_cell_end");
1166 do_template("vcard_list_section_end");
1167 end_tab((num_pages-1), num_pages);
1169 begin_tab(num_pages, num_pages);
1170 /* FIXME there ought to be something here */
1171 end_tab(num_pages, num_pages);
1173 DeleteHash(&headlines);
1179 int vcard_RenderView_or_Tail(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
1182 vcardview_struct *VS;
1184 VS = (vcardview_struct*) *ViewSpecific;
1185 if (VS->is_singlecard)
1186 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
1188 do_addrbook_view(VS); /* Render the address book */
1192 int vcard_Cleanup(void **ViewSpecific)
1194 vcardview_struct *VS;
1196 VS = (vcardview_struct*) *ViewSpecific;
1199 (VS->addrbook != NULL))
1200 DeleteHash(&VS->addrbook);
1207 void render_MIME_VCard(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1209 wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1210 wcsession *WCC = WC;
1211 if (StrLength(Mime->Data) == 0)
1213 if (StrLength(Mime->Data) > 0) {
1218 /** If it's my vCard I can edit it */
1219 if ( (!strcasecmp(ChrPtr(WCC->CurRoom.name), USERCONFIGROOM))
1220 || ((StrLength(WCC->CurRoom.name) > 11) &&
1221 (!strcasecmp(&(ChrPtr(WCC->CurRoom.name)[11]), USERCONFIGROOM)))
1222 || (WCC->CurRoom.view == VIEW_ADDRESSBOOK)
1224 StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld?partnum=%s\">",
1225 Mime->msgnum, ChrPtr(Mime->PartNum));
1226 StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
1229 /* In all cases, display the full card */
1231 v = VCardLoad(Mime->Data);
1236 VC = NewHash(0, lFlathash);
1237 parse_vcard(Target, v, VC, Mime);
1238 display_one_vcard (Target, VC, HKEY("vcard_msg_display"));
1243 StrBufPlain(Buf, _("failed to load vcard"), -1);
1245 FreeStrBuf(&Mime->Data);
1252 ServerStartModule_VCARD
1255 ///VCToEnum = NewHash(0, NULL);
1260 ServerShutdownModule_VCARD
1263 DeleteHash(&DefineToToken);
1264 DeleteHash(&vcNames);
1265 DeleteHash(&VCTokenToDefine);
1266 /// DeleteHash(&VCToEnum);
1273 StrBuf *Prefix = NewStrBufPlain(HKEY("VC:"));
1274 DefineToToken = NewHash(1, lFlathash);
1275 vcNames = NewHash(1, lFlathash);
1276 VCTokenToDefine = NewHash(1, NULL);
1277 autoRegisterTokens(&VCEnumCounter, VCStrE, Prefix, 0, 0);
1278 FreeStrBuf(&Prefix);
1279 RegisterCTX(CTX_VCARD);
1280 RegisterCTX(CTX_VCARD_TYPE);
1281 RegisterReadLoopHandlerset(
1283 vcard_GetParamsGetServerCall,
1287 vcard_LoadMsgFromServer,
1288 vcard_RenderView_or_Tail,
1290 WebcitAddUrlHandler(HKEY("edit_vcard"), "", 0, edit_vcard, 0);
1291 WebcitAddUrlHandler(HKEY("submit_vcard"), "", 0, submit_vcard, 0);
1292 WebcitAddUrlHandler(HKEY("vcardphoto"), "", 0, display_vcard_photo_img, NEED_URL);
1294 RegisterNamespace("VC:ITEM", 2, 2, tmpl_vcard_item, preeval_vcard_item, CTX_VCARD);
1295 RegisterNamespace("VC:CTXITEM", 1, 1, tmpl_vcard_context_item, NULL, CTX_VCARD_TYPE);
1296 RegisterNamespace("VC:NAME", 1, 1, tmpl_vcard_name_str, preeval_vcard_name_str, CTX_VCARD);
1297 RegisterNamespace("VC:CTXNAME", 1, 1, tmpl_vcard_context_name_str, NULL, CTX_VCARD_TYPE);
1298 REGISTERTokenParamDefine(FlatString);
1299 REGISTERTokenParamDefine(StringCluster);
1300 REGISTERTokenParamDefine(PhoneNumber);
1301 REGISTERTokenParamDefine(EmailAddr);
1302 REGISTERTokenParamDefine(Street);
1303 REGISTERTokenParamDefine(Number);
1304 REGISTERTokenParamDefine(AliasFor);
1305 REGISTERTokenParamDefine(Base64BinaryAttachment);
1306 REGISTERTokenParamDefine(TerminateList);
1307 REGISTERTokenParamDefine(Address);
1309 RegisterConditional("VC:HAVE:TYPE", 1, conditional_VC_Havetype, CTX_VCARD);
1310 RegisterFilteredIterator("VC:TYPE", 1, DefineToToken, NULL, NULL, NULL, filter_VC_ByType, CTX_VCARD_TYPE, CTX_VCARD, IT_NOFLAG);
1311 RegisterFilteredIterator("VC:TYPE:ITEMS", 0, NULL, getContextVcard, NULL, NULL, filter_VC_ByContextType, CTX_STRBUF, CTX_VCARD_TYPE, IT_NOFLAG);
1313 RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard, 1, 201);
1314 RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard, 1, 200);